示例#1
0
func Search(index core.Uint32Index, buffer dbio.DataBuffer, key, value string) ([]*core.Record, error) {
	results := []*core.Record{}
	index.All(func(id uint32, rowID core.RowID) {
		record, err := core.NewRecordLoader(buffer).Load(id, rowID)
		if err != nil {
			// Ideally should recover but that means lots of stuff needs to be changed
			panic(err)
		}

		json, err := record.ParseJSON()
		if err != nil {
			panic(err)
		}

		jsonValue, ok := json[key]
		if !ok {
			return
		}
		if jsonValue == value {
			results = append(results, record)
		}
	})

	return results, nil
}
func assertIndexItemsAreSame(t *testing.T, index core.Uint32Index, rowIDs []core.RowID) {
	i := 0
	funcCalled := false
	index.All(func(_ uint32, rowID core.RowID) {
		funcCalled = true
		if rowID != rowIDs[i] {
			t.Errorf("Invalid value returned from the index at %d: %+v (expected %+v)", i, rowID, rowIDs[i])
		}
		i++
	})
	if i != len(rowIDs) {
		t.Errorf("Did not iterate over all entries")
	}
	if !funcCalled && len(rowIDs) > 0 {
		t.Errorf("Function passed to core.Uint32Index was not called")
	}
}