Module: Persist::TSVAdapter

Included in:
CDBAdapter, FWTAdapter, KCAdapter, LMDBAdapter, PKIAdapter, TCAdapter
Defined in:
lib/rbbt/persist/tsv/adapter.rb

Constant Summary collapse

MAX_CHAR =
255.chr

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#closedObject

Returns the value of attribute closed.



4
5
6
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4

def closed
  @closed
end

#mutexObject

Returns the value of attribute mutex.



4
5
6
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4

def mutex
  @mutex
end

#persistence_pathObject

Returns the value of attribute persistence_path.



4
5
6
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4

def persistence_path
  @persistence_path
end

#writableObject

Returns the value of attribute writable.



4
5
6
# File 'lib/rbbt/persist/tsv/adapter.rb', line 4

def writable
  @writable
end

Instance Method Details

#[](*args) ⇒ Object



213
214
215
216
217
# File 'lib/rbbt/persist/tsv/adapter.rb', line 213

def [](*args)
  self.read_lock do
    super(*args) #- TSV::ENTRY_KEYS.to_a
  end
end

#[]=(*args) ⇒ Object



219
220
221
222
223
# File 'lib/rbbt/persist/tsv/adapter.rb', line 219

def []=(*args)
  self.write_lock do
    super(*args) #- TSV::ENTRY_KEYS.to_a
  end
end

#close(*args) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rbbt/persist/tsv/adapter.rb', line 32

def close(*args)
  begin
    super(*args)
    @closed = true
  rescue NoMethodError
  end
  self
end

#closed?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/rbbt/persist/tsv/adapter.rb', line 12

def closed?
  @closed
end

#collectObject



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rbbt/persist/tsv/adapter.rb', line 256

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

#delete(key) ⇒ Object



48
49
50
51
52
# File 'lib/rbbt/persist/tsv/adapter.rb', line 48

def delete(key)
  self.write_lock do
    out(key)
  end
end

#each(*args, &block) ⇒ Object



250
251
252
253
254
# File 'lib/rbbt/persist/tsv/adapter.rb', line 250

def each(*args, &block)
  self.read_lock do
    super(*args, &block)
  end
end

#get_prefix(key) ⇒ Object



238
239
240
241
# File 'lib/rbbt/persist/tsv/adapter.rb', line 238

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

#include?(*args) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
# File 'lib/rbbt/persist/tsv/adapter.rb', line 207

def include?(*args)
  self.read_lock do
    super(*args) #- TSV::ENTRY_KEYS.to_a
  end
end

#keys(*args) ⇒ Object



225
226
227
228
229
# File 'lib/rbbt/persist/tsv/adapter.rb', line 225

def keys(*args)
  self.read_lock do
    super(*args)
  end
end

#lockObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rbbt/persist/tsv/adapter.rb', line 54

def lock
  return yield if @locked
  lock_filename = Persist.persistence_path(persistence_path, {:dir => TSV.lock_dir})
  Misc.lock(lock_filename) do
    begin
      @locked = true
      yield
    ensure
      @locked = false
    end
  end
end

#lock_and_closeObject



67
68
69
70
71
72
73
74
75
# File 'lib/rbbt/persist/tsv/adapter.rb', line 67

def lock_and_close
  lock do
    begin
      yield
    ensure
      close
    end
  end
end

#merge!(hash) ⇒ Object



191
192
193
194
195
# File 'lib/rbbt/persist/tsv/adapter.rb', line 191

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

#prefix(key) ⇒ Object



232
233
234
235
236
# File 'lib/rbbt/persist/tsv/adapter.rb', line 232

def prefix(key)
  self.read_lock do
    range(key, 1, key + MAX_CHAR, 1)
  end
end

#range(*args) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/rbbt/persist/tsv/adapter.rb', line 197

def range(*args)
  begin
    self.read_lock do
      super(*args)
    end
  rescue
    []
  end
end

#read(*args) ⇒ Object



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

def read(*args)
  begin
    super(*args)
  rescue NoMethodError
  end
end

#read?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rbbt/persist/tsv/adapter.rb', line 20

def read?
  ! (write? || closed?)
end

#read_and_closeObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rbbt/persist/tsv/adapter.rb', line 141

def read_and_close
  if read? || write?
    begin
      return yield
    ensure
      close unless @locked
    end
  end

  lock do
    read true if closed? || ! read?
    begin
      yield
    ensure
      close
    end
  end
end

#read_lockObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbbt/persist/tsv/adapter.rb', line 160

def read_lock
  read if closed?
  if read? || write?
    return yield
  end

  lock do
    close
    read true
    begin
      yield
    end
  end
end

#size(*args) ⇒ Object



244
245
246
247
248
# File 'lib/rbbt/persist/tsv/adapter.rb', line 244

def size(*args)
  self.read_lock do
    super(*args)
  end
end

#values_at(*keys) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/rbbt/persist/tsv/adapter.rb', line 268

def values_at(*keys)
  self.read_lock do
    keys.collect do |k|
      self[k]
    end
  end
end

#with_read(&block) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/rbbt/persist/tsv/adapter.rb', line 116

def with_read(&block)
  if read? || write?
    return yield
  else
    read_and_close &block
  end
end

#with_write(&block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rbbt/persist/tsv/adapter.rb', line 124

def with_write(&block)
  if write?
    return yield
  else
    if self.read?
      self.write_and_read do
        return yield
      end
    else
      self.write_and_close do
        return yield
      end
    end
  end
end

#write(*args) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rbbt/persist/tsv/adapter.rb', line 24

def write(*args)
  begin
    super(*args)
    @writable = true
  rescue NoMethodError
  end
end

#write?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rbbt/persist/tsv/adapter.rb', line 16

def write?
  @writable
end

#write_and_closeObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rbbt/persist/tsv/adapter.rb', line 96

def write_and_close
  if write?
    begin
      return yield
    ensure
      close unless @locked
    end
  end

  lock do
    write(true) if closed? || ! 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
91
92
93
94
# File 'lib/rbbt/persist/tsv/adapter.rb', line 77

def write_and_read
  if write?
    begin
      return yield
    ensure
      read
    end
  end

  lock do
    write(true) if closed? || !write?
    begin
      yield
    ensure
      read
    end
  end
end

#write_lockObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rbbt/persist/tsv/adapter.rb', line 175

def write_lock
  write if closed?
  if write?
    return yield
  end

  lock do
    close
    write true
    begin
      yield
    end
  end
end