github.com/traildb/traildb-crystal
0.2.1 / published Jan 20, 2020 / repository
Crystal bindings for TrailDB
TrailDB Crystal Bindings

Official Crystal bindings for TrailDB, an efficient tool for storing and querying series of events. This library is heavily inspired by the official Python bindings. Crystal is an excellent language for TrailDB due to its simplicity and blazing fast performance. Crystal + TrailDB = ❤️.
Install
- Install
traildbto your system. You can install the binaries for your system or compile it from source. Detailed instructions here. - Add this to your
shard.yml
dependencies:
traildb:
github: traildb/traildb-crystal
- Install using
crystal deps
Examples
Constructing a TrailDB
Code
require "traildb"
cons = TrailDBConstructor.new("testtrail.tdb", ["field1", "field2"])
uuid = "12345678123456781234567812345678"
cons.add(uuid, Time.new(2017, 11, 12, 1, 1), ["a", "1"])
cons.add(uuid, Time.new(2017, 11, 13, 1, 1), ["b", "2"])
cons.add(uuid, Time.new(2017, 11, 14, 1, 1), ["c", "3"])
cons.close
Loading all trails
Code
require "traildb"
traildb = TrailDB.new("testtrail.tdb")
puts "Number of trails: #{traildb.num_trails}"
puts "Number of fields: #{traildb.num_fields}"
traildb.trails.each do |(uuid, trail)|
puts "Events for trail #{uuid}"
trail.each do |event|
# Access event items by key
puts event["field1"]
# Or get the full hash
puts event.to_h
end
end
Output
Number of trails: 1
Number of fields: 3
Events for trail 12345678123456781234567812345678
a
{"field1" => "a", "field2" => "1", "time" => 2017-11-12 06:01:00 UTC}
b
{"field1" => "b", "field2" => "2", "time" => 2017-11-13 06:01:00 UTC}
c
{"field1" => "c", "field2" => "3", "time" => 2017-11-14 06:01:00 UTC}
Loading all events in a trail
Code
events = traildb[0].to_a
# or
events = traildb["12345678123456781234567812345678"].to_a
Applying an Event Filter
Code
require "traildb"
event_filter = traildb.create_filter([[{"field1", "a"}]])
traildb.event_filter = event_filter
traildb.trails.each do |(uuid, trail)|
puts "Events for trail #{uuid}"
trail.each do |event|
puts event.to_h
end
end
Output
Events for trail 12345678123456781234567812345678
{"field1" => "a", "field2" => "1", "time" => 2017-11-12 06:01:00 UTC}
For more examples, check out the specs for the library at spec/traildb_spec.cr.
License
These bindings are licensed under the MIT license.
API
- Tdb
C Library type aliases
- TdbChar
- TdbCons
- TdbCursor
- TdbError
- TdbEventFilter
- TdbField
- TdbItem
- TdbVal
- TrailDB
Load a TrailDB, including metadata and a trails iterator
- TrailDBConstructor
Construct a new TrailDB.
- TrailDBEvent
Wrapper struct for TrailDB events.
- TrailDBEventFilter
Create a TrailDB filter from a series of clauses
- TrailDBEventFilterClause
Clause in an event filter
- TrailDBEventFilterQuery
Full event filter, to be parsed into TrailDBEventFilter
- TrailDBEventFilterTerm
Combined single term of an event filter.
- TrailDBEventFilterTermWithNegation
Single term of an event filter.
- TrailDBEventFilterTermWithoutNegation
Single term of an event filter.
- TrailDBEventHash
TrailDB Event, mapping field => value
- TrailDBEventIterator
- TrailDBEventIteratorWithUUID
Returned when iterating over all trails uuid can be accessed by destructuring in the loop e.g.
- TrailDBException
Generic TrailDB exception, used all the place
- TrailDBField
Generic field for all field-like objects.
- TrailDBLexiconIterator
- TrailDBTrailIterator