1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00
any-sync/app/ldiff/diffcontainer.go
Mikhail Rakhmanov 253968a8d8
Use string hash
2025-03-28 19:42:54 +01:00

65 lines
1.4 KiB
Go

package ldiff
import (
"context"
"encoding/hex"
"github.com/zeebo/blake3"
)
type RemoteTypeChecker interface {
DiffTypeCheck(ctx context.Context, diffContainer DiffContainer) (needsSync bool, diff Diff, err error)
}
type DiffContainer interface {
DiffTypeCheck(ctx context.Context, typeChecker RemoteTypeChecker) (needsSync bool, diff Diff, err error)
OldDiff() Diff
NewDiff() Diff
Set(elements ...Element)
RemoveId(id string) error
}
type diffContainer struct {
newDiff Diff
oldDiff Diff
}
func (d *diffContainer) NewDiff() Diff {
return d.newDiff
}
func (d *diffContainer) OldDiff() Diff {
return d.oldDiff
}
func (d *diffContainer) Set(elements ...Element) {
hasher := hashersPool.Get().(*blake3.Hasher)
defer hashersPool.Put(hasher)
for _, el := range elements {
hasher.Reset()
hasher.WriteString(el.Head)
stringHash := hex.EncodeToString(hasher.Sum(nil))
d.newDiff.Set(Element{
Id: el.Id,
Head: stringHash,
})
}
d.oldDiff.Set(elements...)
}
func (d *diffContainer) RemoveId(id string) error {
_ = d.newDiff.RemoveId(id)
_ = d.oldDiff.RemoveId(id)
return nil
}
func (d *diffContainer) DiffTypeCheck(ctx context.Context, typeChecker RemoteTypeChecker) (needsSync bool, diff Diff, err error) {
return typeChecker.DiffTypeCheck(ctx, d)
}
func NewDiffContainer(new, old Diff) DiffContainer {
return &diffContainer{
newDiff: new,
oldDiff: old,
}
}