mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-12 02:30:53 +09:00
Object store: ModifyObjectDetails: copy struct to avoid data races
This commit is contained in:
parent
6f52f45a6a
commit
cf7f2adc6a
5 changed files with 16 additions and 11 deletions
|
@ -192,7 +192,7 @@ func (uw *UpdateWatcher) updateBackLinksInObject(id string, backlinksUpdate *bac
|
|||
backlinks := pbtypes.GetStringList(current, bundle.RelationKeyBacklinks.String())
|
||||
|
||||
for _, removed := range backlinksChange.removed {
|
||||
slice.Remove(backlinks, removed)
|
||||
backlinks = slice.Remove(backlinks, removed)
|
||||
}
|
||||
|
||||
for _, added := range backlinksChange.added {
|
||||
|
|
|
@ -223,7 +223,7 @@ func (i *indexer) addSyncDetails(space clientspace.Space) {
|
|||
for _, id := range ids {
|
||||
err := space.DoLockedIfNotExists(id, func() error {
|
||||
return i.store.ModifyObjectDetails(id, func(details *types.Struct) (*types.Struct, error) {
|
||||
helper.InjectsSyncDetails(details, syncStatus, syncError)
|
||||
details = helper.InjectsSyncDetails(details, syncStatus, syncError)
|
||||
return details, nil
|
||||
})
|
||||
})
|
||||
|
|
|
@ -11,10 +11,8 @@ import (
|
|||
"github.com/anyproto/anytype-heart/util/pbtypes"
|
||||
)
|
||||
|
||||
func InjectsSyncDetails(details *types.Struct, status domain.ObjectSyncStatus, syncError domain.SyncError) {
|
||||
if details == nil || details.Fields == nil {
|
||||
details = &types.Struct{Fields: map[string]*types.Value{}}
|
||||
}
|
||||
func InjectsSyncDetails(details *types.Struct, status domain.ObjectSyncStatus, syncError domain.SyncError) *types.Struct {
|
||||
details = pbtypes.EnsureStructInited(details)
|
||||
if pbtypes.Get(details, bundle.RelationKeySyncStatus.String()) == nil {
|
||||
details.Fields[bundle.RelationKeySyncStatus.String()] = pbtypes.Int64(int64(status))
|
||||
}
|
||||
|
@ -24,6 +22,7 @@ func InjectsSyncDetails(details *types.Struct, status domain.ObjectSyncStatus, s
|
|||
if pbtypes.Get(details, bundle.RelationKeySyncError.String()) == nil {
|
||||
details.Fields[bundle.RelationKeySyncError.String()] = pbtypes.Int64(int64(syncError))
|
||||
}
|
||||
return details
|
||||
}
|
||||
|
||||
func SyncRelationsSmartblockTypes() []smartblock.SmartBlockType {
|
||||
|
|
|
@ -132,14 +132,13 @@ func (s *dsObjectStore) ModifyObjectDetails(id string, proc func(details *types.
|
|||
return fmt.Errorf("get existing details: %w", err)
|
||||
}
|
||||
|
||||
newDetails, err := proc(oldDetails.GetDetails())
|
||||
inputDetails := pbtypes.CopyStruct(oldDetails.GetDetails(), false)
|
||||
inputDetails = pbtypes.EnsureStructInited(inputDetails)
|
||||
newDetails, err := proc(inputDetails)
|
||||
if err != nil {
|
||||
return fmt.Errorf("run a modifier: %w", err)
|
||||
}
|
||||
|
||||
if newDetails == nil || newDetails.Fields == nil {
|
||||
newDetails = &types.Struct{Fields: map[string]*types.Value{}}
|
||||
}
|
||||
newDetails = pbtypes.EnsureStructInited(newDetails)
|
||||
// Ensure ID is set
|
||||
newDetails.Fields[bundle.RelationKeyId.String()] = pbtypes.String(id)
|
||||
s.sendUpdatesToSubscriptions(id, newDetails)
|
||||
|
|
|
@ -30,6 +30,13 @@ func CopyBlock(in *model.Block) (out *model.Block) {
|
|||
return
|
||||
}
|
||||
|
||||
func EnsureStructInited(s *types.Struct) *types.Struct {
|
||||
if s == nil || s.Fields == nil {
|
||||
return &types.Struct{Fields: make(map[string]*types.Value)}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func CopyStruct(s *types.Struct, copyVals bool) *types.Struct {
|
||||
if s == nil {
|
||||
return nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue