Class: Traildb::TrailDB

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/traildb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ TrailDB

Returns a new instance of TrailDB.

Raises:



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/traildb.rb', line 219

def initialize(path)
  super Traildb.tdb_init()
  ret = Traildb.tdb_open(self, path)
  raise TrailDBError.new("Could not open %s" % path, ret) if ret != 0
  @num_trails = Traildb.tdb_num_trails(self)
  @num_events = Traildb.tdb_num_events(self)
  @num_fields = Traildb.tdb_num_fields(self)
  @fields = @num_fields.times.map{|i|Traildb.tdb_get_field_name(self,i)}
  @event_cls = Struct.new(*@fields.map(&:to_sym))
  @uint64_ptr = FFI::MemoryPointer.new(:uint64)
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



217
218
219
# File 'lib/traildb.rb', line 217

def fields
  @fields
end

#num_eventsObject (readonly)

Returns the value of attribute num_events.



217
218
219
# File 'lib/traildb.rb', line 217

def num_events
  @num_events
end

#num_fieldsObject (readonly)

Returns the value of attribute num_fields.



217
218
219
# File 'lib/traildb.rb', line 217

def num_fields
  @num_fields
end

#num_trailsObject (readonly)

Returns the value of attribute num_trails.



217
218
219
# File 'lib/traildb.rb', line 217

def num_trails
  @num_trails
end

Class Method Details

.release(ptr) ⇒ Object



391
392
393
# File 'lib/traildb.rb', line 391

def self.release(ptr)
  Traildb.tdb_close(ptr)
end

Instance Method Details

#[](uuidish) ⇒ Object



238
239
240
241
242
243
244
# File 'lib/traildb.rb', line 238

def [](uuidish)
  if uuidish.is_a? String
    trail(get_trail_id(uuidish))
  else
    trail(uuidish)
  end
end

#apply_blacklist(uuids) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/traildb.rb', line 374

def apply_blacklist(uuids)
  empty_filter = Traildb.tdb_event_filter_new_match_none()
  all_filter = Traildb.tdb_event_filter_new_match_all()
  value = TdbOptValue.new
  value[:ptr] = all_filter
  Traildb.tdb_set_opt(self, TDB_OPT_EVENT_FILTER, value)
  value[:ptr] = empty_filter
  uuids.each do |uuid|
    begin
      trail_id = get_trail_id(uuid)
      Traildb.tdb_set_trail_opt(self, trail_id, TDB_OPT_EVENT_FILTER, value)
    rescue IndexError
      next
    end
  end
end

#apply_whitelist(uuids) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/traildb.rb', line 357

def apply_whitelist(uuids)
  empty_filter = Traildb.tdb_event_filter_new_match_none()
  all_filter = Traildb.tdb_event_filter_new_match_all()
  value = TdbOptValue.new
  value[:ptr] = empty_filter
  Traildb.tdb_set_opt(self, TDB_OPT_EVENT_FILTER, value)
  value[:ptr] = all_filter
  uuids.each do |uuid|
    begin
      trail_id = get_trail_id(uuid)
      Traildb.tdb_set_trail_opt(self, trail_id, TDB_OPT_EVENT_FILTER, value)
    rescue IndexError
      next
    end
  end
end

#create_filter(event_filter) ⇒ Object



353
354
355
# File 'lib/traildb.rb', line 353

def create_filter(event_filter)
  TrailDBEventFilter.new(self, event_filter)
end

#field(fieldish) ⇒ Object



283
284
285
286
287
288
# File 'lib/traildb.rb', line 283

def field(fieldish)
  if fieldish.is_a? String
    fieldish = @fields.index(fieldish)
  end
  fieldish
end

#get_item(fieldish, value) ⇒ Object

Raises:



304
305
306
307
308
309
# File 'lib/traildb.rb', line 304

def get_item(fieldish, value)
  field = field(fieldish)
  item = Traildb.tdb_get_item(self, field, value, value.size)
  raise TrailDBError.new("No such value: '%s'" % value) if item.nil?
  item
end

#get_item_value(item) ⇒ Object

Raises:



311
312
313
314
315
# File 'lib/traildb.rb', line 311

def get_item_value(item)
  value = Traildb.tdb_get_item_value(self, item, @uint64_ptr)
  raise TrailDBError.new("Error reading value") if value.nil?
  value.slice(0, @uint64_ptr.read_uint64)
end

#get_trail_id(uuid) ⇒ Object

Raises:

  • (::IndexError)


333
334
335
336
337
# File 'lib/traildb.rb', line 333

def get_trail_id(uuid)
  ret = Traildb.tdb_get_trail_id(self, Traildb.uuid_raw(uuid), @uint64_ptr)
  raise ::IndexError if ret != 0
  @uint64_ptr.read_uint64
end

#get_uuid(trail_id, raw = false) ⇒ Object



324
325
326
327
328
329
330
331
# File 'lib/traildb.rb', line 324

def get_uuid(trail_id, raw=false)
  uuid = Traildb.tdb_get_uuid(self, trail_id)
  if uuid.nil?
    raise ::IndexError
  else
    raw ? uuid.read_string : Traildb.uuid_hex(uuid)
  end
end

#get_value(fieldish, val) ⇒ Object

Raises:



317
318
319
320
321
322
# File 'lib/traildb.rb', line 317

def get_value(fieldish, val)
  field = field(fieldish)
  value = Traildb.tdb_get_value(self, field, val, @uint64_ptr)
  raise TrailDBError.new("Error reading value") if value.nil?
  value.slice(0, @uint64_ptr.read_uint64)
end

#include?(uuidish) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
234
235
236
# File 'lib/traildb.rb', line 231

def include?(uuidish)
  self[uuidish]
  true
rescue IndexError
  false
end

#lexicon(fieldish) ⇒ Object



290
291
292
293
294
295
# File 'lib/traildb.rb', line 290

def lexicon(fieldish)
  field = field(fieldish)
  (1..lexicon_size(field)-1).lazy.map{|i|
    get_value(field, i)
  }
end

#lexicon_size(fieldish) ⇒ Object

Raises:



297
298
299
300
301
302
# File 'lib/traildb.rb', line 297

def lexicon_size(fieldish)
  field = field(fieldish)
  value = Traildb.tdb_lexicon_size(self, field)
  raise TrailDBError.new("Invalid field index") if value == 0
  value
end

#max_timestampObject



349
350
351
# File 'lib/traildb.rb', line 349

def max_timestamp
  Traildb.tdb_max_timestamp(self)
end

#min_timestampObject



345
346
347
# File 'lib/traildb.rb', line 345

def min_timestamp
  Traildb.tdb_min_timestamp(self)
end

#sizeObject



246
247
248
# File 'lib/traildb.rb', line 246

def size
  @num_trails
end

#time_range(parsetime: false) ⇒ Object



339
340
341
342
343
# File 'lib/traildb.rb', line 339

def time_range(parsetime: false)
  tmin = min_timestamp
  tmax = max_timestamp
  parsetime ? [Time.at(tmin), Time.at(tmax)] : [tmin, tmax]
end

#trail(trail_id, parsetime: false, rawitems: false, only_timestamp: false, event_filter: nil) ⇒ Object

Raises:



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/traildb.rb', line 267

def trail(trail_id, parsetime: false, rawitems: false, only_timestamp: false, event_filter: nil)
  cursor = Traildb.tdb_cursor_new(self)
  ret = Traildb.tdb_get_trail(cursor, trail_id)
  raise TrailDBError.new("Failed to create cursor", ret) if ret != 0
  valuefun = rawitems ? nil : ->(item){get_item_value(item)}
  event_filter_obj = case event_filter
    when TrailDBEventFilter
      event_filter
    when Array
      create_filter(event_filter)
    else
      nil
  end
  TrailDBCursor.new(cursor, @event_cls, valuefun, parsetime, only_timestamp, event_filter_obj)
end

#trails(selected_uuids: nil, **kwds) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/traildb.rb', line 250

def trails(selected_uuids: nil, **kwds)
  if selected_uuids.nil?
    size.times.each.lazy.map do |i|
      [get_uuid(i), trail(i, kwds)]
    end
  else
    selected_uuids.each.lazy.map do |uuid|
      begin
        i = get_trail_id(uuid)
      rescue IndexError
        next
      end
      [uuid, trail(i, kwds)]
    end
  end
end