1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00
any-sync/commonspace/object/tree/synctree/headupdate.go
2024-08-15 09:18:06 +02:00

51 lines
1.2 KiB
Go

package synctree
import (
"slices"
"github.com/anyproto/any-sync/commonspace/object/tree/treechangeproto"
"github.com/anyproto/any-sync/commonspace/sync/objectsync/objectmessages"
)
type InnerHeadUpdate struct {
opts BroadcastOptions
heads []string
changes []*treechangeproto.RawTreeChangeWithId
snapshotPath []string
root *treechangeproto.RawTreeChangeWithId
}
func (h InnerHeadUpdate) MsgSize() (size uint64) {
for _, head := range h.heads {
size += uint64(len(head))
}
for _, snapshotId := range h.snapshotPath {
size += uint64(len(snapshotId))
}
for _, change := range h.changes {
size += uint64(len(change.Id))
size += uint64(len(change.RawChange))
}
return size
}
func (h InnerHeadUpdate) Marshall(data objectmessages.ObjectMeta) ([]byte, error) {
changes := h.changes
if slices.Contains(h.opts.EmptyPeers, data.PeerId) {
changes = nil
}
treeMsg := treechangeproto.WrapHeadUpdate(&treechangeproto.TreeHeadUpdate{
Heads: h.heads,
SnapshotPath: h.snapshotPath,
Changes: changes,
}, h.root)
return treeMsg.Marshal()
}
type BroadcastOptions struct {
EmptyPeers []string
}
func (h InnerHeadUpdate) Heads() []string {
return h.heads
}