Class: Ccp::Kvs::Kyoto::Cabinet

Inherits:
Base
  • Object
show all
Includes:
StateMachine
Defined in:
lib/ccp/kvs/kyoto/cabinet.rb

Direct Known Subclasses

Ccp::Kvs::Kch

Constant Summary

Constants included from StateMachine

StateMachine::CLOSED, StateMachine::LOCKED_BY, StateMachine::READABLE, StateMachine::WRITABLE

Instance Method Summary collapse

Methods included from StateMachine

#C!, #R, #R!, #W, #W!, #__close__, #close, #locker_info, #open, #state, #touch

Methods inherited from Base

#info, #path

Methods included from Core

#[], #[]=, #close, #codec, #codec!, #decode, #encode, #ext, included, #key?, #open, #out, #put, #source, #touch

Constructor Details

#initialize(source) ⇒ Cabinet

Returns a new instance of Cabinet.



7
8
9
10
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 7

def initialize(source)
  @source = source
  @db     = DB.new
end

Instance Method Details

#clearObject



80
81
82
83
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 80

def clear
  tryW("clear")
  @db.clear or kyoto_error!("clear: ")
end

#countObject



53
54
55
56
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 53

def count
  tryR("count")
  return @db.count
end

#del(k) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 34

def del(k)
  tryW("del")
  v = @db[k.to_s]
  if v
    if @db.delete(k.to_s)
      return decode(v)
    else
      kyoto_error!("del(%s): " % k)
    end
  else
    return nil
  end
end

#each(&block) ⇒ Object

iterator



88
89
90
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 88

def each(&block)
  each_pair(&block)
end

#each_key(&block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 103

def each_key(&block)
  tryR("each_key")

  # TODO: Waste memory! But kc ignores exceptions in his each block.
  array = []
  @db.each_key{|k| array << k.first} or kyoto_error!("each_key: ")
  array.each do |k|
    block.call(k)
  end
end

#each_pair(&block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 92

def each_pair(&block)
  tryR("each_pair")

  # TODO: Waste memory! But kc ignores exceptions in his each block.
  array = []
  @db.each{|k, v| array << [k, decode(v)]} or kyoto_error!("each_pair: ")
  array.each do |a|
    block.call(a[0], a[1])
  end
end

#exist?(k) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 48

def exist?(k)
  tryR("exist?")
  return !! @db[k.to_s] # TODO: fast access
end

#firstObject



128
129
130
131
132
133
134
135
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 128

def first
  tryR("first")
  @db.cursor_process {|cur|
    cur.jump
    k, v = cur.get(true)
    return [k, decode(v)]
  }
end

#first_keyObject



124
125
126
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 124

def first_key
  first.first
end

#get(k) ⇒ Object

kvs



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 15

def get(k)
  tryR("get")
  v = @db[k.to_s]
  if v
    return decode(v)
  else
    # tc, kc is not safe for file deletion or unexpected stuffs
    # if @db.error.is_a?(KyotoCabinet::Error::XNOREC)
    return nil
  end
end

#keysObject



114
115
116
117
118
119
120
121
122
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 114

def keys
  tryR("keys")

  array = []
  each_key do |key|
    array << key
  end
  return array
end

#readObject

bulk operations (not DRY but fast)



61
62
63
64
65
66
67
68
69
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 61

def read
  tryR("read")
  hash = {}
  @db.each do |k, v|
    v or kyoto_error!("each(%s): " % k)
    hash[k] = decode(v)
  end
  return hash
end

#set(k, v) ⇒ Object



27
28
29
30
31
32
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 27

def set(k,v)
  tryW("set")
  val = encode(v)
  @db.set(k.to_s, val) or
    kyoto_error!("set(%s): " % k)
end

#write(h) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/ccp/kvs/kyoto/cabinet.rb', line 71

def write(h)
  tryW("write")
  h.each_pair do |k,v|
    val = encode(v)
    @db[k.to_s] = val or kyoto_error!("write(%s): " % k)
  end
  return h
end