ccp 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ccp/kvs/tch.rb CHANGED
@@ -7,8 +7,10 @@ module Ccp
7
7
  def set(k,v) ; W{ super }; end
8
8
  def del(k) ; W{ super }; end
9
9
  def count ; R{ super }; end
10
- def keys ; R{ super }; end
11
10
  def read! ; R{ super }; end
11
+ def keys ; R{ super }; end
12
+ def first_key; R{ super }; end
13
+ def first ; R{ super }; end
12
14
  end
13
15
  end
14
16
  end
data/lib/ccp/kvs/tokyo.rb CHANGED
@@ -3,7 +3,10 @@ require 'tokyocabinet'
3
3
  module Ccp
4
4
  module Kvs
5
5
  module Tokyo
6
- Error = Class.new(Ccp::Kvs::Error)
6
+ Error = Class.new(Ccp::Kvs::Error)
7
+ Locked = Class.new(Error)
8
+
9
+ CONNECTIONS = {}
7
10
  end
8
11
  end
9
12
  end
@@ -33,6 +33,10 @@ module Ccp
33
33
  @db.ecode != HDB::ESUCCESS
34
34
  end
35
35
 
36
+ def threading_error?
37
+ @db.ecode == HDB::ETHREAD
38
+ end
39
+
36
40
  def error_message
37
41
  if @db
38
42
  # TODO: Where is adb_errmsg?
@@ -52,37 +52,69 @@ module Ccp
52
52
  return @db.rnum
53
53
  end
54
54
 
55
+ ######################################################################
56
+ ### bulk operations (not DRY but fast)
57
+
58
+ def read!
59
+ tryR("read!")
60
+ hash = {}
61
+ @db.iterinit or tokyo_error!("read!: ")
62
+ while k = @db.iternext
63
+ v = @db.get(k) or tokyo_error!("get(%s): " % k)
64
+ hash[k] = decode(v)
65
+ end
66
+ return hash
67
+ end
68
+
55
69
  ######################################################################
56
70
  ### iterator
57
71
 
58
72
  def each(&block)
59
- each_keys do |key|
60
- block.call(get(key))
61
- end
73
+ each_pair(&block)
62
74
  end
63
75
 
64
76
  def each_pair(&block)
65
- each_keys do |key|
77
+ each_key do |key|
66
78
  block.call(key, get(key))
67
79
  end
68
80
  end
69
81
 
70
- def each_keys(&block)
71
- tryR("each_keys")
72
- @db.iterinit
82
+ def each_key(&block)
83
+ tryR("each_key")
84
+ @db.iterinit or tokyo_error!("each_key: ")
73
85
  while key = @db.iternext
74
86
  block.call(key)
75
87
  end
76
88
  end
77
89
 
90
+ def each_keys(&block)
91
+ STDERR.puts "DEPRECATION WARNING: #{self.class}#each_keys is deprecated and will be removed in 0.4.0, use each_key instead"
92
+ each_key(&block)
93
+ end
94
+
78
95
  def keys
79
96
  array = []
80
- each_keys do |key|
97
+ each_key do |key|
81
98
  array << key
82
99
  end
83
100
  return array
84
101
  end
85
102
 
103
+ def first_key
104
+ tryR("first_key")
105
+ @db.iterinit or tokyo_error!("first_key: ")
106
+ return @db.iternext
107
+ end
108
+
109
+ def first
110
+ key = first_key
111
+ if key
112
+ return [key, get(key)]
113
+ else
114
+ return nil
115
+ end
116
+ end
117
+
86
118
  end
87
119
  end
88
120
  end
@@ -15,9 +15,41 @@ module Ccp
15
15
  @state || CLOSED
16
16
  end
17
17
 
18
+ def locker_info
19
+ pretty = proc{|c| Array(c).find{|i| i !~ %r{/ruby/[^/]+/gems/}} || c}
20
+
21
+ if CONNECTIONS[@source]
22
+ return pretty[CONNECTIONS[@source]]
23
+ end
24
+
25
+ target = File.basename(@source)
26
+ CONNECTIONS.each_pair do |file, reason|
27
+ return pretty[reason] if File.basename(file) == target
28
+ end
29
+
30
+ if CONNECTIONS.any?
31
+ return CONNECTIONS.inspect
32
+ else
33
+ return 'no brockers. maybe locked by other systems?'
34
+ end
35
+ end
36
+
18
37
  def open(mode)
19
38
  Pathname(@source.to_s).parent.mkpath
20
- @db.open(@source.to_s, mode) or tokyo_error!("%s#open(%s,%s): " % [self.class, @source, mode])
39
+
40
+ # open and mark filename for threading error
41
+ if @db.open(@source.to_s, mode)
42
+ CONNECTIONS[@db.path.to_s] = (caller rescue "???")
43
+ elsif threading_error?
44
+ raise Tokyo::Locked, "%s is locked by %s" % [@source, locker_info]
45
+ else
46
+ tokyo_error!("%s#open(%s,%s): " % [self.class, @source, mode])
47
+ end
48
+ end
49
+
50
+ def __close__
51
+ @db.close
52
+ CONNECTIONS[@db.path] = nil
21
53
  end
22
54
 
23
55
  def close
@@ -28,7 +60,7 @@ module Ccp
28
60
  case state
29
61
  when CLOSED ; # NOP
30
62
  when READABLE,
31
- WRITABLE ; @db.close; @state = CLOSED
63
+ WRITABLE ; __close__; @state = CLOSED
32
64
  else ; raise "unknown state: #{state}"
33
65
  end
34
66
  end
data/lib/ccp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -205,6 +205,35 @@ describe Ccp::Kvs::Tokyo::Cabinet do
205
205
  end
206
206
  end
207
207
 
208
+ ######################################################################
209
+ ### read!
210
+
211
+ describe "#read!" do
212
+ specify do
213
+ put(:foo, 1)
214
+ put(:bar, 2)
215
+
216
+ kvs.R!
217
+ kvs.read!.should == {"foo" => "1", "bar" => "2"}
218
+ end
219
+ end
220
+
221
+ ######################################################################
222
+ ### each
223
+
224
+ describe "#each" do
225
+ specify do
226
+ put(:foo, 1)
227
+ put(:bar, 2)
228
+
229
+ kvs.R!
230
+
231
+ hash = {}
232
+ kvs.each{|k,v| hash[k] = v}
233
+ hash.should == {"foo" => "1", "bar" => "2"}
234
+ end
235
+ end
236
+
208
237
  ######################################################################
209
238
  ### keys
210
239
 
@@ -218,4 +247,32 @@ describe Ccp::Kvs::Tokyo::Cabinet do
218
247
  kvs.keys.sort.should == %w( bar baz foo )
219
248
  end
220
249
  end
250
+
251
+ ######################################################################
252
+ ### first
253
+
254
+ describe "#first" do
255
+ specify do
256
+ put(:foo, 1)
257
+ put(:bar, 2)
258
+
259
+ kvs.R!
260
+ first = kvs.first
261
+ first.should be_kind_of(Array)
262
+ first.size.should == 2
263
+ first[0].should =~ /^(foo|bar)$/
264
+ first[1].should =~ /^(1|2)$/
265
+ end
266
+ end
267
+
268
+ describe "#first_key" do
269
+ specify do
270
+ put(:foo, 1)
271
+ put(:bar, 2)
272
+
273
+ kvs.R!
274
+ kvs.first_key.should =~ /^(foo|bar)$/
275
+ end
276
+ end
277
+
221
278
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 2
10
- version: 0.3.2
9
+ - 3
10
+ version: 0.3.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-08-26 00:00:00 Z
18
+ date: 2013-08-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activesupport