Module: Persist::TCAdapter

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

Constant Summary collapse

MAX_CHAR =
255.chr

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/tokyocabinet.rb', line 6

def closed
  @closed
end

#persistence_pathObject

Returns the value of attribute persistence_path.



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

def persistence_path
  @persistence_path
end

#tokyocabinet_classObject

Returns the value of attribute tokyocabinet_class.



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

def tokyocabinet_class
  @tokyocabinet_class
end

#writableObject

Returns the value of attribute writable.



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

def writable
  @writable
end

Class Method Details

.open(path, write, tokyocabinet_class = TokyoCabinet::HDB) ⇒ Object



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

def self.open(path, write, tokyocabinet_class = TokyoCabinet::HDB)
  tokyocabinet_class = TokyoCabinet::HDB if tokyocabinet_class == "HDB"
  tokyocabinet_class = TokyoCabinet::BDB if tokyocabinet_class == "BDB"

  database = CONNECTIONS[path] ||= tokyocabinet_class.new

  flags = (write ? tokyocabinet_class::OWRITER | tokyocabinet_class::OCREAT : tokyocabinet_class::OREADER)
  database.close 

  if !database.open(path, flags)
    ecode = database.ecode
    raise "Open error: #{database.errmsg(ecode)}. Trying to open file #{path}"
  end

  database.extend Persist::TCAdapter unless Persist::TCAdapter === database
  database.persistence_path ||= path
  database.tokyocabinet_class = tokyocabinet_class

  database
end

Instance Method Details

#closeObject



44
45
46
47
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 44

def close
  @closed = true
  super
end

#closed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 40

def closed?
  @closed
end

#collectObject

def each

iterinit
while key = iternext
  yield key, get(key)
end

end



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 86

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

#delete(key) ⇒ Object



98
99
100
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 98

def delete(key)
  out(key)
end

#get_prefix(key) ⇒ Object



35
36
37
38
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 35

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

#merge!(hash) ⇒ Object



138
139
140
141
142
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 138

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

#prefix(key) ⇒ Object



31
32
33
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 31

def prefix(key)
  range(key, 1, key + MAX_CHAR, 1)
end

#range(*args) ⇒ Object



145
146
147
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 145

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

#read(force = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 49

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

#read_and_closeObject



128
129
130
131
132
133
134
135
136
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 128

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

#write(force = true) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 61

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

  if !self.open(@persistence_path, tokyocabinet_class::OWRITER)
    ecode = self.ecode
    raise "Open error: #{self.errmsg(ecode)}. Trying to open file #{@persistence_path}"
  end

  @writable = true
  @closed = false
  self
end

#write?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 75

def write?
  @writable
end

#write_and_closeObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 115

def write_and_close
  lock_filename = Persist.persistence_path(persistence_path + '.write', {: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



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 102

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