Class: Ccp::Kvs::Tokyo::Cabinet

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

Direct Known Subclasses

Ccp::Kvs::Tch

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/tokyo/cabinet.rb', line 7

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

Instance Method Details

#countObject



55
56
57
58
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 55

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

#del(k) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 36

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

#each(&block) ⇒ Object

iterator



86
87
88
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 86

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

#each_key(&block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 96

def each_key(&block)
  tryR("each_key")
  @db.iterinit or tokyo_error!("each_key: ")
  while key = @db.iternext
    block.call(key)
  end
end

#each_pair(&block) ⇒ Object



90
91
92
93
94
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 90

def each_pair(&block)
  each_key do |key|
    block.call(key, get(key))
  end
end

#exist?(k) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 50

def exist?(k)
  tryR("exist?")
  return @db.has_key?(k.to_s)
end

#firstObject



118
119
120
121
122
123
124
125
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 118

def first
  key = first_key
  if key
    return [key, get(key)]
  else
    return nil
  end
end

#first_keyObject



112
113
114
115
116
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 112

def first_key
  tryR("first_key")
  @db.iterinit or tokyo_error!("first_key: ")
  return @db.iternext
end

#get(k) ⇒ Object

kvs



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

def get(k)
  tryR("get")
  v = @db[k.to_s]
  if v
    return decode(v)
  else
    if @db.ecode == HDB::ENOREC
      return nil
    else
      tokyo_error!("get(%s): " % k)
    end
  end
end

#keysObject



104
105
106
107
108
109
110
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 104

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

#readObject

bulk operations (not DRY but fast)



63
64
65
66
67
68
69
70
71
72
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 63

def read
  tryR("read")
  hash = {}
  @db.iterinit or tokyo_error!("read: ")
  while k = @db.iternext
    v = @db.get(k) or tokyo_error!("get(%s): " % k)
    hash[k] = decode(v)
  end
  return hash
end

#set(k, v) ⇒ Object



29
30
31
32
33
34
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 29

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

#write(h) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ccp/kvs/tokyo/cabinet.rb', line 74

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