class Traildb::TrailDB

Attributes

fields[R]
num_events[R]
num_fields[R]
num_trails[R]

Public Class Methods

new(path) click to toggle source
Calls superclass method
# 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
release(ptr) click to toggle source
# File lib/traildb.rb, line 391
def self.release(ptr)
  Traildb.tdb_close(ptr)
end

Public Instance Methods

[](uuidish) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/traildb.rb, line 353
def create_filter(event_filter)
  TrailDBEventFilter.new(self, event_filter)
end
field(fieldish) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/traildb.rb, line 231
def include?(uuidish)
  self[uuidish]
  true
rescue IndexError
  false
end
lexicon(fieldish) click to toggle source
# 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) click to toggle source
# 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_timestamp() click to toggle source
# File lib/traildb.rb, line 349
def max_timestamp
  Traildb.tdb_max_timestamp(self)
end
min_timestamp() click to toggle source
# File lib/traildb.rb, line 345
def min_timestamp
  Traildb.tdb_min_timestamp(self)
end
size() click to toggle source
# File lib/traildb.rb, line 246
def size
  @num_trails
end
time_range(parsetime: false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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