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


##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -52,76 +52,88 @@ func New(ctx context.Context, responseMapper 
mapper.ResponseMapper, projectId st
        return &Datastore{Client: client, ResponseMapper: responseMapper}, nil
 }
 
+// Delete unused snippets by given persistenceKey
+func (d *Datastore) deleteObsoleteSnippets(ctx context.Context, snipKey 
*datastore.Key, persistenceKey string) error {
+       if persistenceKey == "" || snipKey == nil {
+               logger.Debugf("no persistence key or no current snippet key")
+               return nil
+       }
+       // If persistenceKey is given, find the previous snippet version
+       snippetQuery := datastore.NewQuery(constants.SnippetKind).
+               Namespace(utils.GetNamespace(ctx)).
+               FilterField("persistenceKey", "=", persistenceKey)
+
+               // At the moment, datastore emulator doesn't allow != filters,
+               // hence this crutches
+               // 
https://cloud.google.com/datastore/docs/tools/datastore-emulator#known_issues
+               // When it's fixed, post-query filter could be replaced with
+               //
+               // FilterField("__key__", "!=", snipKey)
+
+       return d.deleteSnippets(ctx, snippetQuery, func(key *datastore.Key) 
bool {
+               // keep the current key
+               return *key == *snipKey
+       })
+}
+
 // PutSnippet puts the snippet entity to datastore
 func (d *Datastore) PutSnippet(ctx context.Context, snipId string, snip 
*entity.Snippet) error {
+       logger.Debugf("putting snippet %q, persistent key %q...", snipId, 
snip.Snippet.PersistenceKey)
        if snip == nil {
                logger.Errorf("Datastore: PutSnippet(): snippet is nil")
                return nil
        }
        snipKey := utils.GetSnippetKey(ctx, snipId)
-       tx, err := d.Client.NewTransaction(ctx)
-       if err != nil {
-               logger.Errorf("Datastore: PutSnippet(): error during the 
transaction creating, err: %s\n", err.Error())
-               return err
-       }
-       if _, err = tx.Put(snipKey, snip.Snippet); err != nil {
-               if rollBackErr := tx.Rollback(); rollBackErr != nil {
-                       err = rollBackErr
-               }
-               logger.Errorf("Datastore: PutSnippet(): error during the 
snippet entity saving, err: %s\n", err.Error())
-               return err
-       }
 
-       var fileKeys []*datastore.Key
-       for index := range snip.Files {
-               fileKeys = append(fileKeys, utils.GetFileKey(ctx, snipId, 
index))
-       }
+       // Create the new snippet
+       _, err := d.Client.RunInTransaction(ctx, func(tx 
*datastore.Transaction) error {
+               if _, err := tx.Put(snipKey, snip.Snippet); err != nil {
+                       logger.Errorf("Datastore: PutSnippet(): error during 
the snippet entity saving, err: %s\n", err.Error())
+                       return err
+               }
 
-       if _, err = tx.PutMulti(fileKeys, snip.Files); err != nil {
-               if rollBackErr := tx.Rollback(); rollBackErr != nil {
-                       err = rollBackErr
+               var fileKeys []*datastore.Key
+               for index := range snip.Files {
+                       fileKeys = append(fileKeys, utils.GetFileKey(ctx, 
snipId, index))
                }
-               logger.Errorf("Datastore: PutSnippet(): error during the file 
entity saving, err: %s\n", err.Error())
-               return err
-       }
 
-       if _, err = tx.Commit(); err != nil {
-               logger.Errorf(errorMsgTemplateTxCommit, err.Error())
+               if _, err := tx.PutMulti(fileKeys, snip.Files); err != nil {
+                       logger.Errorf("Datastore: PutSnippet(): error during 
the file entity saving, err: %s\n", err.Error())
+                       return err
+               }
+               return nil
+       })
+       if err != nil {
+               logger.Errorf("Datastore: PutSnippet(): error during commit, 
err: %s\n", err.Error())
                return err
        }
 
-       return nil
+       return d.deleteObsoleteSnippets(ctx, snipKey, 
snip.Snippet.PersistenceKey)
 }
 
 // GetSnippet returns the snippet entity by identifier
 func (d *Datastore) GetSnippet(ctx context.Context, id string) 
(*entity.SnippetEntity, error) {
        key := utils.GetSnippetKey(ctx, id)
        snip := new(entity.SnippetEntity)
-       tx, err := d.Client.NewTransaction(ctx)

Review Comment:
   here and below we just optimize the code



-- 
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