Class: Rufus::Edo::NetTyrantTable

Inherits:
Object
  • Object
show all
Includes:
TableCore, Tokyo::NoTransactions, Tokyo::TyrantCommons
Defined in:
lib/rufus/edo/ntyrant/table.rb

Overview

A Tokyo Cabinet table, but remote…

require 'rufus/edo/ntyrant'
t = Rufus::Edo::NetTyrant.new('127.0.0.1', 44001)
t['toto'] = { 'name' => 'toto the first', 'age' => '34' }
t['toto']
  # => { 'name' => 'toto the first', 'age' => '34' }

NOTE : The advantage of this class is that it leverages the TokyoTyrant.rb provided by Hirabayashi-san. It’s pure Ruby, it’s slow but works everywhere without the need for Tokyo Cabinet and Tyrant C libraries.

Constant Summary

Constants included from TableCore

TableCore::INDEX_TYPES

Instance Attribute Summary collapse

Attributes included from Tokyo::HashMethods

#default_proc

Instance Method Summary collapse

Methods included from Tokyo::NoTransactions

#abort, #tranabort, #tranbegin, #trancommit, #transaction

Methods included from Tokyo::TyrantCommons

#compute_ext_opts, #stat

Methods included from TableCore

#[]=, #clear, #close, #delete, #delete_keys_with_prefix, #difference, #generate_unique_id, included, #intersection, #keys, #original, #prepare_query, #query, #query_count, #query_delete, #search, #set_index, #size, #tranabort, #tranbegin, #trancommit, #union

Methods included from Tokyo::Transactions

#abort, #transaction

Methods included from Tokyo::HashMethods

#[], #default, #default=, #each, #merge, #merge!, #to_a, #to_h, #values

Constructor Details

#initialize(host, port = 0) ⇒ NetTyrantTable

Connects to the Tyrant table listening at the given host and port.

You start such a Tyrant with :

ttserver -port 44502 data.tct

and then :

require 'rufus/edo/ntyrant'
t = Rufus::Edo::NetTyrantTable.new('127.0.0.1', 44502)
t['client0'] = { 'name' => 'Heike no Kyomori', 'country' => 'jp' }
t.close

You can start a Tokyo Tyrant and make it listen to a unix socket (not TCP) with :

ttserver -host /tmp/table_socket -port 0 data.tct

then :

require 'rufus/edo/ntyrant'
t = Rufus::Edo::NetTyrantTable.new('/tmp/table_socket')
t['client0'] = { 'name' => 'Theodore Roosevelt', 'country' => 'usa' }
t.close


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rufus/edo/ntyrant/table.rb', line 83

def initialize (host, port=0)

  @host = host
  @port = port

  @db = TokyoTyrant::RDBTBL.new
  @db.open(host, port) || raise_error

  @default_proc = nil

  if self.stat['type'] != 'table'

    @db.close

    raise ArgumentError.new(
      "tyrant at #{host}:#{port} is not a table, " +
      "use Rufus::Edo::NetTyrant instead to access it.")
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



54
55
56
# File 'lib/rufus/edo/ntyrant/table.rb', line 54

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



54
55
56
# File 'lib/rufus/edo/ntyrant/table.rb', line 54

def port
  @port
end

Instance Method Details

#ext(func_name, key = '', value = '', opts = {}) ⇒ Object

Calls a lua embedded function (tokyocabinet.sourceforge.net/tyrantdoc/#luaext)

Options are :global_locking and :record_locking

Returns the return value of the called function.

Nil is returned in case of failure.



126
127
128
129
# File 'lib/rufus/edo/ntyrant/table.rb', line 126

def ext (func_name, key='', value='', opts={})

  @db.ext(func_name.to_s, key.to_s, value.to_s, compute_ext_opts(opts))
end

#lget(*keys) ⇒ Object Also known as: mget

Gets multiple records in one sweep.



105
106
107
108
109
110
111
112
113
# File 'lib/rufus/edo/ntyrant/table.rb', line 105

def lget (*keys)

  h = keys.flatten.inject({}) { |hh, k| hh[k] = nil; hh }
  r = @db.mget(h)

  raise 'lget failure' if r == -1

  h
end