Module: Persist::CDBAdapter

Defined in:
lib/rbbt/persist/tsv/cdb.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/cdb.rb', line 6

def closed
  @closed
end

#persistence_pathObject

Returns the value of attribute persistence_path.



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

def persistence_path
  @persistence_path
end

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.exists? 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



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

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

#[]=(k, v) ⇒ Object



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

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

#closeObject



48
49
50
51
52
53
54
55
# File 'lib/rbbt/persist/tsv/cdb.rb', line 48

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @closed
end

#delete(key) ⇒ Object



70
71
72
# File 'lib/rbbt/persist/tsv/cdb.rb', line 70

def delete(key)
  out(key)
end

#fix_ioObject



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

def fix_io
  if instance_variable_get(:@io) != persistence_path
    #close_read if read?
    #close_write if write?
    instance_variable_set(:@io, File.open(persistence_path))
  end
end

#include?(k) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#merge!(hash) ⇒ Object



112
113
114
115
116
# File 'lib/rbbt/persist/tsv/cdb.rb', line 112

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

#range(*args) ⇒ Object



119
120
121
# File 'lib/rbbt/persist/tsv/cdb.rb', line 119

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

#read(force = false) ⇒ Object



57
58
59
60
61
# File 'lib/rbbt/persist/tsv/cdb.rb', line 57

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

#read_and_closeObject



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

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

#write(force = true) ⇒ Object



63
64
65
66
67
68
# File 'lib/rbbt/persist/tsv/cdb.rb', line 63

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

#write_and_closeObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rbbt/persist/tsv/cdb.rb', line 89

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



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

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