ilios 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,211 @@
1
+ #include "ilios.h"
2
+
3
+ static void statement_mark(void *ptr);
4
+ static void statement_destroy(void *ptr);
5
+ static size_t statement_memsize(const void *ptr);
6
+ static void statement_compact(void *ptr);
7
+
8
+ const rb_data_type_t cassandra_statement_data_type = {
9
+ "Ilios::Cassandra::Statement",
10
+ {
11
+ statement_mark,
12
+ statement_destroy,
13
+ statement_memsize,
14
+ statement_compact,
15
+ },
16
+ 0, 0,
17
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
18
+ };
19
+
20
+ void statement_default_config(CassandraStatement *cassandra_statement)
21
+ {
22
+ VALUE config = rb_cvar_get(mCassandra, id_cvar_config);
23
+
24
+ cass_statement_set_paging_size(cassandra_statement->statement, NUM2INT(rb_hash_aref(config, sym_page_size)));
25
+ }
26
+
27
+ static int hash_cb(VALUE key, VALUE value, VALUE statement)
28
+ {
29
+ CassandraStatement *cassandra_statement = (CassandraStatement *)statement;
30
+ const CassDataType* data_type;
31
+ CassValueType value_type;
32
+ CassError result;
33
+ const char *name;
34
+
35
+ if (SYMBOL_P(key)) {
36
+ key = rb_sym2str(key);
37
+ }
38
+ name = StringValueCStr(key);
39
+
40
+ data_type = cass_prepared_parameter_data_type_by_name(cassandra_statement->prepared, name);
41
+ if (data_type == NULL) {
42
+ rb_raise(eStatementError, "Invalid name %s was given.", name);
43
+ }
44
+ value_type = cass_data_type_type(data_type);
45
+
46
+ if (NIL_P(value)) {
47
+ result = cass_statement_bind_null_by_name(cassandra_statement->statement, name);
48
+ goto result_check;
49
+ }
50
+
51
+ switch (value_type) {
52
+ case CASS_VALUE_TYPE_TINY_INT:
53
+ {
54
+ long v = NUM2LONG(value);
55
+
56
+ if (v < INT8_MIN || v > INT8_MAX) {
57
+ rb_raise(rb_eRangeError, "Invalid value: %ld", v);
58
+ }
59
+ result = cass_statement_bind_int8_by_name(cassandra_statement->statement, name, (cass_int8_t)v);
60
+ }
61
+ break;
62
+
63
+ case CASS_VALUE_TYPE_SMALL_INT:
64
+ {
65
+ long v = NUM2LONG(value);
66
+
67
+ if (v < INT16_MIN || v > INT16_MAX) {
68
+ rb_raise(rb_eRangeError, "Invalid value: %ld", v);
69
+ }
70
+
71
+ result = cass_statement_bind_int16_by_name(cassandra_statement->statement, name, (cass_int16_t)v);
72
+ }
73
+ break;
74
+
75
+ case CASS_VALUE_TYPE_INT:
76
+ {
77
+ long v = NUM2LONG(value);
78
+
79
+ if (v < INT32_MIN || v > INT32_MAX) {
80
+ rb_raise(rb_eRangeError, "Invalid value: %ld", v);
81
+ }
82
+
83
+ result = cass_statement_bind_int32_by_name(cassandra_statement->statement, name, (cass_int32_t)v);
84
+ }
85
+ break;
86
+
87
+ case CASS_VALUE_TYPE_BIGINT:
88
+ result = cass_statement_bind_int64_by_name(cassandra_statement->statement, name, NUM2LONG(value));
89
+ break;
90
+
91
+ case CASS_VALUE_TYPE_FLOAT:
92
+ {
93
+ double v = NUM2DBL(value);
94
+
95
+ if (!isnan(v) && !isinf(v) && (v < -FLT_MAX || v > FLT_MAX)) {
96
+ rb_raise(rb_eRangeError, "Invalid value: %lf", v);
97
+ }
98
+
99
+ result = cass_statement_bind_float_by_name(cassandra_statement->statement, name, v);
100
+ }
101
+ break;
102
+
103
+ case CASS_VALUE_TYPE_DOUBLE:
104
+ result = cass_statement_bind_double_by_name(cassandra_statement->statement, name, NUM2DBL(value));
105
+ break;
106
+
107
+ case CASS_VALUE_TYPE_BOOLEAN:
108
+ {
109
+ cass_bool_t v = RTEST(value) ? cass_true : cass_false;
110
+ result = cass_statement_bind_bool_by_name(cassandra_statement->statement, name, v);
111
+ }
112
+ break;
113
+
114
+ case CASS_VALUE_TYPE_TEXT:
115
+ case CASS_VALUE_TYPE_ASCII:
116
+ case CASS_VALUE_TYPE_VARCHAR:
117
+ result = cass_statement_bind_string_by_name(cassandra_statement->statement, name, StringValueCStr(value));
118
+ break;
119
+
120
+ case CASS_VALUE_TYPE_TIMESTAMP:
121
+ if (rb_obj_class(value) != rb_cTime) {
122
+ if (rb_respond_to(value, id_to_time)) {
123
+ value = rb_funcall(value, id_to_time, 0);
124
+ } else {
125
+ rb_raise(rb_eTypeError, "no implicit conversion of %"PRIsVALUE" to Time", rb_obj_class(value));
126
+ }
127
+ }
128
+ result = cass_statement_bind_int64_by_name(cassandra_statement->statement, name, (cass_int64_t)(NUM2DBL(rb_Float(value)) * 1000));
129
+ break;
130
+
131
+ case CASS_VALUE_TYPE_UUID:
132
+ {
133
+ CassUuid uuid;
134
+ const char *uuid_string = StringValueCStr(value);
135
+
136
+ cass_uuid_from_string(uuid_string, &uuid);
137
+ result = cass_statement_bind_uuid_by_name(cassandra_statement->statement, name, uuid);
138
+ }
139
+ break;
140
+
141
+ default:
142
+ rb_raise(rb_eTypeError, "Unsupported %"PRIsVALUE" type: %s=%"PRIsVALUE"", rb_obj_class(value), name, value);
143
+ }
144
+
145
+ result_check:
146
+ if (result != CASS_OK) {
147
+ rb_raise(eStatementError, "Failed to bind value: %s", cass_error_desc(result));
148
+ }
149
+
150
+ return ST_CONTINUE;
151
+ }
152
+
153
+ static VALUE statement_bind(VALUE self, VALUE hash)
154
+ {
155
+ CassandraStatement *cassandra_statement;
156
+
157
+ Check_Type(hash, T_HASH);
158
+ TypedData_Get_Struct(self, CassandraStatement, &cassandra_statement_data_type, cassandra_statement);
159
+
160
+ rb_hash_foreach(hash, hash_cb, (VALUE)cassandra_statement);
161
+ return self;
162
+ }
163
+
164
+ static VALUE statement_page_size(VALUE self, VALUE page_size)
165
+ {
166
+ CassandraStatement *cassandra_statement;
167
+
168
+ GET_STATEMENT(self, cassandra_statement);
169
+ cass_statement_set_paging_size(cassandra_statement->statement, NUM2INT(page_size));
170
+ return self;
171
+ }
172
+
173
+ static void statement_mark(void *ptr)
174
+ {
175
+ CassandraStatement *cassandra_statement = (CassandraStatement *)ptr;
176
+ rb_gc_mark_movable(cassandra_statement->session_obj);
177
+ }
178
+
179
+ static void statement_destroy(void *ptr)
180
+ {
181
+ CassandraStatement *cassandra_statement = (CassandraStatement *)ptr;
182
+
183
+ if (cassandra_statement->prepared) {
184
+ cass_prepared_free(cassandra_statement->prepared);
185
+ }
186
+ if (cassandra_statement->statement) {
187
+ cass_statement_free(cassandra_statement->statement);
188
+ }
189
+ xfree(cassandra_statement);
190
+ }
191
+
192
+ static size_t statement_memsize(const void *ptr)
193
+ {
194
+ return sizeof(CassandraStatement);
195
+ }
196
+
197
+ static void statement_compact(void *ptr)
198
+ {
199
+ CassandraStatement *cassandra_statement = (CassandraStatement *)ptr;
200
+
201
+ cassandra_statement->session_obj = rb_gc_location(cassandra_statement->session_obj);
202
+ }
203
+
204
+
205
+ void Init_statement(void)
206
+ {
207
+ rb_undef_alloc_func(cStatement);
208
+
209
+ rb_define_method(cStatement, "bind", statement_bind, 1);
210
+ rb_define_method(cStatement, "page_size=", statement_page_size, 1);
211
+ }
data/ilios.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ilios/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ilios'
7
+ spec.version = Ilios::VERSION
8
+ spec.authors = ['Watson']
9
+ spec.email = ['watson1978@gmail.com']
10
+
11
+ spec.summary = 'Cassandra driver written by C language'
12
+ spec.description = 'Cassandra driver written by C language'
13
+ spec.homepage = 'https://github.com/Watson1978/ilios'
14
+ spec.required_ruby_version = '>= 3.0.0'
15
+
16
+ spec.requirements << 'cmake'
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/Watson1978/ilios'
20
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+ spec.metadata['rubygems_mfa_required'] = 'true'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files =
26
+ Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:test)/|\.(?:git|editorconfig|rubocop.*))})
29
+ end
30
+ end
31
+ spec.require_paths = ['lib']
32
+ spec.extensions << 'ext/ilios/extconf.rb'
33
+
34
+ spec.add_runtime_dependency('mini_portile2', '~> 2.8')
35
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ilios
4
+ VERSION = '0.1.0'
5
+ public_constant :VERSION
6
+
7
+ CASSANDRA_CPP_DRIVER_VERSION = '2.17.0'
8
+ public_constant :CASSANDRA_CPP_DRIVER_VERSION
9
+
10
+ LIBUV_VERSION = '1.46.0'
11
+ public_constant :LIBUV_VERSION
12
+ end
data/lib/ilios.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ilios.so'
4
+ require 'ilios/version'
5
+
6
+ module Ilios
7
+ module Cassandra
8
+ @@config = {
9
+ keyspace: 'ilios',
10
+ hosts: ['127.0.0.1'],
11
+ timeout_ms: 5_000,
12
+ constant_delay_ms: 15_000,
13
+ max_speculative_executions: 2,
14
+ page_size: 10_000
15
+ }
16
+
17
+ def self.config=(config)
18
+ @@config = @@config.merge(config)
19
+ end
20
+
21
+ def self.session
22
+ Thread.current[:ilios_cassandra_session] ||= connect
23
+ end
24
+ end
25
+ end
data/sig/ilios.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ilios
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ilios
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Watson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mini_portile2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.8'
27
+ description: Cassandra driver written by C language
28
+ email:
29
+ - watson1978@gmail.com
30
+ executables: []
31
+ extensions:
32
+ - ext/ilios/extconf.rb
33
+ extra_rdoc_files: []
34
+ files:
35
+ - Dockerfile
36
+ - Gemfile
37
+ - README.md
38
+ - Rakefile
39
+ - docker-compose.yml
40
+ - ext/ilios/cassandra.c
41
+ - ext/ilios/extconf.rb
42
+ - ext/ilios/future.c
43
+ - ext/ilios/ilios.c
44
+ - ext/ilios/ilios.h
45
+ - ext/ilios/nogvl.c
46
+ - ext/ilios/result.c
47
+ - ext/ilios/session.c
48
+ - ext/ilios/statement.c
49
+ - ilios.gemspec
50
+ - lib/ilios.rb
51
+ - lib/ilios/version.rb
52
+ - sig/ilios.rbs
53
+ homepage: https://github.com/Watson1978/ilios
54
+ licenses: []
55
+ metadata:
56
+ homepage_uri: https://github.com/Watson1978/ilios
57
+ source_code_uri: https://github.com/Watson1978/ilios
58
+ rubygems_mfa_required: 'true'
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.0.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements:
74
+ - cmake
75
+ rubygems_version: 3.4.10
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Cassandra driver written by C language
79
+ test_files: []