kyototycoon 0.5.0 → 0.5.1

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.
data/Changes.md ADDED
@@ -0,0 +1,25 @@
1
+ ## v0.5.1
2
+
3
+ * changing server dicision logic
4
+ * added KT::Stream.run
5
+
6
+ ## v0.5.0
7
+
8
+ * Always Keep-Alive connection
9
+ * remove Tsvrpc::Nethttp, and KT#agent=
10
+ * default KT#colenc = :B (Base64)
11
+ * modified benchmark/*.rb
12
+
13
+ ## v0.1.2
14
+
15
+ * added KT#configure, KT#create
16
+ * added KT#incr, KT#decr, KT#decrement
17
+ * rspec connect to localhost:19999 for safety
18
+
19
+ ## v0.1.1
20
+
21
+ * fixed always xt=0
22
+
23
+ ## v0.1.0
24
+
25
+ * first release
data/README.markdown CHANGED
@@ -109,6 +109,25 @@ KyotoTycoon client for Ruby.
109
109
 
110
110
  - msgpack(optional)
111
111
 
112
+ # Other case for `ktremotemgr slave | ...`
113
+
114
+ $ cat foo.rb
115
+ require "rubygems"
116
+ require "kyototycoon"
117
+
118
+ KyotoTycoon::Stream.run($stdin) do |line|
119
+ case line.cmd
120
+ when 'clear'
121
+ puts "all record cleared!"
122
+ when 'set'
123
+ puts "#{line.key} get #{line.value} value"
124
+ when 'remove'
125
+ puts "#{line.key} is removed at #{line.time.strftime('%Y-%m-%d %H:%M:%S')}"
126
+ end
127
+ end
128
+
129
+ $ ktremotemgr slave -uw | ruby foo.rb
130
+
112
131
  # Trap
113
132
 
114
133
  KyotoTycoon is based on HTTP so all variable types are become String.
data/kyototycoon.gemspec CHANGED
@@ -5,36 +5,39 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kyototycoon}
8
- s.version = "0.5.0"
8
+ s.version = "0.5.1"
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{2010-12-22}
12
+ s.date = %q{2010-12-23}
13
13
  s.description = %q{KyotoTycoon client for Ruby}
14
14
  s.email = %q{a@tt25.org}
15
15
  s.extra_rdoc_files = [
16
16
  "README.markdown"
17
17
  ]
18
18
  s.files = [
19
+ "Changes.md",
19
20
  "Gemfile",
20
21
  "Gemfile.lock",
21
22
  "MIT-LICENSE",
22
23
  "README.markdown",
23
24
  "Rakefile",
24
25
  "VERSION",
25
- "benchmark/helper.rb",
26
26
  "benchmark/bulk.rb",
27
27
  "benchmark/bulk_bigdata.rb",
28
28
  "benchmark/getset.rb",
29
29
  "benchmark/getset_while_1sec.rb",
30
+ "benchmark/helper.rb",
30
31
  "kyototycoon.gemspec",
31
32
  "lib/kyototycoon.rb",
32
33
  "lib/kyototycoon/serializer.rb",
33
34
  "lib/kyototycoon/serializer/default.rb",
34
35
  "lib/kyototycoon/serializer/msgpack.rb",
36
+ "lib/kyototycoon/stream.rb",
35
37
  "lib/kyototycoon/tsvrpc.rb",
36
38
  "lib/kyototycoon/tsvrpc/skinny.rb",
37
- "spec/helper.rb"
39
+ "spec/helper.rb",
40
+ "spec/ktslave.txt"
38
41
  ]
39
42
  s.homepage = %q{http://github.com/uu59/kyototycoon-ruby}
40
43
  s.licenses = ["MIT"]
@@ -0,0 +1,74 @@
1
+ # -- coding: utf-8
2
+
3
+ =begin
4
+ $ cat foo.rb
5
+ require "rubygems"
6
+ require "kyototycoon"
7
+
8
+ KyotoTycoon::Stream.run($stdin) do |line|
9
+ ... do some stuff ..
10
+ end
11
+
12
+ $ ktremotemgr slave -uw | ruby foo.rb
13
+ =end
14
+
15
+ class KyotoTycoon
16
+ module Stream
17
+ def self.run(io=$stdin, &block)
18
+ io.each_line{|line|
19
+ line = Line.new(*line.strip.split("\t", 5))
20
+ block.call(line)
21
+ }
22
+ end
23
+
24
+ class Line < Struct.new(:ts, :sid, :db, :cmd, :raw_args)
25
+ def args
26
+ @args ||= begin
27
+ return [] if raw_args.nil?
28
+ k,v = *raw_args.split("\t").map{|v| v.unpack('m').first}
29
+ return [k] unless v
30
+ xt = 0
31
+ v.unpack('C5').each{|num|
32
+ xt = (xt << 8) + num
33
+ }
34
+ v = v[5, v.length]
35
+ [k, v, xt]
36
+ end
37
+ end
38
+
39
+ def key
40
+ @key ||= begin
41
+ args.first || nil
42
+ end
43
+ end
44
+
45
+ def value
46
+ @value ||= begin
47
+ args[1] || nil
48
+ end
49
+ end
50
+
51
+ def xt
52
+ @xt ||= begin
53
+ args[2] || nil
54
+ end
55
+ end
56
+
57
+ def xt_time
58
+ @xt_time ||= begin
59
+ if args[2]
60
+ # if not set xt:
61
+ # Time.at(1099511627775) # => 36812-02-20 09:36:15 +0900
62
+ Time.at(args[2].to_i)
63
+ else
64
+ Time.at(0)
65
+ end
66
+ end
67
+ end
68
+
69
+ def time
70
+ @time ||= Time.at(*[ts[0,10], ts[10, ts.length]].map(&:to_i))
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/kyototycoon.rb CHANGED
@@ -10,9 +10,10 @@ require "kyototycoon/serializer/default.rb"
10
10
  require "kyototycoon/serializer/msgpack.rb"
11
11
  require "kyototycoon/tsvrpc.rb"
12
12
  require "kyototycoon/tsvrpc/skinny.rb"
13
+ require "kyototycoon/stream.rb"
13
14
 
14
15
  class KyotoTycoon
15
- VERSION = '0.5.0'
16
+ VERSION = '0.5.1'
16
17
 
17
18
  attr_accessor :colenc, :connect_timeout, :servers
18
19
  attr_reader :serializer, :logger, :db
@@ -41,6 +42,7 @@ class KyotoTycoon
41
42
 
42
43
  def initialize(host=DEFAULT_HOST, port=DEFAULT_PORT)
43
44
  @servers = [[host, port]]
45
+ @checked_servers = nil
44
46
  @serializer = KyotoTycoon::Serializer::Default
45
47
  @logger = Logger.new(nil)
46
48
  @colenc = :B
@@ -266,26 +268,23 @@ class KyotoTycoon
266
268
  end
267
269
 
268
270
  def choice_server
269
- current = @servers.first
270
- if @servers.length > 1
271
- @servers.each{|s|
272
- host,port = *s
273
- if ping(host, port)
274
- @servers = [[host, port]]
275
- break
276
- end
277
- }
271
+ if @checked_servers
272
+ return @checked_servers
278
273
  end
279
- if @servers.length == 0
274
+
275
+ @servers.each{|s|
276
+ host,port = *s
277
+ if ping(host, port)
278
+ @checked_servers = [host, port]
279
+ break
280
+ end
281
+ }
282
+ if @checked_servers.nil?
280
283
  msg = "alived server not exists"
281
284
  @logger.crit(msg)
282
285
  raise msg
283
286
  end
284
- result = @servers.first
285
- if current != result
286
- @client = nil
287
- end
288
- result
287
+ @checked_servers
289
288
  end
290
289
 
291
290
  end
data/spec/helper.rb CHANGED
@@ -14,6 +14,7 @@ Be carefully for run, and run `ktserver -port 19999 '*'` before testing.
14
14
  $LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")
15
15
  require "rubygems"
16
16
  require "kyototycoon.rb"
17
+ require "kyototycoon/stream.rb"
17
18
 
18
19
  describe do
19
20
  before(:all) do
@@ -155,4 +156,35 @@ describe do
155
156
  kt.db.should == 'foobar'
156
157
  }
157
158
  end
159
+
160
+ it 'should handle `ktremotemgr slave`' do
161
+ io = File.open("#{File.dirname(__FILE__)}/ktslave.txt", "r")
162
+ current = 0
163
+ KyotoTycoon::Stream.run(io){|line|
164
+ case current
165
+ when 0 # clear command
166
+ line.cmd.should == 'clear'
167
+ line.xt_time.should == Time.at(0)
168
+ line.value.should be_nil
169
+ line.key.should be_nil
170
+ line.value.should be_nil
171
+ when 1 # set foo bar
172
+ line.cmd.should == 'set'
173
+ line.xt_time.should > Time.now
174
+ line.key.should == 'foo'
175
+ line.value.should == 'bar'
176
+ when 2 # set fooxt bar with xt(2010-12-23 22:09:49 +0900)
177
+ line.cmd.should == 'set'
178
+ line.key.should == 'fooxt'
179
+ line.value.should == 'bar'
180
+ line.xt_time.should > Time.at(1234567890)
181
+ line.xt_time.should < Time.at(1334567890)
182
+ when 3 # remove foo
183
+ line.cmd.should == 'remove'
184
+ line.key.should == 'foo'
185
+ line.value.should be_nil
186
+ end
187
+ current += 1
188
+ }
189
+ end
158
190
  end
data/spec/ktslave.txt ADDED
@@ -0,0 +1,4 @@
1
+ 1293108572805000000 1 0 clear
2
+ 1293108575130000000 1 0 set Zm9v //////9iYXI=
3
+ 1293108589068000000 1 0 set Zm9veHQ= AE0TSh1iYXI=
4
+ 1293108597399000000 1 0 remove Zm9v
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 0
9
- version: 0.5.0
8
+ - 1
9
+ version: 0.5.1
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: 2010-12-22 00:00:00 +09:00
17
+ date: 2010-12-23 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -97,25 +97,28 @@ extensions: []
97
97
  extra_rdoc_files:
98
98
  - README.markdown
99
99
  files:
100
+ - Changes.md
100
101
  - Gemfile
101
102
  - Gemfile.lock
102
103
  - MIT-LICENSE
103
104
  - README.markdown
104
105
  - Rakefile
105
106
  - VERSION
106
- - benchmark/helper.rb
107
107
  - benchmark/bulk.rb
108
108
  - benchmark/bulk_bigdata.rb
109
109
  - benchmark/getset.rb
110
110
  - benchmark/getset_while_1sec.rb
111
+ - benchmark/helper.rb
111
112
  - kyototycoon.gemspec
112
113
  - lib/kyototycoon.rb
113
114
  - lib/kyototycoon/serializer.rb
114
115
  - lib/kyototycoon/serializer/default.rb
115
116
  - lib/kyototycoon/serializer/msgpack.rb
117
+ - lib/kyototycoon/stream.rb
116
118
  - lib/kyototycoon/tsvrpc.rb
117
119
  - lib/kyototycoon/tsvrpc/skinny.rb
118
120
  - spec/helper.rb
121
+ - spec/ktslave.txt
119
122
  has_rdoc: true
120
123
  homepage: http://github.com/uu59/kyototycoon-ruby
121
124
  licenses: