Module: Persist::TCAdapter

Includes:
TSVAdapter
Defined in:
lib/rbbt/persist/tsv/tokyocabinet.rb,
lib/rbbt/persist/tsv/tokyocabinet/marshal.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, #each, #get_prefix, #include?, #keys, #lock, #lock_and_close, #merge!, #prefix, #range, #read?, #read_and_close, #read_lock, #size, #values_at, #with_read, #with_write, #write?, #write_and_close, #write_and_read, #write_lock

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, 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 10

def self.open(path, write, tokyocabinet_class = TokyoCabinet::HDB)
  if String === tokyocabinet_class && tokyocabinet_class.include?(":big")
    big = true
    tokyocabinet_class = tokyocabinet_class.split(":").first
  else
    big = false
  end

  dir = File.dirname(File.expand_path(path))
  File.mkdir(dir) unless File.exist?(dir)

  tokyocabinet_class = TokyoCabinet::HDB if tokyocabinet_class == "HDB" or tokyocabinet_class.nil?
  tokyocabinet_class = TokyoCabinet::BDB if tokyocabinet_class == "BDB"

  database = CONNECTIONS[path] ||= Log.ignore_stderr do tokyocabinet_class.new end

  if big and not Open.exists?(path)
    database.tune(nil,nil,nil,tokyocabinet_class::TLARGE | tokyocabinet_class::TDEFLATE) 
  end

  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.read

  CONNECTIONS[path] = database

  database
end

Instance Method Details

#closeObject



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

def close
  @closed = true
  @writable = false
  super
end

#marshal_dumpObject



3
4
5
# File 'lib/rbbt/persist/tsv/tokyocabinet/marshal.rb', line 3

def marshal_dump
  [persistence_path, tokyocabinet_class]
end

#read(force = false) ⇒ Object



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

def read(force = false)
  return if ! write? && ! closed && ! 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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbbt/persist/tsv/tokyocabinet.rb', line 70

def write(force = true)
  return if write? && ! closed && ! 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