kyototycoon 0.1.1 → 0.1.2
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/kyototycoon.gemspec +1 -1
- data/lib/kyototycoon/serializer/msgpack.rb +4 -1
- data/lib/kyototycoon/serializer.rb +0 -4
- data/lib/kyototycoon.rb +33 -3
- data/spec/helper.rb +36 -3
- metadata +2 -2
data/kyototycoon.gemspec
CHANGED
data/lib/kyototycoon.rb
CHANGED
@@ -7,14 +7,38 @@ require "base64"
|
|
7
7
|
require "net/http"
|
8
8
|
require "kyototycoon/serializer.rb"
|
9
9
|
require "kyototycoon/serializer/default.rb"
|
10
|
+
require "kyototycoon/serializer/msgpack.rb"
|
10
11
|
require "kyototycoon/tsvrpc.rb"
|
11
12
|
require "kyototycoon/tsvrpc/skinny.rb"
|
12
13
|
require "kyototycoon/tsvrpc/nethttp.rb"
|
13
14
|
|
14
15
|
class KyotoTycoon
|
15
16
|
attr_accessor :colenc, :connect_timeout, :servers
|
17
|
+
attr_reader :serializer, :logger, :db
|
16
18
|
|
17
|
-
|
19
|
+
DEFAULT_HOST = '0.0.0.0'
|
20
|
+
DEFAULT_PORT = 1978
|
21
|
+
|
22
|
+
def self.configure(name, host=DEFAULT_HOST, port=DEFAULT_PORT, &block)
|
23
|
+
@configure ||= {}
|
24
|
+
if @configure[name]
|
25
|
+
raise "'#{name}' is registered"
|
26
|
+
end
|
27
|
+
@configure[name] = lambda{
|
28
|
+
kt = KyotoTycoon.new(host, port)
|
29
|
+
block.call(kt)
|
30
|
+
kt
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.create(name)
|
35
|
+
if @configure[name].nil?
|
36
|
+
raise "undefined configure: '#{name}'"
|
37
|
+
end
|
38
|
+
@configure[name].call
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(host=DEFAULT_HOST, port=DEFAULT_PORT)
|
18
42
|
@servers = [[host, port]]
|
19
43
|
@serializer = KyotoTycoon::Serializer::Default
|
20
44
|
@logger = Logger.new(nil)
|
@@ -88,6 +112,12 @@ class KyotoTycoon
|
|
88
112
|
res = request('/rpc/increment', {:key => key, :num => num, :xt => xt})
|
89
113
|
Tsvrpc.parse(res[:body])['num'].to_i
|
90
114
|
end
|
115
|
+
alias_method :incr, :increment
|
116
|
+
|
117
|
+
def decrement(key, num=1, xt=nil)
|
118
|
+
increment(key, num * -1, xt)
|
119
|
+
end
|
120
|
+
alias_method :decr, :decrement
|
91
121
|
|
92
122
|
def increment_double(key, num, xt=nil)
|
93
123
|
res = request('/rpc/increment_double', {:key => key, :num => num, :xt => xt})
|
@@ -202,11 +232,11 @@ class KyotoTycoon
|
|
202
232
|
@logger.crit(msg)
|
203
233
|
raise msg
|
204
234
|
end
|
205
|
-
|
235
|
+
tsvrpc ||= begin
|
206
236
|
host, port = *@servers.first
|
207
237
|
Tsvrpc.new(host, port)
|
208
238
|
end
|
209
|
-
res =
|
239
|
+
res = tsvrpc.request(path, params, @agent, @colenc)
|
210
240
|
@logger.info("#{path}: #{res[:status]} with query parameters #{params.inspect}")
|
211
241
|
res
|
212
242
|
end
|
data/spec/helper.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
1
|
# -- coding: utf-8
|
2
2
|
|
3
|
+
=begin
|
4
|
+
|
5
|
+
!!!!!!!!!!!!!
|
6
|
+
!! CAUTION !!
|
7
|
+
!!!!!!!!!!!!!
|
8
|
+
|
9
|
+
This script access http://0.0.0.0:19999/ and destroy all records.
|
10
|
+
Be carefully for run, and run `ktserver -port 19999 '*'` before testing.
|
11
|
+
|
12
|
+
=end
|
13
|
+
|
3
14
|
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")
|
4
15
|
require "rubygems"
|
5
16
|
require "kyototycoon.rb"
|
6
17
|
|
7
18
|
describe do
|
8
19
|
before(:all) do
|
9
|
-
@kt = KyotoTycoon.new
|
20
|
+
@kt = KyotoTycoon.new('0.0.0.0', 19999)
|
10
21
|
@kt.serializer=:default # or :msgpack
|
11
|
-
@kt.db='*' # in memory
|
12
22
|
@kt.logger=nil
|
13
23
|
end
|
14
24
|
|
@@ -20,7 +30,7 @@ describe do
|
|
20
30
|
kt = KyotoTycoon.new('www.example.com', 11111)
|
21
31
|
kt.connect_timeout = 0.1
|
22
32
|
kt.servers << ['example.net', 1978]
|
23
|
-
kt.servers << ['0.0.0.0',
|
33
|
+
kt.servers << ['0.0.0.0', 19999]
|
24
34
|
kt['foo'] = 'bar'
|
25
35
|
kt[:foo].should == 'bar'
|
26
36
|
end
|
@@ -98,6 +108,9 @@ describe do
|
|
98
108
|
@kt.increment('foo').should == 3
|
99
109
|
@kt.increment('foo', 10).should == 13
|
100
110
|
@kt.increment('foo', -10).should == 3
|
111
|
+
@kt.decrement('foo', 5).should == -2
|
112
|
+
@kt.incr('foo', 5).should == 3
|
113
|
+
@kt.decr('foo', 5).should == -2
|
101
114
|
end
|
102
115
|
|
103
116
|
it 'should provide status/report' do
|
@@ -119,4 +132,24 @@ describe do
|
|
119
132
|
@kt.match_regex(/^12/).sort.should == %w!123 124 125!.sort
|
120
133
|
@kt.match_regex(/^9+$/).sort.should == %w!999 9999!.sort
|
121
134
|
end
|
135
|
+
|
136
|
+
it 'should configure/create method works' do
|
137
|
+
logger = Logger.new(STDOUT)
|
138
|
+
KyotoTycoon.configure(:test) do |kt|
|
139
|
+
kt.logger = logger
|
140
|
+
kt.serializer = :msgpack
|
141
|
+
kt.db = 'foobar'
|
142
|
+
end
|
143
|
+
KyotoTycoon.configure(:test2, 'host', 1999) do |kt|
|
144
|
+
kt.logger = logger
|
145
|
+
kt.serializer = :msgpack
|
146
|
+
kt.db = 'foobar'
|
147
|
+
end
|
148
|
+
%w!test test2!.each{|name|
|
149
|
+
kt = KyotoTycoon.create(name.to_sym)
|
150
|
+
kt.logger.should == logger
|
151
|
+
kt.serializer.should == KyotoTycoon::Serializer::Msgpack
|
152
|
+
kt.db.should == 'foobar'
|
153
|
+
}
|
154
|
+
end
|
122
155
|
end
|