Class: Rufus::Edo::NetTyrant

Inherits:
Object
  • Object
show all
Includes:
CabinetCore, 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::TyrantCommons

#compute_ext_opts, #stat

Methods included from CabinetCore

#[]=, #clear, #close, #defrag, #delete, #delete_keys_with_prefix, included, #incr, #keys, #ldelete, #lget, #merge!, #original, #path, #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) ⇒ 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)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 71

def initialize (host, port=0)

  @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
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

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)


111
112
113
114
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 111

def compact_copy (target_path)

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

#copy(target_path) ⇒ Object

isn’t that a bit dangerous ? it creates a file on the server…

DISABLED.



100
101
102
103
104
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 100

def copy (target_path)

  #@db.copy(target_path)
  raise 'not allowed to create files on the server'
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.



125
126
127
128
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 125

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)



91
92
93
94
# File 'lib/rufus/edo/ntyrant/abstract.rb', line 91

def weight

  self.stat['size']
end