Class: Rufus::Tokyo::Tyrant

Inherits:
Cabinet
  • Object
show all
Includes:
Ext, NoTransactions, TyrantCommons
Defined in:
lib/rufus/tokyo/tyrant/abstract.rb

Overview

Connecting to a 'classic' tyrant server remotely

require 'rufus/tokyo/tyrant'
t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 44001)
t['toto'] = 'blah blah'
t['toto'] # => 'blah blah'

Cabinet methods not available to Tyrant

The #defrag method is not available for Tyrant.

More importantly transaction related methods are not available either. No transactions for Tokyo Tyrant.

Instance Attribute Summary collapse

Attributes included from HashMethods

#default_proc

Instance Method Summary collapse

Methods included from NoTransactions

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

Methods included from Ext

#ext

Methods included from TyrantCommons

#compute_ext_opts, #stat

Methods inherited from Cabinet

#[]=, #clear, #close, #compact_copy, #counter_value, #delete, #delete_keys_with_prefix, #get4, #incr, #keys, #ldelete, #lget, #merge!, new_hash, new_tree, #path, #putcat, #putdup, #putkeep, #size, #sync, #tranabort, #tranbegin, #trancommit, #weight

Methods included from Openable

#open

Methods included from Outlen

#outlen_op

Methods included from Transactions

#abort, #transaction

Methods included from HashMethods

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

Constructor Details

#initialize(host, port = 0, params = {}) ⇒ Tyrant

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/tokyo/tyrant'
  db = Rufus::Tokyo::Tyrant.new('/tmp/tyrant_socket')
  db['a'] = 'alpha'
  db.close

)

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

t = Rufus::Tokyo::Tyrant.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::Tokyo::Tyrant.new('127.0.0.1', 1978, :default => 'xxx')
db['fred'] = 'Astaire'
p db['fred'] # => 'Astaire'
p db['ginger'] # => 'xxx'

db = Rufus::Tokyo::Tyrant.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.


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 93

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

  @db = lib.tcrdbnew

  @host = host
  @port = port

  lib.tcrdbopen(@db, host, port) || raise(
    TokyoError.new("couldn't connect to tyrant at #{host}:#{port}"))

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

    self.close

    raise ArgumentError.new(
      "tyrant at #{host}:#{port} is a table, " +
      "use Rufus::Tokyo::TyrantTable 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


51
52
53
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 51

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port


51
52
53
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 51

def port
  @port
end

Instance Method Details

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


135
136
137
138
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 135

def copy (target_path)

  lib.abs_copy(@db, target_path) || raise_error
end

#defragObject

Tyrant databases DO NOT support the 'defrag' call. Calling this method will raise an exception.

Raises:

  • (NoMethodError)

143
144
145
146
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 143

def defrag

  raise(NoMethodError.new("Tyrant dbs don't support #defrag"))
end

#libObject

Using the tyrant lib


121
122
123
124
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 121

def lib

  TyrantLib
end