kyototycoon-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kyototycoon-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 imasho
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Kyototycoon::Client
2
+
3
+ KyotoTycoon ruby client with TCP connection and binary protocol.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'kyototycoon-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install kyototycoon-client
18
+
19
+ ## Usage
20
+
21
+ ### Create instance
22
+
23
+ require "kyototycoon/client"
24
+
25
+ client = Kyototycoon::Client.new(host, port)
26
+ client.open
27
+
28
+ # kyototycoon operations here
29
+
30
+ client.close
31
+
32
+ ### get/ret/remove operations
33
+
34
+ Simple case:
35
+
36
+ > client.set("key", "value") # set single key-value
37
+ => true
38
+ > client.set_bulk({"key1" => "value1", "key2" => "value2"}) # set multiple key-value
39
+ => 2 # number of success key-value set
40
+ > client.get("key") # get single key
41
+ => "value" # return value or nil
42
+ > client.get_bulk(["key1", "key2"]) # set multiple keys
43
+ => {"key1" => "value1", "key2" => "value2"} # return key-value map
44
+
45
+ Key expiration:
46
+
47
+ > client.set("key", "value", 3600) # third parameter is seconds for key expiration
48
+ => true
49
+ > client.set_bulk({"key1" => ["value1", 3600], "key2" => "value2"}) # key1 will expire after 3600 seconds, key2 never expire
50
+ => 2
51
+
52
+ Remove key:
53
+
54
+ > client.remove("key") # remove signle key
55
+ => true
56
+ > client.remove_bulk(["key1", "key2"]) # remove multiple keys
57
+ => 2 # number of keys removed
58
+
59
+ ### Play LUA script
60
+
61
+ KyotoTyccon supports server-side LUA scripting extention.
62
+
63
+ Client can call LUA method with method name and key-value set arguments.
64
+
65
+ Client receives response as key-value set format.
66
+
67
+ > client.script("key", "value") # single key-value argument
68
+ => {"keyA" => "valueA", "keyB" => "valueB", ...} # receive key-value set
69
+ > client.script({"key1" => "value1", "key2" => "value2"}) # multiple key-value argument
70
+ => {"keyX" => "valueX", "keyY" => "valueY", ... }
71
+
72
+ Number of key sent and number of key received are depend on LUA function definition.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'bundler/gem_helper'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.new(Dir.pwd).instance_eval do
5
+ desc "Build #{name}-#{version}.gem into the pkg directory"
6
+ task 'build' do
7
+ build_gem
8
+ end
9
+
10
+ desc "Build and install #{name}-#{version}.gem into system gems"
11
+ task 'install' do
12
+ install_gem
13
+ end
14
+
15
+ desc "benchmark module"
16
+ task 'benchmark' do
17
+ raise "host and port reauired: rake benchmark HOST=[HOST] PORT=[PORT]" if !ENV["HOST"] || !ENV["PORT"]
18
+
19
+ if ENV["COUNT"].to_i > 0
20
+ system "ruby test/benchmark.rb #{ENV["HOST"]} #{ENV["PORT"]} #{ENV["COUNT"]}"
21
+ else
22
+ system "ruby test/benchmark.rb #{ENV["HOST"]} #{ENV["PORT"]}"
23
+ end
24
+ end
25
+ end
26
+
27
+
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ end
32
+
33
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kyototycoon/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kyototycoon-client"
8
+ spec.version = Kyototycoon::Client::VERSION
9
+ spec.authors = ["imasho"]
10
+ spec.email = ["imasho@gmail.com"]
11
+ spec.description = %q{KyotoTycoon binary access module, supports get(bulk), set(bulk), remove(bulk), and play LUA script.}
12
+ spec.summary = %q{KyotoTycoon binary access module}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,89 @@
1
+ require "kyototycoon/client/version"
2
+ require "kyototycoon/client/record"
3
+ require "kyototycoon/client/connection"
4
+
5
+ module Kyototycoon
6
+ class Client
7
+ attr_reader :host, :port, :timeout_ms, :connection
8
+
9
+ def initialize(host, port, timeout_ms = 0)
10
+ @host = host
11
+ @port = port
12
+ @timeout_ms = timeout_ms
13
+ @connection = Connection.new(@host, @port, @timeout_ms)
14
+ end
15
+
16
+ def open
17
+ @connection.open
18
+ end
19
+
20
+ def close
21
+ raise "connection doesn't open" unless @connection && @connection.is_open
22
+ @connection.close
23
+ end
24
+
25
+ def set(key, value, expire=Record::MAX_EXPIRE_SECONDS, dbid=0)
26
+ set_bulk({key => [value, expire]}, dbid) == 1 ? true : false
27
+ end
28
+
29
+ def get(key, dbid=0)
30
+ get_bulk([key], dbid)[key]
31
+ end
32
+
33
+ def remove(key)
34
+ remove_bulk([key]) == 1 ? true : false
35
+ end
36
+
37
+ def script(method, key, value)
38
+ script_bulk(method, { key => value } )
39
+ end
40
+
41
+ def set_bulk(keyvalues, dbid = 0)
42
+ raise "connection closed" unless @connection.is_open
43
+
44
+ records = []
45
+ keyvalues.each do |k, v|
46
+ if v.is_a?(Array)
47
+ records.push(Record.new(k, v[0], v[1].to_i, dbid))
48
+ else
49
+ records.push(Record.new(k, v, Record::MAX_EXPIRE_SECONDS, dbid))
50
+ end
51
+ end
52
+ @connection.set(records)
53
+ end
54
+
55
+ def get_bulk(keys, dbid=0)
56
+ raise "connection closed" unless @connection.is_open
57
+
58
+ records = []
59
+ keys.each do |k|
60
+ records.push(Record.new(k, nil, nil, dbid))
61
+ end
62
+ results = @connection.get(records)
63
+ return {} if results == nil
64
+ results.inject({}) {|map, rec| map[rec.key] = rec.value; map;}
65
+ end
66
+
67
+ def remove_bulk(keys)
68
+ raise "connection closed" unless @connection.is_open
69
+
70
+ records = []
71
+ keys.each do |k|
72
+ records.push(Record.new(k, nil))
73
+ end
74
+ @connection.remove(records)
75
+ end
76
+
77
+ def script_bulk(method, keyvalues)
78
+ raise "connection closed" unless @connection.is_open
79
+
80
+ records = []
81
+ keyvalues.each do |k, v|
82
+ records.push(Record.new(k, v))
83
+ end
84
+ results = @connection.script(records)
85
+ return {} if results == nil
86
+ results.inject({}) {|map, rec| map[rec.key] = rec.value; map;}
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,124 @@
1
+ require "socket"
2
+ require "kyototycoon/client/magic"
3
+ require "kyototycoon/client/flag"
4
+
5
+ module Kyototycoon
6
+ class Connection
7
+ attr_reader :host, :port, :timeout_ms, :socket, :is_open
8
+
9
+ def initialize(host, port, timeout_ms)
10
+ @host = host
11
+ @port = port
12
+ @timeout_ms = timeout_ms
13
+ @is_open = false
14
+ end
15
+
16
+ def open
17
+ return if @is_open
18
+ @socket = TCPSocket.open(host, port)
19
+ @is_open = true
20
+ end
21
+
22
+ def close
23
+ return unless @is_open
24
+ @socket.close
25
+ @is_open = false
26
+ end
27
+
28
+ def set(records)
29
+ header_entries = [Magic::SET_BULK, Flag::RESERVED, records.length]
30
+ request = header_entries.pack("CNN")
31
+
32
+ records.each do |r|
33
+ request << [r.db_id, r.key.length, r.value.length, r.expire.to_i >> 32, r.expire.to_i & 0x00000000FFFFFFFF].pack("nNNNN") <<
34
+ r.key << r.value
35
+ end
36
+
37
+ @socket.write(request)
38
+ response = @socket.read(5)
39
+ raise "no response" unless response
40
+
41
+ magic, count = response.unpack("CN")
42
+ raise "invalid protocol header" unless magic == Magic::SET_BULK
43
+
44
+ count.to_i # number of registerd
45
+ end
46
+
47
+ def get(records)
48
+ header_entries = [Magic::GET_BULK, Flag::RESERVED, records.length]
49
+ request = header_entries.pack("CNN")
50
+
51
+ records.each do |r|
52
+ request << [r.db_id, r.key.length].pack("nN") << r.key
53
+ end
54
+
55
+ @socket.write(request)
56
+ res_header = @socket.read(5)
57
+ raise "no response" unless res_header
58
+
59
+ magic, count = res_header.unpack("CN")
60
+ raise "invalid protocol header" unless magic == Magic::GET_BULK
61
+
62
+ results = []
63
+ count.times do |i|
64
+ res_body = @socket.read(18)
65
+
66
+ dbid, keysize, valuesize, ext_expire, expire = res_body.unpack("nNNNN")
67
+ expire = ext_expire << 32 | expire
68
+
69
+ key = @socket.read(keysize)
70
+ value = @socket.read(valuesize)
71
+ results.push(Record.new(key, value, dbid, expire))
72
+ end
73
+
74
+ results
75
+ end
76
+
77
+ def remove(records)
78
+ header_entries = [Magic::REMOVE_BULK, Flag::RESERVED, records.length]
79
+ request = header_entries.pack("CNN")
80
+
81
+ records.each do |r|
82
+ request << [r.db_id, r.key.length].pack("nN") << r.key
83
+ end
84
+
85
+ @socket.write(request)
86
+ response = @socket.read(5)
87
+ raise "no response" unless response
88
+
89
+ magic, count = response.unpack("CN")
90
+ raise "invalid protocol header" unless magic == Magic::REMOVE_BULK
91
+
92
+ count.to_i # number of registerd
93
+ end
94
+
95
+ def script(records)
96
+ header_entries = [Magic::PLAY_SCRIPT, Flag::RESERVED, records.length]
97
+ request = header_entries.pack("CNN")
98
+
99
+ records.each do |r|
100
+ request << [r.key.length, r.value.length].pack("NN") << r.key << r.value
101
+ end
102
+
103
+ @socket.write(request)
104
+ res_header = @socket.read(5)
105
+ raise "no response" unless res_header
106
+
107
+ magic, count = res_header.unpack("CN")
108
+ raise "invalid protocol header" unless magic == Magic::PLAY_SCRIPT
109
+
110
+ results = []
111
+ count.times do |i|
112
+ res_body = @socket.read(8)
113
+ keysize, valuesize = res_body.unpack("NN")
114
+ key = @socket.read(keysize)
115
+ value = @socket.read(valuesize)
116
+ results.push(Record.new(key, value, 0, 0))
117
+ end
118
+
119
+ results
120
+ end
121
+ end
122
+ end
123
+
124
+
@@ -0,0 +1,5 @@
1
+ module Kyototycoon
2
+ module Flag
3
+ RESERVED = 0
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module Kyototycoon
2
+ module Magic
3
+ SET_BULK = 0xB8
4
+ REMOVE_BULK = 0xB9
5
+ GET_BULK = 0xBA
6
+ PLAY_SCRIPT = 0xB4
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module Kyototycoon
2
+ class Record
3
+ MAX_EXPIRE_SECONDS = 0x7FFFFFFFFFFFFFFF
4
+
5
+ attr_accessor :key, :value, :db_id, :expire
6
+
7
+ def initialize(key, value, expire=MAX_EXPIRE_SECONDS, db_id=0)
8
+ @key = key
9
+ @value = value
10
+ @expire = expire
11
+ @db_id = db_id
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Kyototycoon
2
+ class Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/test/benchmark.rb ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "rubygems"
4
+ require "kyototycoon/client"
5
+ require "benchmark"
6
+
7
+ if ARGV.length != 2 && ARGV.length != 3
8
+ print "usage: ruby benchmark.rb [HOST] [PORT]\n"
9
+ exit 1
10
+ end
11
+
12
+ client = Kyototycoon::Client.new(ARGV[0], ARGV[1])
13
+ client.open
14
+ bench_count = (ARGV[2] || 100000).to_i
15
+
16
+ records = {}
17
+ bench_count.times do |i|
18
+ records["testdata#{i}"] = "value#{i}"
19
+ end
20
+
21
+ Benchmark.bm do |b|
22
+ b.report("set_bulk: ") {
23
+ client.set_bulk(records)
24
+ }
25
+
26
+ b.report("set : ") {
27
+ bench_count.times do |i|
28
+ client.set("iteration#{i}", "value#{i}")
29
+ end
30
+ }
31
+
32
+ b.report("get_bulk: ") {
33
+ result = client.get_bulk(records.keys)
34
+ raise unless result.size == bench_count
35
+ }
36
+
37
+ b.report("get : ") {
38
+ bench_count.times do |i|
39
+ v = client.get("iteration#{i}")
40
+ raise unless v == "value#{i}"
41
+ end
42
+ }
43
+
44
+ end
45
+
46
+ client.close
47
+ exit 0
data/test/helper.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require 'test/unit'
13
+ require "kyototycoon/client"
14
+
15
+ class Test::Unit::TestCase
16
+ end
@@ -0,0 +1,72 @@
1
+ require "helper"
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ def setup
5
+ raise "host and port of KyotoTycoon test server is required: rake test HOST=xxx PORT=xxx" if (!ENV["HOST"] || !ENV["PORT"])
6
+ @client = Kyototycoon::Client.new(ENV["HOST"], ENV["PORT"].to_i)
7
+ end
8
+
9
+ def test_open
10
+ @client.open
11
+ @client.close
12
+ end
13
+
14
+
15
+ def test_clud
16
+ @client.open
17
+ assert_equal(@client.set("key1", "value1"), true)
18
+ assert_equal(@client.get("key1"), "value1")
19
+ assert_nil(@client.get("key999"))
20
+ assert_equal(@client.remove("key1"), true)
21
+ assert_nil(@client.get("key1"))
22
+ @client.close
23
+ end
24
+
25
+ def test_clud_bulk
26
+ @client.open
27
+ assert_equal( @client.set_bulk( {"key1" => "valueA", "key2" => "valueB", "key3" => "valueC"} ), 3)
28
+ assert_equal( @client.get_bulk( ["key1", "key2", "key3", "key4"] ),
29
+ {"key1" => "valueA", "key2" => "valueB", "key3" => "valueC"} )
30
+ assert_equal( @client.remove_bulk( ["key1", "key2"] ), 2 )
31
+ assert_equal( @client.get_bulk( ["key1", "key2", "key3", "key4"] ),
32
+ {"key3" => "valueC"} )
33
+ @client.close
34
+ end
35
+
36
+ def test_expire
37
+ @client.open
38
+ assert_equal(@client.set_bulk( {"key1" => ["value1", 1]} ), 1) # expire after 1 second
39
+ assert_equal(@client.set_bulk( {"key2" => "value2"} ), 1) # not expire
40
+ assert_equal(@client.set("key3", "value3"), true) # not expire
41
+ assert_equal(@client.set("key4", "value4", 1), true) # expire after 1 second
42
+
43
+ assert_equal(@client.get("key1"), "value1")
44
+ assert_equal(@client.get("key2"), "value2")
45
+ assert_equal(@client.get("key3"), "value3")
46
+ assert_equal(@client.get("key4"), "value4")
47
+ sleep 3
48
+ assert_equal(@client.get("key1"), nil)
49
+ assert_equal(@client.get("key2"), "value2")
50
+ assert_equal(@client.get("key3"), "value3")
51
+ assert_equal(@client.get("key4"), nil)
52
+ @client.close
53
+ end
54
+
55
+ def test_dbid
56
+ @client.open
57
+ assert_equal(@client.set_bulk( {"key1" => "value1"}, 0 ), 1)
58
+ assert_equal(@client.set_bulk( {"key1" => "value1"}, 1 ), 0)
59
+ assert_equal(@client.get("key1", 0), "value1")
60
+ assert_equal(@client.get("key1", 1), nil)
61
+ @client.close
62
+ end
63
+
64
+ # def test_script
65
+ # return unless @client
66
+
67
+ # @client.open
68
+ # assert_equal( @client.script("dummy", "key", "value"), {} )
69
+ # assert_equal( @client.script_bulk("dummy", {}), {} )
70
+ # @client.close
71
+ # end
72
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kyototycoon-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - imasho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: KyotoTycoon binary access module, supports get(bulk), set(bulk), remove(bulk),
47
+ and play LUA script.
48
+ email:
49
+ - imasho@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - kyototycoon-client.gemspec
60
+ - lib/kyototycoon/client.rb
61
+ - lib/kyototycoon/client/connection.rb
62
+ - lib/kyototycoon/client/flag.rb
63
+ - lib/kyototycoon/client/magic.rb
64
+ - lib/kyototycoon/client/record.rb
65
+ - lib/kyototycoon/client/version.rb
66
+ - test/benchmark.rb
67
+ - test/helper.rb
68
+ - test/kyototycoon/client/test_client.rb
69
+ homepage: ''
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.23
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: KyotoTycoon binary access module
94
+ test_files:
95
+ - test/benchmark.rb
96
+ - test/helper.rb
97
+ - test/kyototycoon/client/test_client.rb