Module: TSVAdapter
Defined Under Namespace
Classes: BinarySerializer, CleanSerializer, FloatArraySerializer, FloatSerializer, IntegerArraySerializer, IntegerSerializer, StrictFloatArraySerializer, StrictIntegerArraySerializer, StringArraySerializer, StringDoubleArraySerializer, StringSerializer, TSVMarshalSerializer, TSVSerializer
Constant Summary
collapse
- SERIALIZER_ALIAS =
{
:single => StringSerializer,
:list => StringArraySerializer,
:flat => StringArraySerializer,
:double => StringDoubleArraySerializer,
:clean => CleanSerializer,
:integer => IntegerSerializer,
:float => FloatSerializer,
:integer_array => IntegerArraySerializer,
:float_array => FloatArraySerializer,
:strict_integer_array => StrictIntegerArraySerializer,
:strict_float_array => StrictFloatArraySerializer,
:marshal => Marshal,
:json => JSON,
:string => StringSerializer,
:binary => BinarySerializer,
:tsv => TSVSerializer,
:marshal_tsv => TSVMarshalSerializer
}
- ANNOTATION_ATTR_HASH_KEY =
"__annotation_hash__"
- ANNOTATION_ATTR_HASH_SERIALIZER =
Marshal
- MAX_CHAR =
255.chr
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.lock_dir ⇒ Object
Returns the value of attribute lock_dir.
8
9
10
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 8
def lock_dir
@lock_dir
end
|
Instance Attribute Details
#closed ⇒ Object
Returns the value of attribute closed.
5
6
7
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5
def closed
@closed
end
|
#persistence_class ⇒ Object
Returns the value of attribute persistence_class.
5
6
7
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5
def persistence_class
@persistence_class
end
|
#persistence_path ⇒ Object
Returns the value of attribute persistence_path.
5
6
7
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5
def persistence_path
@persistence_path
end
|
#serializer ⇒ Object
Returns the value of attribute serializer.
5
6
7
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5
def serializer
@serializer
end
|
#writable ⇒ Object
Returns the value of attribute writable.
5
6
7
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5
def writable
@writable
end
|
Class Method Details
.extended(base) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 32
def self.extended(base)
if TSV === base
base.instance_variable_get(:@annotations).push(:serializer)
base.serializer = SERIALIZER_ALIAS[base.type] if base.serializer.nil?
base.save_annotation_hash
else
begin
TSV.setup(base, base.load_annotation_hash)
base.filename = base.persistence_path if base.filename.nil?
rescue
TSV.setup(base)
base.save_annotation_hash
end
end
end
|
.serializer_module(serializer) ⇒ Object
118
119
120
|
# File 'lib/scout/persist/tsv/serialize.rb', line 118
def self.serializer_module(serializer)
Symbol === serializer ? SERIALIZER_ALIAS[serializer] : serializer
end
|
Instance Method Details
#[](key, clean = false) ⇒ Object
49
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 49
def [](...) super(...); end
|
#[]=(key, value, clean = false) ⇒ Object
48
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 48
def []=(...) super(...); end
|
#close(*args) ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 171
def close(*args)
begin
super(*args)
@closed = true
rescue NoMethodError
end
self
end
|
#closed? ⇒ Boolean
147
148
149
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 147
def closed?
@closed
end
|
#collect(&block) ⇒ Object
Also known as:
map
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 127
def collect(&block)
res = []
if block_given?
each do |k,v|
res << yield(k, v)
end
else
each do |k,v|
res << [k, v]
end
end
res
end
|
#delete(key) ⇒ Object
187
188
189
190
191
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 187
def delete(key)
self.write_lock do
out(key)
end
end
|
#each_annotation_hash_key(&block) ⇒ Object
Also known as:
each
108
109
110
111
112
113
114
115
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 108
def each_annotation_hash_key(&block)
self.read_lock do
orig_each do |k,v|
next if k == ANNOTATION_ATTR_HASH_KEY
yield(k, load_value(v))
end
end
end
|
#get_prefix(key) ⇒ Object
353
354
355
356
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 353
def get_prefix(key)
keys = prefix(key)
select(:key => keys)
end
|
#include?(*args) ⇒ Boolean
339
340
341
342
343
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 339
def include?(*args)
self.read_lock do
super(*args)
end
end
|
#keys_annotation_hash_key(*args) ⇒ Object
Also known as:
keys
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 93
def keys_annotation_hash_key(*args)
k = self.read_lock do
orig_keys(*args)
end
if k[0] == ANNOTATION_ATTR_HASH_KEY
k.slice(1,k.length)
elsif k[-1] == ANNOTATION_ATTR_HASH_KEY
k.slice(0,k.length-1)
else
k - [ANNOTATION_ATTR_HASH_KEY]
end
end
|
#load_annotation_hash ⇒ Object
21
22
23
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 21
def load_annotation_hash
ANNOTATION_ATTR_HASH_SERIALIZER.load(StringIO.new(self.orig_get(ANNOTATION_ATTR_HASH_KEY)))
end
|
#load_value(str) ⇒ Object
80
81
82
83
84
85
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 80
def load_value(str)
return nil if str.nil?
return str if serializer.nil?
return load_value(str.first) if Array === str
serializer.load(str)
end
|
#lock ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 193
def lock
return yield if @locked
lock_filename = Persist.persistence_path(persistence_path, {:dir => TSVAdapter.lock_dir})
Open.lock(lock_filename) do
begin
@locked = true
yield
ensure
@locked = false
end
end
end
|
#lock_and_close ⇒ Object
206
207
208
209
210
211
212
213
214
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 206
def lock_and_close
lock do
begin
yield
ensure
close
end
end
end
|
#merge!(hash) ⇒ Object
323
324
325
326
327
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 323
def merge!(hash)
hash.each do |key,values|
self[key] = values
end
end
|
#orig_get ⇒ Object
55
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 55
def [](...) super(...); end
|
#orig_set ⇒ Object
54
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 54
def []=(...) super(...); end
|
#prefix(key) ⇒ Object
347
348
349
350
351
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 347
def prefix(key)
self.read_lock do
range(key, 1, key + MAX_CHAR, 1)
end
end
|
#range(*args) ⇒ Object
329
330
331
332
333
334
335
336
337
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 329
def range(*args)
begin
self.read_lock do
super(*args)
end
rescue
[]
end
end
|
#read(*args) ⇒ Object
180
181
182
183
184
185
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 180
def read(*args)
begin
super(*args)
rescue NoMethodError
end
end
|
#read? ⇒ Boolean
155
156
157
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 155
def read?
! (write? || closed?)
end
|
#read_and_close ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 274
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_lock ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 293
def read_lock
read if closed?
if read? || write?
return yield
end
lock do
close
read true
begin
yield
end
end
end
|
#save_annotation_hash ⇒ Object
25
26
27
28
29
30
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 25
def save_annotation_hash
self.close
self.with_write do
self.orig_set(ANNOTATION_ATTR_HASH_KEY, ANNOTATION_ATTR_HASH_SERIALIZER.dump(self.annotation_hash))
end
end
|
#save_value(value) ⇒ Object
87
88
89
90
91
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 87
def save_value(value)
return nil if value.nil?
return value if serializer.nil?
serializer.dump(value)
end
|
#size_annotation_hash_key(*args) ⇒ Object
Also known as:
size
118
119
120
121
122
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 118
def size_annotation_hash_key(*args)
self.read_lock do
orig_size(*args) - 1
end
end
|
#values ⇒ Object
141
142
143
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 141
def values
collect{|k,v| v }
end
|
#values_at(*keys) ⇒ Object
358
359
360
361
362
363
364
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 358
def values_at(*keys)
self.read_lock do
keys.collect do |k|
self[k]
end
end
end
|
#with_read(&block) ⇒ Object
254
255
256
257
258
259
260
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 254
def with_read(&block)
if read? || write?
return yield
else
read_and_close &block
end
end
|
#with_write(&block) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 262
def with_write(&block)
if write?
return yield
else
if self.read?
self.write_and_read(&block)
else
self.write_and_close(&block)
end
end
end
|
#write(*args) ⇒ Object
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 159
def write(*args)
begin
begin
super(*args)
rescue
end
@writable = true
rescue NoMethodError
end
end
|
#write? ⇒ Boolean
151
152
153
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 151
def write?
@writable
end
|
#write_and_close ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 235
def write_and_close
if write?
begin
return yield
ensure
close unless @locked
end
end
lock do
write(true) if closed? || ! write?
begin
yield
ensure
close
end
end
end
|
#write_and_read ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 216
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_lock ⇒ Object
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# File 'lib/scout/persist/tsv/adapter/base.rb', line 308
def write_lock
write if closed?
if write?
return yield
end
lock do
close
write true
begin
yield
end
end
end
|