Beispiel #1
0
func (ahr AddHashRequest) Execute(database *levigo.DB, ro *levigo.ReadOptions, wo *levigo.WriteOptions) (*kminvalues.KMinValues, error) {
	if ahr.Key == "" {
		return nil, NoKeySpecified
	}

	keyBytes := []byte(ahr.Key)

	data, err := database.Get(ro, keyBytes)
	if err != nil {
		return nil, err
	}

	kmv, err := kminvalues.KMinValuesFromBytes(data)
	if err != nil {
		if len(data) == 0 {
			kmv = kminvalues.NewKMinValues(*defaultSize)
		} else {
			return nil, err
		}
	}
	kmv.AddHash(ahr.Hash)

	err = database.Put(wo, keyBytes, kmv.Bytes())
	return kmv, err
}
Beispiel #2
0
func TestDB(t *testing.T) {
	SetupDB()
	defer CloseDB()

	key := "_GOTEST_TESTDB"
	resultChan := make(chan Result)

	clean := func() {
		delRequest := DeleteRequest{
			Key:        key,
			ResultChan: resultChan,
		}
		requestChan <- delRequest
		<-resultChan
	}
	clean()
	defer clean()

	kmv := kminvalues.NewKMinValues(50)

	for i := 0; i < 100; i++ {
		kmv.AddHash(GetRandHash())
	}

	origCard := kmv.Cardinality()

	setRequest := SetRequest{
		Key:        key,
		Kmv:        kmv,
		ResultChan: resultChan,
	}
	requestChan <- setRequest
	result := <-resultChan
	assert.Equal(t, result.Error, nil)

	for i := 0; i < 50; i++ {
		addHashRequest := AddHashRequest{
			Key:        key,
			Hash:       GetRandHash(),
			ResultChan: resultChan,
		}
		requestChan <- addHashRequest
		result = <-resultChan
		assert.Equal(t, result.Error, nil)
	}

	newKmv := result.Data
	if newKmv.Cardinality() <= origCard {
		t.Errorf("Cardinality did not increase after 'AddHashRequest' operations")
		t.FailNow()
	}
}
Beispiel #3
0
func (gr GetRequest) Execute(database *levigo.DB, ro *levigo.ReadOptions, wo *levigo.WriteOptions) (*kminvalues.KMinValues, error) {
	if gr.Key == "" {
		return nil, NoKeySpecified
	}

	data, err := database.Get(ro, []byte(gr.Key))
	if err != nil {
		return nil, err
	}

	kmv, err := kminvalues.KMinValuesFromBytes(data)
	if err != nil {
		if len(data) == 0 {
			kmv = kminvalues.NewKMinValues(*defaultSize)
		} else {
			return nil, err
		}
	}
	return kmv, err
}