Example #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
}
Example #2
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
}