kyototycoon 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/kyototycoon.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kyototycoon}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["uu59"]
@@ -1,7 +1,10 @@
1
1
  # -- coding: utf-8
2
2
 
3
3
  require "rubygems"
4
- require "msgpack"
4
+ begin
5
+ require "msgpack"
6
+ rescue LoadError
7
+ end
5
8
 
6
9
  class KyotoTycoon
7
10
  module Serializer
@@ -3,10 +3,6 @@
3
3
  class KyotoTycoon
4
4
  module Serializer
5
5
  def self.get(adaptor)
6
- dir = "#{File.dirname(__FILE__)}/serializer"
7
- if File.exists?(File.join(dir, "#{adaptor}.rb"))
8
- require "#{dir}/#{adaptor}.rb"
9
- end
10
6
  const_get(adaptor.to_s.capitalize)
11
7
  end
12
8
  end
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
- def initialize(host='0.0.0.0', port=1978)
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
- @tsvrpc ||= begin
235
+ tsvrpc ||= begin
206
236
  host, port = *@servers.first
207
237
  Tsvrpc.new(host, port)
208
238
  end
209
- res = @tsvrpc.request(path, params, @agent, @colenc)
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', 1978]
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - uu59