Module: Persist::TCAdapter

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

Constant Summary

Constants included from TSVAdapter

Persist::TSVAdapter::MAX_CHAR

Instance Attribute Summary collapse

Attributes included from TSVAdapter

#closed, #mutex, #persistence_path, #writable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TSVAdapter

#closed?, #collect, #delete, #get_prefix, #merge!, #prefix, #range, #read?, #read_and_close, #write?, #write_and_close, #write_and_read

Instance Attribute Details

#tokyocabinet_classObject

Returns the value of attribute tokyocabinet_class.



8
9
10
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 8

def tokyocabinet_class
  @tokyocabinet_class
end

Class Method Details

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



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

def self.open(path, write, serializer, tokyocabinet_class = TokyoCabinet::HDB)
  tokyocabinet_class = TokyoCabinet::HDB if tokyocabinet_class == "HDB" or tokyocabinet_class.nil?
  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.mutex = Mutex.new
  database
end

Instance Method Details

#closeObject



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

def close
  @closed = true
  super
end

#read(force = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 37

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

#write(force = true) ⇒ Object



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

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