ignite-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e093caf963cfb87b79bbe650d161419f41557a90e024039ec395709fce977d3
4
+ data.tar.gz: 796674a722547b1e38c2a2b0a31b3ded504b1db80cd5dc5d3334154b90ba050f
5
+ SHA512:
6
+ metadata.gz: e1962bc2a2da0242d76f2476c3555260a4bea3c9920449567ea454e4d3802c5656514d1aa4612bb801c9605db94c7cc10d586925f3dd6ff78670b5d240b37ed0
7
+ data.tar.gz: c96ecb92666ae5ac5937a1a99b411a874def4c7879e5f7a59923c3079fc3278a847dd36fd0ba8bc1621bb9f07d5eacded85682c3bc69ae8e634b066272b93bb4
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (2021-02-02)
2
+
3
+ - First release
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # Ignite Ruby
2
+
3
+ :fire: Ruby client for [Apache Ignite](https://ignite.apache.org/)
4
+
5
+ [![Build Status](https://github.com/ankane/ignite-ruby/workflows/build/badge.svg?branch=master)](https://github.com/ankane/ignite-ruby/actions)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application’s Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ignite-client'
13
+ ```
14
+
15
+ ## Getting Started
16
+
17
+ Create a client
18
+
19
+ ```ruby
20
+ client = Ignite::Client.new
21
+ ```
22
+
23
+ See [connection options](#connection-options) for more info
24
+
25
+ ## Key-Value API
26
+
27
+ Create a cache
28
+
29
+ ```ruby
30
+ cache = client.get_or_create_cache("test")
31
+ ```
32
+
33
+ Add data
34
+
35
+ ```ruby
36
+ cache.put("hello", "world")
37
+ cache.get("hello")
38
+ ```
39
+
40
+ Supports these methods
41
+
42
+ ```ruby
43
+ cache.get(key)
44
+ cache.get_all(keys)
45
+ cache.put(key, value)
46
+ cache.put_all(objects)
47
+ cache.key?(key)
48
+ cache.keys?(keys)
49
+ cache.get_and_put(key, value)
50
+ cache.get_and_replace(key, value)
51
+ cache.get_and_remove(key)
52
+ cache.put_if_absent(key, value)
53
+ cache.get_and_put_if_absent(key, value)
54
+ cache.replace(key, value)
55
+ cache.replace_if_equals(key, compare, value)
56
+ cache.clear
57
+ cache.clear_key(key)
58
+ cache.clear_keys(keys)
59
+ cache.remove_key(key)
60
+ cache.remove_if_equals(key, compare)
61
+ cache.size
62
+ cache.remove_keys(keys)
63
+ cache.remove_all
64
+ ```
65
+
66
+ ## Scan Queries
67
+
68
+ Scan objects
69
+
70
+ ```ruby
71
+ cache.scan do |k, v|
72
+ # ...
73
+ end
74
+ ```
75
+
76
+ ## SQL
77
+
78
+ Execute SQL queries
79
+
80
+ ```ruby
81
+ client.query("SELECT * FROM users")
82
+ ```
83
+
84
+ Pass arguments
85
+
86
+ ```ruby
87
+ client.query("SELECT * FROM products WHERE name = ?", ["Ignite"])
88
+ ```
89
+
90
+ ## Connection Options
91
+
92
+ Specify a host and port
93
+
94
+ ```ruby
95
+ Ignite::Client.new(host: "localhost", port: 10800)
96
+ ```
97
+
98
+ ## Authentication
99
+
100
+ For [authentication](https://ignite.apache.org/docs/latest/security/authentication), use:
101
+
102
+ ```ruby
103
+ Ignite::Client.new(username: "ignite", password: "ignite")
104
+ ```
105
+
106
+ SSL is automatically enabled when credentials are supplied. To disable, use:
107
+
108
+ ```ruby
109
+ Ignite::Client.new(username: "ignite", password: "ignite", use_ssl: false)
110
+ ```
111
+
112
+ ## SSL/TLS
113
+
114
+ For [SSL/TLS](https://ignite.apache.org/docs/latest/security/ssl-tls#ssl-for-clients), use:
115
+
116
+ ```ruby
117
+ Ignite::Client.new(
118
+ use_ssl: true,
119
+ ssl_params: {
120
+ verify_mode: OpenSSL::SSL::VERIFY_PEER,
121
+ ca_file: "ca.pem"
122
+ }
123
+ )
124
+ ```
125
+
126
+ Supports all OpenSSL params
127
+
128
+ ## History
129
+
130
+ View the [changelog](https://github.com/ankane/ignite-ruby/blob/master/CHANGELOG.md)
131
+
132
+ ## Contributing
133
+
134
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
135
+
136
+ - [Report bugs](https://github.com/ankane/ignite-ruby/issues)
137
+ - Fix bugs and [submit pull requests](https://github.com/ankane/ignite-ruby/pulls)
138
+ - Write, clarify, or fix documentation
139
+ - Suggest or add new features
140
+
141
+ To get started with development:
142
+
143
+ ```sh
144
+ git clone https://github.com/ankane/ignite-ruby.git
145
+ cd ignite-ruby
146
+ bundle install
147
+ bundle exec rake test
148
+ ```
data/lib/ignite.rb ADDED
@@ -0,0 +1,17 @@
1
+ # stdlib
2
+ require "bigdecimal"
3
+ require "date"
4
+ require "openssl"
5
+ require "socket"
6
+
7
+ # modules
8
+ require "ignite/cache"
9
+ require "ignite/op_codes"
10
+ require "ignite/request"
11
+ require "ignite/response"
12
+ require "ignite/version"
13
+
14
+ module Ignite
15
+ class Error < StandardError; end
16
+ class HandshakeError < Error; end
17
+ end
@@ -0,0 +1,306 @@
1
+ module Ignite
2
+ class Cache
3
+ attr_reader :cache_id, :client, :name
4
+
5
+ def initialize(client, name)
6
+ @client = client
7
+ @name = name
8
+ @cache_id = hash_code(name)
9
+ end
10
+
11
+ def get(key)
12
+ req = Request.new(OP_CACHE_GET)
13
+ req.int cache_id
14
+ req.byte 0
15
+ req.data_object key
16
+
17
+ res = client.send_request(req)
18
+ res.read_data_object
19
+ end
20
+
21
+ def get_all(keys)
22
+ req = Request.new(OP_CACHE_GET_ALL)
23
+ req.int cache_id
24
+ req.byte 0
25
+ req.int keys.size
26
+ keys.each do |key|
27
+ req.data_object key
28
+ end
29
+
30
+ res = client.send_request(req)
31
+ result = {}
32
+ res.read_int.times do
33
+ result[res.read_data_object] = res.read_data_object
34
+ end
35
+ result
36
+ end
37
+
38
+ def put(key, value)
39
+ req = Request.new(OP_CACHE_PUT)
40
+ req.int cache_id
41
+ req.byte 0
42
+ req.data_object key
43
+ req.data_object value
44
+
45
+ client.send_request(req)
46
+ nil
47
+ end
48
+
49
+ def put_all(objects)
50
+ req = Request.new(OP_CACHE_PUT_ALL)
51
+ req.int cache_id
52
+ req.byte 0
53
+ req.int objects.size
54
+ objects.each do |key, value|
55
+ req.data_object key
56
+ req.data_object value
57
+ end
58
+
59
+ client.send_request(req)
60
+ nil
61
+ end
62
+
63
+ def key?(key)
64
+ req = Request.new(OP_CACHE_CONTAINS_KEY)
65
+ req.int cache_id
66
+ req.byte 0
67
+ req.data_object key
68
+
69
+ client.send_request(req).read_bool
70
+ end
71
+ alias_method :contains_key, :key?
72
+
73
+ def keys?(keys)
74
+ req = Request.new(OP_CACHE_CONTAINS_KEYS)
75
+ req.int cache_id
76
+ req.byte 0
77
+ req.int keys.size
78
+ keys.each do |key|
79
+ req.data_object key
80
+ end
81
+
82
+ client.send_request(req).read_bool
83
+ end
84
+ alias_method :contains_keys, :keys?
85
+
86
+ def get_and_put(key, value)
87
+ req = Request.new(OP_CACHE_GET_AND_PUT)
88
+ req.int cache_id
89
+ req.byte 0
90
+ req.data_object key
91
+ req.data_object value
92
+
93
+ client.send_request(req).read_data_object
94
+ end
95
+
96
+ def get_and_replace(key, value)
97
+ req = Request.new(OP_CACHE_GET_AND_REPLACE)
98
+ req.int cache_id
99
+ req.byte 0
100
+ req.data_object key
101
+ req.data_object value
102
+
103
+ client.send_request(req).read_data_object
104
+ end
105
+
106
+ def get_and_remove(key)
107
+ req = Request.new(OP_CACHE_GET_AND_REMOVE)
108
+ req.int cache_id
109
+ req.byte 0
110
+ req.data_object key
111
+
112
+ client.send_request(req).read_data_object
113
+ end
114
+
115
+ def put_if_absent(key, value)
116
+ req = Request.new(OP_CACHE_PUT_IF_ABSENT)
117
+ req.int cache_id
118
+ req.byte 0
119
+ req.data_object key
120
+ req.data_object value
121
+
122
+ client.send_request(req).read_bool
123
+ end
124
+
125
+ def get_and_put_if_absent(key, value)
126
+ req = Request.new(OP_CACHE_GET_AND_PUT_IF_ABSENT)
127
+ req.int cache_id
128
+ req.byte 0
129
+ req.data_object key
130
+ req.data_object value
131
+
132
+ client.send_request(req).read_data_object
133
+ end
134
+
135
+ def replace(key, value)
136
+ req = Request.new(OP_CACHE_REPLACE)
137
+ req.int cache_id
138
+ req.byte 0
139
+ req.data_object key
140
+ req.data_object value
141
+
142
+ client.send_request(req).read_bool
143
+ end
144
+
145
+ def replace_if_equals(key, compare, value)
146
+ req = Request.new(OP_CACHE_REPLACE_IF_EQUALS)
147
+ req.int cache_id
148
+ req.byte 0
149
+ req.data_object key
150
+ req.data_object compare
151
+ req.data_object value
152
+
153
+ client.send_request(req).read_bool
154
+ end
155
+
156
+ def clear
157
+ req = Request.new(OP_CACHE_CLEAR)
158
+ req.int cache_id
159
+ req.byte 0
160
+
161
+ client.send_request(req)
162
+ nil
163
+ end
164
+
165
+ def clear_key(key)
166
+ req = Request.new(OP_CACHE_CLEAR_KEY)
167
+ req.int cache_id
168
+ req.byte 0
169
+ req.data_object key
170
+
171
+ client.send_request(req)
172
+ nil
173
+ end
174
+
175
+ def clear_keys(keys)
176
+ req = Request.new(OP_CACHE_CLEAR_KEYS)
177
+ req.int cache_id
178
+ req.byte 0
179
+ req.int keys.size
180
+ keys.each do |key|
181
+ req.data_object key
182
+ end
183
+
184
+ client.send_request(req)
185
+ nil
186
+ end
187
+
188
+ def remove_key(key)
189
+ req = Request.new(OP_CACHE_REMOVE_KEY)
190
+ req.int cache_id
191
+ req.byte 0
192
+ req.data_object key
193
+
194
+ client.send_request(req).read_bool
195
+ end
196
+
197
+ def remove_if_equals(key, compare)
198
+ req = Request.new(OP_CACHE_REMOVE_IF_EQUALS)
199
+ req.int cache_id
200
+ req.byte 0
201
+ req.data_object key
202
+ req.data_object compare
203
+
204
+ client.send_request(req).read_bool
205
+ end
206
+
207
+ # TODO add arguments
208
+ def size
209
+ req = Request.new(OP_CACHE_GET_SIZE)
210
+ req.int cache_id
211
+ req.byte 0
212
+ req.int 0
213
+ req.byte 0
214
+
215
+ client.send_request(req).read_long
216
+ end
217
+ alias_method :get_size, :size
218
+
219
+ def remove_keys(keys)
220
+ req = Request.new(OP_CACHE_REMOVE_KEYS)
221
+ req.int cache_id
222
+ req.byte 0
223
+ req.int keys.size
224
+ keys.each do |key|
225
+ req.data_object key
226
+ end
227
+
228
+ client.send_request(req)
229
+ nil
230
+ end
231
+
232
+ def remove_all
233
+ req = Request.new(OP_CACHE_REMOVE_ALL)
234
+ req.int cache_id
235
+ req.byte 0
236
+
237
+ client.send_request(req)
238
+ nil
239
+ end
240
+
241
+ def scan(page_size: 1000)
242
+ return to_enum(:scan, page_size: page_size) unless block_given?
243
+
244
+ # TODO filter
245
+ filter = nil
246
+
247
+ req = Request.new(OP_QUERY_SCAN)
248
+ req.int cache_id
249
+ req.byte 0
250
+ req.data_object filter
251
+ req.byte 0 unless filter.nil?
252
+ req.int page_size
253
+ req.int(-1)
254
+ req.bool false
255
+
256
+ res = client.send_request(req)
257
+ cursor_id = res.read_long
258
+ row_count = res.read_int
259
+ row_count.times do
260
+ yield res.read_data_object, res.read_data_object
261
+ end
262
+ more_results = res.read_bool
263
+
264
+ while more_results
265
+ req = Request.new(OP_QUERY_SCAN_CURSOR_GET_PAGE)
266
+ req.long cursor_id
267
+
268
+ # docs for OP_QUERY_SCAN_CURSOR_GET_PAGE response are incorrect
269
+ # 1. no cursor_id
270
+ # 2. row_count is int, not log
271
+ res = client.send_request(req)
272
+ row_count = res.read_int
273
+ row_count.times do
274
+ yield res.read_data_object, res.read_data_object
275
+ end
276
+ more_results = res.read_bool
277
+ end
278
+ end
279
+
280
+ def get_or_create
281
+ req = Request.new(OP_CACHE_GET_OR_CREATE_WITH_NAME)
282
+ req.string name
283
+ client.send_request(req)
284
+ self
285
+ end
286
+
287
+ def destroy
288
+ req = Request.new(OP_CACHE_DESTROY)
289
+ req.int cache_id
290
+ client.send_request(req)
291
+ nil
292
+ end
293
+
294
+ private
295
+
296
+ # same as Python
297
+ # https://ignite.apache.org/docs/latest/binary-client-protocol/data-format#hash-code
298
+ def hash_code(string)
299
+ result = 0
300
+ string.each_byte do |char|
301
+ result = (((31 * result + char.ord) ^ 0x80000000) & 0xffffffff) - 0x80000000
302
+ end
303
+ result
304
+ end
305
+ end
306
+ end
@@ -0,0 +1,172 @@
1
+ require "ignite"
2
+
3
+ module Ignite
4
+ class Client
5
+ def initialize(host: "localhost", port: 10800, username: nil, password: nil, use_ssl: nil, ssl_params: {})
6
+ @socket = TCPSocket.new(host, port)
7
+
8
+ use_ssl = use_ssl.nil? ? (username || password) : use_ssl
9
+ if use_ssl
10
+ ssl_context = OpenSSL::SSL::SSLContext.new
11
+
12
+ # very important!!
13
+ # call set_params so default params are applied
14
+ # (like min_version and verify_mode)
15
+ ssl_context.set_params(ssl_params)
16
+
17
+ @socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
18
+ @socket.sync_close = true
19
+ @socket.connect
20
+ end
21
+
22
+ send_handshake(username, password)
23
+ end
24
+
25
+ def close
26
+ @socket.close
27
+ end
28
+
29
+ def cache(name)
30
+ Cache.new(self, name)
31
+ end
32
+
33
+ def get_or_create_cache(name)
34
+ cache(name).get_or_create
35
+ end
36
+
37
+ def caches
38
+ req = Request.new(OP_CACHE_GET_NAMES)
39
+
40
+ res = send_request(req)
41
+ cache_count = res.read_int
42
+ cache_count.times.map { cache(res.read_string_object) }
43
+ end
44
+
45
+ def query(statement, args = [], schema: "PUBLIC", page_size: 1000, max_rows: nil, statement_type: :any, timeout: nil)
46
+ statement_type = [:any, :select, :update].index(statement_type)
47
+ raise ArgumentError, "Invalid statement type" unless statement_type
48
+
49
+ schema = get_or_create_cache(schema)
50
+
51
+ req = Request.new(OP_QUERY_SQL_FIELDS)
52
+ req.int schema.cache_id
53
+ req.byte 0
54
+ req.string schema.name
55
+ req.int page_size
56
+ req.int(max_rows || -1)
57
+ req.string statement
58
+ req.int args.size
59
+ args.each do |arg|
60
+ req.data_object arg
61
+ end
62
+ req.byte statement_type
63
+ req.bool false
64
+ req.bool false
65
+ req.bool false
66
+ req.bool false
67
+ req.bool false
68
+ req.bool false
69
+ req.long(timeout || 0)
70
+ req.bool true
71
+
72
+ res = send_request(req)
73
+ cursor_id = res.read_long
74
+ field_count = res.read_int
75
+ field_names = []
76
+ field_count.times do
77
+ field_names << res.read_string_object
78
+ end
79
+
80
+ rows = []
81
+ row_count = res.read_int
82
+ row_count.times do
83
+ row = {}
84
+ field_names.each do |field_name|
85
+ row[field_name] = res.read_data_object
86
+ end
87
+ rows << row
88
+ end
89
+ more_results = res.read_bool
90
+
91
+ while more_results && (!max_rows || rows.size < max_rows)
92
+ req = Request.new(OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE)
93
+ req.long cursor_id
94
+
95
+ res = send_request(req)
96
+ row_count = res.read_int
97
+ row_count.times do
98
+ row = {}
99
+ field_names.each do |field_name|
100
+ row[field_name] = res.read_data_object
101
+ end
102
+ rows << row
103
+ end
104
+ more_results = res.read_bool
105
+ end
106
+
107
+ if max_rows && rows.size > max_rows
108
+ rows.pop(rows.size - max_rows)
109
+ end
110
+
111
+ rows
112
+ end
113
+
114
+ def close_resource(resource_id)
115
+ req = Request.new(OP_RESOURCE_CLOSE)
116
+ req.long resource_id
117
+ send_request(req)
118
+ nil
119
+ end
120
+
121
+ # internal
122
+ def read(len)
123
+ @socket.read(len)
124
+ end
125
+
126
+ # internal
127
+ def send_request(req)
128
+ @socket.write(req.to_bytes)
129
+ res = Response.new(self)
130
+ check_header res
131
+ res
132
+ end
133
+
134
+ private
135
+
136
+ def check_header(res)
137
+ _req_id = res.read_long
138
+ status = res.read_int
139
+
140
+ if status != OP_SUCCESS
141
+ raise Error, res.read_string_object
142
+ end
143
+ end
144
+
145
+ def send_handshake(username, password)
146
+ req = Request.new(OP_HANDSHAKE)
147
+ req.byte 1
148
+ req.short 1
149
+ req.short 2
150
+ req.short 0
151
+ req.byte 2
152
+ if username || password
153
+ req.string username
154
+ req.string password
155
+ end
156
+ @socket.write(req.to_bytes)
157
+
158
+ res = Response.new(self)
159
+ check_handshake res
160
+ end
161
+
162
+ def check_handshake(res)
163
+ status = res.read_byte
164
+ if status != 1
165
+ _server_version_major = res.read_short
166
+ _server_version_minor = res.read_short
167
+ _server_version_patch = res.read_short
168
+ raise HandshakeError, res.read_string_object
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,50 @@
1
+ # same as Python
2
+ module Ignite
3
+ OP_SUCCESS = 0
4
+
5
+ OP_RESOURCE_CLOSE = 0
6
+
7
+ OP_HANDSHAKE = 1
8
+
9
+ OP_CACHE_GET = 1000
10
+ OP_CACHE_PUT = 1001
11
+ OP_CACHE_PUT_IF_ABSENT = 1002
12
+ OP_CACHE_GET_ALL = 1003
13
+ OP_CACHE_PUT_ALL = 1004
14
+ OP_CACHE_GET_AND_PUT = 1005
15
+ OP_CACHE_GET_AND_REPLACE = 1006
16
+ OP_CACHE_GET_AND_REMOVE = 1007
17
+ OP_CACHE_GET_AND_PUT_IF_ABSENT = 1008
18
+ OP_CACHE_REPLACE = 1009
19
+ OP_CACHE_REPLACE_IF_EQUALS = 1010
20
+ OP_CACHE_CONTAINS_KEY = 1011
21
+ OP_CACHE_CONTAINS_KEYS = 1012
22
+ OP_CACHE_CLEAR = 1013
23
+ OP_CACHE_CLEAR_KEY = 1014
24
+ OP_CACHE_CLEAR_KEYS = 1015
25
+ OP_CACHE_REMOVE_KEY = 1016
26
+ OP_CACHE_REMOVE_IF_EQUALS = 1017
27
+ OP_CACHE_REMOVE_KEYS = 1018
28
+ OP_CACHE_REMOVE_ALL = 1019
29
+ OP_CACHE_GET_SIZE = 1020
30
+
31
+ OP_CACHE_GET_NAMES = 1050
32
+ OP_CACHE_CREATE_WITH_NAME = 1051
33
+ OP_CACHE_GET_OR_CREATE_WITH_NAME = 1052
34
+ OP_CACHE_CREATE_WITH_CONFIGURATION = 1053
35
+ OP_CACHE_GET_OR_CREATE_WITH_CONFIGURATION = 1054
36
+ OP_CACHE_GET_CONFIGURATION = 1055
37
+ OP_CACHE_DESTROY = 1056
38
+
39
+ OP_QUERY_SCAN = 2000
40
+ OP_QUERY_SCAN_CURSOR_GET_PAGE = 2001
41
+ OP_QUERY_SQL = 2002
42
+ OP_QUERY_SQL_CURSOR_GET_PAGE = 2003
43
+ OP_QUERY_SQL_FIELDS = 2004
44
+ OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE = 2005
45
+
46
+ P_GET_BINARY_TYPE_NAME = 3000
47
+ OP_REGISTER_BINARY_TYPE_NAME = 3001
48
+ OP_GET_BINARY_TYPE = 3002
49
+ OP_PUT_BINARY_TYPE = 3003
50
+ end
@@ -0,0 +1,84 @@
1
+ module Ignite
2
+ class Request
3
+ MIN_LONG = -9223372036854775808 # -2**63
4
+ MAX_LONG = 9223372036854775807 # 2**63-1
5
+
6
+ def initialize(op_code)
7
+ @buffer = String.new
8
+ int 0 # length placeholder
9
+
10
+ if op_code != OP_HANDSHAKE
11
+ short op_code
12
+ long rand(MIN_LONG..MAX_LONG) # request id
13
+ end
14
+ end
15
+
16
+ def to_bytes
17
+ # update length
18
+ @buffer[0..3] = [@buffer.bytesize - 4].pack("i!<")
19
+ @buffer
20
+ end
21
+
22
+ def bool(value)
23
+ byte(value ? 1 : 0)
24
+ end
25
+
26
+ def byte(value)
27
+ [value].pack("C", buffer: @buffer)
28
+ end
29
+
30
+ def short(value)
31
+ [value].pack("s!<", buffer: @buffer)
32
+ end
33
+
34
+ def int(value)
35
+ [value].pack("i!<", buffer: @buffer)
36
+ end
37
+
38
+ def long(value)
39
+ [value].pack("l!<", buffer: @buffer)
40
+ end
41
+
42
+ def float(value)
43
+ [value].pack("e", buffer: @buffer)
44
+ end
45
+
46
+ def double(value)
47
+ [value].pack("E", buffer: @buffer)
48
+ end
49
+
50
+ def string(value)
51
+ byte 9
52
+ int value.bytesize
53
+ @buffer << value
54
+ end
55
+
56
+ def data_object(value)
57
+ case value
58
+ when Integer
59
+ byte 4
60
+ long value
61
+ when Float
62
+ byte 6
63
+ double value
64
+ when TrueClass, FalseClass
65
+ byte 8
66
+ bool value
67
+ when String
68
+ string value
69
+ when Date
70
+ byte 11
71
+ time = value.to_time
72
+ long(time.to_i * 1000 + (time.nsec / 1000000))
73
+ when Time
74
+ byte 33
75
+ long(value.to_i * 1000 + (value.nsec / 1000000))
76
+ int value.nsec % 1000000
77
+ when NilClass
78
+ byte 101
79
+ else
80
+ raise "Unknown type: #{value.class.name}"
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,127 @@
1
+ module Ignite
2
+ class Response
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+
8
+ # use buffer so errors don't leave unread data on socket
9
+ len = client.read(4).unpack1("i!<")
10
+ @buffer = StringIO.new(client.read(len))
11
+ end
12
+
13
+ def read(len)
14
+ @buffer.read(len)
15
+ end
16
+
17
+ def read_byte
18
+ read(1).unpack1("C")
19
+ end
20
+
21
+ def read_short
22
+ read(2).unpack1("s!<")
23
+ end
24
+
25
+ def read_int
26
+ read(4).unpack1("i!<")
27
+ end
28
+
29
+ def read_long
30
+ read(8).unpack1("l!<")
31
+ end
32
+
33
+ def read_float
34
+ read(4).unpack1("e")
35
+ end
36
+
37
+ def read_double
38
+ read(8).unpack1("E")
39
+ end
40
+
41
+ def read_char
42
+ read(1).unpack1("c")
43
+ end
44
+
45
+ def read_bool
46
+ read_byte != 0
47
+ end
48
+
49
+ def read_string
50
+ len = read_int
51
+ read(len)
52
+ end
53
+
54
+ def read_string_object
55
+ type = read_byte
56
+ raise "Expected string, not type #{type}" unless type == 9
57
+ read_string
58
+ end
59
+
60
+ def read_date
61
+ msecs_since_epoch = read_long
62
+ sec = msecs_since_epoch / 1000
63
+ Time.at(sec).to_date
64
+ end
65
+
66
+ # same as Python
67
+ def read_decimal
68
+ scale = read_int
69
+ length = read_int
70
+ data = read(length).unpack("C*")
71
+
72
+ sign = (data[0] & 0x80) != 0
73
+ data[0] = data[0] & 0x7f
74
+
75
+ result = 0
76
+ data.reverse.each_with_index do |v, i|
77
+ result += v * 0x100 ** i
78
+ end
79
+
80
+ result = result / BigDecimal("10") ** BigDecimal(scale)
81
+ result = -result if sign
82
+ result
83
+ end
84
+
85
+ def read_timestamp
86
+ msecs_since_epoch = read_long
87
+ msec_fraction_in_nsecs = read_int
88
+ sec = msecs_since_epoch / 1000
89
+ nsec = (msecs_since_epoch % 1000) * 1000000 + msec_fraction_in_nsecs
90
+ Time.at(sec, nsec, :nanosecond)
91
+ end
92
+
93
+ def read_data_object
94
+ type_code = read_byte
95
+ case type_code
96
+ when 1
97
+ read_byte
98
+ when 2
99
+ read_short
100
+ when 3
101
+ read_int
102
+ when 4
103
+ read_long
104
+ when 5
105
+ read_float
106
+ when 6
107
+ read_double
108
+ when 7
109
+ read_char
110
+ when 8
111
+ read_bool
112
+ when 9
113
+ read_string
114
+ when 11
115
+ read_date
116
+ when 30
117
+ read_decimal
118
+ when 33
119
+ read_timestamp
120
+ when 101
121
+ nil
122
+ else
123
+ raise Error, "Type not supported yet: #{type_code}. Please create an issue."
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,3 @@
1
+ module Ignite
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ignite-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: andrew@ankane.org
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/ignite.rb
23
+ - lib/ignite/cache.rb
24
+ - lib/ignite/client.rb
25
+ - lib/ignite/op_codes.rb
26
+ - lib/ignite/request.rb
27
+ - lib/ignite/response.rb
28
+ - lib/ignite/version.rb
29
+ homepage: https://github.com/ankane/ignite-ruby
30
+ licenses:
31
+ - Apache-2.0
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '2.6'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.2.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Ruby client for Apache Ignite
52
+ test_files: []