func Open(s string) (*TrailDB, error) { ok, er := exists(s) if er != nil { return nil, er } if !ok { return nil, errors.New(s + ": Path doesn't exist") } db := C.tdb_init() cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) err := C.tdb_open(db, cs) if err != 0 { return nil, errors.New(s + ": Failed to open traildb: " + errToString(err)) } numFields := uint64(C.tdb_num_fields(db)) var fields []string fieldNameToId := make(map[string]uint64) // numFields contains timestamp too for i := uint64(0); i <= uint64(numFields)-1; i++ { fieldName := C.GoString(C.tdb_get_field_name(db, C.tdb_field(i))) fieldNameToId[fieldName] = uint64(i) fields = append(fields, fieldName) } return &TrailDB{ db: db, NumTrails: uint64(C.tdb_num_trails(db)), NumEvents: uint64(C.tdb_num_events(db)), NumFields: numFields, minTimestamp: uint64(C.tdb_min_timestamp(db)), maxTimestamp: uint64(C.tdb_max_timestamp(db)), fieldNames: fields, fieldNameToId: fieldNameToId, }, nil }
func (evt *Event) ToMap() map[string]string { fields := make(map[string]string) var vlength C.uint64_t for _, item := range evt.items { itemValue := C.tdb_get_item_value(evt.trail.db.db, item, &vlength) value := C.GoStringN(itemValue, C.int(vlength)) key := C.GoString(C.tdb_get_field_name(evt.trail.db.db, C.tdb_item_field(item))) fields[key] = value } return fields }