steveyen / gkvlite

Simple, ordered, key-value persistence library for the Go Language
MIT License
264 stars 27 forks source link

Empty string is not a valid key #5

Closed eqv closed 10 years ago

eqv commented 10 years ago

Storing a value under the empty string fails silently:

import "testing"
import "bytes"
import "os"
import "github.com/steveyen/gkvlite"

func TestSimpleEmptyStore(t *testing.T){
    f, err := os.Create("/tmp/test.gkvlite")
    if err != nil {
        t.Errorf("Failed to create Storage File: %v",err)
    }
    res, err := gkvlite.NewStore(f)
    if err != nil {
        t.Errorf("Failed to create Storage Object: %v",err)
    }
    db := res.SetCollection("test",nil)
    key :=  []byte{}
    text := []byte("fnord")
    db.Set(key,text)
    readback,err := db.Get(key)
    if err != nil {
        t.Errorf("Failed to read back %v",err)
    }
    if bytes.Compare(text,readback) != 0 {
        t.Errorf("Failed to read back, got: %v expected: %v",readback,text)
    }
}

The test case fails after returning the empty string instead of the stored value:

--- FAIL: TestSimpleEmptyStore (0.00 seconds)
    gkvlite_test.go:26: Failed to read back, got: [] expected: [102 110 111 114 100]
steveyen commented 10 years ago

Looks like the provided sample test case isn't checking for the error return value from the Set() invocation.

The Set() implementation in gkvlite is doing the empty key correct check...

https://github.com/steveyen/gkvlite/blob/master/collection.go#L129