Skip to content

Commit

Permalink
GO-4202 make AddToIndexQueue atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
requilence committed Oct 7, 2024
1 parent fe084b5 commit a7d2789
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
9 changes: 5 additions & 4 deletions core/indexer/fulltext.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,16 @@ func (i *indexer) ftInit() error {
return err
}
if docCount == 0 {
// query objects that are existing in the store
// if they are not existing in the object store, they will be indexed and added via reindexOutdatedObjects or on receiving via any-sync
ids, err := i.store.ListIdsCrossSpace()
if err != nil {
return err
}
for _, id := range ids {
if err := i.store.AddToIndexQueue(i.runCtx, id); err != nil {
return err
}
if err := i.store.AddToIndexQueue(i.runCtx, ids...); err != nil {
return err
}

}
}
return nil
Expand Down
19 changes: 14 additions & 5 deletions pkg/lib/localstore/objectstore/indexer_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ import (
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
)

func (s *dsObjectStore) AddToIndexQueue(ctx context.Context, id string) error {
func (s *dsObjectStore) AddToIndexQueue(ctx context.Context, ids ...string) error {
txn, err := s.fulltextQueue.WriteTx(ctx)
if err != nil {
return fmt.Errorf("start write tx: %w", err)
}
arena := s.arenaPool.Get()
defer func() {
arena.Reset()
s.arenaPool.Put(arena)
}()
obj := arena.NewObject()
obj.Set("id", arena.NewString(id))

_, err := s.fulltextQueue.UpsertOne(ctx, obj)
return err
obj := arena.NewObject()
for _, id := range ids {
obj.Set("id", arena.NewString(id))
_, err = s.fulltextQueue.UpsertOne(txn.Context(), obj)
if err != nil {
return fmt.Errorf("upsert: %w", err)
}
}
return txn.Commit()
}

func (s *dsObjectStore) BatchProcessFullTextQueue(ctx context.Context, limit int, processIds func(ids []string) error) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/localstore/objectstore/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ObjectStore interface {
}

type IndexerStore interface {
AddToIndexQueue(ctx context.Context, id string) error
AddToIndexQueue(ctx context.Context, id ...string) error
ListIdsFromFullTextQueue(limit int) ([]string, error)
RemoveIdsFromFullTextQueue(ids []string) error
GetGlobalChecksums() (checksums *model.ObjectStoreChecksums, err error)
Expand Down
8 changes: 5 additions & 3 deletions pkg/lib/localstore/objectstore/spaceindex/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ func (q *dummyFulltextQueue) RemoveIdsFromFullTextQueue(ids []string) error {
return nil
}

func (q *dummyFulltextQueue) AddToIndexQueue(ctx context.Context, id string) error {
func (q *dummyFulltextQueue) AddToIndexQueue(ctx context.Context, ids ...string) error {
q.lock.Lock()
defer q.lock.Unlock()
if !lo.Contains(q.ids, id) {
q.ids = append(q.ids, id)
for _, id := range ids {
if !lo.Contains(q.ids, id) {
q.ids = append(q.ids, id)
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/localstore/objectstore/spaceindex/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type SourceDetailsFromID interface {

type FulltextQueue interface {
RemoveIdsFromFullTextQueue(ids []string) error
AddToIndexQueue(ctx context.Context, id string) error
AddToIndexQueue(ctx context.Context, ids ...string) error
ListIdsFromFullTextQueue(limit int) ([]string, error)
}

Expand Down

0 comments on commit a7d2789

Please sign in to comment.