chore: switch database engine to sqlite (fixes #9954) (#9965)

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:
Jakob Borg
2025-03-29 13:50:08 +01:00
committed by GitHub
co-authored by Tommy van der Vorst bt90
parent b1c8f88a44
commit 025905fcdf
146 changed files with 8315 additions and 11984 deletions
+41
View File
@@ -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