Module: Persist::CDBAdapter

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

Constant Summary

Constants included from TSVAdapter

TSVAdapter::MAX_CHAR

Instance Attribute Summary

Attributes included from TSVAdapter

#closed, #mutex, #persistence_path, #writable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TSVAdapter

#collect, #each, #get_prefix, #keys, #lock, #lock_and_close, #prefix, #read?, #read_lock, #size, #values_at, #with_read, #with_write, #write?, #write_lock

Class Method Details

.open(path, write) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rbbt/persist/tsv/cdb.rb', line 8

def self.open(path, write)
  write = true unless File.exist? path

 database = CONNECTIONS[path] ||= begin
                                     file = File.open(path, 'w')
                                     LibCDB::CDB.new(file)
                                   end

  database.extend Persist::CDBAdapter unless Persist::CDBAdapter === database
  database.persistence_path ||= path

  database
end

Instance Method Details

#[](*args) ⇒ Object



30
31
32
# File 'lib/rbbt/persist/tsv/cdb.rb', line 30

def [](*args)
  write? ?  nil : super(*args)
end

#[]=(k, v) ⇒ Object



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

def []=(k,v)
  if write?
    add(k,v)
  end
end

#closeObject



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

def close
  self.closed = true
  begin
    fix_io
    super
  rescue
  end
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @closed
end

#delete(key) ⇒ Object



73
74
75
# File 'lib/rbbt/persist/tsv/cdb.rb', line 73

def delete(key)
  out(key)
end

#fix_ioObject



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

def fix_io
  ddd instance_variable_get(:@io)
  if instance_variable_get(:@io) != persistence_path
    instance_variable_set(:@io, File.open(persistence_path))
  end
end

#include?(k) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rbbt/persist/tsv/cdb.rb', line 26

def include?(k)
  not write? and super(k) 
end

#merge!(hash) ⇒ Object



115
116
117
118
119
# File 'lib/rbbt/persist/tsv/cdb.rb', line 115

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

#range(*args) ⇒ Object



122
123
124
# File 'lib/rbbt/persist/tsv/cdb.rb', line 122

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

#read(force = false) ⇒ Object



60
61
62
63
64
# File 'lib/rbbt/persist/tsv/cdb.rb', line 60

def read(force = false)
  self.closed = false
  fix_io
  open_read
end

#read_and_closeObject



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

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

#serializerObject



22
23
24
# File 'lib/rbbt/persist/tsv/cdb.rb', line 22

def serializer
  :clean
end

#write(force = true) ⇒ Object



66
67
68
69
70
71
# File 'lib/rbbt/persist/tsv/cdb.rb', line 66

def write(force = true)
  self.closed = false
  fix_io
  open_write
  self
end

#write_and_closeObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbbt/persist/tsv/cdb.rb', line 92

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rbbt/persist/tsv/cdb.rb', line 77

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
          rescue
            Log.error $!.message
            Log.debug $!.backtrace * "\n"
          ensure
            read  if write?
          end
  end
end