Repository: oom-ai/oomstore
Pull request #1159 (2022-02-09 08:18:58)

Jinghan/modify metadata method `ListFeature`
This PR modifies metadata method `ListFeature`: 
- change option `EntityID` to `EntityIDs`
- change option `GroupID` to `GroupIDs`

This change will be used in later PRs to serve more situations.


Patch:
diff --git a/internal/database/metadata/sqlutil/feature.go b/internal/database/metadata/sqlutil/feature.go
index aa7c5381..a0f7f7e1 100644
--- a/internal/database/metadata/sqlutil/feature.go
+++ b/internal/database/metadata/sqlutil/feature.go
@@ -114,9 +114,14 @@ func ListFeature(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata
 	}
 
 	// filter by entity
-	if opt.EntityID != nil {
+	if opt.EntityIDs != nil {
 		features = features.Filter(func(f *types.Feature) bool {
-			return f.Group.EntityID == *opt.EntityID
+			for _, id := range *opt.EntityIDs {
+				if f.Group.EntityID == id {
+					return true
+				}
+			}
+			return false
 		})
 	}
 	return features, nil
@@ -126,8 +131,11 @@ func buildListFeatureCond(opt metadata.ListFeatureOpt) ([]string, []interface{},
 	in := make(map[string]interface{})
 	and := make(map[string]interface{})
 
-	if opt.GroupID != nil {
-		and["group_id"] = *opt.GroupID
+	if opt.GroupIDs != nil {
+		if len(*opt.GroupIDs) == 0 {
+			return []string{"false"}, nil, nil
+		}
+		in["group_id"] = *opt.GroupIDs
 	}
 
 	if opt.FeatureIDs != nil {
diff --git a/internal/database/metadata/types.go b/internal/database/metadata/types.go
index 5a51cd0c..3f31a173 100644
--- a/internal/database/metadata/types.go
+++ b/internal/database/metadata/types.go
@@ -61,8 +61,8 @@ type UpdateRevisionOpt struct {
 }
 
 type ListFeatureOpt struct {
-	EntityID   *int
-	GroupID    *int
+	EntityIDs  *[]int
+	GroupIDs   *[]int
 	FeatureIDs *[]int
 }
 
diff --git a/pkg/oomstore/apply.go b/pkg/oomstore/apply.go
index 50bb1d41..d58dba3a 100644
--- a/pkg/oomstore/apply.go
+++ b/pkg/oomstore/apply.go
@@ -192,7 +192,7 @@ func (s *OomStore) applyFeature(ctx context.Context, tx metadata.DBStore, newFea
 
 		if group.Category == types.CategoryStream {
 			features, err := tx.ListFeature(ctx, metadata.ListFeatureOpt{
-				GroupID: &group.ID,
+				GroupIDs: &[]int{group.ID},
 			})
 			if err != nil {
 				return nil, err
diff --git a/pkg/oomstore/feature.go b/pkg/oomstore/feature.go
index 3ca0d094..6d9f4307 100644
--- a/pkg/oomstore/feature.go
+++ b/pkg/oomstore/feature.go
@@ -39,14 +39,14 @@ func (s *OomStore) ListFeature(ctx context.Context, opt types.ListFeatureOpt) (t
 		if err != nil {
 			return nil, err
 		}
-		metadataOpt.EntityID = &entity.ID
+		metadataOpt.EntityIDs = &[]int{entity.ID}
 	}
 	if opt.GroupName != nil {
 		group, err := s.metadata.GetGroupByName(ctx, *opt.GroupName)
 		if err != nil {
 			return nil, err
 		}
-		metadataOpt.GroupID = &group.ID
+		metadataOpt.GroupIDs = &[]int{group.ID}
 	}
 	features, err := s.metadata.ListFeature(ctx, metadataOpt)
 	if err != nil {
@@ -100,7 +100,7 @@ func (s *OomStore) CreateFeature(ctx context.Context, opt types.CreateFeatureOpt
 
 	if group.Category == types.CategoryStream {
 		features, err := s.metadata.ListFeature(ctx, metadata.ListFeatureOpt{
-			GroupID: &group.ID,
+			GroupIDs: &[]int{group.ID},
 		})
 		if err != nil {
 			return 0, err
diff --git a/pkg/oomstore/import.go b/pkg/oomstore/import.go
index 4c01bf0b..bb830006 100644
--- a/pkg/oomstore/import.go
+++ b/pkg/oomstore/import.go
@@ -67,7 +67,7 @@ func (s *OomStore) parseImportOpt(ctx context.Context, opt types.ImportOpt) (*im
 	}
 
 	features, err := s.metadata.ListFeature(ctx, metadata.ListFeatureOpt{
-		GroupID: &group.ID,
+		GroupIDs: &[]int{group.ID},
 	})
 	if err != nil {
 		return nil, err
diff --git a/pkg/oomstore/revision.go b/pkg/oomstore/revision.go
index 3e651719..49a9e995 100644
--- a/pkg/oomstore/revision.go
+++ b/pkg/oomstore/revision.go
@@ -99,7 +99,7 @@ func (s *OomStore) createSnapshotAndCdcTable(ctx context.Context, revisionID int
 
 	// Create snapshot table in offline store
 	features, err := s.metadata.ListFeature(ctx, metadata.ListFeatureOpt{
-		GroupID: &revision.GroupID,
+		GroupIDs: &[]int{revision.GroupID},
 	})
 	if err != nil {
 		return err
diff --git a/pkg/oomstore/snapshot.go b/pkg/oomstore/snapshot.go
index cd65cd14..07215a70 100644
--- a/pkg/oomstore/snapshot.go
+++ b/pkg/oomstore/snapshot.go
@@ -19,7 +19,7 @@ func (s *OomStore) Snapshot(ctx context.Context, groupName string) error {
 	}
 
 	features, err := s.metadata.ListFeature(ctx, metadata.ListFeatureOpt{
-		GroupID: &group.ID,
+		GroupIDs: &[]int{group.ID},
 	})
 	if err != nil {
 		return err


Test patch:
diff --git a/internal/database/metadata/test_impl/feature.go b/internal/database/metadata/test_impl/feature.go
index 7c6a4b8b..1248f066 100644
--- a/internal/database/metadata/test_impl/feature.go
+++ b/internal/database/metadata/test_impl/feature.go
@@ -233,7 +233,7 @@ func TestListFeature(t *testing.T, prepareStore PrepareStoreFn, destroyStore Des
 
 	// case 4: list features by EntityID and FeatureIDs
 	features, err = store.ListFeature(ctx, metadata.ListFeatureOpt{
-		EntityID:   intPtr(entityID + 1),
+		EntityIDs:  &[]int{entityID + 1},
 		FeatureIDs: &[]int{featureID},
 	})
 	assert.NoError(t, err)
@@ -241,7 +241,7 @@ func TestListFeature(t *testing.T, prepareStore PrepareStoreFn, destroyStore Des
 
 	// case 5: list features by GroupID and FeatureIDs
 	features, err = store.ListFeature(ctx, metadata.ListFeatureOpt{
-		GroupID:    intPtr(groupID + 1),
+		GroupIDs:   &[]int{groupID + 1},
 		FeatureIDs: &[]int{featureID},
 	})
 	assert.NoError(t, err)
@@ -249,18 +249,32 @@ func TestListFeature(t *testing.T, prepareStore PrepareStoreFn, destroyStore Des
 
 	// case 6: list features by EntityID and empty FeatureIDs, return no feature
 	features, err = store.ListFeature(ctx, metadata.ListFeatureOpt{
-		EntityID:   &entityID,
+		EntityIDs:  &[]int{entityID},
 		FeatureIDs: &[]int{},
 	})
 	assert.NoError(t, err)
 	assert.Equal(t, 0, len(features))
 
-	// case 7: list features by EntityID
+	// case 7: list features by EntityIDs
 	features, err = store.ListFeature(ctx, metadata.ListFeatureOpt{
-		EntityID: &entityID,
+		EntityIDs: &[]int{entityID, entityID + 1},
 	})
 	assert.NoError(t, err)
 	assert.Equal(t, 1, len(features))
+
+	// case 8: list features by empty EntityIDs
+	features, err = store.ListFeature(ctx, metadata.ListFeatureOpt{
+		EntityIDs: &[]int{},
+	})
+	assert.NoError(t, err)
+	assert.Equal(t, 0, len(features))
+
+	// case 9: list features by empty GroupIDs
+	features, err = store.ListFeature(ctx, metadata.ListFeatureOpt{
+		GroupIDs: &[]int{},
+	})
+	assert.NoError(t, err)
+	assert.Equal(t, 0, len(features))
 }
 
 func TestUpdateFeature(t *testing.T, prepareStore PrepareStoreFn, destroyStore DestroyStoreFn) {
diff --git a/internal/database/metadata/test_impl/helper.go b/internal/database/metadata/test_impl/helper.go
index feeece08..4467438a 100644
--- a/internal/database/metadata/test_impl/helper.go
+++ b/internal/database/metadata/test_impl/helper.go
@@ -4,10 +4,6 @@ func boolPtr(i bool) *bool {
 	return &i
 }
 
-func intPtr(i int) *int {
-	return &i
-}
-
 func stringPtr(s string) *string {
 	return &s
 }