db: Simplify exists check, fix IsKeyInDomain signature, Iterator Close

+ *FSDB.HasKey now uses common.FileExists to test for file existence
+ IsKeyInDomain takes key as a []byte slice instead of as a string
to avoid extraneous []byte<-->string conversions for start and end
+ Iterator.Close() instead of Iterator.Release()
+ withDB helper to encapsulate DB creation, deferred cleanups
so that for loops can use opened DBs and discard them ASAP

Addressing accepted changes from review with @jaekwon
This commit is contained in:
Emmanuel Odeke
2017-12-15 02:58:51 -07:00
parent a2f7898b6d
commit a7b20d4e46
7 changed files with 37 additions and 45 deletions
+3 -8
View File
@@ -10,6 +10,7 @@ import (
"sync"
"github.com/pkg/errors"
cmn "github.com/tendermint/tmlibs/common"
)
const (
@@ -64,13 +65,7 @@ func (db *FSDB) Has(key []byte) bool {
panicNilKey(key)
path := db.nameToPath(key)
_, err := read(path)
if os.IsNotExist(err) {
return false
} else if err != nil {
panic(errors.Wrapf(err, "Getting key %s (0x%X)", string(key), key))
}
return true
return cmn.FileExists(path)
}
func (db *FSDB) Set(key []byte, value []byte) {
@@ -246,7 +241,7 @@ func list(dirPath string, start, end []byte) ([]string, error) {
if err != nil {
return nil, fmt.Errorf("Failed to unescape %s while listing", name)
}
if IsKeyInDomain(n, start, end) {
if IsKeyInDomain([]byte(n), start, end) {
paths = append(paths, n)
}
}