Class: Rufus::Tokyo::Tyrant
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Attributes included from HashMethods
Instance Method Summary collapse
-
#copy(target_path) ⇒ Object
Tells the Tyrant server to create a copy of itself at the given (remote) target_path.
-
#defrag ⇒ Object
Tyrant databases DO NOT support the ‘defrag’ call.
-
#initialize(host, port = 0, params = {}) ⇒ Tyrant
constructor
Connects to a given Tokyo Tyrant server.
-
#lib ⇒ Object
Using the tyrant lib.
Methods included from NoTransactions
#abort, #tranabort, #tranbegin, #trancommit, #transaction
Methods included from Ext
Methods included from TyrantCommons
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
Methods included from Outlen
Methods included from Transactions
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 118 119 120 121 122 123 124 125 |
# 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] # # timeout and reconnect # defaults to two minutes timeout = params[:timeout] || 120.0 lib.tcrdbtune(@db, timeout, 1) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
51 52 53 |
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 51 def host @host end |
#port ⇒ Object (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”.
143 144 145 146 |
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 143 def copy (target_path) lib.abs_copy(@db, target_path) || raise_error end |
#defrag ⇒ Object
Tyrant databases DO NOT support the ‘defrag’ call. Calling this method will raise an exception.
151 152 153 154 |
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 151 def defrag raise(NoMethodError.new("Tyrant dbs don't support #defrag")) end |
#lib ⇒ Object
Using the tyrant lib
129 130 131 132 |
# File 'lib/rufus/tokyo/tyrant/abstract.rb', line 129 def lib TyrantLib end |