You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/11/09 12:34:30 UTC
[GitHub] [beam] eantyshev opened a new pull request, #24056: [Playground] update snippet by persistence_key
eantyshev opened a new pull request, #24056:
URL: https://github.com/apache/beam/pull/24056
We add optional `persistence_key` parameter to Playground::SaveSnippet endpoint
- add this to the snippet_id generation
- implement the deletion of old snippets with same `persistence_key`
[specification](https://docs.google.com/document/d/1xt0jwdohy5I21XnLn9XkLjN6NvL6dupCs9do_uK0TT0/edit#)
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
- [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] eantyshev commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] eantyshev commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
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 100 lines below we just optimize the code: switch it to using RunInTransaction
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] eantyshev commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
eantyshev commented on code in PR #24056:
URL: https://github.com/apache/beam/pull/24056#discussion_r1019411755
##########
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:
Good point, thanks! I pushed it even further, since we need only one `skipKey`
sorry for a forced-push
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] github-actions[bot] commented on pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #24056:
URL: https://github.com/apache/beam/pull/24056#issuecomment-1308742527
Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
R: @Abacn for label build.
Available commands:
- `stop reviewer notifications` - opt out of the automated review tooling
- `remind me after tests pass` - tag the comment author after tests pass
- `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
The PR bot will only process comments in the main thread (not review comments).
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] eantyshev commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
eantyshev commented on code in PR #24056:
URL: https://github.com/apache/beam/pull/24056#discussion_r1019411755
##########
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:
Good point, thanks! I pushed it even further, since we need only one `skipKey`
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] olehborysevych commented on pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
olehborysevych commented on PR #24056:
URL: https://github.com/apache/beam/pull/24056#issuecomment-1308889752
LGTM. Thank you!
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] eantyshev commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
eantyshev commented on code in PR #24056:
URL: https://github.com/apache/beam/pull/24056#discussion_r1018149025
##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -388,16 +400,9 @@ func (d *Datastore) GetExampleOutput(ctx context.Context, id string) (string, er
}
func (d *Datastore) GetExampleLogs(ctx context.Context, id string) (string, error) {
Review Comment:
GetExampleLogs, GetExampleGraph don't need transaction, it's a single Get request which is atomic
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] damccorm merged pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
damccorm merged PR #24056:
URL: https://github.com/apache/beam/pull/24056
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] github-actions[bot] commented on pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #24056:
URL: https://github.com/apache/beam/pull/24056#issuecomment-1310217377
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] olehborysevych commented on pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
olehborysevych commented on PR #24056:
URL: https://github.com/apache/beam/pull/24056#issuecomment-1310216306
R: @damccorm
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] eantyshev commented on a diff in pull request #24056: [Playground] update snippet by persistence_key
Posted by GitBox <gi...@apache.org>.
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:
GetSnippet is simplified to use RunInTransaction
instead of manual transaction handling using commit/rollback
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org