Пример #1
0
func (db *TrailDB) NewEventFilter(query [][]FilterTerm) *EventFilter {
	filter := EventFilter{filter: C.tdb_event_filter_new()}
	for i, clause := range query {
		if i > 0 {
			err := C.tdb_event_filter_new_clause(filter.filter)
			if err != 0 {
				return nil
			}
		}
		for _, term := range clause {
			item := C.tdb_item(0)
			field_id, err := db.GetField(term.Field)
			if err == nil {
				cs := C.CString(term.Value)
				defer C.free(unsafe.Pointer(cs))
				item = C.tdb_get_item(db.db,
					C.tdb_field(field_id),
					cs,
					C.uint64_t(len(term.Value)))
			}
			isNegative := C.int(0)
			if term.IsNegative {
				isNegative = 1
			}
			ret := C.tdb_event_filter_add_term(filter.filter, item, isNegative)
			if ret != 0 {
				return nil
			}
		}
	}
	return &filter
}
Пример #2
0
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
}
Пример #3
0
func (db *TrailDB) GetField(field_name string) (uint64, error) {
	field := C.tdb_field(0)
	err := C.tdb_get_field(db.db, C.CString(field_name), &field)
	if err != 0 {
		return 0, errors.New(errToString(err))
	}
	return uint64(field), nil
}
Пример #4
0
func (db *TrailDB) FindTrails(filters map[string]string) ([]*Trail, error) {
	var items []C.tdb_item

	for k, v := range filters {
		cs := C.CString(v)
		defer C.free(unsafe.Pointer(cs))

		item := C.tdb_get_item(db.db, C.tdb_field(db.fieldNameToId[k]), cs, C.uint64_t(len(v)))
		items = append(items, item)
	}

	var result []*Trail
	for i := uint64(0); i < db.NumTrails; i++ {
		trail, err := NewTrail(db, i)
		if err != nil {
			return nil, err
		}
		for {
			evt := trail.NextEvent()
			if evt == nil {
				trail.Close()
				break
			}
			if evt.contains(items) {
				returnTrail, err := NewTrail(db, i)
				if err != nil {
					return nil, err
				}
				result = append(result, returnTrail)
				trail.Close()
				break
			}
		}
	}
	return result, nil
}