lib/protocol, lib/discover, lib/db: Use protocol buffer serialization (fixes #3080)
This changes the BEP protocol to use protocol buffer serialization instead of XDR, and therefore also the database format. The local discovery protocol is also updated to be protocol buffer format. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3276 LGTM: AudriusButkevicius
This commit is contained in:
committed by
Audrius Butkevicius
parent
21f5b16e47
commit
fa0101bd60
@@ -6,35 +6,30 @@ package protocol
|
||||
// version vector. The vector has slice semantics and some operations on it
|
||||
// are "append-like" in that they may return the same vector modified, or v
|
||||
// new allocated Vector with the modified contents.
|
||||
type Vector []Counter
|
||||
|
||||
// Counter represents a single counter in the version vector.
|
||||
type Counter struct {
|
||||
ID ShortID
|
||||
Value uint64
|
||||
}
|
||||
|
||||
// Update returns a Vector with the index for the specific ID incremented by
|
||||
// one. If it is possible, the vector v is updated and returned. If it is not,
|
||||
// a copy will be created, updated and returned.
|
||||
func (v Vector) Update(id ShortID) Vector {
|
||||
for i := range v {
|
||||
if v[i].ID == id {
|
||||
for i := range v.Counters {
|
||||
if v.Counters[i].ID == id {
|
||||
// Update an existing index
|
||||
v[i].Value++
|
||||
v.Counters[i].Value++
|
||||
return v
|
||||
} else if v[i].ID > id {
|
||||
} else if v.Counters[i].ID > id {
|
||||
// Insert a new index
|
||||
nv := make(Vector, len(v)+1)
|
||||
copy(nv, v[:i])
|
||||
nv := make([]Counter, len(v.Counters)+1)
|
||||
copy(nv, v.Counters[:i])
|
||||
nv[i].ID = id
|
||||
nv[i].Value = 1
|
||||
copy(nv[i+1:], v[i:])
|
||||
return nv
|
||||
copy(nv[i+1:], v.Counters[i:])
|
||||
return Vector{nv}
|
||||
}
|
||||
}
|
||||
// Append a new index
|
||||
return append(v, Counter{id, 1})
|
||||
return Vector{append(v.Counters, Counter{id, 1})}
|
||||
}
|
||||
|
||||
// Merge returns the vector containing the maximum indexes from v and b. If it
|
||||
@@ -42,28 +37,28 @@ func (v Vector) Update(id ShortID) Vector {
|
||||
// will be created, updated and returned.
|
||||
func (v Vector) Merge(b Vector) Vector {
|
||||
var vi, bi int
|
||||
for bi < len(b) {
|
||||
if vi == len(v) {
|
||||
for bi < len(b.Counters) {
|
||||
if vi == len(v.Counters) {
|
||||
// We've reach the end of v, all that remains are appends
|
||||
return append(v, b[bi:]...)
|
||||
return Vector{append(v.Counters, b.Counters[bi:]...)}
|
||||
}
|
||||
|
||||
if v[vi].ID > b[bi].ID {
|
||||
if v.Counters[vi].ID > b.Counters[bi].ID {
|
||||
// The index from b should be inserted here
|
||||
n := make(Vector, len(v)+1)
|
||||
copy(n, v[:vi])
|
||||
n[vi] = b[bi]
|
||||
copy(n[vi+1:], v[vi:])
|
||||
v = n
|
||||
n := make([]Counter, len(v.Counters)+1)
|
||||
copy(n, v.Counters[:vi])
|
||||
n[vi] = b.Counters[bi]
|
||||
copy(n[vi+1:], v.Counters[vi:])
|
||||
v.Counters = n
|
||||
}
|
||||
|
||||
if v[vi].ID == b[bi].ID {
|
||||
if val := b[bi].Value; val > v[vi].Value {
|
||||
v[vi].Value = val
|
||||
if v.Counters[vi].ID == b.Counters[bi].ID {
|
||||
if val := b.Counters[bi].Value; val > v.Counters[vi].Value {
|
||||
v.Counters[vi].Value = val
|
||||
}
|
||||
}
|
||||
|
||||
if bi < len(b) && v[vi].ID == b[bi].ID {
|
||||
if bi < len(b.Counters) && v.Counters[vi].ID == b.Counters[bi].ID {
|
||||
bi++
|
||||
}
|
||||
vi++
|
||||
@@ -74,9 +69,9 @@ func (v Vector) Merge(b Vector) Vector {
|
||||
|
||||
// Copy returns an identical vector that is not shared with v.
|
||||
func (v Vector) Copy() Vector {
|
||||
nv := make(Vector, len(v))
|
||||
copy(nv, v)
|
||||
return nv
|
||||
nv := make([]Counter, len(v.Counters))
|
||||
copy(nv, v.Counters)
|
||||
return Vector{nv}
|
||||
}
|
||||
|
||||
// Equal returns true when the two vectors are equivalent.
|
||||
@@ -106,10 +101,96 @@ func (v Vector) Concurrent(b Vector) bool {
|
||||
|
||||
// Counter returns the current value of the given counter ID.
|
||||
func (v Vector) Counter(id ShortID) uint64 {
|
||||
for _, c := range v {
|
||||
for _, c := range v.Counters {
|
||||
if c.ID == id {
|
||||
return c.Value
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Ordering represents the relationship between two Vectors.
|
||||
type Ordering int
|
||||
|
||||
const (
|
||||
Equal Ordering = iota
|
||||
Greater
|
||||
Lesser
|
||||
ConcurrentLesser
|
||||
ConcurrentGreater
|
||||
)
|
||||
|
||||
// There's really no such thing as "concurrent lesser" and "concurrent
|
||||
// greater" in version vectors, just "concurrent". But it's useful to be able
|
||||
// to get a strict ordering between versions for stable sorts and so on, so we
|
||||
// return both variants. The convenience method Concurrent() can be used to
|
||||
// check for either case.
|
||||
|
||||
// Compare returns the Ordering that describes a's relation to b.
|
||||
func (v Vector) Compare(b Vector) Ordering {
|
||||
var ai, bi int // index into a and b
|
||||
var av, bv Counter // value at current index
|
||||
|
||||
result := Equal
|
||||
|
||||
for ai < len(v.Counters) || bi < len(b.Counters) {
|
||||
var aMissing, bMissing bool
|
||||
|
||||
if ai < len(v.Counters) {
|
||||
av = v.Counters[ai]
|
||||
} else {
|
||||
av = Counter{}
|
||||
aMissing = true
|
||||
}
|
||||
|
||||
if bi < len(b.Counters) {
|
||||
bv = b.Counters[bi]
|
||||
} else {
|
||||
bv = Counter{}
|
||||
bMissing = true
|
||||
}
|
||||
|
||||
switch {
|
||||
case av.ID == bv.ID:
|
||||
// We have a counter value for each side
|
||||
if av.Value > bv.Value {
|
||||
if result == Lesser {
|
||||
return ConcurrentLesser
|
||||
}
|
||||
result = Greater
|
||||
} else if av.Value < bv.Value {
|
||||
if result == Greater {
|
||||
return ConcurrentGreater
|
||||
}
|
||||
result = Lesser
|
||||
}
|
||||
|
||||
case !aMissing && av.ID < bv.ID || bMissing:
|
||||
// Value is missing on the b side
|
||||
if av.Value > 0 {
|
||||
if result == Lesser {
|
||||
return ConcurrentLesser
|
||||
}
|
||||
result = Greater
|
||||
}
|
||||
|
||||
case !bMissing && bv.ID < av.ID || aMissing:
|
||||
// Value is missing on the a side
|
||||
if bv.Value > 0 {
|
||||
if result == Greater {
|
||||
return ConcurrentGreater
|
||||
}
|
||||
result = Lesser
|
||||
}
|
||||
}
|
||||
|
||||
if ai < len(v.Counters) && (av.ID <= bv.ID || bMissing) {
|
||||
ai++
|
||||
}
|
||||
if bi < len(b.Counters) && (bv.ID <= av.ID || aMissing) {
|
||||
bi++
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user