diff --git a/commonspace/headsync/statestorage/statestorage.go b/commonspace/headsync/statestorage/statestorage.go index 24a16c61..78f2e9dd 100644 --- a/commonspace/headsync/statestorage/statestorage.go +++ b/commonspace/headsync/statestorage/statestorage.go @@ -60,6 +60,9 @@ func (s *stateStorage) SetObserver(observer Observer) { func (s *stateStorage) SetHash(ctx context.Context, hash string) (err error) { defer func() { + if last, ok := s.store.(lastWriteDb); ok { + last.SetLastWrite() + } if s.observer != nil && err == nil { s.observer.OnHashChange(hash) } @@ -80,6 +83,11 @@ func (s *stateStorage) SetHash(ctx context.Context, hash string) (err error) { return tx.Commit() } +type lastWriteDb interface { + SetLastWrite() + anystore.DB +} + func New(ctx context.Context, spaceId string, store anystore.DB) (StateStorage, error) { stateCollection, err := store.Collection(ctx, stateCollectionKey) if err != nil { diff --git a/commonspace/object/tree/objecttree/storage.go b/commonspace/object/tree/objecttree/storage.go index cefcf6f3..99a6792a 100644 --- a/commonspace/object/tree/objecttree/storage.go +++ b/commonspace/object/tree/objecttree/storage.go @@ -4,13 +4,11 @@ import ( "context" "errors" "fmt" - "sync" "time" anystore "github.com/anyproto/any-store" "github.com/anyproto/any-store/anyenc" "github.com/anyproto/any-store/query" - "go.uber.org/atomic" "github.com/anyproto/any-sync/commonspace/headsync/headstorage" "github.com/anyproto/any-sync/commonspace/object/tree/treechangeproto" @@ -208,11 +206,6 @@ func (s *storage) GetAfterOrder(ctx context.Context, orderId string, storageIter return nil } -var ( - lastCheckpoint = atomic.Time{} - lock = sync.Mutex{} -) - func (s *storage) AddAll(ctx context.Context, changes []StorageChange, heads []string, commonSnapshot string) error { arena := s.arena defer arena.Reset() @@ -229,19 +222,6 @@ func (s *storage) AddAll(ctx context.Context, changes []StorageChange, heads []s tx.Rollback() return err } - now := time.Now() - checkpoint := lastCheckpoint.Load() - - if now.Sub(checkpoint) > time.Second { - lock.Lock() - checkpoint := lastCheckpoint.Load() - now = time.Now() - if now.Sub(checkpoint) > time.Second { - s.store.Checkpoint(context.Background(), false) - lastCheckpoint.Store(time.Now()) - } - lock.Unlock() - } } update := headstorage.HeadsUpdate{ Id: s.id,