Skip to content

Commit

Permalink
Merge pull request #1429 from anyproto/fix-objectstore-get-relation
Browse files Browse the repository at this point in the history
Objectstore: fix GetRelationByKey: add space ID
  • Loading branch information
deff7 authored Jul 30, 2024
2 parents 000f61a + 97317b2 commit 1ded717
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 100 deletions.
1 change: 1 addition & 0 deletions core/block/source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type accountService interface {

type Space interface {
Id() string
IsPersonal() bool
TreeBuilder() objecttreebuilder.TreeBuilder
GetRelationIdByKey(ctx context.Context, key domain.RelationKey) (id string, err error)
GetTypeIdByKey(ctx context.Context, key domain.TypeKey) (id string, err error)
Expand Down
2 changes: 1 addition & 1 deletion core/block/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type fileObjectMigrator interface {
}

type RelationGetter interface {
GetRelationByKey(key string) (*model.Relation, error)
GetRelationByKey(spaceId string, key string) (*model.Relation, error)
}

type source struct {
Expand Down
7 changes: 6 additions & 1 deletion core/block/source/sub_object_links_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func (m *subObjectsAndProfileLinksMigration) replaceLinksInDetails(s *state.Stat
}
}

// Migrate works only in personal space
func (m *subObjectsAndProfileLinksMigration) Migrate(s *state.State) {
if !m.space.IsPersonal() {
return
}

uk, err := domain.NewUniqueKey(smartblock.SmartBlockTypeProfilePage, "")
if err != nil {
log.Errorf("migration: failed to create unique key for profile: %s", err)
Expand Down Expand Up @@ -187,7 +192,7 @@ func (m *subObjectsAndProfileLinksMigration) migrateFilter(filter *model.BlockCo
log.With("relationKey", filter.RelationKey).Warnf("empty filter value")
return nil
}
relation, err := m.objectStore.GetRelationByKey(filter.RelationKey)
relation, err := m.objectStore.GetRelationByKey(m.space.Id(), filter.RelationKey)
if err != nil {
log.Warnf("migration: failed to get relation by key %s: %s", filter.RelationKey, err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/subscription/dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func (ds *dependencyService) isRelationObject(key string) bool {
if isObj, ok := ds.isRelationObjMap[key]; ok {
return isObj
}
rel, err := ds.s.objectStore.GetRelationByKey(key)
relFormat, err := ds.s.objectStore.GetRelationFormatByKey(key)
if err != nil {
log.Errorf("can't get relation %s: %v", key, err)
return false
}
isObj := rel.Format == model.RelationFormat_object || rel.Format == model.RelationFormat_file || rel.Format == model.RelationFormat_tag || rel.Format == model.RelationFormat_status
isObj := relFormat == model.RelationFormat_object || relFormat == model.RelationFormat_file || relFormat == model.RelationFormat_tag || relFormat == model.RelationFormat_status
ds.isRelationObjMap[key] = isObj
return isObj
}
Expand Down
92 changes: 19 additions & 73 deletions core/subscription/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ func TestService_Search(t *testing.T) {
},
nil,
)
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyAuthor.String()).Return(&model.Relation{
Key: bundle.RelationKeyAuthor.String(),
Format: model.RelationFormat_object,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyAuthor.String()).Return(model.RelationFormat_object, nil).AnyTimes()

fx.store.EXPECT().QueryByID([]string{"author1"}).Return([]database.Record{
{Details: &types.Struct{Fields: map[string]*types.Value{
Expand Down Expand Up @@ -135,14 +129,8 @@ func TestService_Search(t *testing.T) {
},
nil,
)
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyAuthor.String()).Return(&model.Relation{
Key: bundle.RelationKeyAuthor.String(),
Format: model.RelationFormat_object,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyAuthor.String()).Return(model.RelationFormat_object, nil).AnyTimes()

fx.store.EXPECT().QueryByID([]string{"force1", "force2"}).Return([]database.Record{
{Details: &types.Struct{Fields: map[string]*types.Value{
Expand Down Expand Up @@ -194,10 +182,7 @@ func TestService_Search(t *testing.T) {
},
nil,
)
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

resp, err := fx.Search(SubscribeRequest{
SubId: "test",
Expand Down Expand Up @@ -261,10 +246,7 @@ func TestService_Search(t *testing.T) {
},
nil,
)
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

resp, err := fx.Search(SubscribeRequest{
SubId: "test",
Expand Down Expand Up @@ -359,14 +341,8 @@ func TestService_Search(t *testing.T) {
}}},
}, nil)

fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyId.String()).Return(&model.Relation{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyId.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

var resp, err = fx.Search(SubscribeRequest{
SubId: subscriptionID,
Expand Down Expand Up @@ -409,14 +385,8 @@ func TestService_Search(t *testing.T) {
}}},
}, nil)

fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyId.String()).Return(&model.Relation{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyId.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

var resp, err = fx.Search(SubscribeRequest{
SubId: subscriptionID,
Expand Down Expand Up @@ -466,14 +436,8 @@ func TestService_Search(t *testing.T) {
}}},
}, nil)

fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyId.String()).Return(&model.Relation{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyId.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

var resp, err = fx.Search(SubscribeRequest{
SubId: subscriptionID,
Expand Down Expand Up @@ -508,15 +472,9 @@ func TestService_Search(t *testing.T) {
}}},
}, nil)

fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

fx.store.EXPECT().GetRelationByKey(testRelationKey).Return(&model.Relation{
Key: testRelationKey,
Format: model.RelationFormat_object,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(testRelationKey).Return(model.RelationFormat_object, nil).AnyTimes()

s := fx.Service.(*service)
s.ds = newDependencyService(s)
Expand Down Expand Up @@ -563,15 +521,9 @@ func TestService_Search(t *testing.T) {
}}},
}, nil)

fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

fx.store.EXPECT().GetRelationByKey(testRelationKey).Return(&model.Relation{
Key: testRelationKey,
Format: model.RelationFormat_object,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(testRelationKey).Return(model.RelationFormat_object, nil).AnyTimes()

s := fx.Service.(*service)
s.ds = newDependencyService(s)
Expand Down Expand Up @@ -618,14 +570,8 @@ func TestService_Search(t *testing.T) {
}}},
}, nil)

fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyName.String()).Return(&model.Relation{
Key: bundle.RelationKeyName.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationByKey(bundle.RelationKeyId.String()).Return(&model.Relation{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_shorttext,
}, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyName.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()
fx.store.EXPECT().GetRelationFormatByKey(bundle.RelationKeyId.String()).Return(model.RelationFormat_shorttext, nil).AnyTimes()

var resp, err = fx.Search(SubscribeRequest{
SubId: subscriptionID,
Expand Down Expand Up @@ -1066,7 +1012,7 @@ func xTestNestedSubscription(t *testing.T) {

func testCreateSubscriptionWithNestedFilter(t *testing.T) *fixtureRealStore {
fx := newFixtureWithRealObjectStore(t)
// fx.store.EXPECT().GetRelationByKey(mock.Anything).Return(&model.Relation{}, nil)
// fx.store.EXPECT().GetRelationFormatByKey(mock.Anything).Return(&model.Relation{}, nil)
resp, err := fx.Search(SubscribeRequest{
SubId: "test",
Filters: []*model.BlockContentDataviewFilter{
Expand Down
85 changes: 71 additions & 14 deletions pkg/lib/localstore/objectstore/mock_objectstore/mock_ObjectStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/lib/localstore/objectstore/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ type ObjectStore interface {
FetchRelationByLinks(spaceId string, links pbtypes.RelationLinks) (relations relationutils.Relations, err error)
ListAllRelations(spaceId string) (relations relationutils.Relations, err error)
GetRelationByID(id string) (relation *model.Relation, err error)
GetRelationByKey(key string) (*model.Relation, error)
GetRelationByKey(spaceId string, key string) (*model.Relation, error)
GetRelationFormatByKey(key string) (model.RelationFormat, error)

GetObjectType(url string) (*model.ObjectType, error)
BatchProcessFullTextQueue(ctx context.Context, limit int, processIds func(processIds []string) error) error
Expand Down
Loading

0 comments on commit 1ded717

Please sign in to comment.