damccorm commented on code in PR #24056:
URL: https://github.com/apache/beam/pull/24056#discussion_r1019212980


##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -429,39 +427,52 @@ func (d *Datastore) GetExampleGraph(ctx context.Context, 
id string) (string, err
        return pcObj.Content, nil
 }
 
-//DeleteUnusedSnippets deletes all unused snippets
-func (d *Datastore) DeleteUnusedSnippets(ctx context.Context, dayDiff int32) 
error {
-       var hoursDiff = dayDiff * 24
-       boundaryDate := time.Now().Add(-time.Hour * time.Duration(hoursDiff))
-       snippetQuery := datastore.NewQuery(constants.SnippetKind).
-               Namespace(utils.GetNamespace(ctx)).
-               Filter("lVisited <= ", boundaryDate).
-               Filter("origin =", constants.UserSnippetOrigin).
+func (d *Datastore) deleteSnippets(ctx context.Context, snippetQuery 
*datastore.Query,
+       postQueryFilter func(*datastore.Key) bool) error {
+       snippetQuery = snippetQuery.
                Project("numberOfFiles")
        var snpDtos []*dto.SnippetDeleteDTO
        snpKeys, err := d.Client.GetAll(ctx, snippetQuery, &snpDtos)
        if err != nil {
-               logger.Errorf("Datastore: DeleteUnusedSnippets(): error during 
deleting unused snippets, err: %s\n", err.Error())
+               logger.Errorf("Datastore: deleteSnippets(): query snippets, 
err: %s\n", err.Error())
                return err
        }
-       var fileKeys []*datastore.Key
+       logger.Debugf("deleting %d unused snippets: %v", len(snpKeys), snpKeys)
        for snpIndex, snpKey := range snpKeys {
+               if snpKey != nil && postQueryFilter(snpKey) {
+                       logger.Debugf("keeping the current snippet %v", snpKey)
+                       continue
+               }
+               var fileKeys []*datastore.Key
                for fileIndex := 0; fileIndex < 
snpDtos[snpIndex].NumberOfFiles; fileIndex++ {
                        fileKeys = append(fileKeys, utils.GetFileKey(ctx, 
snpKey.Name, fileIndex))
                }
-       }
-       _, err = d.Client.RunInTransaction(ctx, func(tx *datastore.Transaction) 
error {
-               err = tx.DeleteMulti(fileKeys)
-               err = tx.DeleteMulti(snpKeys)
-               return err
-       })
-       if err != nil {
-               logger.Errorf("Datastore: DeleteUnusedSnippets(): error during 
deleting unused snippets, err: %s\n", err.Error())
-               return err
+               // delete snippet & its artefacts in a transaction

Review Comment:
   ```suggestion
                // delete snippet & its artifacts in a transaction
   ```



##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -429,39 +427,52 @@ func (d *Datastore) GetExampleGraph(ctx context.Context, 
id string) (string, err
        return pcObj.Content, nil
 }
 
-//DeleteUnusedSnippets deletes all unused snippets
-func (d *Datastore) DeleteUnusedSnippets(ctx context.Context, dayDiff int32) 
error {
-       var hoursDiff = dayDiff * 24
-       boundaryDate := time.Now().Add(-time.Hour * time.Duration(hoursDiff))
-       snippetQuery := datastore.NewQuery(constants.SnippetKind).
-               Namespace(utils.GetNamespace(ctx)).
-               Filter("lVisited <= ", boundaryDate).
-               Filter("origin =", constants.UserSnippetOrigin).
+func (d *Datastore) deleteSnippets(ctx context.Context, snippetQuery 
*datastore.Query,
+       postQueryFilter func(*datastore.Key) bool) error {

Review Comment:
   This filter is a little confusing since the things that are passing through 
the filter are not the things we're acting on (and deleting). Could you add an 
explanatory comment? And maybe rename to something like `skipDeletionFilter`? 
   
   Alternately, this might just be clearer as a slice `skipKeys 
[]*datastore.Key` instead of a function (that's probably an easier experience 
for the invoker as well).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to