rufus-tokyo 0.1.12 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +9 -0
- data/CREDITS.txt +1 -0
- data/README.txt +6 -1
- data/TODO.txt +11 -7
- data/lib/rufus/edo/cabcore.rb +31 -16
- data/lib/rufus/edo/cabinet/abstract.rb +11 -2
- data/lib/rufus/edo/cabinet/table.rb +19 -2
- data/lib/rufus/edo/ntyrant/table.rb +13 -0
- data/lib/rufus/edo/tabcore.rb +10 -29
- data/lib/rufus/tokyo.rb +13 -1
- data/lib/rufus/tokyo/cabinet/abstract.rb +123 -31
- data/lib/rufus/tokyo/cabinet/lib.rb +32 -30
- data/lib/rufus/tokyo/cabinet/table.rb +77 -44
- data/lib/rufus/tokyo/cabinet/util.rb +130 -93
- data/lib/rufus/tokyo/config.rb +5 -2
- data/lib/rufus/tokyo/hmethods.rb +14 -7
- data/lib/rufus/tokyo/tyrant/abstract.rb +16 -3
- data/lib/rufus/tokyo/tyrant/lib.rb +16 -10
- data/lib/rufus/tokyo/tyrant/table.rb +0 -2
- metadata +6 -4
@@ -69,7 +69,6 @@ module Rufus::Tokyo
|
|
69
69
|
# this class has tranbegin/trancommit/tranabort so let's include the
|
70
70
|
# transaction mixin
|
71
71
|
|
72
|
-
#
|
73
72
|
# Creates a Table instance (creates or opens it depending on the args)
|
74
73
|
#
|
75
74
|
# For example,
|
@@ -111,6 +110,9 @@ module Rufus::Tokyo
|
|
111
110
|
# not more than 0, the extra mapped memory is disabled.
|
112
111
|
# The default size is 67108864.
|
113
112
|
#
|
113
|
+
# * :dfunit unit step number. If it is not more than 0,
|
114
|
+
# the auto defragmentation is disabled. (Since TC 1.4.21)
|
115
|
+
#
|
114
116
|
# Some examples :
|
115
117
|
#
|
116
118
|
# t = Rufus::Tokyo::Table.new('table.tdb')
|
@@ -139,33 +141,46 @@ module Rufus::Tokyo
|
|
139
141
|
|
140
142
|
libcall(:tctdbsetxmsiz, conf[:xmsiz])
|
141
143
|
|
144
|
+
libcall(:tctdbsetdfunit, conf[:dfunit]) \
|
145
|
+
if lib.respond_to?(:tctdbsetdfunit) # TC >= 1.4.21
|
146
|
+
|
142
147
|
#
|
143
148
|
# open table
|
144
149
|
|
145
|
-
|
150
|
+
@path = conf[:path]
|
151
|
+
|
152
|
+
libcall(:tctdbopen, @path, conf[:mode])
|
146
153
|
end
|
147
154
|
|
148
|
-
#
|
149
|
-
# using the cabinet lib
|
155
|
+
# Using the cabinet lib
|
150
156
|
#
|
151
157
|
def lib
|
158
|
+
|
152
159
|
CabinetLib
|
153
160
|
end
|
154
161
|
|
162
|
+
# Returns the path to the table.
|
155
163
|
#
|
164
|
+
def path
|
165
|
+
|
166
|
+
@path
|
167
|
+
end
|
168
|
+
|
156
169
|
# Closes the table (and frees the datastructure allocated for it),
|
157
170
|
# returns true in case of success.
|
158
171
|
#
|
159
172
|
def close
|
173
|
+
|
160
174
|
result = lib.tab_close(@db)
|
161
175
|
lib.tab_del(@db)
|
176
|
+
|
162
177
|
(result == 1)
|
163
178
|
end
|
164
179
|
|
165
|
-
#
|
166
180
|
# Generates a unique id (in the context of this Table instance)
|
167
181
|
#
|
168
182
|
def generate_unique_id
|
183
|
+
|
169
184
|
lib.tab_genuid(@db)
|
170
185
|
end
|
171
186
|
alias :genuid :generate_unique_id
|
@@ -178,7 +193,6 @@ module Rufus::Tokyo
|
|
178
193
|
:keep => 1 << 24
|
179
194
|
}
|
180
195
|
|
181
|
-
#
|
182
196
|
# Sets an index on a column of the table.
|
183
197
|
#
|
184
198
|
# Types maybe be :lexical or :decimal, use :keep to "add" and
|
@@ -197,7 +211,6 @@ module Rufus::Tokyo
|
|
197
211
|
(lib.tab_setindex(@db, column_name, i) == 1)
|
198
212
|
end
|
199
213
|
|
200
|
-
#
|
201
214
|
# Inserts a record in the table db
|
202
215
|
#
|
203
216
|
# table['pk0'] = [ 'name', 'fred', 'age', '45' ]
|
@@ -213,7 +226,7 @@ module Rufus::Tokyo
|
|
213
226
|
|
214
227
|
m = Rufus::Tokyo::Map[h_or_a]
|
215
228
|
|
216
|
-
r = lib.tab_put(@db, pk,
|
229
|
+
r = lib.tab_put(@db, pk, Rufus::Tokyo.blen(pk), m.pointer)
|
217
230
|
|
218
231
|
m.free
|
219
232
|
|
@@ -222,27 +235,27 @@ module Rufus::Tokyo
|
|
222
235
|
h_or_a
|
223
236
|
end
|
224
237
|
|
225
|
-
#
|
226
238
|
# Removes an entry in the table
|
227
239
|
#
|
228
240
|
# (might raise an error if the delete itself failed, but returns nil
|
229
241
|
# if there was no entry for the given key)
|
230
242
|
#
|
231
243
|
def delete (k)
|
244
|
+
|
232
245
|
v = self[k]
|
233
246
|
return nil unless v
|
234
|
-
libcall(:tab_out, k,
|
247
|
+
libcall(:tab_out, k, Rufus::Tokyo.blen(k))
|
248
|
+
|
235
249
|
v
|
236
250
|
end
|
237
251
|
|
238
|
-
#
|
239
252
|
# Removes all records in this table database
|
240
253
|
#
|
241
254
|
def clear
|
255
|
+
|
242
256
|
libcall(:tab_vanish)
|
243
257
|
end
|
244
258
|
|
245
|
-
#
|
246
259
|
# Returns an array of all the primary keys in the table
|
247
260
|
#
|
248
261
|
# With no options given, this method will return all the keys (strings)
|
@@ -258,10 +271,15 @@ module Rufus::Tokyo
|
|
258
271
|
#
|
259
272
|
def keys (options={})
|
260
273
|
|
261
|
-
|
274
|
+
outlen = nil
|
275
|
+
|
276
|
+
if pre = options[:prefix]
|
277
|
+
|
278
|
+
l = lib.tab_fwmkeys(
|
279
|
+
@db, pre, Rufus::Tokyo.blen(pre), options[:limit] || -1)
|
262
280
|
|
263
|
-
l = lib.tab_fwmkeys2(@db, pref, options[:limit] || -1)
|
264
281
|
l = Rufus::Tokyo::List.new(l)
|
282
|
+
|
265
283
|
options[:native] ? l : l.release
|
266
284
|
|
267
285
|
else
|
@@ -273,24 +291,32 @@ module Rufus::Tokyo
|
|
273
291
|
|
274
292
|
lib.tab_iterinit(@db)
|
275
293
|
|
276
|
-
|
294
|
+
outlen = FFI::MemoryPointer.new(:int)
|
295
|
+
|
296
|
+
loop do
|
277
297
|
break if limit and l.size >= limit
|
278
|
-
|
298
|
+
out = lib.tab_iternext(@db, outlen)
|
299
|
+
break if out.address == 0
|
300
|
+
l << out.get_bytes(0, outlen.get_int(0))
|
279
301
|
end
|
280
302
|
|
281
303
|
l
|
282
304
|
end
|
305
|
+
|
306
|
+
ensure
|
307
|
+
|
308
|
+
outlen.free if outlen
|
283
309
|
end
|
284
310
|
|
285
|
-
#
|
286
311
|
# Deletes all the entries whose key begin with the given prefix.
|
287
312
|
#
|
288
313
|
def delete_keys_with_prefix (prefix)
|
289
314
|
|
290
315
|
# TODO : use ...searchout
|
291
316
|
|
292
|
-
ks = lib.
|
293
|
-
|
317
|
+
ks = lib.tab_fwmkeys(@db, prefix, Rufus::Tokyo.blen(prefix), -1)
|
318
|
+
# -1 for no limit
|
319
|
+
|
294
320
|
begin
|
295
321
|
ks = Rufus::Tokyo::List.new(ks)
|
296
322
|
ks.each { |k| self.delete(k) }
|
@@ -299,41 +325,55 @@ module Rufus::Tokyo
|
|
299
325
|
end
|
300
326
|
end
|
301
327
|
|
328
|
+
# No 'misc' methods for the table library, so this lget is equivalent
|
329
|
+
# to calling get for each key. Hoping later versions of TC will provide
|
330
|
+
# a mget method.
|
302
331
|
#
|
332
|
+
def lget (keys)
|
333
|
+
|
334
|
+
# TODO : maybe investigate a query on the column 'primary_key' ?
|
335
|
+
|
336
|
+
keys.inject({}) { |h, k| v = self[k]; h[k] = v if v; h }
|
337
|
+
end
|
338
|
+
|
303
339
|
# Returns the number of records in this table db
|
304
340
|
#
|
305
341
|
def size
|
342
|
+
|
306
343
|
lib.tab_rnum(@db)
|
307
344
|
end
|
308
345
|
|
309
|
-
#
|
310
346
|
# Prepares a query instance (block is optional)
|
311
347
|
#
|
312
348
|
def prepare_query (&block)
|
349
|
+
|
313
350
|
q = TableQuery.new(self)
|
314
351
|
block.call(q) if block
|
352
|
+
|
315
353
|
q
|
316
354
|
end
|
317
355
|
|
318
|
-
#
|
319
356
|
# Prepares and runs a query, returns a ResultSet instance
|
320
357
|
# (takes care of freeing the query structure)
|
321
358
|
#
|
322
359
|
def do_query (&block)
|
360
|
+
|
323
361
|
q = prepare_query(&block)
|
324
362
|
rs = q.run
|
325
363
|
q.free
|
364
|
+
|
326
365
|
rs
|
327
366
|
end
|
328
367
|
|
329
|
-
#
|
330
368
|
# Prepares and runs a query, returns an array of hashes (all Ruby)
|
331
369
|
# (takes care of freeing the query and the result set structures)
|
332
370
|
#
|
333
371
|
def query (&block)
|
372
|
+
|
334
373
|
rs = do_query(&block)
|
335
374
|
a = rs.to_a
|
336
375
|
rs.free
|
376
|
+
|
337
377
|
a
|
338
378
|
end
|
339
379
|
|
@@ -343,6 +383,7 @@ module Rufus::Tokyo
|
|
343
383
|
# Direct call for 'transaction begin'.
|
344
384
|
#
|
345
385
|
def tranbegin
|
386
|
+
|
346
387
|
libcall(:tctdbtranbegin)
|
347
388
|
end
|
348
389
|
|
@@ -364,7 +405,6 @@ module Rufus::Tokyo
|
|
364
405
|
libcall(:tctdbtranabort)
|
365
406
|
end
|
366
407
|
|
367
|
-
#
|
368
408
|
# Returns the actual pointer to the Tokyo Cabinet table
|
369
409
|
#
|
370
410
|
def pointer
|
@@ -373,14 +413,16 @@ module Rufus::Tokyo
|
|
373
413
|
|
374
414
|
protected
|
375
415
|
|
376
|
-
#
|
377
416
|
# Returns the value (as a Ruby Hash) else nil
|
378
417
|
#
|
379
418
|
# (the actual #[] method is provided by HashMethods)
|
380
419
|
#
|
381
420
|
def get (k)
|
382
|
-
|
383
|
-
|
421
|
+
|
422
|
+
m = lib.tab_get(@db, k, Rufus::Tokyo.blen(k))
|
423
|
+
|
424
|
+
return nil if m.address == 0
|
425
|
+
|
384
426
|
Map.to_h(m) # which frees the map
|
385
427
|
end
|
386
428
|
|
@@ -393,7 +435,6 @@ module Rufus::Tokyo
|
|
393
435
|
# works with JRuby 1.1.6
|
394
436
|
end
|
395
437
|
|
396
|
-
#
|
397
438
|
# Obviously something got wrong, let's ask the db about it and raise
|
398
439
|
# a TokyoError
|
399
440
|
#
|
@@ -413,7 +454,6 @@ module Rufus::Tokyo
|
|
413
454
|
|
414
455
|
include QueryConstants
|
415
456
|
|
416
|
-
#
|
417
457
|
# Creates a query for a given Rufus::Tokyo::Table
|
418
458
|
#
|
419
459
|
# Queries are usually created via the #query (#prepare_query #do_query)
|
@@ -440,7 +480,6 @@ module Rufus::Tokyo
|
|
440
480
|
@table.lib
|
441
481
|
end
|
442
482
|
|
443
|
-
#
|
444
483
|
# Adds a condition
|
445
484
|
#
|
446
485
|
# table.query { |q|
|
@@ -507,7 +546,6 @@ module Rufus::Tokyo
|
|
507
546
|
end
|
508
547
|
alias :add_condition :add
|
509
548
|
|
510
|
-
#
|
511
549
|
# Sets the max number of records to return for this query.
|
512
550
|
#
|
513
551
|
# (If you're using TC >= 1.4.10 the optional 'offset' (skip) parameter
|
@@ -520,7 +558,6 @@ module Rufus::Tokyo
|
|
520
558
|
lib.qry_setmax(@query, i)
|
521
559
|
end
|
522
560
|
|
523
|
-
#
|
524
561
|
# Sets the sort order for the result of the query
|
525
562
|
#
|
526
563
|
# The 'direction' may be :
|
@@ -536,7 +573,6 @@ module Rufus::Tokyo
|
|
536
573
|
lib.qry_setorder(@query, colname, DIRECTIONS[direction])
|
537
574
|
end
|
538
575
|
|
539
|
-
#
|
540
576
|
# When set to true, only the primary keys of the matching records will
|
541
577
|
# be returned.
|
542
578
|
#
|
@@ -544,7 +580,6 @@ module Rufus::Tokyo
|
|
544
580
|
@opts[:pk_only] = on
|
545
581
|
end
|
546
582
|
|
547
|
-
#
|
548
583
|
# When set to true, the :pk (primary key) is not inserted in the record
|
549
584
|
# (hashes) returned
|
550
585
|
#
|
@@ -552,7 +587,6 @@ module Rufus::Tokyo
|
|
552
587
|
@opts[:no_pk] = on
|
553
588
|
end
|
554
589
|
|
555
|
-
#
|
556
590
|
# Runs this query (returns a TableResultSet instance)
|
557
591
|
#
|
558
592
|
def run
|
@@ -561,7 +595,6 @@ module Rufus::Tokyo
|
|
561
595
|
TableResultSet.new(@table, lib.qry_search(@query), @opts)
|
562
596
|
end
|
563
597
|
|
564
|
-
#
|
565
598
|
# Gets the count of records returned by this query.
|
566
599
|
#
|
567
600
|
# Note : the 'real' impl is only available since TokyoCabinet 1.4.12.
|
@@ -575,7 +608,6 @@ module Rufus::Tokyo
|
|
575
608
|
end
|
576
609
|
end
|
577
610
|
|
578
|
-
#
|
579
611
|
# Frees this data structure
|
580
612
|
#
|
581
613
|
def free
|
@@ -591,29 +623,30 @@ module Rufus::Tokyo
|
|
591
623
|
# The thing queries return
|
592
624
|
#
|
593
625
|
class TableResultSet
|
626
|
+
|
594
627
|
include Enumerable
|
595
628
|
|
596
629
|
def initialize (table, list_pointer, query_opts)
|
630
|
+
|
597
631
|
@table = table
|
598
|
-
@list = list_pointer
|
632
|
+
@list = Rufus::Tokyo::List.new(list_pointer)
|
599
633
|
@opts = query_opts
|
600
634
|
end
|
601
635
|
|
602
|
-
#
|
603
636
|
# Returns the count of element in this result set
|
604
637
|
#
|
605
638
|
def size
|
606
|
-
|
639
|
+
|
640
|
+
@list.size
|
607
641
|
end
|
608
642
|
|
609
643
|
alias :length :size
|
610
644
|
|
611
|
-
#
|
612
645
|
# The classical each
|
613
646
|
#
|
614
647
|
def each
|
615
648
|
(0..size-1).each do |i|
|
616
|
-
pk =
|
649
|
+
pk = @list[i]
|
617
650
|
if @opts[:pk_only]
|
618
651
|
yield(pk)
|
619
652
|
else
|
@@ -624,18 +657,18 @@ module Rufus::Tokyo
|
|
624
657
|
end
|
625
658
|
end
|
626
659
|
|
627
|
-
#
|
628
660
|
# Returns an array of hashes
|
629
661
|
#
|
630
662
|
def to_a
|
663
|
+
|
631
664
|
collect { |m| m }
|
632
665
|
end
|
633
666
|
|
634
|
-
#
|
635
667
|
# Frees this query (the underlying Tokyo Cabinet list structure)
|
636
668
|
#
|
637
669
|
def free
|
638
|
-
|
670
|
+
|
671
|
+
@list.free
|
639
672
|
@list = nil
|
640
673
|
end
|
641
674
|
|
@@ -28,6 +28,49 @@ require 'rufus/tokyo/hmethods'
|
|
28
28
|
|
29
29
|
module Rufus::Tokyo
|
30
30
|
|
31
|
+
#
|
32
|
+
# :nodoc:
|
33
|
+
#
|
34
|
+
module ListMapMixin
|
35
|
+
|
36
|
+
# A shortcut
|
37
|
+
#
|
38
|
+
def clib
|
39
|
+
|
40
|
+
CabinetLib
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the underlying 'native' (FFI) memory pointer
|
44
|
+
#
|
45
|
+
def pointer
|
46
|
+
|
47
|
+
@pointer
|
48
|
+
end
|
49
|
+
|
50
|
+
def pointer_or_raise
|
51
|
+
|
52
|
+
@pointer || raise("#{self.class} got freed, cannot use anymore")
|
53
|
+
end
|
54
|
+
|
55
|
+
def outlen_op (method, *args)
|
56
|
+
|
57
|
+
args.unshift(pointer_or_raise)
|
58
|
+
|
59
|
+
outlen = FFI::MemoryPointer.new(:int)
|
60
|
+
args << outlen
|
61
|
+
|
62
|
+
out = clib.send(method, *args)
|
63
|
+
|
64
|
+
return nil if out.address == 0
|
65
|
+
|
66
|
+
return out.get_bytes(0, outlen.get_int(0))
|
67
|
+
|
68
|
+
ensure
|
69
|
+
|
70
|
+
outlen.free
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
31
74
|
#
|
32
75
|
# A Tokyo Cabinet in-memory (tcutil.h) map
|
33
76
|
#
|
@@ -36,117 +79,118 @@ module Rufus::Tokyo
|
|
36
79
|
class Map
|
37
80
|
|
38
81
|
include HashMethods
|
82
|
+
include ListMapMixin
|
39
83
|
|
40
|
-
#
|
41
84
|
# Creates an empty instance of a Tokyo Cabinet in-memory map
|
42
85
|
#
|
43
86
|
# (It's OK to pass the pointer of a C map directly, this is in fact
|
44
87
|
# used in rufus/tokyo/table when retrieving entries)
|
45
88
|
#
|
46
|
-
def initialize (pointer
|
47
|
-
@map = pointer || clib.tcmapnew
|
48
|
-
end
|
89
|
+
def initialize (pointer=nil)
|
49
90
|
|
50
|
-
|
51
|
-
# a shortcut
|
52
|
-
#
|
53
|
-
def clib
|
54
|
-
CabinetLib
|
91
|
+
@pointer = pointer || clib.tcmapnew
|
55
92
|
end
|
56
93
|
|
57
|
-
#
|
58
94
|
# Inserts key/value pair
|
59
95
|
#
|
60
96
|
def []= (k, v)
|
61
|
-
|
97
|
+
|
98
|
+
clib.tcmapput(pointer, k, Rufus::Tokyo::blen(k), v, Rufus::Tokyo::blen(v))
|
99
|
+
|
62
100
|
v
|
63
101
|
end
|
64
102
|
|
65
|
-
#
|
66
103
|
# Deletes an entry
|
67
104
|
#
|
68
105
|
def delete (k)
|
106
|
+
|
69
107
|
v = self[k]
|
70
108
|
return nil unless v
|
71
|
-
|
109
|
+
|
110
|
+
(clib.tcmapout(pointer_or_raise, k, Rufus::Tokyo::blen(k)) == 1) ||
|
111
|
+
raise("failed to remove key '#{k}'")
|
112
|
+
|
72
113
|
v
|
73
114
|
end
|
74
115
|
|
75
|
-
#
|
76
116
|
# Empties the map
|
77
117
|
#
|
78
118
|
def clear
|
79
|
-
|
119
|
+
|
120
|
+
clib.tcmapclear(pointer_or_raise)
|
80
121
|
end
|
81
122
|
|
82
|
-
#
|
83
123
|
# (the actual #[] method is provided by HashMethods)
|
84
124
|
#
|
85
125
|
def get (k)
|
86
|
-
|
126
|
+
|
127
|
+
outlen_op(:tcmapget, k, Rufus::Tokyo.blen(k))
|
87
128
|
end
|
88
129
|
protected :get
|
89
130
|
|
90
|
-
#
|
91
131
|
# Returns an array of all the keys in the map
|
92
132
|
#
|
93
133
|
def keys
|
134
|
+
|
135
|
+
clib.tcmapiterinit(pointer_or_raise)
|
94
136
|
a = []
|
95
|
-
|
96
|
-
|
97
|
-
|
137
|
+
|
138
|
+
klen = FFI::MemoryPointer.new(:int)
|
139
|
+
|
140
|
+
loop do
|
141
|
+
k = clib.tcmapiternext(@pointer, klen)
|
142
|
+
break if k.address == 0
|
143
|
+
a << k.get_bytes(0, klen.get_int(0))
|
144
|
+
end
|
145
|
+
|
146
|
+
return a
|
147
|
+
|
148
|
+
ensure
|
149
|
+
|
150
|
+
klen.free
|
98
151
|
end
|
99
152
|
|
100
|
-
#
|
101
153
|
# Returns the count of entries in the map
|
102
154
|
#
|
103
155
|
def size
|
104
|
-
|
156
|
+
|
157
|
+
clib.tcmaprnum(pointer_or_raise)
|
105
158
|
end
|
106
159
|
|
107
160
|
alias :length :size
|
108
161
|
|
109
|
-
#
|
110
162
|
# Frees the map (nukes it from memory)
|
111
163
|
#
|
112
164
|
def free
|
113
|
-
|
114
|
-
|
165
|
+
|
166
|
+
clib.tcmapdel(pointer_or_raise)
|
167
|
+
@pointer = nil
|
115
168
|
end
|
116
169
|
|
117
170
|
alias :destroy :free
|
118
171
|
alias :close :free
|
119
172
|
|
120
|
-
#
|
121
|
-
# Returns the pointer to the underlying Tokyo Cabinet map
|
122
|
-
#
|
123
|
-
def pointer
|
124
|
-
@map || raise('map got freed, cannot use anymore')
|
125
|
-
end
|
126
|
-
|
127
|
-
alias :m :pointer
|
128
|
-
|
129
|
-
#
|
130
173
|
# Turns a given Tokyo map structure into a Ruby Hash. By default
|
131
174
|
# (free = true) will dispose of the map before replying with the Ruby
|
132
175
|
# Hash.
|
133
176
|
#
|
134
177
|
def self.to_h (map_pointer, free=true)
|
178
|
+
|
135
179
|
m = self.new(map_pointer)
|
136
180
|
h = m.to_h
|
137
181
|
m.free if free
|
182
|
+
|
138
183
|
h
|
139
184
|
end
|
140
185
|
|
141
|
-
#
|
142
186
|
# Turns a Ruby hash into a Tokyo Cabinet Map and returns it
|
143
187
|
# (don't forget to free the map when you're done with it !)
|
144
188
|
#
|
145
189
|
def self.from_h (h)
|
190
|
+
|
146
191
|
h.inject(Map.new) { |m, (k, v)| m[k] = v; m }
|
147
192
|
end
|
148
193
|
|
149
|
-
#
|
150
194
|
# Behaves much like Hash#[] but outputs a Rufus::Tokyo::Map
|
151
195
|
# (don't forget to free the map when you're done with it !)
|
152
196
|
#
|
@@ -166,9 +210,10 @@ module Rufus::Tokyo
|
|
166
210
|
# http://tokyocabinet.sourceforge.net/spex-en.html#tcutilapi
|
167
211
|
#
|
168
212
|
class List
|
213
|
+
|
169
214
|
include Enumerable
|
215
|
+
include ListMapMixin
|
170
216
|
|
171
|
-
#
|
172
217
|
# Creates a new Tokyo Cabinet list.
|
173
218
|
#
|
174
219
|
# (by passing a list pointer, one can wrap an existing list pointer
|
@@ -177,71 +222,70 @@ module Rufus::Tokyo
|
|
177
222
|
def initialize (list_pointer=nil)
|
178
223
|
|
179
224
|
if list_pointer.is_a?(FFI::Pointer)
|
180
|
-
@
|
225
|
+
@pointer = list_pointer
|
181
226
|
else
|
182
|
-
@
|
227
|
+
@pointer = clib.tclistnew
|
183
228
|
list_pointer.each { |e| self << e } if list_pointer
|
184
229
|
end
|
185
230
|
end
|
186
231
|
|
187
|
-
#
|
188
|
-
# a shortcut
|
189
|
-
#
|
190
|
-
def clib
|
191
|
-
CabinetLib
|
192
|
-
end
|
193
|
-
|
194
|
-
#
|
195
232
|
# Inserts an element in the list (note that the lib will raise an
|
196
233
|
# ArgumentError if s is not a String)
|
197
234
|
#
|
198
235
|
def << (s)
|
199
|
-
|
236
|
+
|
237
|
+
clib.tclistpush(@pointer, s, Rufus::Tokyo.blen(s))
|
238
|
+
|
200
239
|
self
|
201
240
|
end
|
202
241
|
|
203
|
-
#
|
204
242
|
# Pushes an argument or a list of arguments to this list
|
205
243
|
#
|
206
244
|
def push (*args)
|
245
|
+
|
207
246
|
args.each { |a| self << a }
|
247
|
+
|
208
248
|
self
|
209
249
|
end
|
210
250
|
|
211
|
-
#
|
212
251
|
# Pops the last element in the list
|
213
252
|
#
|
214
253
|
def pop
|
215
|
-
|
254
|
+
|
255
|
+
outlen_op(:tclistpop)
|
216
256
|
end
|
217
257
|
|
218
|
-
#
|
219
258
|
# Removes and returns the first element in a list
|
220
259
|
#
|
221
260
|
def shift
|
222
|
-
|
261
|
+
|
262
|
+
#clib.tclistshift2(@pointer) rescue nil
|
263
|
+
outlen_op(:tclistshift)
|
223
264
|
end
|
224
265
|
|
225
|
-
#
|
226
266
|
# Inserts a string at the beginning of the list
|
227
267
|
#
|
228
268
|
def unshift (s)
|
229
|
-
|
269
|
+
|
270
|
+
clib.tclistunshift(@pointer, s, Rufus::Tokyo.blen(s))
|
271
|
+
|
230
272
|
self
|
231
273
|
end
|
232
274
|
|
275
|
+
# The put operation.
|
276
|
+
#
|
233
277
|
def []= (a, b, c=nil)
|
234
278
|
|
235
279
|
i, s = c.nil? ? [ a, b ] : [ [a, b], c ]
|
236
280
|
|
237
281
|
range = if i.is_a?(Range)
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
282
|
+
i
|
283
|
+
elsif i.is_a?(Array)
|
284
|
+
start, count = i
|
285
|
+
(start..start + count - 1)
|
286
|
+
else
|
287
|
+
[ i ]
|
288
|
+
end
|
245
289
|
|
246
290
|
range = norm(range)
|
247
291
|
|
@@ -251,21 +295,21 @@ module Rufus::Tokyo
|
|
251
295
|
range.each_with_index do |offset, index|
|
252
296
|
val = values[index]
|
253
297
|
if val
|
254
|
-
clib.
|
298
|
+
clib.tclistover(@pointer, offset, val, Rufus::Tokyo.blen(val))
|
255
299
|
else
|
256
|
-
|
300
|
+
outlen_op(:tclistremove, values.size)
|
257
301
|
end
|
258
302
|
end
|
259
303
|
|
260
304
|
self
|
261
305
|
end
|
262
306
|
|
263
|
-
#
|
264
307
|
# Removes the value at a given index and returns the value
|
265
308
|
# (returns nil if no value available)
|
266
309
|
#
|
267
310
|
def delete_at (i)
|
268
|
-
|
311
|
+
|
312
|
+
outlen_op(:tclistremove, i)
|
269
313
|
end
|
270
314
|
|
271
315
|
def delete_if
|
@@ -279,16 +323,15 @@ module Rufus::Tokyo
|
|
279
323
|
# TODO
|
280
324
|
end
|
281
325
|
|
282
|
-
#
|
283
326
|
# Returns the size of this Tokyo Cabinet list
|
284
327
|
#
|
285
328
|
def size
|
286
|
-
|
329
|
+
|
330
|
+
clib.tclistnum(@pointer)
|
287
331
|
end
|
288
332
|
|
289
333
|
alias :length :size
|
290
334
|
|
291
|
-
#
|
292
335
|
# The equivalent of Ruby Array#[]
|
293
336
|
#
|
294
337
|
def [] (i, count=nil)
|
@@ -298,43 +341,48 @@ module Rufus::Tokyo
|
|
298
341
|
len = self.size
|
299
342
|
|
300
343
|
range = if count.nil?
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
344
|
+
i.is_a?(Range) ? i : [i]
|
345
|
+
else
|
346
|
+
(i..i + count - 1)
|
347
|
+
end
|
305
348
|
|
306
|
-
r = norm(range).collect { |i|
|
349
|
+
r = norm(range).collect { |i| outlen_op(:tclistval, i) }
|
307
350
|
|
308
351
|
range.first == range.last ? r.first : r
|
309
352
|
end
|
310
353
|
|
354
|
+
# Empties the list.
|
355
|
+
#
|
311
356
|
def clear
|
312
|
-
|
357
|
+
|
358
|
+
clib.tclistclear(@pointer)
|
313
359
|
end
|
314
360
|
|
361
|
+
# The classical each.
|
362
|
+
#
|
315
363
|
def each
|
364
|
+
|
316
365
|
(0..self.size - 1).each { |i| yield self[i] }
|
317
366
|
end
|
318
367
|
|
319
|
-
#
|
320
368
|
# Turns this Tokyo Cabinet list into a Ruby array
|
321
369
|
#
|
322
370
|
def to_a
|
371
|
+
|
323
372
|
self.collect { |e| e }
|
324
373
|
end
|
325
374
|
|
326
|
-
#
|
327
375
|
# Closes (frees) this list
|
328
376
|
#
|
329
377
|
def free
|
330
|
-
|
331
|
-
@
|
378
|
+
|
379
|
+
self.class.free(@pointer)
|
380
|
+
@pointer = nil
|
332
381
|
end
|
333
382
|
|
334
383
|
alias :close :free
|
335
384
|
alias :destroy :free
|
336
385
|
|
337
|
-
#
|
338
386
|
# Frees (closes) the given 'native' (FFI) list (memory pointer)
|
339
387
|
#
|
340
388
|
def self.free (list_pointer)
|
@@ -342,7 +390,6 @@ module Rufus::Tokyo
|
|
342
390
|
CabinetLib.tclistdel(list_pointer)
|
343
391
|
end
|
344
392
|
|
345
|
-
#
|
346
393
|
# Closes (frees memory from it) this list and returns the ruby version
|
347
394
|
# of it
|
348
395
|
#
|
@@ -353,15 +400,6 @@ module Rufus::Tokyo
|
|
353
400
|
a
|
354
401
|
end
|
355
402
|
|
356
|
-
#
|
357
|
-
# Returns the underlying 'native' (FFI) memory pointer
|
358
|
-
#
|
359
|
-
def pointer
|
360
|
-
|
361
|
-
@list
|
362
|
-
end
|
363
|
-
|
364
|
-
#
|
365
403
|
# Turns a list pointer into a Ruby Array instance (and makes sure to
|
366
404
|
# release the pointer
|
367
405
|
#
|
@@ -372,7 +410,6 @@ module Rufus::Tokyo
|
|
372
410
|
|
373
411
|
protected
|
374
412
|
|
375
|
-
#
|
376
413
|
# Makes sure this offset/range fits the size of the list
|
377
414
|
#
|
378
415
|
def norm (i)
|