예제 #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
}
예제 #2
0
func Find(index core.Uint32Index, buffer dbio.DataBuffer, id uint32) (*core.Record, error) {
	rowID, err := index.Find(id)
	if err != nil {
		return nil, err
	}

	return core.NewRecordLoader(buffer).Load(id, rowID)
}
예제 #3
0
func Update(index core.Uint32Index, buffer dbio.DataBuffer, record *core.Record) error {
	rowID, err := index.Find(record.ID)
	if err != nil {
		return err
	}

	allocator := core.NewRecordAllocator(buffer)
	if err = allocator.Update(rowID, record); err != nil {
		return err
	}

	return nil
}
예제 #4
0
func Delete(index core.Uint32Index, buffer dbio.DataBuffer, id uint32) error {
	rowID, err := index.Find(id)
	if err != nil {
		return err
	}

	allocator := core.NewRecordAllocator(buffer)
	if err := allocator.Remove(rowID); err != nil {
		return err
	}

	return index.Delete(id)
}
func assertIndexCanInsertAndFind(t *testing.T, index core.Uint32Index, key int, rowID core.RowID) (core.Uint32Key, core.RowID) {
	insertOnIndex(t, index, key, rowID)
	rowIDFound, err := index.Find(uint32(key))
	if err != nil {
		t.Fatalf("Error when trying to find rowID with key=%+v: %s", key, err)
	}
	if rowIDFound == (core.RowID{}) {
		t.Errorf("Could not retrieve %d from index right after inserting it", key)
	}
	if rowIDFound != rowID {
		t.Errorf("Invalid value returned from the index: %+v", rowIDFound)
	}
	return core.Uint32Key(key), rowID
}
예제 #6
0
func Insert(index core.Uint32Index, buffer dbio.DataBuffer, record *core.Record) error {
	cb := core.NewDataBlockRepository(buffer).ControlBlock()
	buffer.MarkAsDirty(cb.DataBlockID())

	rowID, _ := index.Find(record.ID)
	if rowID != (core.RowID{}) {
		return fmt.Errorf("Key already exists: %d", record.ID)
	}

	allocator := core.NewRecordAllocator(buffer)
	rowID, err := allocator.Add(record)
	if err != nil {
		return err
	}

	return index.Insert(record.ID, rowID)
}
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")
	}
}
func assertIndexCantFindByKey(t *testing.T, index core.Uint32Index, key int) {
	if _, err := index.Find(uint32(key)); err == nil {
		t.Error("Did not remove key from index")
	}
}
func assertIndexCanDeleteByKey(t *testing.T, index core.Uint32Index, key int) {
	index.Delete(uint32(key))
	assertIndexCantFindByKey(t, index, key)
}
func insertOnIndex(t *testing.T, index core.Uint32Index, key int, rowID core.RowID) {
	if err := index.Insert(uint32(key), rowID); err != nil {
		t.Fatalf("Error inserting rowID with key %d: %s", key, err)
	}
}