xfiredb 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bd830b09b250371533632ecf4aa70b9f9d40a7d
4
- data.tar.gz: 63c8d424676a7b44b615a7be898c66e5248ffed7
3
+ metadata.gz: d11e78d17ca8c37ae5988f4f51d116bc6c8d5b95
4
+ data.tar.gz: ccb4c25286946a02a5b22b99afc9334510093a9e
5
5
  SHA512:
6
- metadata.gz: 3ac853af16c4d8341f9d7e8076adc4e9f3c836ac8cfd1b7270c65ebea89ea08706e1f183bfb12673da199a1080d4d69fd942c02afd2bbb08a28d904a01032897
7
- data.tar.gz: baef7b08a54e305e05040b95f14f9ce118ab85c958086e05b7a3af8063d9fcf44027aca048ee5a98537e6401c35dfb408e3676c5a8476d4b6b6b787017345cfa
6
+ metadata.gz: 9a196ec5d624c61c21ebd4074ddf00cf040e7b8709c0683a37a8918c7657714c6db3f2a61166931a3ef29eeff1708d0df2a99414e524a5bbfe06a9ed773107c1
7
+ data.tar.gz: ec4329bb304621dcd0299da0c901086ddbfa4307ce9f8e91e1d2955dd335b164c62be5feb00d9645b635f41042f9c4df57b41c5da7b3fc955aae7c96af477d30
data/lib/xfiredb.rb CHANGED
@@ -18,6 +18,7 @@
18
18
 
19
19
  require 'socket'
20
20
  require 'openssl'
21
+ require 'set'
21
22
 
22
23
  require 'xfiredb/socket'
23
24
  require 'xfiredb/version'
@@ -69,12 +69,9 @@ module XFireDB
69
69
  reply.chomp! if reply
70
70
  num = reply.scan(/\ /).count + 1
71
71
 
72
- puts num
73
-
74
72
  num.times do
75
73
  data = @socket.gets
76
74
  data.chomp! if data
77
- puts data
78
75
  res = XFireDB::Result.new data
79
76
  res.process
80
77
  yield res
@@ -102,6 +99,262 @@ module XFireDB
102
99
  def ssl?
103
100
  @ssl
104
101
  end
102
+
103
+ # -- String commands --- #
104
+
105
+ # Lookup a string key.
106
+ # @param [String] key Key to lookup.
107
+ # @return [String] The value of key. nil is returned if the key doesn't exist.
108
+ def get(key)
109
+ str = nil
110
+ self.query("GET #{key}") {|result|
111
+ str = result.data unless result.null?
112
+ }
113
+
114
+ str
115
+ end
116
+
117
+ # Set a key to a string value.
118
+ # @param [String] key Key to set.
119
+ # @param [String] data Data to set the key to.
120
+ # @return [Boolean] True on success, false otherwise.
121
+ def set(key, data)
122
+ success = false
123
+ self.query("SET #{key} \"#{data}\"") { |result|
124
+ success = result.success?
125
+ }
126
+ success
127
+ end
128
+
129
+ # Delete a key.
130
+ # @param [String] key Key to delete.
131
+ # @return [Boolean] True on success, false otherwise.
132
+ def delete(key)
133
+ success = false
134
+ self.query("DELETE #{key}") {|result|
135
+ success = result.success?
136
+ }
137
+ success
138
+ end
139
+
140
+ # Add a hashmap node to a hashmap.
141
+ # @param [String] key Hashmap to add a key to.
142
+ # @param [String] hkey Hashmap key to add.
143
+ # @param [String] data Data to store under hkey.
144
+ # @return [Boolean] True on success, false otherwise.
145
+ def map_add(key, hkey, data)
146
+ success = false
147
+ self.query("MADD #{key} #{hkey} \"#{data}\"") {|result|
148
+ success = result.success?
149
+ }
150
+ success
151
+ end
152
+
153
+ # Reference a hashmap.
154
+ # @param [String] key Hashmap to lookup.
155
+ # @param [String] args List of hashmap keys to lookup.
156
+ # @return [Hash] Hashmap of the lookup keys.
157
+ #
158
+ # @example map_ref
159
+ # map_ref('test-map', 'key1', 'key2', 'key3')
160
+ #
161
+ # @example returned data
162
+ # key1 => Key1 data
163
+ # key2 => nil # This key didn't exist in 'test-map'
164
+ # key3 => Key3 data
165
+ def map_ref(key, *args)
166
+ map = Hash.new
167
+ hkeys = args.join(' ')
168
+ i = 0
169
+ self.query("MREF #{key} #{hkeys}") {|result|
170
+ if result.success?
171
+ map[args[i]] = result.data
172
+ else
173
+ map[args[i]] = nil
174
+ end
175
+ i += 1
176
+ }
177
+
178
+ map
179
+ end
180
+
181
+ # Clear a hashmap.
182
+ # @param [String] key Hashmap to clear.
183
+ # @return [Boolean] True on success, false otherwise.
184
+ def map_clear(key)
185
+ self.delete(key)
186
+ end
187
+
188
+ # Get the size of a hashmap.
189
+ # @param [String] key Hashmap to get the size of.
190
+ # @return [Fixnum] Number of hashmap entry's in the hashmap.
191
+ def map_size(key)
192
+ size = 0
193
+ self.query("MSIZE #{key}") {|result|
194
+ size = result.data if result.success?
195
+ }
196
+ size
197
+ end
198
+
199
+ # Delete a number of keys from a hashmap.
200
+ # @param [String] key Hashmap to delete from.
201
+ # @param [String] args List of keys to delete.
202
+ # @return [Fixnum] Number of keys deleted.
203
+ #
204
+ # map_delete('test-map', 'key1', 'key2', 'key3') will
205
+ # attempt to delete the keys key1, key2 and key3 from the
206
+ # hashmap stored at 'test-map'.
207
+ def map_delete(key, *args)
208
+ num = 0
209
+ hkeys = args.join ' '
210
+ self.query("MDEL #{key} #{hkeys}") {|result|
211
+ num = result.data if result.success?
212
+ }
213
+ num
214
+ end
215
+
216
+ # Check if a key is included in a set.
217
+ # @param [String] key Set to look into.
218
+ # @param [String] set_key The set will be checked for this key.
219
+ # @return [Boolean] True if set_key exists, false otherwise.
220
+ def set_include?(key, set_key)
221
+ included = false
222
+ self.query("SINCLUDE #{key} \"#{set_key}\"") {|result|
223
+ included = result.data if result.success?
224
+ }
225
+ included
226
+ end
227
+
228
+ # Add one or more keys to a set.
229
+ # @param [String] key Set to add the keys to.
230
+ # @param [String] args Keys which have to be added.
231
+ # @return [Fixnum] Number of keys added.
232
+ # @example set_add
233
+ # set_add('test-set', 'set key 1', 'set key 2', 'set key 3')
234
+ def set_add(key, *args)
235
+ args = array_add_quotes(args)
236
+ keys = args.join ' '
237
+ num = 0
238
+ self.query("SADD #{key} #{keys}") {|result|
239
+ num = result.data if result.success?
240
+ }
241
+ num
242
+ end
243
+
244
+ # Delete one or more keys from a set.
245
+ # @param [String] key Set to delete from.
246
+ # @param [String] args Keys to delete from the set.
247
+ # @return [Fixnum] Number of keys deleted.
248
+ def set_delete(key, *args)
249
+ args = array_add_quotes(args)
250
+ keys = args.join ' '
251
+ num = 0
252
+ self.query("SDEL #{key} #{keys}") {|result|
253
+ num = result.data if result.success?
254
+ }
255
+ num
256
+ end
257
+
258
+ # Clear out an entire set.
259
+ # @param [String] key Set to delete.
260
+ # @return [Boolean] True on success, false otherwise.
261
+ def set_clear(key)
262
+ self.delete(key)
263
+ end
264
+
265
+ # Add data to a list.
266
+ # @param [String] key List to push into.
267
+ # @param [String] data Data to push into the list.
268
+ # @return [Boolean] True on success, false otherwise.
269
+ def list_push(key, data)
270
+ success = false
271
+ self.query("LPUSH #{key} \"#{data}\"") {|result|
272
+ success = result.success?
273
+ }
274
+ success
275
+ end
276
+
277
+ # Reference a list by index or range.
278
+ # @param [String] key List to reference.
279
+ # @param [String] idx Index or range.
280
+ # @return [Array] Array of the results.
281
+ #
282
+ # @example by index
283
+ # list_ref('test-list', 1)
284
+ # @example by range
285
+ # list_ref('test-list', '0..-1')
286
+ def list_ref(key, idx)
287
+ ary = Array.new
288
+ self.query("LREF #{key} #{idx}") {|result|
289
+ if result.success?
290
+ ary.push result.data
291
+ else
292
+ ary.push nil
293
+ end
294
+ }
295
+ ary
296
+ end
297
+
298
+ # Pop elements from a list by index or range.
299
+ # @param [String] key List to reference.
300
+ # @param [String] idx Index or range.
301
+ # @return [Array] Array of the results.
302
+ #
303
+ # @example by index
304
+ # list_pop('test-list', 1)
305
+ # @example by range
306
+ # list_pop('test-list', '0..-1')
307
+ def list_pop(key, idx)
308
+ ary = Array.new
309
+ self.query("LPOP #{key} #{idx}") {|result|
310
+ if result.success?
311
+ ary.push result.data
312
+ else
313
+ ary.push nil
314
+ end
315
+ }
316
+ ary
317
+ end
318
+
319
+ # Set the data in a list at a specific index.
320
+ # @param [String] key List to reference.
321
+ # @param [Fixnum] idx Index to edit.
322
+ # @param [String] data Data to set.
323
+ # @note The index has to exist before you are able to use list_set on it.
324
+ def list_set(key, idx, data)
325
+ success = false
326
+ self.query("LSET #{key} #{idx} \"#{data}\"") {|result|
327
+ success = result.success?
328
+ }
329
+ success
330
+ end
331
+
332
+ # Get the size of a list (number of elements).
333
+ # @param [String] key List to get the size of.
334
+ # @return [Fixnum] Number of elements in the list.
335
+ def list_size(key)
336
+ num = 0
337
+ self.query("LSIZE #{key}") {|result|
338
+ num = result.data if result.success?
339
+ }
340
+ num
341
+ end
342
+
343
+ # Clear out an entire list.
344
+ # @param [String] key List to delete.
345
+ # @return [Boolean] True on success, false otherwise.
346
+ def list_clear(key)
347
+ self.delete(key)
348
+ end
349
+
350
+ private
351
+ def array_add_quotes(ary)
352
+ return unless ary.kind_of? Array
353
+ ary.collect! {|element|
354
+ "\"#{element}\""
355
+ }
356
+ ary
357
+ end
105
358
  end
106
359
  end
107
360
 
@@ -81,6 +81,7 @@ module XFireDB
81
81
  @status = ACK
82
82
  elsif @data == "nil"
83
83
  @status = NULL
84
+ @data = nil
84
85
  else
85
86
  @status = MSG
86
87
  end
@@ -1,4 +1,4 @@
1
1
  module XFireDB
2
2
  # XFireDB ruby client version number.
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xfiredb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Megens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-16 00:00:00.000000000 Z
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler