Skip to content

Commit a35a5ae

Browse files
committed
docs: Add README and INSTALLATION files
- Added a README file with an overview of the project and usage instructions. - Added an INSTALLATION file to guide users through setup steps. - Resolved multiple issues related to packaging and compilation, ensuring smoother builds.
1 parent 4f31e91 commit a35a5ae

File tree

12 files changed

+237
-43
lines changed

12 files changed

+237
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
# chdb-ruby Changelog
2+
13
## [Unreleased]

INSTALLATION.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
# Installation and Using chdb-ruby extensions
3+
4+
This document will help you install the `chdb-ruby` ruby gem.
5+
6+
## Installation
7+
8+
### Native Gems
9+
10+
Native (precompiled) gems are available for recent Ruby versions on these platforms:
11+
12+
- `aarch64-linux`
13+
- `arm64-darwin`
14+
- `x86_64-linux`
15+
- `x86_64-darwin`
16+
17+
If you are using one of these Ruby versions on one of these platforms, the native gem is the recommended way to install chdb-ruby.
18+
19+
`chdb-ruby` gem does not provide a pure Ruby implementation. Installation will fail if your platform is unsupported.
20+
21+
## Post-Installation: Setting Up libchdb C++ Library
22+
23+
After installing the `chdb-ruby` gem, you must also install the `libchdb` C++ library locally. If the library path is not in your system's default search paths, you'll need to configure the runtime library loading path.
24+
25+
### 1. Download the C++ Library
26+
27+
You can either:
28+
- Use the automated installation script:
29+
```bash
30+
curl -sSL https://github.com/chdb-io/chdb-io.github.io/blob/main/install_libchdb.sh | bash
31+
```
32+
33+
- Or manually download from chdb releases(example for arm64-darwin (v3.12)):
34+
```bash
35+
wget https://github.com/chdb-io/chdb/releases/download/v3.12/macos-arm64-libchdb.tar.gz
36+
tar -xzf macos-arm64-libchdb.tar.gz
37+
```
38+
39+
### 2. Configure Library Path
40+
- MacOS:
41+
```bash
42+
export DYLD_LIBRARY_PATH="/path/to/libchdb:$DYLD_LIBRARY_PATH"
43+
```
44+
(Add to your shell config file like ~/.zshrc for persistence)
45+
46+
- Linux:
47+
```bash
48+
export LD_LIBRARY_PATH="/path/to/libchdb:$LD_LIBRARY_PATH"
49+
```
50+
51+
### 3. Verify Installation
52+
- Ruby:
53+
```bash
54+
require 'chdb'
55+
```
56+
57+
- Troubleshooting(If you get "Library not loaded" errors):
58+
- Verify the path in DYLD_LIBRARY_PATH/LD_LIBRARY_PATH is correct
59+
- Ensure you downloaded the right version for your platform

README.md

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,119 @@
44

55
[![chDB-ruby](https://github.com/chdb-io/chdb-ruby/actions/workflows/chdb.yml/badge.svg)](https://github.com/chdb-io/chdb-ruby/actions/workflows/chdb.yml)
66

7-
# chdb-ruby
8-
[chDB](https://github.com/chdb-io/chdb) ruby bindings and chDB cli.
7+
# Ruby Interface for chdb
8+
9+
## Overview
10+
11+
This library allows Ruby programs to use the chDB embedded analytical database (https://github.com/chdb-io/chdb).
12+
13+
**Designed with SQLite3-compatible API style** - If you're familiar with Ruby's sqlite3 gem, you'll feel right at home with chdb-ruby's similar interface design.
14+
15+
Note that this module is only compatible with ChDB 3.0.0 or newer.
16+
17+
* Source code: https://github.com/chdb-io/chdb-ruby
18+
* Download: http://rubygems.org/gems/chdb-ruby
19+
* Documentation: https://clickhouse.com/docs/chdb
20+
21+
## Quick start
22+
23+
``` ruby
24+
require 'chdb'
25+
26+
# Open a database
27+
db = ChDB::Database.new('test_db', results_as_hash: true)
28+
29+
# Create a table
30+
rows = db.execute <<-SQL
31+
CREATE TABLE test_table(
32+
id Int32,
33+
name String)
34+
ENGINE = MergeTree()
35+
ORDER BY id);
36+
SQL
37+
38+
# Execute a few inserts
39+
{
40+
1 => 'Alice',
41+
2 => 'Bob'
42+
}.each do |pair|
43+
db.execute 'INSERT INTO test_table VALUES ( ?, ? )', pair
44+
end
45+
46+
# Find a few rows
47+
db.execute('SELECT * FROM test_table ORDER BY id') do |row|
48+
p row
49+
end
50+
# [{ 'id' => '1', 'name' => 'Alice' },
51+
# { 'id' => '2', 'name' => 'Bob' }]
52+
53+
# Open another database
54+
db = ChDB::Database.new 'test2.db'
55+
56+
# Create another table
57+
rows = db.execute <<-SQL
58+
CREATE TABLE test2_table(
59+
id Int32,
60+
name String)
61+
ENGINE = MergeTree()
62+
ORDER BY id");
63+
SQL
64+
65+
# Execute inserts with parameter markers
66+
db.execute('INSERT INTO test2_table (id, name)
67+
VALUES (?, ?)', [3, 'Charlie'])
68+
69+
db.execute2('SELECT * FROM test2_table') do |row|
70+
p row
71+
end
72+
# [['id', 'name'], [3, 'Charlie']],
73+
```
74+
75+
## Thread Safety
76+
77+
When using `ChDB::Database.new` to open a session, all read/write operations within that session are thread-safe. However, currently only one active session is allowed per process. Therefore, when you need to open another session, you must first close the previous session.
78+
79+
For example, the following code is fine because only the database
80+
instance is shared among threads:
81+
82+
```ruby
83+
require 'chdb'
84+
85+
db = ChDB::Database.new ":memory:'
86+
87+
latch = Queue.new
88+
89+
ts = 10.times.map {
90+
Thread.new {
91+
latch.pop
92+
db.execute 'SELECT 1'
93+
}
94+
}
95+
10.times { latch << nil }
96+
97+
p ts.map(&:value)
98+
```
99+
100+
Other instances can be shared among threads, but they require that you provide
101+
your own locking for thread safety. For example, `ChDB::Statement` objects
102+
(prepared statements) are mutable, so applications must take care to add
103+
appropriate locks to avoid data race conditions when sharing these objects
104+
among threads.
105+
106+
It is generally recommended that if applications want to share a database among
107+
threads, they _only_ share the database instance object. Other objects are
108+
fine to share, but may require manual locking for thread safety.
109+
110+
## Support
111+
112+
### Installation
113+
114+
If you're having trouble with installation, please first read [`INSTALLATION.md`](./INSTALLATION.md).
115+
116+
### Bug reports
117+
118+
You can file the bug at the [github issues page](https://github.com/chdb-io/chdb-ruby/issues).
119+
120+
## License
121+
122+
This library is licensed under `Apache License 2.0`, see [`LICENSE`](./LICENSE).

chdb.gemspec

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rescue LoadError
77
end
88

99
Gem::Specification.new do |s|
10-
s.name = 'chdb'
10+
s.name = 'chdb-ruby'
1111
s.version = defined?(ChDB::VERSION) ? ChDB::VERSION : '0.0.0'
1212

1313
s.summary = 'Ruby library to interface with the chDB database engine (https://clickhouse.com/docs/chdb).'
@@ -34,7 +34,8 @@ Gem::Specification.new do |s|
3434
}
3535

3636
s.files = [
37-
'CHANGELOG.md',
37+
# 'CHANGELOG.md',
38+
'INSTALLATION.md',
3839
'LICENSE',
3940
'README.md',
4041
'lib/chdb.rb',
@@ -56,5 +57,5 @@ Gem::Specification.new do |s|
5657

5758
s.add_dependency 'csv', '~> 3.1'
5859

59-
s.extensions << 'ext/chdb/extconf.rb'
60+
# s.extensions << 'ext/chdb/extconf.rb'
6061
end

dependencies.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
chdb:
2-
version: "3.1.1"
2+
version: "3.1.2"

ext/chdb/extconf.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module ChDB
99
module ExtConf
1010
class << self
1111
def configure
12+
configure_cross_compiler
13+
1214
download_and_extract
1315

1416
configure_extension
@@ -76,8 +78,8 @@ def determine_target_platform
7678
return ENV['TARGET'].strip if ENV['TARGET'] && !ENV['TARGET'].strip.empty?
7779

7880
case RUBY_PLATFORM
79-
when /aarch64-linux/ then 'aarch64-linux-gnu'
80-
when /x86_64-linux/ then 'x86_64-linux-gnu'
81+
when /aarch64-linux/ then 'aarch64-linux'
82+
when /x86_64-linux/ then 'x86_64-linux'
8183
when /arm64-darwin/ then 'arm64-darwin'
8284
when /x86_64-darwin/ then 'x86_64-darwin'
8385
else
@@ -96,8 +98,8 @@ def determine_download_directory(target_platform, version)
9698

9799
def get_file_name(target_platform)
98100
case target_platform
99-
when 'aarch64-linux-gnu' then 'linux-aarch64-libchdb.tar.gz'
100-
when 'x86_64-linux-gnu' then 'linux-x86_64-libchdb.tar.gz'
101+
when 'aarch64-linux' then 'linux-aarch64-libchdb.tar.gz'
102+
when 'x86_64-linux' then 'linux-x86_64-libchdb.tar.gz'
101103
when 'arm64-darwin' then 'macos-arm64-libchdb.tar.gz'
102104
when 'x86_64-darwin' then 'macos-x86_64-libchdb.tar.gz'
103105
else raise "Unsupported platform: #{target_platform}"
@@ -136,11 +138,11 @@ def copy_files(download_dir, _version)
136138
FileUtils.mkdir_p(dest)
137139
FileUtils.cp_r(Dir.glob(src), dest, remove_destination: true)
138140

139-
target_platform = determine_target_platform
140-
if target_platform.include?('darwin') && (pattern == '*.so')
141-
system("install_name_tool -id '@rpath/libchdb.so' #{File.join(dest, 'libchdb.so')}")
142-
system("codesign -f -s - #{File.join(dest, 'libchdb.so')}")
143-
end
141+
# target_platform = determine_target_platform
142+
# if target_platform.include?('darwin') && (pattern == '*.so')
143+
# system("install_name_tool -id '@rpath/libchdb.so' #{File.join(dest, 'libchdb.so')}")
144+
# system("codesign -f -s - #{File.join(dest, 'libchdb.so')}")
145+
# end
144146
end
145147
end
146148

lib/chdb/database.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# frozen_string_literal: true
22

3-
require 'chdb/chdb_native'
3+
begin
4+
RUBY_VERSION =~ /(\d+\.\d+)/
5+
require "chdb/#{Regexp.last_match(1)}/chdb_native"
6+
rescue LoadError
7+
require 'chdb/chdb_native'
8+
end
49
require 'chdb/data_path'
510
require 'chdb/statement'
611

lib/chdb/statement.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# frozen_string_literal: true
22

33
require 'csv'
4-
require 'chdb/chdb_native'
4+
begin
5+
RUBY_VERSION =~ /(\d+\.\d+)/
6+
require "chdb/#{Regexp.last_match(1)}/chdb_native"
7+
rescue LoadError
8+
require 'chdb/chdb_native'
9+
end
510
require 'chdb/local_result'
611
require 'chdb/result_set'
712
require 'chdb/result_handler'

lib/chdb/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
module ChDB
44
# (String) the version of the chdb gem, e.g. "0.1.0"
5-
VERSION = '0.1.0'
5+
VERSION = '0.1.0.rc.1'
66
end

rakelib/check_manifest.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ task :check_manifest do # rubocop:disable Metrics/BlockLength
4242
[0-9]*
4343
*.gemspec
4444
*.so
45+
CHANGELOG.md
46+
ext/chdb/extconf.rb
4547
]
4648

4749
intended_directories = Dir.children('.')

0 commit comments

Comments
 (0)