Skip to content

Commit

Permalink
Merge pull request #1308 from anyproto/go-3602-do-not-reindex-market-…
Browse files Browse the repository at this point in the history
…links

GO-3602 Do not reindex links of marketplace
  • Loading branch information
KirillSto authored Jun 12, 2024
2 parents 84010cf + 02ac0d4 commit cf40d62
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 28 deletions.
47 changes: 28 additions & 19 deletions core/indexer/reindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,24 @@ func (i *indexer) ReindexMarketplaceSpace(space clientspace.Space) error {
if err != nil {
return err
}
err = i.removeCommonIndexes(space.Id(), flags)
if err != nil {
return fmt.Errorf("remove common indexes: %w", err)

if flags.removeAllIndexedObjects {
_, err = i.removeDetails(space.Id())
if err != nil {
return fmt.Errorf("remove details for marketplace space: %w", err)
}
}

ctx := context.Background()

if flags.bundledRelations {
err := i.reindexIDsForSmartblockTypes(ctx, space, metrics.ReindexTypeBundledRelations, smartblock2.SmartBlockTypeBundledRelation)
err = i.reindexIDsForSmartblockTypes(ctx, space, metrics.ReindexTypeBundledRelations, smartblock2.SmartBlockTypeBundledRelation)
if err != nil {
return fmt.Errorf("reindex bundled relations: %w", err)
}
}
if flags.bundledTypes {
err := i.reindexIDsForSmartblockTypes(ctx, space, metrics.ReindexTypeBundledTypes, smartblock2.SmartBlockTypeBundledObjectType, smartblock2.SmartBlockTypeAnytypeProfile)
err = i.reindexIDsForSmartblockTypes(ctx, space, metrics.ReindexTypeBundledTypes, smartblock2.SmartBlockTypeBundledObjectType, smartblock2.SmartBlockTypeAnytypeProfile)
if err != nil {
return fmt.Errorf("reindex bundled types: %w", err)
}
Expand Down Expand Up @@ -336,6 +340,24 @@ func (i *indexer) ReindexMarketplaceSpace(space clientspace.Space) error {
return i.saveLatestChecksums(space.Id())
}

func (i *indexer) removeDetails(spaceId string) (ids []string, err error) {
err = i.removeOldObjects()
if err != nil {
err = nil
log.Errorf("reindex failed to removeOldObjects: %v", err)
}
ids, err = i.store.ListIdsBySpace(spaceId)
if err != nil {
log.Errorf("reindex failed to get all ids(removeAllIndexedObjects): %v", err)
}
for _, id := range ids {
if err = i.store.DeleteDetails(id); err != nil {
log.Errorf("reindex failed to delete details(removeAllIndexedObjects): %v", err)
}
}
return ids, err
}

// removeOldObjects removes all objects that are not supported anymore (e.g. old subobjects) and no longer returned by the underlying source
func (i *indexer) removeOldObjects() (err error) {
ids, err := i.store.ListIds()
Expand Down Expand Up @@ -381,20 +403,7 @@ func (i *indexer) removeCommonIndexes(spaceId string, flags reindexFlags) (err e
var ids []string
if flags.removeAllIndexedObjects {
flags.eraseLinks = true
err = i.removeOldObjects()
if err != nil {
err = nil
log.Errorf("reindex failed to removeOldObjects: %v", err)
}
ids, err = i.store.ListIdsBySpace(spaceId)
if err != nil {
log.Errorf("reindex failed to get all ids(removeAllIndexedObjects): %v", err)
}
for _, id := range ids {
if err = i.store.DeleteDetails(id); err != nil {
log.Errorf("reindex failed to delete details(removeAllIndexedObjects): %v", err)
}
}
ids, err = i.removeDetails(spaceId)
}

if flags.eraseLinks {
Expand Down
69 changes: 60 additions & 9 deletions core/indexer/reindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,10 @@ import (
)

func TestReindexMarketplaceSpace(t *testing.T) {
t.Run("reindex missing object", func(t *testing.T) {
// given
indexerFx := NewIndexerFixture(t)
checksums := indexerFx.getLatestChecksums()
err := indexerFx.store.SaveChecksums("spaceId", &checksums)
assert.Nil(t, err)

virtualSpace := clientspace.NewVirtualSpace("spaceId", clientspace.VirtualSpaceDeps{
Indexer: indexerFx,
spaceId := "market"
getMockSpace := func(fx *IndexerFixture) *clientspace.VirtualSpace {
virtualSpace := clientspace.NewVirtualSpace(spaceId, clientspace.VirtualSpaceDeps{
Indexer: fx,
})
mockCache := mock_objectcache.NewMockCache(t)
smartTest := smarttest.New(addr.MissingObject)
Expand All @@ -48,6 +43,18 @@ func TestReindexMarketplaceSpace(t *testing.T) {
mockCache.EXPECT().GetObject(context.Background(), addr.AnytypeProfileId).Return(smartTest, nil)
virtualSpace.Cache = mockCache

return virtualSpace
}

t.Run("reindex missing object", func(t *testing.T) {
// given
indexerFx := NewIndexerFixture(t)
checksums := indexerFx.getLatestChecksums()
err := indexerFx.store.SaveChecksums(spaceId, &checksums)
assert.Nil(t, err)

virtualSpace := getMockSpace(indexerFx)

storage := mock_storage.NewMockClientStorage(t)
storage.EXPECT().BindSpaceID(mock.Anything, mock.Anything).Return(nil)
indexerFx.storageService = storage
Expand All @@ -60,6 +67,50 @@ func TestReindexMarketplaceSpace(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, details)
})

t.Run("do not reindex links in marketplace", func(t *testing.T) {
// given
fx := NewIndexerFixture(t)

favs := []string{"fav1", "fav2"}
trash := []string{"trash1", "trash2"}
err := fx.store.UpdateObjectLinks("home", favs)
require.NoError(t, err)
err = fx.store.UpdateObjectLinks("bin", trash)
require.NoError(t, err)

homeLinks, err := fx.store.GetOutboundLinksByID("home")
require.Equal(t, favs, homeLinks)

archiveLinks, err := fx.store.GetOutboundLinksByID("bin")
require.Equal(t, trash, archiveLinks)

checksums := fx.getLatestChecksums()
checksums.LinksErase = checksums.LinksErase - 1

err = fx.objectStore.SaveChecksums(spaceId, &checksums)
require.NoError(t, err)

storage := mock_storage.NewMockClientStorage(t)
storage.EXPECT().BindSpaceID(mock.Anything, mock.Anything).Return(nil)
fx.storageService = storage

// when
err = fx.ReindexMarketplaceSpace(getMockSpace(fx))
assert.NoError(t, err)

// then
homeLinks, err = fx.store.GetOutboundLinksByID("home")
assert.NoError(t, err)
assert.Equal(t, favs, homeLinks)

archiveLinks, err = fx.store.GetOutboundLinksByID("bin")
assert.NoError(t, err)
assert.Equal(t, trash, archiveLinks)

storeChecksums, err := fx.store.GetChecksums(spaceId)
assert.Equal(t, ForceLinksReindexCounter, storeChecksums.LinksErase)
})
}

func TestReindexDeletedObjects(t *testing.T) {
Expand Down

0 comments on commit cf40d62

Please sign in to comment.