Beispiel #1
0
func (i *Index) FilterTermID(field, value []byte, limit uint64) ([]uint64, uint64, error) {
	cmd := engine.Command{}
	cmd.Index = i.Name
	// TODO: implement search for every type
	cmd.Database = utils.FieldNorm(string(field)) + "_string.idx"
	cmd.Command = "get"
	cmd.Key = value
	cmd.KeyType = engine.TypeString
	data, err := i.engine.Execute(cmd)

	if err != nil {
		return nil, 0, err
	}

	dataLimit := uint64(len(data) / 8)
	total := dataLimit

	if limit > 0 && limit < dataLimit {
		dataLimit = limit
	}

	docIDs := make([]uint64, dataLimit)

	if len(data) > 0 {
		for i, j := uint64(0), uint64(0); i < dataLimit*8; i, j = i+8, j+1 {
			v := utils.BytesToUint64(data[i : i+8])
			docIDs[j] = v
		}

	}

	return docIDs, total, nil
}
Beispiel #2
0
func (i *Index) matchPrefix(field []byte, value []byte) ([]uint64, error) {
	var (
		docIDs []uint64
	)

	// TODO: Implement search for all of field types
	storekv, err := i.engine.GetStore(i.Name, utils.FieldNorm(string(field))+"_string.idx")

	if err != nil {
		return nil, err
	}

	it := storekv.GetIterator()

	defer it.Close()

	for it.Seek(value); it.Valid(); it.Next() {
		if bytes.HasPrefix(it.Key(), value) {
			var ids []uint64
			dataBytes := it.Value()

			if len(dataBytes) == 0 {
				continue
			}

			for i := 0; i < len(dataBytes); i += 8 {
				v := utils.BytesToUint64(dataBytes[i : i+8])
				ids = append(ids, v)
			}

			if len(docIDs) == 0 {
				docIDs = ids
				continue
			}

			for _, id := range ids {
				docIDs = utils.UniqueUint64Add(docIDs, id)
			}
		}
	}

	if err := it.GetError(); err != nil {
		return nil, err
	}

	return docIDs, nil
}