Module: Persist::KCAdapter

Defined in:
lib/rbbt/persist/tsv/kyotocabinet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#closedObject

Returns the value of attribute closed.



6
7
8
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 6

def closed
  @closed
end

#kyotocabinet_classObject

Returns the value of attribute kyotocabinet_class.



6
7
8
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 6

def kyotocabinet_class
  @kyotocabinet_class
end

#persistence_pathObject

Returns the value of attribute persistence_path.



6
7
8
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 6

def persistence_path
  @persistence_path
end

#writableObject

Returns the value of attribute writable.



6
7
8
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 6

def writable
  @writable
end

Class Method Details

.open(path, write, kyotocabinet_class = "kch") ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 8

def self.open(path, write, kyotocabinet_class = "kch")
  real_path = path + ".#{kyotocabinet_class}"

  @persistence_path = real_path

  flags = (write ? KyotoCabinet::DB::OWRITER | KyotoCabinet::DB::OCREATE : nil)
  database = 
    CONNECTIONS[path] ||= begin
                            db = KyotoCabinet::DB.new
                            db.open(real_path, flags)
                            db
                          end

  database.extend KCAdapter
  database.persistence_path ||= real_path

  database
end

Instance Method Details

#closeObject



51
52
53
54
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 51

def close
  @closed = true
  super
end

#closed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 47

def closed?
  @closed
end

#collectObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 84

def collect
  res = []
  each do |key, value|
    res << if block_given?
             yield key, value
    else
      [key, value]
    end
  end
  res
end

#delete(key) ⇒ Object



96
97
98
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 96

def delete(key)
  out(key)
end

#get_prefix(key) ⇒ Object



37
38
39
40
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 37

def get_prefix(key)
  keys = prefix(key)
  select(:key => keys)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 42

def include?(key)
  value = get(key)
  ! value.nil?
end

#keysObject



27
28
29
30
31
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 27

def keys
  keys = []
  each_key{|k| keys.concat k}
  keys
end

#merge!(hash) ⇒ Object



136
137
138
139
140
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 136

def merge!(hash)
  hash.each do |key,values|
    self[key] = values
  end
end

#prefix(key) ⇒ Object



33
34
35
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 33

def prefix(key)
  range(key, 1, key + 255.chr, 1)
end

#range(*args) ⇒ Object



148
149
150
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 148

def range(*args)
  super(*args) - TSV::ENTRY_KEYS.to_a
end

#read(force = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 56

def read(force = false)
  return if not write? and not closed and not force
  self.close
  if !self.open(@persistence_path, KyotoCabinet::DB::OREADER)
    raise "Open error #{ res }. Trying to open file #{@persistence_path}"
  end
  @writable = false
  @closed = false
  self
end

#read_and_closeObject



126
127
128
129
130
131
132
133
134
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 126

def read_and_close
  read if @closed or write?
  res = begin
          yield
        ensure
          close
        end
  res
end

#write(force = true) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 67

def write(force = true)
  return if write? and not closed and not force
  self.close

  if !self.open(@persistence_path, KyotoCabinet::DB::OWRITER)
    raise "Open error. Trying to open file #{@persistence_path}"
  end

  @writable = true
  @closed = false
  self
end

#write?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 80

def write?
  @writable
end

#write_and_closeObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 113

def write_and_close
  lock_filename = Persist.persistence_path(persistence_path, {:dir => TSV.lock_dir})
  Misc.lock(lock_filename) do
    write if @closed or not write?
    res = begin
            yield
          ensure
            close
          end
    res
  end
end

#write_and_readObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbbt/persist/tsv/kyotocabinet.rb', line 100

def write_and_read
  lock_filename = Persist.persistence_path(persistence_path, {:dir => TSV.lock_dir})
  Misc.lock(lock_filename) do
    write if @closed or not write?
    res = begin
            yield
          ensure
            read
          end
    res
  end
end