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
Mikhail Rakhmanov 12ea073b98
Add object type to message for ignore purposes
# Conflicts:
#	commonspace/spacesyncproto/protos/spacesync.proto
#	commonspace/spacesyncproto/spacesync.pb.go
2025-04-03 14:58:35 +02:00

79 lines
1.9 KiB
Go

package synctree
import (
"slices"
"github.com/anyproto/any-sync/commonspace/object/tree/treechangeproto"
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
"github.com/anyproto/any-sync/commonspace/sync/objectsync/objectmessages"
)
type InnerHeadUpdate struct {
opts BroadcastOptions
heads []string
changes []*treechangeproto.RawTreeChangeWithId
snapshotPath []string
root *treechangeproto.RawTreeChangeWithId
prepared []byte
emptyPrepared []byte
}
func (h *InnerHeadUpdate) MsgSize() (size uint64) {
return uint64(len(h.prepared))
}
func (h *InnerHeadUpdate) ObjectType() spacesyncproto.ObjectType {
return spacesyncproto.ObjectType_Tree
}
func (h *InnerHeadUpdate) Prepare() error {
treeMsg := treechangeproto.WrapHeadUpdate(&treechangeproto.TreeHeadUpdate{
Heads: h.heads,
SnapshotPath: h.snapshotPath,
Changes: h.changes,
}, h.root)
bytes, err := treeMsg.Marshal()
if err != nil {
return err
}
h.prepared = bytes
emptyTreeMsg := treechangeproto.WrapHeadUpdate(&treechangeproto.TreeHeadUpdate{
Heads: h.heads,
SnapshotPath: h.snapshotPath,
}, h.root)
bytes, err = emptyTreeMsg.Marshal()
if err != nil {
return err
}
h.changes = nil
h.emptyPrepared = bytes
return nil
}
func (h *InnerHeadUpdate) Marshall(data objectmessages.ObjectMeta) ([]byte, error) {
if h.prepared != nil {
if slices.Contains(h.opts.EmptyPeers, data.PeerId) {
return h.emptyPrepared, nil
} else {
return h.prepared, nil
}
}
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
}