Switch the database from LevelDB to SQLite, for greater stability and simpler code. Co-authored-by: Tommy van der Vorst <tommy@pixelspark.nl> Co-authored-by: bt90 <btom1990@googlemail.com>
This commit is contained in:
co-authored by
Tommy van der Vorst
bt90
parent
b1c8f88a44
commit
025905fcdf
@@ -7,6 +7,11 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/gen/bep"
|
||||
@@ -20,6 +25,17 @@ type Vector struct {
|
||||
Counters []Counter
|
||||
}
|
||||
|
||||
func (v *Vector) String() string {
|
||||
var buf strings.Builder
|
||||
for i, c := range v.Counters {
|
||||
if i > 0 {
|
||||
buf.WriteRune(',')
|
||||
}
|
||||
fmt.Fprintf(&buf, "%x:%d", c.ID, c.Value)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func (v *Vector) ToWire() *bep.Vector {
|
||||
counters := make([]*bep.Counter, len(v.Counters))
|
||||
for i, c := range v.Counters {
|
||||
@@ -42,6 +58,31 @@ func VectorFromWire(w *bep.Vector) Vector {
|
||||
return v
|
||||
}
|
||||
|
||||
func VectorFromString(s string) (Vector, error) {
|
||||
pairs := strings.Split(s, ",")
|
||||
var v Vector
|
||||
v.Counters = make([]Counter, len(pairs))
|
||||
for i, pair := range pairs {
|
||||
idStr, valStr, ok := strings.Cut(pair, ":")
|
||||
if !ok {
|
||||
return Vector{}, fmt.Errorf("bad pair %q", pair)
|
||||
}
|
||||
idslice, err := hex.DecodeString(idStr)
|
||||
if err != nil {
|
||||
return Vector{}, fmt.Errorf("bad id in pair %q", pair)
|
||||
}
|
||||
var idbs [8]byte
|
||||
copy(idbs[8-len(idslice):], idslice)
|
||||
id := binary.BigEndian.Uint64(idbs[:])
|
||||
val, err := strconv.ParseUint(valStr, 10, 64)
|
||||
if err != nil {
|
||||
return Vector{}, fmt.Errorf("bad val in pair %q", pair)
|
||||
}
|
||||
v.Counters[i] = Counter{ID: ShortID(id), Value: val}
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Counter represents a single counter in the version vector.
|
||||
type Counter struct {
|
||||
ID ShortID
|
||||
|
||||
Reference in New Issue
Block a user