Browse Source

Nil bytes are OK for Get/Set etc

And s/Release/Close/g
pull/1842/head
Jae Kwon 7 years ago
parent
commit
ca56a274bd
3 changed files with 19 additions and 18 deletions
  1. +8
    -7
      db/backend_test.go
  2. +9
    -9
      db/c_level_db.go
  3. +2
    -2
      db/mem_db.go

+ 8
- 7
db/backend_test.go View File

@ -21,27 +21,27 @@ func testBackendGetSetDelete(t *testing.T, backend string) {
defer dir.Close()
db := NewDB("testdb", backend, dirname)
// A nonexistent key should return nil, even if the key is empty.
// A nonexistent key should return nil, even if the key is empty
require.Nil(t, db.Get([]byte("")))
// A nonexistent key should return nil, even if the key is nil.
// A nonexistent key should return nil, even if the key is nil
require.Nil(t, db.Get(nil))
// A nonexistent key should return nil.
key := []byte("abc")
require.Nil(t, db.Get(key))
// Set empty ("")
// Set empty value.
db.Set(key, []byte(""))
require.NotNil(t, db.Get(key))
require.Empty(t, db.Get(key))
// Set empty (nil)
// Set nil value.
db.Set(key, nil)
require.NotNil(t, db.Get(key))
require.Empty(t, db.Get(key))
// Delete
// Delete.
db.Delete(key)
require.Nil(t, db.Get(key))
}
@ -62,12 +62,13 @@ func withDB(t *testing.T, creator dbCreator, fn func(DB)) {
}
func TestBackendsNilKeys(t *testing.T) {
// test all backends.
// nil keys are treated as the empty key for most operations.
// Test all backends.
for dbType, creator := range backends {
withDB(t, creator, func(db DB) {
t.Run(fmt.Sprintf("Testing %s", dbType), func(t *testing.T) {
// Nil keys are treated as the empty key for most operations.
expect := func(key, value []byte) {
if len(key) == 0 { // nil or empty
assert.Equal(t, db.Get(nil), db.Get([]byte("")))


+ 9
- 9
db/c_level_db.go View File

@ -199,12 +199,12 @@ func newCLevelDBIterator(source *levigo.Iterator, start, end []byte, isReverse b
}
}
func (itr *cLevelDBIterator) Domain() ([]byte, []byte) {
func (itr cLevelDBIterator) Domain() ([]byte, []byte) {
return itr.start, itr.end
}
func (itr *cLevelDBIterator) Valid() bool {
func (itr cLevelDBIterator) Valid() bool {
// Once invalid, forever invalid.
if itr.isInvalid {
return false
@ -227,33 +227,33 @@ func (itr *cLevelDBIterator) Valid() bool {
return false
}
// Valid
// It's valid.
return true
}
func (itr *cLevelDBIterator) Key() []byte {
func (itr cLevelDBIterator) Key() []byte {
itr.assertNoError()
itr.assertIsValid()
return itr.source.Key()
}
func (itr *cLevelDBIterator) Value() []byte {
func (itr cLevelDBIterator) Value() []byte {
itr.assertNoError()
itr.assertIsValid()
return itr.source.Value()
}
func (itr *cLevelDBIterator) Next() {
func (itr cLevelDBIterator) Next() {
itr.assertNoError()
itr.assertIsValid()
itr.source.Next()
}
func (itr *cLevelDBIterator) Close() {
func (itr cLevelDBIterator) Close() {
itr.source.Close()
}
func (itr *cLevelDBIterator) assertNoError() {
func (itr cLevelDBIterator) assertNoError() {
if err := itr.source.GetError(); err != nil {
panic(err)
}


+ 2
- 2
db/mem_db.go View File

@ -30,7 +30,7 @@ func (db *MemDB) Get(key []byte) []byte {
db.mtx.Lock()
defer db.mtx.Unlock()
key = nonNilBytes(key)
return db.db[string(key)]
}
@ -215,4 +215,4 @@ func (itr *memDBIterator) assertIsValid() {
if !itr.Valid() {
panic("memDBIterator is invalid")
}
}
}

Loading…
Cancel
Save