kyototycoon 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/Changes.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.5.3
2
+
3
+ * fixed bug using base64 encoding
4
+ * regenerate socket when that closed
5
+
1
6
  ## v0.5.2
2
7
 
3
8
  * fixed encoded response handling
data/kyototycoon.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kyototycoon}
8
- s.version = "0.5.2"
8
+ s.version = "0.5.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["uu59"]
12
- s.date = %q{2011-01-03}
12
+ s.date = %q{2011-01-05}
13
13
  s.description = %q{KyotoTycoon client for Ruby}
14
14
  s.email = %q{a@tt25.org}
15
15
  s.extra_rdoc_files = [
@@ -40,10 +40,11 @@ class KyotoTycoon
40
40
  end
41
41
  end
42
42
  body = @sock.read(bodylen)
43
- [status.to_i, Tsvrpc.decode_responce_body(body, colenc)]
43
+ [status.to_i, body, colenc]
44
44
  end
45
45
 
46
46
  def start
47
+ @sock = nil if @sock && @sock.closed?
47
48
  @sock ||= ::TCPSocket.new(@host, @port)
48
49
  end
49
50
 
@@ -3,9 +3,19 @@
3
3
 
4
4
  class KyotoTycoon
5
5
  module Tsvrpc
6
- def self.parse(body)
6
+ def self.parse(body, colenc)
7
+ decoder = case colenc
8
+ when "U"
9
+ lambda{|body| CGI.unescape(body)}
10
+ when "B"
11
+ lambda{|body| Base64.decode64(body)}
12
+ when nil
13
+ lambda{|body| body}
14
+ else
15
+ raise "Unknown colenc(response) '#{colenc}'"
16
+ end
7
17
  body.split("\n").inject({}){|r, line|
8
- k,v = *line.split("\t", 2).map{|v| CGI.unescape(v)}
18
+ k,v = *line.split("\t", 2).map{|v| decoder.call(v)}
9
19
  r[k] = v
10
20
  r
11
21
  }
@@ -14,45 +24,22 @@ class KyotoTycoon
14
24
  def self.build_query(params, colenc='U')
15
25
  query = ""
16
26
  if params
17
- case colenc.to_s.upcase.to_sym
27
+ encoder = case colenc.to_s.upcase.to_sym
18
28
  when :U
19
- query = params.inject([]){|r, tmp|
20
- unless tmp.last.nil?
21
- r << tmp.map{|v| CGI.escape(v.to_s)}.join("\t")
22
- end
23
- r
24
- }.join("\r\n")
29
+ lambda{|body| CGI.escape(body.to_s)}
25
30
  when :B
26
- query = params.inject([]){|r, tmp|
27
- unless tmp.last.nil?
28
- r << tmp.map{|v| Base64.encode64(v.to_s).rstrip}.join("\t")
29
- end
30
- r
31
- }.join("\r\n")
31
+ lambda{|body| [body.to_s].pack('m').gsub("\n","")}
32
32
  else
33
33
  raise "Unknown colenc '#{colenc}'"
34
34
  end
35
+ query = params.inject([]){|r, tmp|
36
+ unless tmp.last.nil?
37
+ r << tmp.map{|v| encoder.call(v)}.join("\t")
38
+ end
39
+ r
40
+ }.join("\r\n")
35
41
  end
36
42
  query
37
43
  end
38
-
39
- def self.decode_responce_body(body, colenc)
40
- case colenc
41
- when "B"
42
- body.split("\n").map{|row|
43
- row.split("\t").map{|col| Base64.decode64(col)}.join("\t")
44
- }.join("\n")
45
- when "U"
46
- body.split("\n").map{|row|
47
- row.split("\t").map{|col|
48
- CGI.unescape(col)
49
- }.join("\t")
50
- }.join("\n")
51
- when nil
52
- body
53
- else
54
- raise "Unknown colenc(response) '#{colenc}'"
55
- end
56
- end
57
44
  end
58
45
  end
data/lib/kyototycoon.rb CHANGED
@@ -13,7 +13,7 @@ require "kyototycoon/tsvrpc/skinny.rb"
13
13
  require "kyototycoon/stream.rb"
14
14
 
15
15
  class KyotoTycoon
16
- VERSION = '0.5.1'
16
+ VERSION = '0.5.3'
17
17
 
18
18
  attr_accessor :colenc, :connect_timeout, :servers
19
19
  attr_reader :serializer, :logger, :db
@@ -67,7 +67,7 @@ class KyotoTycoon
67
67
 
68
68
  def get(key)
69
69
  res = request('/rpc/get', {:key => key})
70
- @serializer.decode(Tsvrpc.parse(res[:body])['value'])
70
+ @serializer.decode(Tsvrpc.parse(res[:body], res[:colenc])['value'])
71
71
  end
72
72
  alias_method :[], :get
73
73
 
@@ -78,18 +78,18 @@ class KyotoTycoon
78
78
 
79
79
  def set(key, value, xt=nil)
80
80
  res = request('/rpc/set', {:key => key, :value => @serializer.encode(value), :xt => xt})
81
- Tsvrpc.parse(res[:body])
81
+ Tsvrpc.parse(res[:body], res[:colenc])
82
82
  end
83
83
  alias_method :[]=, :set
84
84
 
85
85
  def add(key, value, xt=nil)
86
86
  res = request('/rpc/add', {:key => key, :value => @serializer.encode(value), :xt => xt})
87
- Tsvrpc.parse(res[:body])
87
+ Tsvrpc.parse(res[:body], res[:colenc])
88
88
  end
89
89
 
90
90
  def replace(key, value, xt=nil)
91
91
  res = request('/rpc/replace', {:key => key, :value => @serializer.encode(value), :xt => xt})
92
- Tsvrpc.parse(res[:body])
92
+ Tsvrpc.parse(res[:body], res[:colenc])
93
93
  end
94
94
 
95
95
  def append(key, value, xt=nil)
@@ -108,7 +108,7 @@ class KyotoTycoon
108
108
 
109
109
  def increment(key, num=1, xt=nil)
110
110
  res = request('/rpc/increment', {:key => key, :num => num, :xt => xt})
111
- Tsvrpc.parse(res[:body])['num'].to_i
111
+ Tsvrpc.parse(res[:body], res[:colenc])['num'].to_i
112
112
  end
113
113
  alias_method :incr, :increment
114
114
 
@@ -119,7 +119,7 @@ class KyotoTycoon
119
119
 
120
120
  def increment_double(key, num, xt=nil)
121
121
  res = request('/rpc/increment_double', {:key => key, :num => num, :xt => xt})
122
- Tsvrpc.parse(res[:body])['num'].to_f
122
+ Tsvrpc.parse(res[:body], res[:colenc])['num'].to_f
123
123
  end
124
124
 
125
125
  def set_bulk(records)
@@ -129,7 +129,7 @@ class KyotoTycoon
129
129
  values["_#{k}"] = @serializer.encode(v)
130
130
  }
131
131
  res = request('/rpc/set_bulk', values)
132
- Tsvrpc.parse(res[:body])
132
+ Tsvrpc.parse(res[:body], res[:colenc])
133
133
  end
134
134
 
135
135
  def get_bulk(keys)
@@ -139,7 +139,7 @@ class KyotoTycoon
139
139
  }
140
140
  res = request('/rpc/get_bulk', params)
141
141
  ret = {}
142
- Tsvrpc.parse(res[:body]).each{|k,v|
142
+ Tsvrpc.parse(res[:body], res[:colenc]).each{|k,v|
143
143
  ret[k] = k.match(/^_/) ? @serializer.decode(v) : v
144
144
  }
145
145
  ret
@@ -151,7 +151,7 @@ class KyotoTycoon
151
151
  params
152
152
  }
153
153
  res = request('/rpc/remove_bulk', params)
154
- Tsvrpc.parse(res[:body])
154
+ Tsvrpc.parse(res[:body], res[:colenc])
155
155
  end
156
156
 
157
157
  def clear
@@ -169,23 +169,23 @@ class KyotoTycoon
169
169
 
170
170
  def echo(value)
171
171
  res = request('/rpc/echo', value)
172
- Tsvrpc.parse(res[:body])
172
+ Tsvrpc.parse(res[:body], res[:colenc])
173
173
  end
174
174
 
175
175
  def report
176
176
  res = request('/rpc/report')
177
- Tsvrpc.parse(res[:body])
177
+ Tsvrpc.parse(res[:body], res[:colenc])
178
178
  end
179
179
 
180
180
  def status
181
181
  res = request('/rpc/status')
182
- Tsvrpc.parse(res[:body])
182
+ Tsvrpc.parse(res[:body], res[:colenc])
183
183
  end
184
184
 
185
185
  def match_prefix(prefix)
186
186
  res = request('/rpc/match_prefix', {:prefix => prefix})
187
187
  keys = []
188
- Tsvrpc.parse(res[:body]).each{|k,v|
188
+ Tsvrpc.parse(res[:body], res[:colenc]).each{|k,v|
189
189
  if k != 'num'
190
190
  keys << k[1, k.length]
191
191
  end
@@ -199,7 +199,7 @@ class KyotoTycoon
199
199
  end
200
200
  res = request('/rpc/match_regex', {:regex => re})
201
201
  keys = []
202
- Tsvrpc.parse(res[:body]).each{|k,v|
202
+ Tsvrpc.parse(res[:body], res[:colenc]).each{|k,v|
203
203
  if k != 'num'
204
204
  keys << k[1, k.length]
205
205
  end
@@ -217,11 +217,11 @@ class KyotoTycoon
217
217
  params[:DB] = @db
218
218
  end
219
219
 
220
- status,body = client.request(path, params, @colenc)
220
+ status,body,colenc = client.request(path, params, @colenc)
221
221
  if ![200, 450].include?(status.to_i)
222
222
  raise body
223
223
  end
224
- res = {:status => status, :body => body}
224
+ res = {:status => status, :body => body, :colenc => colenc}
225
225
  @logger.info("#{path}: #{res[:status]} with query parameters #{params.inspect}")
226
226
  res
227
227
  end
data/spec/helper.rb CHANGED
@@ -64,6 +64,10 @@ describe do
64
64
  @kt[:a] = 1
65
65
  @kt[:b] = 1
66
66
  @kt.keys.sort.should == %w!a b!.sort
67
+ @kt.clear
68
+
69
+ @kt[:longvalue] = "-" * 2048
70
+ @kt[:longvalue].should == '-' * 2048
67
71
  end
68
72
 
69
73
  it 'should provide bulk' do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 2
9
- version: 0.5.2
8
+ - 3
9
+ version: 0.5.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - uu59
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-03 00:00:00 +09:00
17
+ date: 2011-01-05 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency