Class: Rufus::Edo::NetTyrant

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

Overview

Connecting to a ‘classic’ Tokyo Tyrant server remotely

require 'rufus/edo/ntyrant'
t = Rufus::Edo::NetTyrant.new('127.0.0.1', 44001)
t['toto'] = 'blah blah'
t['toto'] # => 'blah blah'

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 CabinetCore

#[]=, #clear, #close, #counter_value, #defrag, #delete, #delete_keys_with_prefix, included, #incr, #keys, #ldelete, #lget, #merge!, #original, #path, #putcat, #putkeep, #size, #sync, #tranabort, #tranbegin, #trancommit

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, params = {}) ⇒ NetTyrant

Connects to a given Tokyo Tyrant server.

Note that if the port is not specified, the host parameter is expected to hold the path to a unix socket (not a TCP socket).

(You can start a unix socket listening Tyrant with :

  ttserver -host /tmp/tyrant_socket -port 0 data.tch

and then connect to it with rufus-tokyo via :

  require 'rufus/edo/ntyrant'
  db = Rufus::Edo::NetTyrant.new('/tmp/tyrant_socket')
  db['a'] = 'alpha'
  db.close

)

To connect to a classic TCP bound Tyrant (port 44001) :

t = Rufus::Edo::NetTyrant.new('127.0.0.1', 44001)

:default and :default_proc

Much like a Ruby Hash, a Tyrant accepts a default value or a default_proc

db = Rufus::Edo::NetTyrant.new('127.0.0.1', 1978, :default => 'xxx')
db['fred'] = 'Astaire'
p db['fred'] # => 'Astaire'
p db['ginger'] # => 'xxx'

db = Rufus::Edo::NetTyrant.new(
  '127.0.0.1',
  1978,
  :default_proc => lambda { |cab, key| "not found : '#{k}'" }
p db['ginger'] # => "not found : 'ginger'"

The first arg passed to the default_proc is the tyrant itself, so this opens up interesting possibilities.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 91

def initialize (host, port=0, params={})

  @host = host
  @port = port

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

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

    @db.close

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

  #
  # default value|proc

  self.default = params[:default]
  @default_proc ||= params[:default_proc]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



49
50
51
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 49

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



49
50
51
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 49

def port
  @port
end

Instance Method Details

#compact_copy(target_path) ⇒ Object

Copies the current cabinet to a new file.

Does it by copying each entry afresh to the target file. Spares some space, hence the ‘compact’ label…

Raises:

  • (NotImplementedError)


141
142
143
144
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 141

def compact_copy (target_path)

  raise NotImplementedError.new('not creating files locally')
end

#copy(target_path) ⇒ Object

Tells the Tyrant server to create a copy of itself at the given (remote) target_path.

Returns true when successful.

Note : if you started your ttserver with a path like “tyrants/data.tch” you have to provide a target path in the same subdir, like “tyrants/data_prime.tch”.



131
132
133
134
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 131

def copy (target_path)

  @db.copy(target_path) || raise_error
end

#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.



155
156
157
158
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 155

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

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

#weightObject

Returns the ‘weight’ of the db (in bytes)



117
118
119
120
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 117

def weight

  self.stat['size']
end