ccp 0.3.5 → 0.4.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.
@@ -1,17 +1,33 @@
1
- require 'tokyocabinet'
1
+ begin
2
+ require 'tokyocabinet'
3
+ rescue LoadError
4
+ load_error = true
5
+ end
6
+
7
+ unless load_error
8
+ require 'ccp/kvs/tokyo'
2
9
 
3
- module Ccp
4
- module Kvs
5
- class Tch < Tokyo::Cabinet
6
- def get(k) ; R{ super }; end
7
- def set(k,v) ; W{ super }; end
8
- def del(k) ; W{ super }; end
9
- def count ; R{ super }; end
10
- def keys ; R{ super }; end
11
- def first_key; R{ super }; end
12
- def first ; R{ super }; end
13
- def read ; R{ super }; end
14
- def write(h) ; W{ super }; end
10
+ module Ccp
11
+ module Kvs
12
+ class Tch < Tokyo::Cabinet
13
+ # core
14
+ def get(k) ; R{ super }; end
15
+ def set(k,v) ; W{ super }; end
16
+ def del(k) ; W{ super }; end
17
+ def count ; R{ super }; end
18
+ def read ; R{ super }; end
19
+ def write(h) ; W{ super }; end
20
+
21
+ # enum
22
+ def each(&b) ; R{ super }; end
23
+ def each_pair(&b) ; R{ super }; end
24
+ def each_key(&b) ; R{ super }; end
25
+ def keys ; R{ super }; end
26
+ def first_key ; R{ super }; end
27
+ def first ; R{ super }; end
28
+ end
15
29
  end
16
30
  end
31
+
32
+ Ccp::Kvs << Ccp::Kvs::Tch
17
33
  end
@@ -101,11 +101,6 @@ module Ccp
101
101
  end
102
102
  end
103
103
 
104
- def each_keys(&block)
105
- STDERR.puts "DEPRECATION WARNING: #{self.class}#each_keys is deprecated and will be removed in 0.4.0, use each_key instead"
106
- each_key(&block)
107
- end
108
-
109
104
  def keys
110
105
  array = []
111
106
  each_key do |key|
@@ -65,11 +65,5 @@ module Ccp
65
65
  return @kvs.read
66
66
  end
67
67
  end
68
-
69
- # backward compat (until 0.3.6)
70
- def read!
71
- STDERR.puts "DEPRECATION WARNING: #{self.class}#read! will be removed in 0.3.6, use read instead"
72
- read
73
- end
74
68
  end
75
69
  end
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.3.5"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,131 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ codecs = %w( json msgpack )
5
+ exts = %w( tch kch )
6
+
7
+ codecs.product(exts).each do |codec, ext|
8
+ klass = Ccp::Kvs[ext]
9
+ describe "#{klass}(#{codec})" do
10
+ before { FileUtils.rm_rf(tmp_path) if tmp_path.directory? }
11
+ let(:kvs) { klass.new(tmp_path + "kvs.#{ext}") }
12
+
13
+ before {
14
+ kvs.codec!(codec)
15
+
16
+ kvs.touch
17
+ kvs["1"] = 10
18
+ kvs["2"] = 20
19
+ kvs["3"] = 30
20
+ kvs["4"] = 40
21
+ kvs.del("3")
22
+ # => {"1"=>10, "2"=>20, "4"=>40}
23
+ }
24
+
25
+ specify "(given)" do
26
+ kvs.read.should == {"1"=>10, "2"=>20, "4"=>40}
27
+ end
28
+
29
+ ######################################################################
30
+ ### enumerable
31
+
32
+ describe "#each" do
33
+ specify do
34
+ hash = {}
35
+ kvs.each do |k,v|
36
+ hash[k] = v
37
+ end
38
+ hash.should == {"1"=>10, "2"=>20, "4"=>40}
39
+ end
40
+ end
41
+
42
+ describe "#each_pair" do
43
+ specify do
44
+ hash = {}
45
+ kvs.each_pair do |k,v|
46
+ hash[k] = v
47
+ end
48
+ hash.should == {"1"=>10, "2"=>20, "4"=>40}
49
+ end
50
+ end
51
+
52
+ describe "#each_key" do
53
+ specify do
54
+ array = []
55
+ kvs.each_key do |k|
56
+ array << k
57
+ end
58
+ array.sort.should == %w( 1 2 4 )
59
+ end
60
+ end
61
+
62
+ describe "#keys" do
63
+ specify do
64
+ kvs.keys.sort.should == %w( 1 2 4 )
65
+ end
66
+ end
67
+
68
+ describe "#first_key" do
69
+ specify do
70
+ kvs.first_key.should =~ /^[124]$/
71
+ end
72
+ end
73
+
74
+ describe "#first" do
75
+ specify do
76
+ k,v = kvs.first
77
+ (k.to_i*10).should == v
78
+ end
79
+ end
80
+
81
+ ######################################################################
82
+ ### usecase
83
+
84
+ describe "#each with break" do
85
+ specify do
86
+ head = []
87
+ i = 0
88
+ kvs.each do |k,v|
89
+ i += 1
90
+ break if i > 1
91
+ head << v
92
+ end
93
+
94
+ head.should == [10]
95
+ end
96
+ end
97
+
98
+ describe "#each with raise" do
99
+ specify do
100
+ head = []
101
+ i = 0
102
+
103
+ lambda {
104
+ kvs.each do |k,v|
105
+ i += 1
106
+ raise "ok" if i > 1
107
+ head << v
108
+ end
109
+ }.should raise_error(/ok/)
110
+
111
+ head.should == [10]
112
+ end
113
+ end
114
+
115
+ describe "#each with next" do
116
+ specify do
117
+ tail = []
118
+ i = 0
119
+ kvs.each do |k,v|
120
+ i += 1
121
+ next if i <= 1
122
+ tail << v
123
+ end
124
+
125
+ tail.sort.should == [20, 40]
126
+ end
127
+ end
128
+
129
+ end
130
+
131
+ end
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  kvs_args = {}
5
5
  kvs_args["tch"] = "#{tmp_path}/kvs/foo.tch"
6
+ kvs_args["kch"] = "#{tmp_path}/kvs/foo.kch"
6
7
 
7
8
  Ccp::Kvs.each do |klass|
8
9
  describe klass do
@@ -0,0 +1,282 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Ccp::Kvs::Kyoto::Cabinet do
5
+ let(:tmp) { tmp_path + "kvs/kyoto/cabinet" }
6
+ let(:kch) { tmp + "foo.kch" }
7
+ let(:kvs) { subject }
8
+
9
+ subject { Ccp::Kvs::Kyoto::Cabinet.new(kch) }
10
+ before { FileUtils.rm_rf(tmp) if tmp.directory? }
11
+ after { subject.close }
12
+
13
+ def put(key, val)
14
+ kvs.touch
15
+ system("kchashmgr set #{kvs.path} #{key} #{val}")
16
+ end
17
+
18
+ def del(key)
19
+ kvs.touch
20
+ system("kchashmgr remove #{kvs.path} #{key}")
21
+ end
22
+
23
+ ######################################################################
24
+ ### count
25
+
26
+ describe "#count" do
27
+ context "(not opened)" do
28
+ specify { lambda { subject.count }.should raise_error(Ccp::Kvs::NotAllowed) }
29
+ end
30
+
31
+ context "(for read)" do
32
+ specify {
33
+ kvs.touch
34
+ kvs.R!()
35
+ kvs.count.should == 0
36
+ }
37
+
38
+ specify {
39
+ kvs.touch
40
+ kvs.R{ kvs.count.should == 0 }
41
+ }
42
+ end
43
+
44
+ context "(for write)" do
45
+ specify {
46
+ kvs.W!()
47
+ kvs.count.should == 0
48
+ }
49
+
50
+ specify {
51
+ kvs.W{ kvs.count.should == 0 }
52
+ }
53
+ end
54
+ end
55
+
56
+ ######################################################################
57
+ ### get
58
+
59
+ describe "#get" do
60
+ context "(not opened)" do
61
+ specify { lambda { kvs.get("foo") }.should raise_error(Ccp::Kvs::NotAllowed) }
62
+ end
63
+
64
+ context "(for read)[no records]" do
65
+ before { kvs.touch; kvs.R! }
66
+ specify { kvs.get("foo").should == nil }
67
+ end
68
+
69
+ context "(for read block)[no records]" do
70
+ before { kvs.touch }
71
+ specify { kvs.R { kvs.get("foo").should == nil } }
72
+ end
73
+
74
+ context "(for read)[exists]" do
75
+ before { put(:foo, 1); kvs.R! }
76
+ specify { kvs.get("foo").should == "1" }
77
+ end
78
+
79
+ context "(for read block)[exists]" do
80
+ before { put(:foo, 1); kvs.R! }
81
+ specify { kvs.R { kvs.get("foo").should == "1" } }
82
+ end
83
+
84
+ context "(for write)[no records]" do
85
+ before { kvs.W! }
86
+ specify { kvs.get("foo").should == nil }
87
+ end
88
+
89
+ context "(for write block)[no records]" do
90
+ specify { kvs.W { kvs.get("foo").should == nil } }
91
+ end
92
+
93
+ context "(for write)[exists]" do
94
+ before { put(:foo, 1); kvs.W! }
95
+ specify { kvs.get("foo").should == "1" }
96
+ end
97
+
98
+ context "(for write block)[exists]" do
99
+ before { put(:foo, 1); kvs.W! }
100
+ specify { kvs.W { kvs.get("foo").should == "1" } }
101
+ end
102
+ end
103
+
104
+ ######################################################################
105
+ ### set
106
+
107
+ describe "#set" do
108
+ before { kvs.touch }
109
+
110
+ context "(not opened)" do
111
+ specify { lambda { kvs.set("foo", 2) }.should raise_error(Ccp::Kvs::NotAllowed) }
112
+ end
113
+
114
+ context "(for read)" do
115
+ before { kvs.R! }
116
+ specify { lambda { kvs.set("foo", 2) }.should raise_error(Ccp::Kvs::NotAllowed) }
117
+ end
118
+
119
+ context "(for read block)" do
120
+ specify { lambda { kvs.R{ kvs.set("foo", 2) } }.should raise_error(Ccp::Kvs::NotAllowed) }
121
+ end
122
+
123
+ context "(for write)" do
124
+ before { kvs.W! }
125
+ specify { lambda { kvs.set("foo", 2) }.should_not raise_error }
126
+ end
127
+
128
+ context "(for write block)" do
129
+ specify { lambda { kvs.W{ kvs.set("foo", 2) }.should_not raise_error } }
130
+ end
131
+ end
132
+
133
+ ######################################################################
134
+ ### del
135
+
136
+ describe "#del" do
137
+ before { kvs.touch; put(:foo, 3) }
138
+
139
+ context "(not opened)" do
140
+ specify { lambda { kvs.del("foo") }.should raise_error(Ccp::Kvs::NotAllowed) }
141
+ end
142
+
143
+ context "(for read)" do
144
+ before { kvs.R! }
145
+ specify { lambda { kvs.del("foo") }.should raise_error(Ccp::Kvs::NotAllowed) }
146
+ end
147
+
148
+ context "(for read block)" do
149
+ specify { lambda { kvs.R{ kvs.del("foo") } }.should raise_error(Ccp::Kvs::NotAllowed) }
150
+ end
151
+
152
+ context "(for write)" do
153
+ before { kvs.W! }
154
+ specify {
155
+ kvs.get("foo").should == "3"
156
+ kvs.del("foo").should == "3"
157
+ kvs.get("foo").should == nil
158
+ }
159
+ end
160
+
161
+ context "(for write block)" do
162
+ specify {
163
+ kvs.W {
164
+ kvs.get("foo").should == "3"
165
+ kvs.del("foo").should == "3"
166
+ kvs.get("foo").should == nil
167
+ }
168
+ }
169
+ end
170
+
171
+ context "(for write)[no records]" do
172
+ before { del(:foo); kvs.W! }
173
+ specify {
174
+ kvs.get("foo").should == nil
175
+ kvs.del("foo").should == nil
176
+ }
177
+ end
178
+
179
+ context "(for write block)[no records]" do
180
+ before { del(:foo) }
181
+ specify {
182
+ kvs.W {
183
+ kvs.get("foo").should == nil
184
+ kvs.del("foo").should == nil
185
+ }
186
+ }
187
+ end
188
+ end
189
+
190
+ ######################################################################
191
+ ### read / write
192
+
193
+ describe "#read" do
194
+ specify do
195
+ put(:foo, 1)
196
+ put(:bar, 2)
197
+
198
+ kvs.R!
199
+ kvs.read.should == {"foo" => "1", "bar" => "2"}
200
+ end
201
+ end
202
+
203
+ describe "#write" do
204
+ specify do
205
+ kvs.W!
206
+ kvs.write("foo" => "1", "bar" => "2")
207
+
208
+ kvs.get("foo").should == "1"
209
+ kvs.get("bar").should == "2"
210
+ end
211
+ end
212
+
213
+ ######################################################################
214
+ ### each
215
+
216
+ describe "#each" do
217
+ specify do
218
+ put(:foo, 1)
219
+ put(:bar, 2)
220
+
221
+ kvs.R!
222
+
223
+ hash = {}
224
+ kvs.each{|k,v| hash[k] = v}
225
+ hash.should == {"foo" => "1", "bar" => "2"}
226
+ end
227
+ end
228
+
229
+ ######################################################################
230
+ ### keys
231
+
232
+ describe "#keys" do
233
+ specify do
234
+ put(:foo, 1)
235
+ put(:bar, 2)
236
+ put(:baz, 3)
237
+
238
+ kvs.R!
239
+ kvs.keys.sort.should == %w( bar baz foo )
240
+ end
241
+ end
242
+
243
+ ######################################################################
244
+ ### first
245
+
246
+ describe "#first" do
247
+ specify do
248
+ put(:foo, 1)
249
+ put(:bar, 2)
250
+
251
+ kvs.R!
252
+ first = kvs.first
253
+ first.should be_kind_of(Array)
254
+ first.size.should == 2
255
+ first[0].should =~ /^(foo|bar)$/
256
+ first[1].should =~ /^(1|2)$/
257
+ end
258
+ end
259
+
260
+ describe "#first_key" do
261
+ specify do
262
+ put(:foo, 1)
263
+ put(:bar, 2)
264
+
265
+ kvs.R!
266
+ kvs.first_key.should =~ /^(foo|bar)$/
267
+ end
268
+ end
269
+
270
+ ######################################################################
271
+ ### exist?
272
+
273
+ describe "#exist?" do
274
+ specify do
275
+ put(:foo, 1)
276
+
277
+ kvs.R!
278
+ kvs.exist?(:foo).should be
279
+ kvs.exist?(:bar).should_not be
280
+ end
281
+ end
282
+ end