When one tries to retrieve a key whose associated value is an empty string []byte("")
twice one after another for the default configuration of the store, the code returns an error with an Slice Out of bounds error.
Expected Behavior
Ideally, the empty string value should be returned every time an attempt is made to get it.
Current Behavior
The first time just after setting the value, when a store.Get
is called, it works fine.
On the second time (and any number of times after that), it errors with Out of bounds error: slice %d - %d out of bounds for %s}
, where %d
is the same number e.g. for a key like "foo", the error is `Out of bounds error: slice 20 - 20 out of bounds for maxLength 20 for data [0 0 0 20 0 0 0 3 102 111 111 0 0 0 0 0 0 0 0 0]
Possible Solution
I expect the cause to be that the data slice from the file naturally only goes up to the expiry section for that KeyValueEntry
. Technically, the KeyValueEntry
has no value section because an empty string cannot not be saved in the file. The reason for this deduction is that when you look at the error message, it is expecting a slice [20..20] yet the data is from 0 to 19.
If we try to cater for the special case where value
is of length 0, the issue can be fixed.
Steps to Reproduce
package main
import (
"fmt"
"github.com/sopherapps/go-scdb/scdb"
"log"
)
func main() {
key := []byte("foo")
value := []byte("")
store, err := scdb.New("db", nil, nil, nil, nil, false)
if err != nil {
log.Fatalf("error opening store: %s", err)
}
defer func() {
_ = store.Close()
}()
err := store.Set(key, value, nil)
if err != nil {
log.Fatalf("error inserting key value: %s", err)
}
for i := 1; i <= 3; i++ {
got, err := store.Get(key)
if err != nil {
log.Fatalf("error getting key value: %s", err)
}
// assert.Equal(t, value, got)
}
}
Context (Environment)
I was trying to use scdb as a map to quickly check for the existence or absence of a key. The value didn't matter so I chose to use an empty string. I was actually using py_scdb but the error was in scdb, which is the underlying engine of py_scdb. I figured that go-scdb is also affected.
Detailed Description
Possible Implementation