tarantool 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "eventmachine"
4
- gem "em-synchrony"
5
3
  gem "activemodel"
6
4
 
7
5
  gem "rr"
@@ -11,4 +9,6 @@ gem "bson_ext"
11
9
 
12
10
  gem "ruby-debug19"
13
11
 
12
+ gem "em-synchrony"
13
+
14
14
  gemspec
data/Gemfile.lock CHANGED
@@ -1,10 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tarantool (0.1)
4
+ tarantool (0.1.1)
5
5
  activemodel (>= 3.1, < 4.0)
6
- em-synchrony (>= 1.0.0, < 2.0)
7
- eventmachine (>= 1.0.0.beta.4, < 2.0.0)
6
+ iproto (>= 0.1)
8
7
 
9
8
  GEM
10
9
  remote: http://rubygems.org/
@@ -24,6 +23,7 @@ GEM
24
23
  eventmachine (>= 1.0.0.beta.1)
25
24
  eventmachine (1.0.0.beta.4)
26
25
  i18n (0.6.0)
26
+ iproto (0.1)
27
27
  linecache19 (0.5.12)
28
28
  ruby_core_source (>= 0.1.4)
29
29
  multi_json (1.0.3)
@@ -48,7 +48,6 @@ DEPENDENCIES
48
48
  bson
49
49
  bson_ext
50
50
  em-synchrony
51
- eventmachine
52
51
  rr
53
52
  ruby-debug19
54
53
  tarantool!
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # About
2
2
 
3
- Its asynchronyous EventMachine ruby client for [Tarantool Key-Value Storage](http://github.com/mailru/tarantool).
3
+ Its ruby client for [Tarantool Key-Value Storage](http://github.com/mailru/tarantool).
4
4
 
5
5
  # Install
6
6
 
@@ -8,48 +8,46 @@ Its asynchronyous EventMachine ruby client for [Tarantool Key-Value Storage](htt
8
8
  gem install tarantool
9
9
  ```
10
10
 
11
+ # Usage
12
+
11
13
  ```ruby
12
14
  require 'tarantool'
13
- # or
14
- require 'tarantool/synchrony'
15
- # or
16
- require 'tarantool/record'
17
15
  ```
18
16
 
19
- # Usage
20
-
21
17
  To be able to send requests to the server, you must
22
- configure and establish a client connection:
18
+ initialize Tarantool and Tarantool space:
23
19
 
24
20
  ```ruby
25
- Tarantool.configure host: 'locahost', port: 33013, space_no: 0
21
+ DB = Tarantool.new host: 'locahost', port: 33013
22
+ space = DB.space 0
26
23
  ```
27
24
 
28
- The driver internals can work in two modes: EM deferrables and EM-Synchrony.
25
+ The driver internals can work in two modes: block via TCPSocket and non block via EventMachine and fibers. By default it uses block mode.
29
26
 
30
- EM deferrables:
31
27
 
32
28
  ```ruby
33
- req = Tarantool.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
34
- req.callback do
35
- req = Tarantool.select 'prepor'
36
- req.callback do |res|
37
- puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
38
- end
39
- end
40
- req.errback do |err|
41
- puts "Error while insert: #{err}"
42
- end
29
+ space.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
30
+ res = space.select 'prepor'
31
+ puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
32
+ space.delete 'prepor'
43
33
  ```
44
34
 
45
- 'Synchrony' mode:
35
+ **Notice** `Tarantool` instances (connections actually) are not threadsafe. So, you should create `Tarantool` instance per thread.
46
36
 
47
- ```ruby
48
- require 'tarantool/synchrony'
37
+ To use EventMachine pass type: em in options:
49
38
 
50
- Tarantool.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
51
- res = Tarantool.select 'prepor'
52
- puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
39
+ ```ruby
40
+ require 'em-synchrony'
41
+ DB = Tarantool.new host: 'locahost', port: 33013
42
+ space = DB.space 0
43
+ EM.synchrony do
44
+ Fiber.new do
45
+ space.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
46
+ res = space.select 'prepor'
47
+ puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
48
+ space.delete 'prepor'
49
+ end.resume
50
+ end
53
51
  ```
54
52
 
55
53
  The driver itself provides ActiveModel API: Callbacks, Validations, Serialization, Dirty.
data/Rakefile CHANGED
@@ -78,7 +78,7 @@ task :release => :build do
78
78
  puts "You must be on the master branch to release!"
79
79
  exit!
80
80
  end
81
- sh "git commit --allow-empty -a -m 'Release #{version}'"
81
+ sh "git commit --allow-empty -m 'Release #{version}'"
82
82
  sh "git tag v#{version}"
83
83
  sh "git push origin master"
84
84
  sh "git push origin v#{version}"
@@ -4,18 +4,15 @@ Bundler.setup
4
4
 
5
5
  require 'tarantool'
6
6
 
7
- EM.run do
8
- Tarantool.configure host: 'localhost', port: 33013, space_no: 0
9
- req = Tarantool.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
10
- req.callback do
11
- req = Tarantool.select 'prepor'
12
- req.callback do |res|
13
- puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
14
- EM.stop
15
- end
16
- end
17
- req.errback do |err|
18
- puts "Error while insert: #{err}"
7
+ require 'em-synchrony'
8
+ DB = Tarantool.new host: 'localhost', port: 33013
9
+ space = DB.space 0
10
+ EM.synchrony do
11
+ Fiber.new do
12
+ space.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
13
+ res = space.select 'prepor'
14
+ puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
15
+ space.delete 'prepor'
19
16
  EM.stop
20
- end
17
+ end.resume
21
18
  end
data/examples/record.rb CHANGED
@@ -4,7 +4,16 @@ Bundler.setup
4
4
 
5
5
  require 'tarantool/record'
6
6
  require 'tarantool/serializers/bson'
7
- class User < Tarantool::Record
7
+
8
+ DB = Tarantool.new host: 'localhost', port: 33013
9
+
10
+ class MyTarantool < Tarantool::Record
11
+ set_tarantool DB
12
+ end
13
+
14
+ class User < MyTarantool
15
+ set_space_no 0
16
+
8
17
  field :login, :string
9
18
  field :name, :string
10
19
  field :email, :string
@@ -19,38 +28,34 @@ class User < Tarantool::Record
19
28
  end
20
29
  end
21
30
 
22
- EM.synchrony do
23
- Tarantool.configure host: 'localhost', port: 33013, space_no: 0
24
- # Now attribute positions are not important.
25
- User.create login: 'prepor', email: 'ceo@prepor.ru', name: 'Andrew'
26
- User.create login: 'ruden', name: 'Andrew', email: 'rudenkoco@gmail.com'
27
-
28
- # find by primary key login
29
- User.find 'prepor'
30
- # first 2 users with name Andrew
31
- User.where(name: 'Andrew').limit(2).all
32
- # second user with name Andrew
33
- User.where(name: 'Andrew').offset(1).limit(1).all
34
- # user with name Andrew and email ceo@prepor.ru
35
- User.where(name: 'Andrew', email: 'ceo@prepor.ru').first
36
- # raise exception, becouse we can't select from not first part of index
37
- begin
38
- User.where(email: 'ceo@prepor.ru')
39
- rescue Tarantool::ArgumentError => e
40
- end
41
- # increment field apples_count by one. Its atomic operation vie native Tarantool interface
42
- User.find('prepor').increment :apples_count
43
-
44
- # update only dirty attributes
45
- user = User.find('prepor')
46
- user.name = "Petr"
47
- user.save
48
-
49
- # field serialization to bson
50
- user.info = { 'bio' => "hi!", 'age' => 23, 'hobbies' => ['mufa', 'tuka'] }
51
- user.save
52
- User.find('prepor').info['bio'] # => 'hi!'
53
- user.destroy
54
- puts "Ok!"
55
- EM.stop
56
- end
31
+ # Now attribute positions are not important.
32
+ User.create login: 'prepor', email: 'ceo@prepor.ru', name: 'Andrew'
33
+ User.create login: 'ruden', name: 'Andrew', email: 'rudenkoco@gmail.com'
34
+
35
+ # find by primary key login
36
+ User.find 'prepor'
37
+ # first 2 users with name Andrew
38
+ User.where(name: 'Andrew').limit(2).all
39
+ # second user with name Andrew
40
+ User.where(name: 'Andrew').offset(1).limit(1).all
41
+ # user with name Andrew and email ceo@prepor.ru
42
+ User.where(name: 'Andrew', email: 'ceo@prepor.ru').first
43
+ # raise exception, becouse we can't select from not first part of index
44
+ begin
45
+ User.where(email: 'ceo@prepor.ru')
46
+ rescue Tarantool::ArgumentError => e
47
+ end
48
+ # increment field apples_count by one. Its atomic operation vie native Tarantool interface
49
+ User.find('prepor').increment :apples_count
50
+
51
+ # update only dirty attributes
52
+ user = User.find('prepor')
53
+ user.name = "Petr"
54
+ user.save
55
+
56
+ # field serialization to bson
57
+ user.info = { 'bio' => "hi!", 'age' => 23, 'hobbies' => ['mufa', 'tuka'] }
58
+ user.save
59
+ User.find('prepor').info['bio'] # => 'hi!'
60
+ user.destroy
61
+ puts "Ok!"
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
3
+ Bundler.setup
4
+
5
+ require 'tarantool'
6
+
7
+ DB = Tarantool.new host: 'localhost', port: 33013
8
+ space = DB.space 0
9
+
10
+ space.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
11
+ res = space.select 'prepor'
12
+ puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
13
+ space.delete 'prepor'
data/lib/tarantool.rb CHANGED
@@ -1,50 +1,36 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'eventmachine'
3
- require 'em-synchrony'
2
+ require 'iproto'
4
3
 
5
- module Tarantool
6
- VERSION = '0.1.1'
7
- extend self
4
+ class Tarantool
5
+ VERSION = '0.2'
6
+
8
7
  require 'tarantool/space'
9
- require 'tarantool/connection'
10
8
  require 'tarantool/requests'
11
9
  require 'tarantool/response'
12
10
  require 'tarantool/exceptions'
13
11
  require 'tarantool/serializers'
14
12
 
15
- def singleton_space
16
- @singleton_space ||= space
17
- end
18
-
19
- def connection
20
- @connection ||= begin
21
- raise "Tarantool.configure before connect" unless @config
22
- EM.connect @config[:host], @config[:port], Tarantool::Connection
23
- end
13
+ attr_reader :config
14
+ def initialize(config = {})
15
+ @config = config
24
16
  end
25
17
 
26
- def reset_connection
27
- @connection = nil
28
- @singleton_space = nil
18
+ def configure(config)
19
+ @config.merge! config
29
20
  end
30
21
 
31
- def space(no = nil)
32
- Space.new connection, no || @config[:space_no]
22
+ def connection(c = config)
23
+ @connection ||= begin
24
+ raise "Tarantool.configure before connect" unless c
25
+ IProto.get_connection c[:host], c[:port], c[:type] || :block
26
+ end
33
27
  end
34
28
 
35
- def configure(config = {})
36
- EM.add_shutdown_hook { reset_connection }
37
- @config = config
29
+ def space(no, conn = connection)
30
+ Space.new conn, no
38
31
  end
39
32
 
40
- def hexdump(string)
33
+ def self.hexdump(string)
41
34
  string.unpack('C*').map{ |c| "%02x" % c }.join(' ')
42
35
  end
43
-
44
- [:select, :update, :insert, :delete, :call, :ping].each do |v|
45
- define_method v do |*params|
46
- singleton_space.send v, *params
47
- end
48
- end
49
-
50
36
  end
@@ -1,11 +1,9 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  class TarantoolError < StandardError; end
3
3
  class UndefinedRequestType < TarantoolError; end
4
- class CouldNotConnect < TarantoolError; end
5
4
  class BadReturnCode < TarantoolError; end
6
5
  class StringTooLong < TarantoolError; end
7
6
  class ArgumentError < TarantoolError; end
8
- class UnexpectedResponse < TarantoolError; end
9
7
  class UndefinedSpace < TarantoolError; end
10
8
  class ValueError < TarantoolError; end
11
9
  end
@@ -1,6 +1,6 @@
1
1
  require 'active_model'
2
- require 'tarantool/synchrony'
3
- module Tarantool
2
+ require 'tarantool'
3
+ class Tarantool
4
4
  class Select
5
5
  include Enumerable
6
6
  attr_reader :record
@@ -13,7 +13,7 @@ module Tarantool
13
13
  end
14
14
 
15
15
  def each(&blk)
16
- res = Tarantool.select(*@tuples, index_no: @index_no, limit: @limit, offset: @offset).tuples
16
+ res = record.space.select(*@tuples, index_no: @index_no, limit: @limit, offset: @offset).tuples
17
17
  res.each do |tuple|
18
18
  blk.call record.from_server(tuple)
19
19
  end
@@ -114,10 +114,16 @@ module Tarantool
114
114
  self.indexes = []
115
115
 
116
116
  class_attribute :space_no
117
- define_attr_method :space_no do
118
- original_space_no || 0
119
- end
117
+ class_attribute :tarantool
120
118
  class << self
119
+ def set_space_no(val)
120
+ self.space_no = val
121
+ end
122
+
123
+ def set_tarantool(val)
124
+ self.tarantool = val
125
+ end
126
+
121
127
  def field(name, type, params = {})
122
128
  define_attribute_method name
123
129
  self.fields = fields.merge name => { type: type, field_no: fields.size, params: params }
@@ -173,7 +179,7 @@ module Tarantool
173
179
  end
174
180
 
175
181
  def space
176
- @space ||= Tarantool.space space_no
182
+ @space ||= tarantool.space(space_no)
177
183
  end
178
184
 
179
185
  def tuple_to_hash(tuple)
@@ -1,6 +1,5 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  class Request
3
- include EM::Deferrable
4
3
 
5
4
  class << self
6
5
  def request_type(name = nil)
@@ -48,8 +47,8 @@ module Tarantool
48
47
  end
49
48
 
50
49
  def perform
51
- send_packet(make_packet(make_body))
52
- self
50
+ data = connection.send_packet request_id, make_packet(make_body)
51
+ make_response data
53
52
  end
54
53
 
55
54
  def parse_args
@@ -65,19 +64,13 @@ module Tarantool
65
64
  body
66
65
  end
67
66
 
68
- def send_packet(packet)
69
- connection.send_packet request_id, packet do |data|
70
- make_response data
71
- end
72
- end
73
-
74
67
  def make_response(data)
75
68
  return_code, = data[0,4].unpack('L')
76
69
  if return_code == 0
77
- succeed Response.new(data[4, data.size], response_params)
70
+ Response.new(data[4, data.size], response_params)
78
71
  else
79
72
  msg = data[4, data.size].unpack('A*')
80
- fail BadReturnCode.new("Error code #{return_code}: #{msg}")
73
+ raise BadReturnCode.new("Error code #{return_code}: #{msg}")
81
74
  end
82
75
  end
83
76
 
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  require 'tarantool/request'
3
3
  module Requests
4
4
  REQUEST_TYPES = {
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Requests
3
3
  class Call < Request
4
4
  request_type :call
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Requests
3
3
  class Delete < Request
4
4
  request_type :delete
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Requests
3
3
  class Insert < Request
4
4
  request_type :insert
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Requests
3
3
  class Ping < Request
4
4
  request_type :ping
@@ -9,7 +9,7 @@ module Tarantool
9
9
  end
10
10
 
11
11
  def make_response(data)
12
- succeed(Time.now - @start_time)
12
+ Time.now - @start_time
13
13
  end
14
14
  end
15
15
  end
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Requests
3
3
  class Select < Request
4
4
  request_type :select
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Requests
3
3
  class Update < Request
4
4
  request_type :update
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  class Field
3
3
  attr_reader :data
4
4
  def initialize(data)
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Serializers
3
3
  MAP = {}
4
4
  %w{string integer}.each do |v|
@@ -1,5 +1,5 @@
1
1
  require 'bson'
2
- module Tarantool
2
+ class Tarantool
3
3
  module Serializers
4
4
  class BSON
5
5
  Serializers::MAP[:bson] = self
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Serializers
3
3
  class Integer
4
4
  Serializers::MAP[:integer] = self
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  module Serializers
3
3
  class String
4
4
  Serializers::MAP[:string] = self
@@ -1,4 +1,4 @@
1
- module Tarantool
1
+ class Tarantool
2
2
  class Space
3
3
  attr_accessor :space_no
4
4
  attr_reader :connection
@@ -1,9 +1,9 @@
1
1
  module Helpers
2
2
  module Truncate
3
3
  def teardown
4
- while (res = Tarantool.call(proc_name: 'box.select_range', args: [Tarantool.singleton_space.space_no.to_s, '0', '100'], return_tuple: true)) && res.tuples.size > 0
4
+ while (res = space.call(proc_name: 'box.select_range', args: [space.space_no.to_s, '0', '100'], return_tuple: true)) && res.tuples.size > 0
5
5
  res.tuples.each do |k, *_|
6
- Tarantool.delete key: k
6
+ space.delete key: k
7
7
  end
8
8
  end
9
9
  super
data/spec/spec_helper.rb CHANGED
@@ -3,26 +3,19 @@ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
3
3
  Bundler.setup
4
4
 
5
5
  require 'minitest/spec'
6
+ require 'minitest/autorun'
6
7
 
7
8
  require 'helpers/let'
8
9
  require 'helpers/truncate'
9
10
  require 'rr'
10
11
 
11
- require 'tarantool/synchrony'
12
+ require 'tarantool'
12
13
 
13
- config = { host: '192.168.127.128', port: 33013, space_no: 0 }
14
+ TARANTOOL_CONFIG = { host: '127.0.0.1', port: 33013, type: :block }
14
15
 
15
- Tarantool.configure config
16
+ DB = Tarantool.new TARANTOOL_CONFIG
16
17
 
17
18
  class MiniTest::Unit::TestCase
18
19
  extend Helpers::Let
19
20
  include RR::Adapters::MiniTest
20
- end
21
-
22
- at_exit {
23
- EM.synchrony do
24
- exit_code = MiniTest::Unit.new.run(ARGV)
25
- EM.stop
26
- exit_code
27
- end
28
- }
21
+ end
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require "em-synchrony"
4
+ describe "Tarantool with EM" do
5
+ def space
6
+ @space ||= Tarantool.new(TARANTOOL_CONFIG.merge(type: :em)).space 1
7
+ end
8
+
9
+ describe "insert, select and delete" do
10
+ it "should insert tuple and return it" do
11
+ EM.synchrony do
12
+ Fiber.new do
13
+ space.insert 100, 'привет', return_tuple: true
14
+ res = space.select 100
15
+ int, string = res.tuple
16
+ int.to_i.must_equal 100
17
+ string.to_s.must_equal 'привет'
18
+ space.delete 100
19
+ EM.stop
20
+ end.resume
21
+ end
22
+ end
23
+ end
24
+ end
@@ -5,11 +5,16 @@ require 'yajl'
5
5
  require 'tarantool/serializers/bson'
6
6
  describe Tarantool::Record do
7
7
  include Helpers::Truncate
8
- before do
9
- Tarantool.singleton_space.space_no = 0
8
+
9
+ def space
10
+ @space ||= DB.space 0
10
11
  end
12
+
11
13
  let(:user_class) do
12
14
  Class.new(Tarantool::Record) do
15
+ set_tarantool DB
16
+ set_space_no 0
17
+
13
18
  def self.name # For naming
14
19
  "User"
15
20
  end
@@ -2,8 +2,8 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Tarantool::Request do
5
- before do
6
- Tarantool.singleton_space.space_no = 1
5
+ def space
6
+ @space ||= DB.space 1
7
7
  end
8
8
  describe "pack method" do
9
9
  describe "for field" do
@@ -32,7 +32,7 @@ describe Tarantool::Request do
32
32
  end
33
33
 
34
34
  describe "instance" do
35
- let(:request) { Tarantool::Requests::Insert.new Tarantool.space }
35
+ let(:request) { Tarantool::Requests::Insert.new space }
36
36
 
37
37
  it "should make packet with right request type, body size and next request id + body" do
38
38
  body = 'hi'
@@ -46,8 +46,8 @@ describe Tarantool::Request do
46
46
  include Helpers::Truncate
47
47
  describe "insert and select" do
48
48
  it "should insert tuple and return it" do
49
- Tarantool.insert 100, 'привет', return_tuple: true
50
- res = Tarantool.select 100
49
+ space.insert 100, 'привет', return_tuple: true
50
+ res = space.select 100
51
51
  int, string = res.tuple
52
52
  int.to_i.must_equal 100
53
53
  string.to_s.must_equal 'привет'
@@ -55,40 +55,40 @@ describe Tarantool::Request do
55
55
 
56
56
  describe "with equal ids" do
57
57
  it "should raise error" do
58
- Tarantool.insert 100, 'lala'
59
- lambda { Tarantool.insert 100, 'yo' }.must_raise(Tarantool::BadReturnCode)
58
+ space.insert 100, 'lala'
59
+ lambda { space.insert 100, 'yo' }.must_raise(Tarantool::BadReturnCode)
60
60
  end
61
61
  end
62
62
  end
63
63
 
64
64
  describe "select" do
65
65
  it "should select multiple tuples" do
66
- Tarantool.insert 100, 'привет'
67
- Tarantool.insert 101, 'hi'
68
- res = Tarantool.select 100, 101
66
+ space.insert 100, 'привет'
67
+ space.insert 101, 'hi'
68
+ res = space.select 100, 101
69
69
  res.tuples.map { |v| v.last.to_s }.must_equal ['привет', 'hi']
70
70
  end
71
71
  end
72
72
 
73
73
  describe "call" do
74
74
  it "should call lua proc" do
75
- res = Tarantool.call proc_name: 'box.pack', args: ['i', '100'], return_tuple: true
75
+ res = space.call proc_name: 'box.pack', args: ['i', '100'], return_tuple: true
76
76
  res.tuple[0].to_i.must_equal 100
77
77
  end
78
78
 
79
79
  it "should return batches via select_range" do
80
- Tarantool.insert 100, 'привет'
81
- Tarantool.insert 101, 'hi'
82
- res = Tarantool.call proc_name: 'box.select_range', args: ['1', '0', '100'], return_tuple: true
80
+ space.insert 100, 'привет'
81
+ space.insert 101, 'hi'
82
+ res = space.call proc_name: 'box.select_range', args: ['1', '0', '100'], return_tuple: true
83
83
  res.tuples.size.must_equal 2
84
84
  end
85
85
  end
86
86
 
87
87
  describe "update" do
88
88
  it "should update tuple" do
89
- Tarantool.insert 100, 'привет'
90
- Tarantool.update 100, ops: [[1, :set, 'yo!']]
91
- res = Tarantool.select 100
89
+ space.insert 100, 'привет'
90
+ space.update 100, ops: [[1, :set, 'yo!']]
91
+ res = space.select 100
92
92
  int, string = res.tuple
93
93
  string.to_s.must_equal 'yo!'
94
94
  end
@@ -96,16 +96,16 @@ describe Tarantool::Request do
96
96
 
97
97
  describe "delete" do
98
98
  it "should delete record" do
99
- inserted = Tarantool.insert 100, 'привет', return_tuple: true
100
- Tarantool.delete inserted.tuple[0], return_tuple: true
101
- res = Tarantool.select 100
99
+ inserted = space.insert 100, 'привет', return_tuple: true
100
+ space.delete inserted.tuple[0], return_tuple: true
101
+ res = space.select 100
102
102
  res.tuple.must_be_nil
103
103
  end
104
104
  end
105
105
 
106
106
  describe "ping" do
107
107
  it "should ping without exceptions" do
108
- res = Tarantool.ping
108
+ res = space.ping
109
109
  res.must_be_kind_of Numeric
110
110
  end
111
111
  end
data/tarantool.gemspec CHANGED
@@ -4,8 +4,8 @@ Gem::Specification.new do |s|
4
4
  s.rubygems_version = '1.3.5'
5
5
 
6
6
  s.name = 'tarantool'
7
- s.version = '0.1.1'
8
- s.date = '2011-12-13'
7
+ s.version = '0.2'
8
+ s.date = '2012-01-23'
9
9
  s.rubyforge_project = 'tarantool'
10
10
 
11
11
  s.summary = "Tarantool KV-storage client."
@@ -20,9 +20,9 @@ Gem::Specification.new do |s|
20
20
  s.rdoc_options = ["--charset=UTF-8"]
21
21
  s.extra_rdoc_files = %w[README.md LICENSE]
22
22
 
23
- s.add_dependency('eventmachine', [">= 1.0.0.beta.4", "< 2.0.0"])
23
+ s.add_dependency('iproto', [">= 0.1"])
24
24
  s.add_dependency('activemodel', [">= 3.1", "< 4.0"])
25
- s.add_dependency('em-synchrony', [">= 1.0.0", "< 2.0"])
25
+
26
26
 
27
27
  # = MANIFEST =
28
28
  s.files = %w[
@@ -33,10 +33,8 @@ Gem::Specification.new do |s|
33
33
  Rakefile
34
34
  examples/em_simple.rb
35
35
  examples/record.rb
36
- examples/synchrony_simple.rb
37
- lib/em/protocols/fixed_header_and_body.rb
36
+ examples/simple.rb
38
37
  lib/tarantool.rb
39
- lib/tarantool/connection.rb
40
38
  lib/tarantool/exceptions.rb
41
39
  lib/tarantool/record.rb
42
40
  lib/tarantool/request.rb
@@ -53,11 +51,11 @@ Gem::Specification.new do |s|
53
51
  lib/tarantool/serializers/integer.rb
54
52
  lib/tarantool/serializers/string.rb
55
53
  lib/tarantool/space.rb
56
- lib/tarantool/synchrony.rb
57
54
  spec/helpers/let.rb
58
55
  spec/helpers/truncate.rb
59
56
  spec/spec_helper.rb
60
57
  spec/tarantool.cfg
58
+ spec/tarantool/em_spec.rb
61
59
  spec/tarantool/record_spec.rb
62
60
  spec/tarantool/request_spec.rb
63
61
  tarantool.gemspec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarantool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,25 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000Z
12
+ date: 2012-01-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: eventmachine
16
- requirement: &70194702023280 !ruby/object:Gem::Requirement
15
+ name: iproto
16
+ requirement: &70163621772280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.0.0.beta.4
22
- - - <
23
- - !ruby/object:Gem::Version
24
- version: 2.0.0
21
+ version: '0.1'
25
22
  type: :runtime
26
23
  prerelease: false
27
- version_requirements: *70194702023280
24
+ version_requirements: *70163621772280
28
25
  - !ruby/object:Gem::Dependency
29
26
  name: activemodel
30
- requirement: &70194702022400 !ruby/object:Gem::Requirement
27
+ requirement: &70163613525100 !ruby/object:Gem::Requirement
31
28
  none: false
32
29
  requirements:
33
30
  - - ! '>='
@@ -38,21 +35,7 @@ dependencies:
38
35
  version: '4.0'
39
36
  type: :runtime
40
37
  prerelease: false
41
- version_requirements: *70194702022400
42
- - !ruby/object:Gem::Dependency
43
- name: em-synchrony
44
- requirement: &70194702021400 !ruby/object:Gem::Requirement
45
- none: false
46
- requirements:
47
- - - ! '>='
48
- - !ruby/object:Gem::Version
49
- version: 1.0.0
50
- - - <
51
- - !ruby/object:Gem::Version
52
- version: '2.0'
53
- type: :runtime
54
- prerelease: false
55
- version_requirements: *70194702021400
38
+ version_requirements: *70163613525100
56
39
  description: Tarantool KV-storage client.
57
40
  email: ceo@prepor.ru
58
41
  executables: []
@@ -68,10 +51,8 @@ files:
68
51
  - Rakefile
69
52
  - examples/em_simple.rb
70
53
  - examples/record.rb
71
- - examples/synchrony_simple.rb
72
- - lib/em/protocols/fixed_header_and_body.rb
54
+ - examples/simple.rb
73
55
  - lib/tarantool.rb
74
- - lib/tarantool/connection.rb
75
56
  - lib/tarantool/exceptions.rb
76
57
  - lib/tarantool/record.rb
77
58
  - lib/tarantool/request.rb
@@ -88,11 +69,11 @@ files:
88
69
  - lib/tarantool/serializers/integer.rb
89
70
  - lib/tarantool/serializers/string.rb
90
71
  - lib/tarantool/space.rb
91
- - lib/tarantool/synchrony.rb
92
72
  - spec/helpers/let.rb
93
73
  - spec/helpers/truncate.rb
94
74
  - spec/spec_helper.rb
95
75
  - spec/tarantool.cfg
76
+ - spec/tarantool/em_spec.rb
96
77
  - spec/tarantool/record_spec.rb
97
78
  - spec/tarantool/request_spec.rb
98
79
  - tarantool.gemspec
@@ -122,5 +103,6 @@ signing_key:
122
103
  specification_version: 2
123
104
  summary: Tarantool KV-storage client.
124
105
  test_files:
106
+ - spec/tarantool/em_spec.rb
125
107
  - spec/tarantool/record_spec.rb
126
108
  - spec/tarantool/request_spec.rb
@@ -1,13 +0,0 @@
1
- require 'bundler'
2
- ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
3
- Bundler.setup
4
-
5
- require 'tarantool/synchrony'
6
-
7
- EM.synchrony do
8
- Tarantool.configure host: 'localhost', port: 33013, space_no: 0
9
- Tarantool.insert 'prepor', 'Andrew', 'ceo@prepor.ru'
10
- res = Tarantool.select 'prepor'
11
- puts "Name: #{res.tuple[1].to_s}; Email: #{res.tuple[2].to_s}"
12
- EM.stop
13
- end
@@ -1,67 +0,0 @@
1
- module EventMachine
2
- module Protocols
3
- module FixedHeaderAndBody
4
-
5
- def self.included(base)
6
- base.extend ClassMethods
7
- end
8
-
9
- module ClassMethods
10
- def header_size(size = nil)
11
- if size
12
- @_header_size = size
13
- else
14
- @_header_size
15
- end
16
- end
17
- end
18
-
19
- attr_accessor :header, :body
20
-
21
- def receive_data(data)
22
- @buffer ||= ''
23
- offset = 0
24
- while (chunk = data[offset, _needed_size - @buffer.size]).size > 0 || _needed_size == 0
25
- @buffer += chunk
26
- offset += chunk.size
27
- if @buffer.size == _needed_size
28
- case _state
29
- when :receive_header
30
- @_state = :receive_body
31
- receive_header @buffer
32
- when :receive_body
33
- @_state = :receive_header
34
- receive_body @buffer
35
- end
36
- @buffer = ''
37
- end
38
- end
39
- end
40
-
41
- def receive_header(header)
42
- # for override
43
- end
44
-
45
- def body_size
46
- # for override
47
- end
48
-
49
- def receive_body(body)
50
- # for override
51
- end
52
-
53
- def _needed_size
54
- case _state
55
- when :receive_header
56
- self.class.header_size
57
- when :receive_body
58
- body_size
59
- end
60
- end
61
-
62
- def _state
63
- @_state ||= :receive_header
64
- end
65
- end
66
- end
67
- end
@@ -1,54 +0,0 @@
1
- require 'em/protocols/fixed_header_and_body'
2
- module Tarantool
3
- class Connection < EM::Connection
4
- include EM::Protocols::FixedHeaderAndBody
5
-
6
- header_size 12
7
-
8
- def next_request_id
9
- @next_request_id ||= 0
10
- @next_request_id += 1
11
- if @next_request_id > 0xffffffff
12
- @next_request_id = 0
13
- end
14
- @next_request_id
15
- end
16
-
17
- def connection_completed
18
- @connected = true
19
- end
20
-
21
- # begin FixedHeaderAndBody API
22
- def body_size
23
- @body_size
24
- end
25
-
26
- def receive_header(header)
27
- @type, @body_size, @request_id = header.unpack('L3')
28
- end
29
-
30
- def receive_body(data)
31
- clb = waiting_requests.delete @request_id
32
- raise UnexpectedResponse.new("For request id #{@request_id}") unless clb
33
- clb.call data
34
- end
35
- # end FixedHeaderAndBody API
36
-
37
- def waiting_requests
38
- @waiting_requests ||= {}
39
- end
40
-
41
- def send_packet(request_id, data, &clb)
42
- send_data data
43
- waiting_requests[request_id] = clb
44
- end
45
-
46
- def close_connection(*args)
47
- super(*args)
48
- end
49
-
50
- def unbind
51
- raise CouldNotConnect.new unless @connected
52
- end
53
- end
54
- end
@@ -1,13 +0,0 @@
1
- require 'tarantool'
2
- require 'em-synchrony'
3
-
4
- module Tarantool
5
- class Space
6
- alias :deffered_request :request
7
- def request(*args)
8
- EM::Synchrony.sync(deffered_request(*args)).tap do |v|
9
- raise v if v.is_a?(Exception)
10
- end
11
- end
12
- end
13
- end