Class: KyotoTycoon

Inherits:
Object
  • Object
show all
Defined in:
lib/kyototycoon/stream.rb,
lib/kyototycoon.rb,
lib/kyototycoon/cursor.rb,
lib/kyototycoon/tsvrpc.rb,
lib/kyototycoon/serializer.rb,
lib/kyototycoon/tsvrpc/skinny.rb,
lib/kyototycoon/serializer/default.rb,
lib/kyototycoon/serializer/msgpack.rb

Overview

$ cat foo.rb require “rubygems” require “kyototycoon”

KyotoTycoon::Stream.run($stdin) do |line|

... do some stuff ..

end

$ ktremotemgr slave -uw | ruby foo.rb

Defined Under Namespace

Modules: Serializer, Stream, Tsvrpc Classes: Cursor

Constant Summary collapse

VERSION =
'0.6.1'
DEFAULT_HOST =
'0.0.0.0'
DEFAULT_PORT =
1978

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = DEFAULT_HOST, port = DEFAULT_PORT) ⇒ KyotoTycoon

Returns a new instance of KyotoTycoon.



52
53
54
55
56
57
58
59
60
# File 'lib/kyototycoon.rb', line 52

def initialize(host=DEFAULT_HOST, port=DEFAULT_PORT)
  @servers = [[host, port]]
  @checked_servers = nil
  @serializer = KyotoTycoon::Serializer::Default
  @logger = Logger.new(nil)
  @colenc = :B
  @connect_timeout = 0.5
  @cursor = 1
end

Instance Attribute Details

#colencObject

Returns the value of attribute colenc.



19
20
21
# File 'lib/kyototycoon.rb', line 19

def colenc
  @colenc
end

#connect_timeoutObject

Returns the value of attribute connect_timeout.



19
20
21
# File 'lib/kyototycoon.rb', line 19

def connect_timeout
  @connect_timeout
end

#dbObject

Returns the value of attribute db.



20
21
22
# File 'lib/kyototycoon.rb', line 20

def db
  @db
end

#loggerObject

Returns the value of attribute logger.



20
21
22
# File 'lib/kyototycoon.rb', line 20

def logger
  @logger
end

#serializerObject

Returns the value of attribute serializer.



20
21
22
# File 'lib/kyototycoon.rb', line 20

def serializer
  @serializer
end

#serversObject

Returns the value of attribute servers.



19
20
21
# File 'lib/kyototycoon.rb', line 19

def servers
  @servers
end

Class Method Details

.configure(name, host = DEFAULT_HOST, port = DEFAULT_PORT, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kyototycoon.rb', line 25

def self.configure(name, host=DEFAULT_HOST, port=DEFAULT_PORT, &block)
  @configure ||= {}
  if @configure[name]
    raise "'#{name}' is registered"
  end
  @configure[name] = lambda{
    kt = KyotoTycoon.new(host, port)
    block.call(kt)
    kt
  }
end

.configure_reset!Object



41
42
43
# File 'lib/kyototycoon.rb', line 41

def self.configure_reset!
  @configure = {}
end

.configuresObject



37
38
39
# File 'lib/kyototycoon.rb', line 37

def self.configures
  @configure
end

.create(name) ⇒ Object



45
46
47
48
49
50
# File 'lib/kyototycoon.rb', line 45

def self.create(name)
  if @configure[name].nil?
    raise "undefined configure: '#{name}'"
  end
  @configure[name].call
end

Instance Method Details

#add(key, value, xt = nil) ⇒ Object



95
96
97
98
# File 'lib/kyototycoon.rb', line 95

def add(key, value, xt=nil)
  res = request('/rpc/add', {:key => key, :value => @serializer.encode(value), :xt => xt})
  Tsvrpc.parse(res[:body], res[:colenc])
end

#append(key, value, xt = nil) ⇒ Object



105
106
107
# File 'lib/kyototycoon.rb', line 105

def append(key, value, xt=nil)
  request('/rpc/append', {:key => key, :value => @serializer.encode(value), :xt => xt})
end

#cas(key, oldval, newval, xt = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/kyototycoon.rb', line 109

def cas(key, oldval, newval, xt=nil)
  res = request('/rpc/cas', {:key => key, :oval=> @serializer.encode(oldval), :nval => @serializer.encode(newval), :xt => xt})
  case res[:status].to_i
    when 200
      true
    when 450
      false
  end
end

#clearObject



171
172
173
# File 'lib/kyototycoon.rb', line 171

def clear
  request('/rpc/clear')
end

#clientObject



243
244
245
246
247
248
# File 'lib/kyototycoon.rb', line 243

def client
  host, port = *choice_server
  @client ||= begin
    Tsvrpc::Skinny.new(host, port)
  end
end

#cursor(cur_id = nil) ⇒ Object



167
168
169
# File 'lib/kyototycoon.rb', line 167

def cursor(cur_id=nil)
  Cursor.new(self, cur_id || @cursor += 1)
end

#decrement(key, num = 1, xt = nil) ⇒ Object Also known as: decr



125
126
127
# File 'lib/kyototycoon.rb', line 125

def decrement(key, num=1, xt=nil)
  increment(key, num * -1, xt)
end

#echo(value) ⇒ Object



184
185
186
187
# File 'lib/kyototycoon.rb', line 184

def echo(value)
  res = request('/rpc/echo', value)
  Tsvrpc.parse(res[:body], res[:colenc])
end

#finishObject



254
255
256
# File 'lib/kyototycoon.rb', line 254

def finish
  client.finish
end

#get(key) ⇒ Object Also known as: []



78
79
80
81
# File 'lib/kyototycoon.rb', line 78

def get(key)
  res = request('/rpc/get', {:key => key})
  @serializer.decode(Tsvrpc.parse(res[:body], res[:colenc])['value'])
end

#get_bulk(keys) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kyototycoon.rb', line 145

def get_bulk(keys)
  params = keys.inject({}){|params, k|
    params[k.to_s.match(/^_/) ? k.to_s : "_#{k}"] = ''
    params
  }
  res = request('/rpc/get_bulk', params)
  bulk = Tsvrpc.parse(res[:body], res[:colenc])
  bulk.delete_if{|k,v| k.match(/^[^_]/)}.inject({}){|r, (k,v)|
    r[k[1..-1]] = @serializer.decode(v)
    r
  }
end

#increment(key, num = 1, xt = nil) ⇒ Object Also known as: incr



119
120
121
122
# File 'lib/kyototycoon.rb', line 119

def increment(key, num=1, xt=nil)
  res = request('/rpc/increment', {:key => key, :num => num, :xt => xt})
  Tsvrpc.parse(res[:body], res[:colenc])['num'].to_i
end

#increment_double(key, num, xt = nil) ⇒ Object



130
131
132
133
# File 'lib/kyototycoon.rb', line 130

def increment_double(key, num, xt=nil)
  res = request('/rpc/increment_double', {:key => key, :num => num, :xt => xt})
  Tsvrpc.parse(res[:body], res[:colenc])['num'].to_f
end

#keysObject



224
225
226
# File 'lib/kyototycoon.rb', line 224

def keys
  match_prefix("")
end

#match_prefix(prefix) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/kyototycoon.rb', line 199

def match_prefix(prefix)
  res = request('/rpc/match_prefix', {:prefix => prefix})
  keys = []
  Tsvrpc.parse(res[:body], res[:colenc]).each{|k,v|
    if k != 'num'
      keys << k[1, k.length]
    end
  }
  keys
end

#match_regex(re) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kyototycoon.rb', line 210

def match_regex(re)
  if re.class == Regexp
    re = re.source
  end
  res = request('/rpc/match_regex', {:regex => re})
  keys = []
  Tsvrpc.parse(res[:body], res[:colenc]).each{|k,v|
    if k != 'num'
      keys << k[1, k.length]
    end
  }
  keys
end

#remove(*keys) ⇒ Object Also known as: delete



84
85
86
# File 'lib/kyototycoon.rb', line 84

def remove(*keys)
  remove_bulk(keys.flatten)
end

#remove_bulk(keys) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/kyototycoon.rb', line 158

def remove_bulk(keys)
  params = keys.inject({}){|params, k|
    params[k.to_s.match(/^_/) ? k.to_s : "_#{k}"] = ''
    params
  }
  res = request('/rpc/remove_bulk', params)
  Tsvrpc.parse(res[:body], res[:colenc])
end

#replace(key, value, xt = nil) ⇒ Object



100
101
102
103
# File 'lib/kyototycoon.rb', line 100

def replace(key, value, xt=nil)
  res = request('/rpc/replace', {:key => key, :value => @serializer.encode(value), :xt => xt})
  Tsvrpc.parse(res[:body], res[:colenc])
end

#reportObject



189
190
191
192
# File 'lib/kyototycoon.rb', line 189

def report
  res = request('/rpc/report')
  Tsvrpc.parse(res[:body], res[:colenc])
end

#request(path, params = nil) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/kyototycoon.rb', line 228

def request(path, params=nil)
  if @db
    params ||= {}
    params[:DB] = @db
  end

  status,body,colenc = client.request(path, params, @colenc)
  if ![200, 450].include?(status.to_i)
    raise body
  end
  res = {:status => status, :body => body, :colenc => colenc}
  @logger.info("#{path}: #{res[:status]} with query parameters #{params.inspect}")
  res
end

#set(key, value, xt = nil) ⇒ Object Also known as: []=



89
90
91
92
# File 'lib/kyototycoon.rb', line 89

def set(key, value, xt=nil)
  res = request('/rpc/set', {:key => key, :value => @serializer.encode(value), :xt => xt})
  Tsvrpc.parse(res[:body], res[:colenc])
end

#set_bulk(records) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/kyototycoon.rb', line 135

def set_bulk(records)
  # records={'a' => 'aa', 'b' => 'bb'}
  values = {}
  records.each{|k,v|
    values[k.to_s.match(/^_/) ? k.to_s : "_#{k}"] = @serializer.encode(v)
  }
  res = request('/rpc/set_bulk', values)
  Tsvrpc.parse(res[:body], res[:colenc])
end

#startObject



250
251
252
# File 'lib/kyototycoon.rb', line 250

def start
  client.start
end

#statusObject



194
195
196
197
# File 'lib/kyototycoon.rb', line 194

def status
  res = request('/rpc/status')
  Tsvrpc.parse(res[:body], res[:colenc])
end

#sync(params = {}) ⇒ Object Also known as: syncronize



179
180
181
# File 'lib/kyototycoon.rb', line 179

def sync(params={})
  request('/rpc/synchronize', params)
end

#vacuumObject



175
176
177
# File 'lib/kyototycoon.rb', line 175

def vacuum
  request('/rpc/vacuum')
end