Browse Source

db: fixes to fsdb and clevledb

pull/1842/head
Ethan Buchman 7 years ago
parent
commit
ba8c5045b5
3 changed files with 19 additions and 16 deletions
  1. +10
    -14
      db/backend_test.go
  2. +6
    -0
      db/c_level_db.go
  3. +3
    -2
      db/fsdb.go

+ 10
- 14
db/backend_test.go View File

@ -15,30 +15,26 @@ func testBackendGetSetDelete(t *testing.T, backend string) {
dir, dirname := cmn.Tempdir(fmt.Sprintf("test_backend_%s_", backend))
defer dir.Close()
db := NewDB("testdb", backend, dirname)
require.Nil(t, db.Get([]byte("")))
key := []byte("abc")
require.Nil(t, db.Get(key))
// Set empty ("")
db.Set([]byte(""), []byte(""))
require.NotNil(t, db.Get([]byte("")))
require.Empty(t, db.Get([]byte("")))
db.Set(key, []byte(""))
require.NotNil(t, db.Get(key))
require.Empty(t, db.Get(key))
// Set empty (nil)
db.Set([]byte(""), nil)
require.NotNil(t, db.Get([]byte("")))
require.Empty(t, db.Get([]byte("")))
db.Set(key, nil)
require.NotNil(t, db.Get(key))
require.Empty(t, db.Get(key))
// Delete
db.Delete([]byte(""))
require.Nil(t, db.Get([]byte("")))
db.Delete(key)
require.Nil(t, db.Get(key))
}
func TestBackendsGetSetDelete(t *testing.T) {
for dbType, _ := range backends {
if dbType == "fsdb" {
// TODO: handle
// fsdb cant deal with length 0 keys
continue
}
testBackendGetSetDelete(t, dbType)
}
}


+ 6
- 0
db/c_level_db.go View File

@ -48,6 +48,7 @@ func NewCLevelDB(name string, dir string) (*CLevelDB, error) {
}
func (db *CLevelDB) Get(key []byte) []byte {
panicNilKey(key)
res, err := db.db.Get(db.ro, key)
if err != nil {
panic(err)
@ -56,10 +57,12 @@ func (db *CLevelDB) Get(key []byte) []byte {
}
func (db *CLevelDB) Has(key []byte) bool {
panicNilKey(key)
panic("not implemented yet")
}
func (db *CLevelDB) Set(key []byte, value []byte) {
panicNilKey(key)
err := db.db.Put(db.wo, key, value)
if err != nil {
panic(err)
@ -67,6 +70,7 @@ func (db *CLevelDB) Set(key []byte, value []byte) {
}
func (db *CLevelDB) SetSync(key []byte, value []byte) {
panicNilKey(key)
err := db.db.Put(db.woSync, key, value)
if err != nil {
panic(err)
@ -74,6 +78,7 @@ func (db *CLevelDB) SetSync(key []byte, value []byte) {
}
func (db *CLevelDB) Delete(key []byte) {
panicNilKey(key)
err := db.db.Delete(db.wo, key)
if err != nil {
panic(err)
@ -81,6 +86,7 @@ func (db *CLevelDB) Delete(key []byte) {
}
func (db *CLevelDB) DeleteSync(key []byte) {
panicNilKey(key)
err := db.db.Delete(db.woSync, key)
if err != nil {
panic(err)


+ 3
- 2
db/fsdb.go View File

@ -119,7 +119,8 @@ func (db *FSDB) DeleteSync(key []byte) {
// NOTE: Implements atomicSetDeleter.
func (db *FSDB) DeleteNoLock(key []byte) {
panicNilKey(key)
err := remove(string(key))
path := db.nameToPath(key)
err := remove(path)
if os.IsNotExist(err) {
return
} else if err != nil {
@ -210,7 +211,7 @@ func read(path string) ([]byte, error) {
// Write some bytes from a file.
// CONTRACT: returns os errors directly without wrapping.
func write(path string, d []byte) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, keyPerm)
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, keyPerm)
if err != nil {
return err
}


Loading…
Cancel
Save