-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkyotocabinet.rb
432 lines (362 loc) · 10.2 KB
/
kyotocabinet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
##
## JRuby adaptation layer for Kyoto Cabinet Java interface.
##
## This requires the kyotocabinet-java library to be installed. This
## library builds a jar file, which should be on the Ruby $LOAD_PATH,
## the Java CLASSPATH, or in /usr/local/lib/ where it is placed by the
## default 'make install' target in kyotocabinet-java.
##
## kyotocabinet-java also builds a necessary JNI library,
## libjkyotocabinet.{jnilib,dylib}. This cannot be added to
## java.library.path at runtime, so either its installation path must
## be added to java.library.path with a JVM option like
## -Djava.library.path=/some/dir, or it must be copied to somewhere
## already on java.library.path.
##
unless RUBY_PLATFORM == 'java'
raise "This library requires JRuby!"
end
require 'java'
unless Java.const_defined? :Kyotocabinet
# need to require kyotocabinet.jar
soname = java.lang.System.mapLibraryName("jkyotocabinet")
jdir = $LOAD_PATH.find { |d| File.exist? "#{d}/kyotocabinet.jar" }
raise "Could not find kyotocabinet.jar on $LOAD_PATH (#{$LOAD_PATH.join(':')})!" unless jdir
sopath = "#{jdir}/#{soname}"
unless File.exist? sopath
raise "JNI library #{soname} not installed in #{jdir}!"
end
java.lang.System.setProperty("kyotocabinet.lib", sopath)
require 'kyotocabinet.jar'
begin
Java::Kyotocabinet::Loader.load
rescue NameError
raise "Kyoto Cabinet classes could not be loaded!"
end
end
module KyotoCabinet
module Adaptation
BYTE_ARRAY = [1].to_java(:byte).java_class
def conv_string_array(a)
ba = Java::byte[a.size, 0].new
a.each_with_index do |k, i|
ba[i] = k.to_java_bytes
end
ba
end
def to_string_array(ba)
ba.collect { |e| String.from_java_bytes(e) }
end
def self.included(mod)
super(mod)
mod.instance_eval do
def with_java_method(mname, args)
mh = self.java_method(mname, args)
yield mh
end
def convert_args(mname, args)
indices = []
args.each_with_index do |a, i|
if a == BYTE_ARRAY
indices << i
end
end
mh = self.java_method(mname, args)
self.send(:define_method, mname) do |*argv|
indices.each { |i| argv[i] = argv[i].to_java_bytes }
mh.bind(self).call(*argv)
end
end
def convert_args_ret(mname, args)
indices = []
args.each_with_index do |a, i|
if a == BYTE_ARRAY
indices << i
end
end
mh = self.java_method(mname, args)
self.send(:define_method, mname) do |*argv|
indices.each { |i| argv[i] = argv[i].to_java_bytes }
rv = mh.bind(self).call(*argv)
if rv
String.from_java_bytes(rv)
end
end
end
end
end
def ret_bytes(v)
if v
String.from_java_bytes(v)
end
end
end
class VisitorProxy
include Java::Kyotocabinet::Visitor
def initialize(v, readonly=false)
@v = v
@readonly = readonly
end
def visit_empty(key)
rv = @v.visit_empty(String.from_java_bytes(key))
rv && (! @readonly) ? rv.to_java_bytes : Visitor::NOP
end
def visit_full(key, value)
rv = @v.visit_full(String.from_java_bytes(key),
String.from_java_bytes(value))
rv && (! @readonly) ? rv.to_java_bytes : Visitor::NOP
end
end
class BlockVisitor
def self.wrap(proc, ro=false)
VisitorProxy.new(self.new(proc), ro)
end
def self.wrap_nop(proc)
NOPVisitor.new(self.wrap(proc, true))
end
def initialize(proc)
@proc = proc
end
def visit_empty(key)
@proc.call(key)
end
def visit_full(key, value)
@proc.call(key, value)
end
end
class NOPVisitor
def initialize(base)
@base = base
end
def visit_empty(key)
@base.visit_empty(key)
Visitor::NOP
end
def visit_full(key, value)
@base.visit_full(key, value)
Visitor::NOP
end
end
end
module Java::Kyotocabinet
VisitorProxy = KyotoCabinet::VisitorProxy
BlockVisitor = KyotoCabinet::BlockVisitor
class Cursor
include KyotoCabinet::Adaptation
alias_method :_accept, :accept
def accept(visitor=nil, writable=true, step=false)
vp = if visitor
VisitorProxy.new(visitor, !writable)
else
if writable
BlockVisitor.wrap(blk)
else
BlockVisitor.wrap_nop(blk)
end
end
_accept(vp, writable, step)
end
alias_method :_get, :get
def get(step=false)
r = self._get(step)
if r
return [String.from_java_bytes(r[0]),
String.from_java_bytes(r[1])]
else
return nil
end
end
alias_method :_get_key, :get_key
def get_key(step=false)
ret_bytes(_get_key(step))
end
alias_method :_get_value, :get_value
def get_value(step=false)
ret_bytes(_get_value(step))
end
alias_method :_jump, :jump
def jump(key=nil)
if key
_jump(key.to_java_bytes)
else
_jump()
end
end
alias_method :_jump_back, :jump_back
def jump_back(key=nil)
if key
_jump_back(key.to_java_bytes)
else
_jump_back()
end
end
alias_method :_seize, :seize
def seize()
to_string_array(_seize())
end
alias_method :_set_value, :set_value
def set_value(value, step=false)
_set_value(value ? value.to_java_bytes : nil,
step)
end
end # class Cursor
class DB
include KyotoCabinet::Adaptation
# The Java API is thread-safe so this can be a no-op
GCONCURRENT = 0
alias_method :_accept, :accept
def accept(key, visitor=nil, writable=true, &blk)
vp = if visitor
VisitorProxy.new(visitor, !writable)
else
if writable
BlockVisitor.wrap(blk)
else
BlockVisitor.wrap_nop(blk)
end
end
self._accept(key.to_java_bytes, vp, writable)
end
alias_method :_accept_bulk, :accept_bulk
def accept_bulk(keys, visitor=nil, writable=true, &blk)
vp = if visitor
VisitorProxy.new(visitor, !writable)
else
if writable
BlockVisitor.wrap(blk)
else
BlockVisitor.wrap_nop(blk)
end
end
self._accept_bulk(conv_string_array(keys), vp, writable)
end
convert_args :add, [BYTE_ARRAY, BYTE_ARRAY]
convert_args :append, [BYTE_ARRAY, BYTE_ARRAY]
convert_args :cas, [BYTE_ARRAY, BYTE_ARRAY, BYTE_ARRAY]
convert_args :check, [BYTE_ARRAY]
def each(&blk)
_iterate(BlockVisitor.wrap_nop(blk), false)
end
def each_key
_iterate(BlockVisitor.wrap_nop(lambda { |k, v| yield [k] }),
false)
end
def each_value
_iterate(BlockVisitor.wrap_nop(lambda { |k, v| yield [v] }),
false)
end
alias_method :_get, :get
def get(key)
ret_bytes(self._get(key.to_java_bytes))
end
alias_method :[], :get
alias_method :_get_bulk, :get_bulk
def get_bulk(keys, atomic=true)
ra = _get_bulk(conv_string_array(keys), atomic)
h = {}
i = 0
while i < ra.size
h[String.from_java_bytes(ra[i])] = String.from_java_bytes(ra[i + 1])
i += 2
end
h
end
alias_method :_increment, :increment
def increment(key, num=0, orig=0)
self._increment(key.to_java_bytes, num, orig)
end
alias_method :_increment_double, :increment_double
def increment_double(key, num=0, orig=0)
_increment_double(key.to_java_bytes, num, orig)
end
alias_method :_iterate, :iterate
def iterate(visitor=nil, writable=true, &blk)
if visitor
_iterate(VisitorProxy.new(visitor),
writable)
else
_iterate(BlockVisitor.wrap(blk),
writable)
end
end
alias_method :_match_prefix, :match_prefix
def match_prefix(prefix, max=-1)
_match_prefix(prefix, max)
end
alias_method :_match_regex, :match_regex
def match_regex(regex, max=-1)
_match_regex(regex, max)
end
alias_method :_match_similar, :match_similar
def match_similar(origin, range=1, utf=false, max=-1)
_match_similar(origin, range, utf, max)
end
alias_method :_merge, :merge
def merge(srcary, mode=DB::MSET)
_merge(srcary.to_java(DB), mode)
end
alias_method :_occupy, :occupy
def occupy(writable=false, proc=nil)
_occupy(writable, proc)
end
alias_method :_open, :open
def open(path=':', mode=DB::OWRITER|DB::OCREATE)
_open(path, mode)
end
convert_args :remove, [BYTE_ARRAY]
alias_method :delete, :remove
alias_method :_remove_bulk, :remove_bulk
def remove_bulk(keys, atomic=true)
_remove_bulk(conv_string_array(keys), atomic)
end
convert_args :replace, [BYTE_ARRAY, BYTE_ARRAY]
convert_args_ret :seize, [BYTE_ARRAY]
alias_method :_set, :set
def set(k, v)
self._set(k.to_java_bytes, v.to_s.to_java_bytes)
end
alias_method :[]=, :set
alias_method :store, :set
alias_method :_set_bulk, :set_bulk
def set_bulk(rec_h, atomic)
ba = Java::byte[rec_h.size * 2, 0].new
i = 0
rec_h.each_pair do |k, v|
ba[i] = k.to_java_bytes
ba[i+1] = v.to_java_bytes
i += 2
end
self._set_bulk(ba, atomic)
end
## TODO: store?
alias_method :_synchronize, :synchronize
def synchronize(hard=false, proc=nil)
self._synchronize(hard, proc)
end
def transaction(hard=false)
begin_transaction(hard)
commit = false
begin
commit = yield
ensure
end_transaction(commit)
end
commit
end
def cursor_process
cur = self.cursor()
begin
yield cur
ensure
cur.disable()
end
end
end # class DB
end
module KyotoCabinet
java_import Java::Kyotocabinet::Cursor
java_import Java::Kyotocabinet::DB
java_import Java::Kyotocabinet::Error
java_import Java::Kyotocabinet::FileProcessor
java_import Java::Kyotocabinet::Visitor
end