Module: TSVAdapter

Included in:
FWTAdapter, PKIAdapter, ShardAdapter, TKAdapter
Defined in:
lib/scout/persist/tsv/serialize.rb,
lib/scout/persist/tsv/adapter/base.rb

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_dirObject

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

#closedObject

Returns the value of attribute closed.



5
6
7
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5

def closed
  @closed
end

#persistence_classObject

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_pathObject

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

#serializerObject

Returns the value of attribute serializer.



5
6
7
# File 'lib/scout/persist/tsv/adapter/base.rb', line 5

def serializer
  @serializer
end

#writableObject

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/scout/persist/tsv/adapter/base.rb', line 29

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)
    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



45
# File 'lib/scout/persist/tsv/adapter/base.rb', line 45

def [](...) super(...); end

#[]=(key, value, clean = false) ⇒ Object



44
# File 'lib/scout/persist/tsv/adapter/base.rb', line 44

def []=(...) super(...); end

#close(*args) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/scout/persist/tsv/adapter/base.rb', line 166

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

#closed?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/scout/persist/tsv/adapter/base.rb', line 143

def closed?
  @closed
end

#collect(&block) ⇒ Object Also known as: map



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/scout/persist/tsv/adapter/base.rb', line 123

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



182
183
184
185
186
# File 'lib/scout/persist/tsv/adapter/base.rb', line 182

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

#each_annotation_hash_key(&block) ⇒ Object Also known as: each



104
105
106
107
108
109
110
111
# File 'lib/scout/persist/tsv/adapter/base.rb', line 104

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



354
355
356
357
# File 'lib/scout/persist/tsv/adapter/base.rb', line 354

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

#include?(*args) ⇒ Boolean

Returns:

  • (Boolean)


340
341
342
343
344
# File 'lib/scout/persist/tsv/adapter/base.rb', line 340

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

#keys_annotation_hash_key(*args) ⇒ Object Also known as: keys



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/scout/persist/tsv/adapter/base.rb', line 89

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_hashObject



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



76
77
78
79
80
81
# File 'lib/scout/persist/tsv/adapter/base.rb', line 76

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

#lockObject



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/scout/persist/tsv/adapter/base.rb', line 188

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_closeObject



201
202
203
204
205
206
207
208
209
# File 'lib/scout/persist/tsv/adapter/base.rb', line 201

def lock_and_close
  lock do
    begin
      yield
    ensure
      close
    end
  end
end

#merge!(hash) ⇒ Object



324
325
326
327
328
# File 'lib/scout/persist/tsv/adapter/base.rb', line 324

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

#orig_getObject



51
# File 'lib/scout/persist/tsv/adapter/base.rb', line 51

def [](...) super(...); end

#orig_setObject



50
# File 'lib/scout/persist/tsv/adapter/base.rb', line 50

def []=(...) super(...); end

#prefix(key) ⇒ Object



348
349
350
351
352
# File 'lib/scout/persist/tsv/adapter/base.rb', line 348

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

#range(*args) ⇒ Object



330
331
332
333
334
335
336
337
338
# File 'lib/scout/persist/tsv/adapter/base.rb', line 330

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

#read(*args) ⇒ Object



175
176
177
178
179
180
# File 'lib/scout/persist/tsv/adapter/base.rb', line 175

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

#read?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/scout/persist/tsv/adapter/base.rb', line 151

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

#read_and_closeObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/scout/persist/tsv/adapter/base.rb', line 275

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/scout/persist/tsv/adapter/base.rb', line 294

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

  lock do
    close
    read true
    begin
      yield
    end
  end
end

#save_annotation_hashObject



25
26
27
# File 'lib/scout/persist/tsv/adapter/base.rb', line 25

def save_annotation_hash
  self.orig_set(ANNOTATION_ATTR_HASH_KEY, ANNOTATION_ATTR_HASH_SERIALIZER.dump(self.annotation_hash))
end

#save_value(value) ⇒ Object



83
84
85
86
87
# File 'lib/scout/persist/tsv/adapter/base.rb', line 83

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



114
115
116
117
118
# File 'lib/scout/persist/tsv/adapter/base.rb', line 114

def size_annotation_hash_key(*args)
  self.read_lock do
    orig_size(*args) - 1 # Discount the ANNOTATION_ATTR_HASH_KEY
  end
end

#valuesObject



137
138
139
# File 'lib/scout/persist/tsv/adapter/base.rb', line 137

def values
  collect{|k,v| v }
end

#values_at(*keys) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/scout/persist/tsv/adapter/base.rb', line 359

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

#with_read(&block) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/scout/persist/tsv/adapter/base.rb', line 250

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

#with_write(&block) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/scout/persist/tsv/adapter/base.rb', line 258

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



155
156
157
158
159
160
161
162
163
164
# File 'lib/scout/persist/tsv/adapter/base.rb', line 155

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

#write?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/scout/persist/tsv/adapter/base.rb', line 147

def write?
  @writable
end

#write_and_closeObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/scout/persist/tsv/adapter/base.rb', line 230

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/scout/persist/tsv/adapter/base.rb', line 211

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



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/scout/persist/tsv/adapter/base.rb', line 309

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

  lock do
    close
    write true
    begin
      yield
    end
  end
end