okuyama 0.0.1 → 0.1.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.
@@ -0,0 +1,306 @@
1
+ module Okuyama
2
+ module Protocol
3
+ class Version1 < AbstractProtocol
4
+ def initialize(options=nil)
5
+ if options then
6
+ @base64_encode_flag = options[:base64_encode_flag]
7
+ end
8
+ @base64_encode_flag = true if @base64_encode_flag.nil?
9
+ end
10
+
11
+ def version
12
+ return '1.0.0'
13
+ end
14
+
15
+ def encode(text)
16
+ return text if ! @base64_encode_flag
17
+ return Base64.encode64(text).chomp
18
+ end
19
+
20
+ def decode(text)
21
+ return text if ! @base64_encode_flag
22
+ return Base64.decode64(text)
23
+ end
24
+
25
+ def init_count(socket)
26
+ socket.puts '0'
27
+ end
28
+
29
+ def set_value(socket, key, val, tag_list=nil)
30
+ return self.send_key_tags_value_message(socket, '1', key, tag_list, val)
31
+ end
32
+
33
+ def get_value(socket, key)
34
+ return self.send_key_message(socket, '2', key)
35
+ end
36
+
37
+ def get_tag_keys(socket, tag, flag)
38
+ return self.send_tag_flag_message(socket, '3', tag, flag)
39
+ end
40
+
41
+ def remove_value(socket, key)
42
+ return self.send_key_dlock_message(socket, '5', key, '0')
43
+ end
44
+
45
+ def set_new_value(socket, key, val, tag_list=nil)
46
+ return self.send_key_tags_value_message(socket, '6', key, tag_list, val)
47
+ end
48
+
49
+ def get_value_version_check(socket, key)
50
+ return self.send_key_message(socket, '15', key)
51
+ end
52
+
53
+ def set_value_version_check(socket, key, val, version, tag_list=nil)
54
+ return self.send_key_tags_value_message(socket, '16', key, tag_list, val, version)
55
+ end
56
+
57
+ def incr_value(socket, key, val)
58
+ return self.send_key_value_message(socket, '13', key, val)
59
+ end
60
+
61
+ def decr_value(socket, key, val)
62
+ return self.send_key_value_message(socket, '14', key, val)
63
+ end
64
+
65
+ def get_multi_value(socket, key_list)
66
+ return self.send_keys_message(socket, '22', key_list)
67
+ end
68
+
69
+ def get_tag_values(socket, tag)
70
+ return self.send_tag_message(socket, '23', tag)
71
+ end
72
+
73
+ def remove_tag_from_key(socket, tag, key)
74
+ return self.send_tag_key_message(socket, '40', tag, key)
75
+ end
76
+
77
+ def set_value_and_create_index(socket, key, val, tag_list=nil, group=nil, min_index_n='1', max_index_n='3')
78
+ return self.send_key_tags_value_group_index_message(socket, '42', key, tag_list, val, group, min_index_n, max_index_n)
79
+ end
80
+
81
+ def search_value(socket, query_list, condition='1', group=nil, nsize='3')
82
+ return self.send_query_condition_group_index_message(socket, '43', query_list, condition, group, nsize)
83
+ end
84
+
85
+ def parse_line_result(result, to_i_flag=nil)
86
+ record = result.split(/,/)
87
+
88
+ opcode = record.shift
89
+ exit_code = record.shift
90
+
91
+ case exit_code
92
+ when 'false'
93
+ case opcode
94
+ when '4'
95
+ return []
96
+ when '43'
97
+ return []
98
+ else
99
+ return nil
100
+ end
101
+ when 'error'
102
+ raise Okuyama::ServerError, record[0]
103
+ end
104
+
105
+ case opcode
106
+ when '0' # init_count
107
+ result = record[0]
108
+ result = result.to_i if to_i_flag
109
+ return result
110
+ when '2' # get_value
111
+ ret = record[0]
112
+ ret = self.decode(record[0])
113
+ return ret
114
+ when '4' # get_tag_keys
115
+ return record[0].split(/:/).map{|b|self.decode(b)}
116
+ when '13' # incr_value
117
+ if to_i_flag then
118
+ result = Base64.decode64(record[0]).chomp
119
+ result = result.to_i
120
+ else
121
+ result = self.decode(record[0])
122
+ end
123
+ return result
124
+ when '14' # decr_value
125
+ if to_i_flag then
126
+ result = Base64.decode64(record[0]).chomp
127
+ result = result.to_i
128
+ else
129
+ result = self.decode(record[0])
130
+ end
131
+ return result
132
+ when '15' # get_value_version_check
133
+ record[0] = self.decode(record[0])
134
+ return record
135
+ when '22' # get_multi_value
136
+ return self.decode(record[0])
137
+ when '23' # get_tag_values
138
+ record[0] = self.decode(record[0])
139
+ record[1] = self.decode(record[1])
140
+ return record
141
+ when '43' # search_value
142
+ return record[0].split(/:/).map{|b|self.decode(b)}
143
+ else
144
+ return true
145
+ end
146
+ return record
147
+ end
148
+
149
+ protected
150
+ def send_key_message(socket, opcode, key)
151
+ key_base64 = self.encode(key)
152
+ socket.print opcode
153
+ socket.print ","
154
+ socket.print key_base64
155
+ socket.puts
156
+ end
157
+
158
+ def send_key_dlock_message(socket, opcode, key, dlock)
159
+ key_base64 = self.encode(key)
160
+ socket.print opcode
161
+ socket.print ","
162
+ socket.print key_base64
163
+ socket.print ","
164
+ socket.print dlock
165
+ socket.puts
166
+ end
167
+
168
+ def send_keys_message(socket, opcode, key_list)
169
+ keys_base64 = key_list.map{|key|self.encode(key)}.join(',')
170
+ socket.print opcode
171
+ socket.print ","
172
+ socket.print keys_base64
173
+ socket.puts
174
+ end
175
+
176
+ def send_key_value_message(socket, opcode, key, val)
177
+ key_base64 = self.encode(key)
178
+ val_base64 = self.encode(val)
179
+ dlock = '0'
180
+ socket.print opcode
181
+ socket.print ","
182
+ socket.print key_base64
183
+ socket.print ","
184
+ socket.print dlock
185
+ socket.print ","
186
+ socket.print val_base64
187
+ socket.puts
188
+ end
189
+
190
+ def send_key_tags_value_message(socket, opcode, key, tag_list, val, version=nil)
191
+ key_base64 = self.encode(key)
192
+ val_base64 = self.encode(val)
193
+ tags = nil
194
+ if tag_list then
195
+ tags = tag_list.map{|t|self.encode(t)}.join(':')
196
+ end
197
+ tags ||= "(B)"
198
+ dlock = '0'
199
+ socket.print opcode
200
+ socket.print ","
201
+ socket.print key_base64
202
+ socket.print ","
203
+ socket.print tags
204
+ socket.print ","
205
+ socket.print dlock
206
+ socket.print ","
207
+ socket.print val_base64
208
+
209
+ if version then
210
+ socket.print ","
211
+ socket.print version
212
+ end
213
+ socket.puts
214
+ end
215
+
216
+ def send_key_tags_value_group_index_message(socket, opcode, key, tag_list, val, group, min_index_n, max_index_n)
217
+ key_base64 = self.encode(key)
218
+ val_base64 = self.encode(val)
219
+
220
+ tags = nil
221
+ if tag_list then
222
+ tags = tag_list.map{|t|self.encode(t)}.join(':')
223
+ end
224
+ tags ||= "(B)"
225
+
226
+ dlock = '0'
227
+
228
+ group_base64 = nil
229
+ if group then
230
+ group_base64 = self.encode(group)
231
+ end
232
+ group_base64 ||= "(B)"
233
+
234
+ socket.print opcode
235
+ socket.print ","
236
+ socket.print key_base64
237
+ socket.print ","
238
+ socket.print tags
239
+ socket.print ","
240
+ socket.print dlock
241
+ socket.print ","
242
+ socket.print val_base64
243
+ socket.print ","
244
+ socket.print group_base64
245
+ socket.print ","
246
+ socket.print min_index_n
247
+ socket.print ","
248
+ socket.print max_index_n
249
+ socket.puts
250
+ end
251
+
252
+ def send_tag_message(socket, opcode, tag)
253
+ tag_base64 = self.encode(tag)
254
+ socket.print opcode
255
+ socket.print ","
256
+ socket.print tag_base64
257
+ socket.puts
258
+ end
259
+
260
+ def send_tag_flag_message(socket, opcode, tag, flag)
261
+ tag_base64 = self.encode(tag)
262
+ socket.print opcode
263
+ socket.print ","
264
+ socket.print tag_base64
265
+ socket.print ","
266
+ socket.print flag
267
+ socket.puts
268
+ end
269
+
270
+ def send_tag_key_message(socket, opcode, tag, key)
271
+ tag_base64 = self.encode(tag)
272
+ key_base64 = self.encode(key)
273
+ dlock = '0'
274
+ socket.print opcode
275
+ socket.print ","
276
+ socket.print tag_base64
277
+ socket.print ","
278
+ socket.print key_base64
279
+ socket.print ","
280
+ socket.print dlock
281
+ socket.puts
282
+ end
283
+
284
+ def send_query_condition_group_index_message(socket, opcode, query_list, condition, group, nsize)
285
+ queries = query_list.map{|q|self.encode(q)}.join(':')
286
+
287
+ group_base64 = nil
288
+ if group then
289
+ group_base64 = self.encode(group)
290
+ end
291
+ group_base64 ||= "(B)"
292
+ socket.print opcode
293
+ socket.print ","
294
+ socket.print queries
295
+ socket.print ","
296
+ socket.print condition
297
+ socket.print ","
298
+ socket.print group_base64
299
+ socket.print ","
300
+ socket.print nsize
301
+ socket.puts
302
+ end
303
+
304
+ end
305
+ end
306
+ end
data/lib/okuyama.rb CHANGED
@@ -5,9 +5,18 @@ autoload :Logger, 'logger'
5
5
 
6
6
  module Okuyama
7
7
  autoload :Client, 'okuyama/client'
8
- autoload :Protocol, 'okuyama/protocol'
8
+ autoload :FastClient, 'okuyama/fast_client'
9
+ module Protocol
10
+ autoload :AbstractProtocol, 'okuyama/protocol/abstract_protocol'
11
+ autoload :Version1, 'okuyama/protocol/version1'
12
+ end
9
13
 
14
+ # generic error
10
15
  class OkuyamaError < RuntimeError; end
16
+ # socket/server communication error
17
+ class NetworkError < OkuyamaError; end
18
+ # server error
19
+ class ServerError < OkuyamaError; end
11
20
 
12
21
  def self.logger
13
22
  @logger ||= (rails_logger || default_logger)
data/okuyama.gemspec ADDED
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "okuyama"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kenji Hara"]
12
+ s.date = "2012-03-25"
13
+ s.description = "okuyama client for Ruby"
14
+ s.email = "haracane@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/okuyama.rb",
29
+ "lib/okuyama/client.rb",
30
+ "lib/okuyama/fast_client.rb",
31
+ "lib/okuyama/protocol/abstract_protocol.rb",
32
+ "lib/okuyama/protocol/version1.rb",
33
+ "okuyama.gemspec",
34
+ "spec/okuyama/client/decr_value_spec.rb",
35
+ "spec/okuyama/client/incr_value_spec.rb",
36
+ "spec/okuyama/client/search_spec.rb",
37
+ "spec/okuyama/client_spec.rb",
38
+ "spec/okuyama/protocol/version1_spec.rb",
39
+ "spec/okuyama_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = "http://github.com/haracane/okuyama"
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = "1.8.21"
46
+ s.summary = "okuyama client for Ruby"
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
53
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
54
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
56
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
59
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
66
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
67
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
68
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
69
+ s.add_dependency(%q<rcov>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe Okuyama::Client do
4
+
5
+ [true, false].each do |base64_encode_flag|
6
+ describe "when base64_encode_flag = #{base64_encode_flag}" do
7
+
8
+ before :each do
9
+ @testnumval_int = 10
10
+
11
+ @testnumkey = 'testnumkey'
12
+ @testnumval = @testnumval_int.to_s
13
+ @testkey1 = 'testkey1'
14
+ @testval1 = 'testval1'
15
+ @testkey2 = 'testkey2'
16
+ @testval2 = 'testval2'
17
+ @testnewkey = 'testnewkey'
18
+ @testnewval = 'testnewval'
19
+ @testnewval1 = 'testnewval1'
20
+ @testtag = 'testtag'
21
+ @testnewtag = 'testnewtag'
22
+ @testgroup = 'testgroup'
23
+ @testnewgroup = 'testnewgroup'
24
+ @testquery1 = 'testval'
25
+ @testquery2 = 'val1'
26
+ @testnewquery = 'testnewval'
27
+
28
+ if base64_encode_flag == false then
29
+ @testnumkey = Base64.encode64(@testnumkey).chomp
30
+ @testnumval = Base64.encode64(@testnumval).chomp
31
+ @testkey1 = Base64.encode64(@testkey1).chomp
32
+ @testval1 = Base64.encode64(@testval1).chomp
33
+ @testkey2 = Base64.encode64(@testkey2).chomp
34
+ @testval2 = Base64.encode64(@testval2).chomp
35
+ @testnewkey = Base64.encode64(@testnewkey).chomp
36
+ @testnewval = Base64.encode64(@testnewval).chomp
37
+ @testnewval1 = Base64.encode64(@testnewval1).chomp
38
+ @testtag = Base64.encode64(@testtag).chomp
39
+ @testnewtag = Base64.encode64(@testnewtag).chomp
40
+ @testgroup = Base64.encode64(@testgroup).chomp
41
+ @testnewgroup = Base64.encode64(@testnewgroup).chomp
42
+ @testquery1 = Base64.encode64(@testquery1).chomp
43
+ @testquery2 = Base64.encode64(@testquery2).chomp
44
+ @testnewquery = Base64.encode64(@testnewquery).chomp
45
+ end
46
+
47
+
48
+ @client = Okuyama::Client.new(:host=>'localhost', :port=>8888, :base64_encode_flag=>base64_encode_flag)
49
+ @client.debug = true
50
+ @client.remove_value(@testnumkey)
51
+ @client.remove_value(@testkey1)
52
+ @client.remove_value(@testkey2)
53
+ @client.remove_value(@testnewkey)
54
+ @client.remove_tag_from_key(@testnewtag, @testkey1)
55
+ @client.set_value(@testnumkey, @testnumval)
56
+ @client.set_value_and_create_index(@testkey1, @testval1, :tags=>[@testtag], :group=>@testgroup, :min_n=>1, :max_n=>3)
57
+ @client.set_value(@testkey2, @testval2, @testtag)
58
+
59
+ end
60
+
61
+ after :each do
62
+ @client.close
63
+ end
64
+
65
+ describe "decr_value(key)" do
66
+ describe "when key exists," do
67
+ describe "when value is a number" do
68
+ it "should return decremented value(integer)" do
69
+ result = @client.decr_value(@testnumkey, 1)
70
+ result.should == (@testnumval_int - 1)
71
+ end
72
+ it "should return decremented value(text)" do
73
+ @client.to_i_flag = false
74
+ val = @client.get_value(@testnumkey)
75
+ result = @client.decr_value(@testnumkey, 1)
76
+ expected = (@testnumval_int - 1).to_s
77
+ expected = Base64.encode64(expected).chomp if base64_encode_flag == false
78
+ result.should == expected
79
+ end
80
+ end
81
+ describe "when value is not a number" do
82
+ it "should return 1(integer)" do
83
+ result = @client.decr_value(@testkey1, 1)
84
+ result.should == 0
85
+ end
86
+ end
87
+ end
88
+ describe "when key does not exist," do
89
+ it "should fail" do
90
+ # result = @client.decr_value(@testnewkey, 1)
91
+ # result.should be_nil
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+ end
98
+ end
99
+
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe Okuyama::Client do
4
+
5
+ [true,true].each do |base64_encode_flag|
6
+ describe "when base64_encode_flag = #{base64_encode_flag}" do
7
+
8
+ before :each do
9
+ @testnumval_int = 10
10
+
11
+ @testnumkey = 'testnumkey'
12
+ @testnumval = @testnumval_int.to_s
13
+ @testkey1 = 'testkey1'
14
+ @testval1 = 'testval1'
15
+ @testkey2 = 'testkey2'
16
+ @testval2 = 'testval2'
17
+ @testnewkey = 'testnewkey'
18
+ @testnewval = 'testnewval'
19
+ @testnewval1 = 'testnewval1'
20
+ @testtag = 'testtag'
21
+ @testnewtag = 'testnewtag'
22
+ @testgroup = 'testgroup'
23
+ @testnewgroup = 'testnewgroup'
24
+ @testquery1 = 'testval'
25
+ @testquery2 = 'val1'
26
+ @testnewquery = 'testnewval'
27
+
28
+ if base64_encode_flag == false then
29
+ @testnumkey = Base64.encode64(@testnumkey).chomp
30
+ @testnumval = Base64.encode64(@testnumval).chomp
31
+ @testkey1 = Base64.encode64(@testkey1).chomp
32
+ @testval1 = Base64.encode64(@testval1).chomp
33
+ @testkey2 = Base64.encode64(@testkey2).chomp
34
+ @testval2 = Base64.encode64(@testval2).chomp
35
+ @testnewkey = Base64.encode64(@testnewkey).chomp
36
+ @testnewval = Base64.encode64(@testnewval).chomp
37
+ @testnewval1 = Base64.encode64(@testnewval1).chomp
38
+ @testtag = Base64.encode64(@testtag).chomp
39
+ @testnewtag = Base64.encode64(@testnewtag).chomp
40
+ @testgroup = Base64.encode64(@testgroup).chomp
41
+ @testnewgroup = Base64.encode64(@testnewgroup).chomp
42
+ @testquery1 = Base64.encode64(@testquery1).chomp
43
+ @testquery2 = Base64.encode64(@testquery2).chomp
44
+ @testnewquery = Base64.encode64(@testnewquery).chomp
45
+ end
46
+
47
+ @client = Okuyama::Client.new(:host=>'localhost', :port=>8888, :base64_encode_flag=>base64_encode_flag)
48
+ @client.debug = true
49
+ @client.remove_value(@testnumkey)
50
+ @client.remove_value(@testkey1)
51
+ @client.remove_value(@testkey2)
52
+ @client.remove_value(@testnewkey)
53
+ @client.remove_tag_from_key(@testnewtag, @testkey1)
54
+ @client.set_value(@testnumkey, @testnumval)
55
+ @client.set_value_and_create_index(@testkey1, @testval1, :tags=>[@testtag], :group=>@testgroup, :min_n=>1, :max_n=>3)
56
+ @client.set_value(@testkey2, @testval2, @testtag)
57
+
58
+ end
59
+
60
+ after :each do
61
+ @client.close
62
+ end
63
+
64
+ describe "incr_value(key, val)" do
65
+ describe "when key exists," do
66
+ describe "when value is a number" do
67
+ it "should return incremented value(integer)" do
68
+ Okuyama.logger.debug("send: #{@client.protocol.message_of_incr_value(@testnumkey, '1').inspect}")
69
+ result = @client.incr_value(@testnumkey, 1)
70
+ result.should == (@testnumval_int + 1)
71
+ end
72
+ it "should return incremented value(text)" do
73
+ @client.to_i_flag = false
74
+ result = @client.incr_value(@testnumkey, 1)
75
+ expected = (@testnumval_int + 1).to_s
76
+ expected = Base64.encode64(expected).chomp if base64_encode_flag == false
77
+ result.should == expected
78
+ end
79
+ end
80
+ describe "when value is not a number" do
81
+ it "should return 1(integer)" do
82
+ result = @client.incr_value(@testkey1, 1)
83
+ result.should == 1
84
+ end
85
+ end
86
+ end
87
+ describe "when key does not exist," do
88
+ it "should fail" do
89
+ Okuyama.logger.debug("send: #{@client.protocol.message_of_incr_value(@testnewkey, '1').inspect}")
90
+ result = @client.incr_value(@testnewkey, 1)
91
+ result.should be_nil
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+ end
98
+ end
99
+