summaryrefslogtreecommitdiffstats
path: root/lib/db/namespaced.go
diff options
context:
space:
mode:
authorJakob Borg <jakob@kastelo.net>2019-11-29 09:11:52 +0100
committerGitHub <noreply@github.com>2019-11-29 09:11:52 +0100
commitc71116ee945706f5ce7f038822b20085e2579ac9 (patch)
tree584ad88f6de308845297b7b80b2847b6315424e2 /lib/db/namespaced.go
parenta5bbc126250260158f1538771349bc0ba303089f (diff)
Implement database abstraction, error checking (ref #5907) (#6107)
This PR does two things, because one lead to the other: - Move the leveldb specific stuff into a small "backend" package that defines a backend interface and the leveldb implementation. This allows, potentially, in the future, switching the db implementation so another KV store should we wish to do so. - Add proper error handling all along the way. The db and backend packages are now errcheck clean. However, I drew the line at modifying the FileSet API in order to keep this manageable and not continue refactoring all of the rest of Syncthing. As such, the FileSet methods still panic on database errors, except for the "database is closed" error which is instead handled by silently returning as quickly as possible, with the assumption that we're anyway "on the way out".
Diffstat (limited to 'lib/db/namespaced.go')
-rw-r--r--lib/db/namespaced.go51
1 files changed, 18 insertions, 33 deletions
diff --git a/lib/db/namespaced.go b/lib/db/namespaced.go
index 5cbe720bba..30a888fa05 100644
--- a/lib/db/namespaced.go
+++ b/lib/db/namespaced.go
@@ -9,8 +9,6 @@ package db
import (
"encoding/binary"
"time"
-
- "github.com/syndtr/goleveldb/leveldb/util"
)
// NamespacedKV is a simple key-value store using a specific namespace within
@@ -34,30 +32,18 @@ func NewNamespacedKV(db *Lowlevel, prefix string) *NamespacedKV {
}
}
-// Reset removes all entries in this namespace.
-func (n *NamespacedKV) Reset() {
- it := n.db.NewIterator(util.BytesPrefix(n.prefix), nil)
- defer it.Release()
- batch := n.db.newBatch()
- for it.Next() {
- batch.Delete(it.Key())
- batch.checkFlush()
- }
- batch.flush()
-}
-
// PutInt64 stores a new int64. Any existing value (even if of another type)
// is overwritten.
-func (n *NamespacedKV) PutInt64(key string, val int64) {
+func (n *NamespacedKV) PutInt64(key string, val int64) error {
var valBs [8]byte
binary.BigEndian.PutUint64(valBs[:], uint64(val))
- n.db.Put(n.prefixedKey(key), valBs[:], nil)
+ return n.db.Put(n.prefixedKey(key), valBs[:])
}
// Int64 returns the stored value interpreted as an int64 and a boolean that
// is false if no value was stored at the key.
func (n *NamespacedKV) Int64(key string) (int64, bool) {
- valBs, err := n.db.Get(n.prefixedKey(key), nil)
+ valBs, err := n.db.Get(n.prefixedKey(key))
if err != nil {
return 0, false
}
@@ -67,16 +53,16 @@ func (n *NamespacedKV) Int64(key string) (int64, bool) {
// PutTime stores a new time.Time. Any existing value (even if of another
// type) is overwritten.
-func (n *NamespacedKV) PutTime(key string, val time.Time) {
+func (n *NamespacedKV) PutTime(key string, val time.Time) error {
valBs, _ := val.MarshalBinary() // never returns an error
- n.db.Put(n.prefixedKey(key), valBs, nil)
+ return n.db.Put(n.prefixedKey(key), valBs)
}
// Time returns the stored value interpreted as a time.Time and a boolean
// that is false if no value was stored at the key.
func (n NamespacedKV) Time(key string) (time.Time, bool) {
var t time.Time
- valBs, err := n.db.Get(n.prefixedKey(key), nil)
+ valBs, err := n.db.Get(n.prefixedKey(key))
if err != nil {
return t, false
}
@@ -86,14 +72,14 @@ func (n NamespacedKV) Time(key string) (time.Time, bool) {
// PutString stores a new string. Any existing value (even if of another type)
// is overwritten.
-func (n *NamespacedKV) PutString(key, val string) {
- n.db.Put(n.prefixedKey(key), []byte(val), nil)
+func (n *NamespacedKV) PutString(key, val string) error {
+ return n.db.Put(n.prefixedKey(key), []byte(val))
}
// String returns the stored value interpreted as a string and a boolean that
// is false if no value was stored at the key.
func (n NamespacedKV) String(key string) (string, bool) {
- valBs, err := n.db.Get(n.prefixedKey(key), nil)
+ valBs, err := n.db.Get(n.prefixedKey(key))
if err != nil {
return "", false
}
@@ -102,14 +88,14 @@ func (n NamespacedKV) String(key string) (string, bool) {
// PutBytes stores a new byte slice. Any existing value (even if of another type)
// is overwritten.
-func (n *NamespacedKV) PutBytes(key string, val []byte) {
- n.db.Put(n.prefixedKey(key), val, nil)
+func (n *NamespacedKV) PutBytes(key string, val []byte) error {
+ return n.db.Put(n.prefixedKey(key), val)
}
// Bytes returns the stored value as a raw byte slice and a boolean that
// is false if no value was stored at the key.
func (n NamespacedKV) Bytes(key string) ([]byte, bool) {
- valBs, err := n.db.Get(n.prefixedKey(key), nil)
+ valBs, err := n.db.Get(n.prefixedKey(key))
if err != nil {
return nil, false
}
@@ -118,18 +104,17 @@ func (n NamespacedKV) Bytes(key string) ([]byte, bool) {
// PutBool stores a new boolean. Any existing value (even if of another type)
// is overwritten.
-func (n *NamespacedKV) PutBool(key string, val bool) {
+func (n *NamespacedKV) PutBool(key string, val bool) error {
if val {
- n.db.Put(n.prefixedKey(key), []byte{0x0}, nil)
- } else {
- n.db.Put(n.prefixedKey(key), []byte{0x1}, nil)
+ return n.db.Put(n.prefixedKey(key), []byte{0x0})
}
+ return n.db.Put(n.prefixedKey(key), []byte{0x1})
}
// Bool returns the stored value as a boolean and a boolean that
// is false if no value was stored at the key.
func (n NamespacedKV) Bool(key string) (bool, bool) {
- valBs, err := n.db.Get(n.prefixedKey(key), nil)
+ valBs, err := n.db.Get(n.prefixedKey(key))
if err != nil {
return false, false
}
@@ -138,8 +123,8 @@ func (n NamespacedKV) Bool(key string) (bool, bool) {
// Delete deletes the specified key. It is allowed to delete a nonexistent
// key.
-func (n NamespacedKV) Delete(key string) {
- n.db.Delete(n.prefixedKey(key), nil)
+func (n NamespacedKV) Delete(key string) error {
+ return n.db.Delete(n.prefixedKey(key))
}
func (n NamespacedKV) prefixedKey(key string) []byte {