mirror of
https://github.com/octoleo/syncthing.git
synced 2024-09-19 05:09:01 +00:00
lib/db, lib/model: Code simplifications (#6484)
NamespacedKV.prefixedKey is still small enough to be inlined.
This commit is contained in:
parent
123941cf62
commit
2f26a95973
@ -17,20 +17,15 @@ import (
|
|||||||
// a leveldb.
|
// a leveldb.
|
||||||
type NamespacedKV struct {
|
type NamespacedKV struct {
|
||||||
db *Lowlevel
|
db *Lowlevel
|
||||||
prefix []byte
|
prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNamespacedKV returns a new NamespacedKV that lives in the namespace
|
// NewNamespacedKV returns a new NamespacedKV that lives in the namespace
|
||||||
// specified by the prefix.
|
// specified by the prefix.
|
||||||
func NewNamespacedKV(db *Lowlevel, prefix string) *NamespacedKV {
|
func NewNamespacedKV(db *Lowlevel, prefix string) *NamespacedKV {
|
||||||
prefixBs := []byte(prefix)
|
|
||||||
// After the conversion from string the cap will be larger than the len (in Go 1.11.5,
|
|
||||||
// 32 bytes cap for small strings). We need to cut it down to ensure append() calls
|
|
||||||
// on the prefix make a new allocation.
|
|
||||||
prefixBs = prefixBs[:len(prefixBs):len(prefixBs)]
|
|
||||||
return &NamespacedKV{
|
return &NamespacedKV{
|
||||||
db: db,
|
db: db,
|
||||||
prefix: prefixBs,
|
prefix: prefix,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +125,7 @@ func (n NamespacedKV) Delete(key string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n NamespacedKV) prefixedKey(key string) []byte {
|
func (n NamespacedKV) prefixedKey(key string) []byte {
|
||||||
return append(n.prefix, []byte(key)...)
|
return []byte(n.prefix + key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Well known namespaces that can be instantiated without knowing the key
|
// Well known namespaces that can be instantiated without knowing the key
|
||||||
|
@ -167,7 +167,7 @@ func reset(n *NamespacedKV) {
|
|||||||
}
|
}
|
||||||
defer tr.Release()
|
defer tr.Release()
|
||||||
|
|
||||||
it, err := tr.NewPrefixIterator(n.prefix)
|
it, err := tr.NewPrefixIterator([]byte(n.prefix))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1359,14 +1359,9 @@ func verifyBuffer(buf []byte, block protocol.BlockInfo) error {
|
|||||||
if len(buf) != int(block.Size) {
|
if len(buf) != int(block.Size) {
|
||||||
return fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
|
return fmt.Errorf("length mismatch %d != %d", len(buf), block.Size)
|
||||||
}
|
}
|
||||||
hf := sha256.New()
|
|
||||||
_, err := hf.Write(buf)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
hash := hf.Sum(nil)
|
|
||||||
|
|
||||||
if !bytes.Equal(hash, block.Hash) {
|
hash := sha256.Sum256(buf)
|
||||||
|
if !bytes.Equal(hash[:], block.Hash) {
|
||||||
return fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
|
return fmt.Errorf("hash mismatch %x != %x", hash, block.Hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user