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/07/02 14:53:36 UTC

[GitHub] [beam] vchunikhin opened a new pull request, #22140: [Playground] Sharing any code API

vchunikhin opened a new pull request, #22140:
URL: https://github.com/apache/beam/pull/22140

   Fixes #22139 
   - API to save a code snippet to the cloud datastore
   - API to get a code snippet from the cloud datastore
   - Unit and integration tests to check functionality
   - Updated gradle tasks to run go tests
   - Cloud datastore client
   
   ------------------------
   
   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/#make-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)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+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] vchunikhin commented on pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
vchunikhin commented on PR #22140:
URL: https://github.com/apache/beam/pull/22140#issuecomment-1183975256

   @pabloem Hi, we were discussing this issue with my team. I'm sorry for that large PR
   The next PR's will be less, of course. Now, I stick to this rule


-- 
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] vchunikhin commented on pull request #22140: [Playground] Sharing any code API

Posted by GitBox <gi...@apache.org>.
vchunikhin commented on PR #22140:
URL: https://github.com/apache/beam/pull/22140#issuecomment-1173955446

   R: @pabloem 


-- 
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] vchunikhin commented on a diff in pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
vchunikhin commented on code in PR #22140:
URL: https://github.com/apache/beam/pull/22140#discussion_r912786314


##########
playground/backend/cmd/server/server.go:
##########
@@ -143,6 +168,23 @@ func setupExamplesCatalog(ctx context.Context, cacheService cache.Cache, bucketN
 	return nil
 }
 
+// setupDBStructure initializes the data structure
+func setupDBStructure(ctx context.Context, db db.Database, appEnv *environment.ApplicationEnvs, props *environment.Properties) error {
+	versions := []schema.Version{new(migration.InitialStructure)}
+	dbSchema := schema.New(ctx, db, appEnv, props, versions)
+	actualSchemaVersion, err := dbSchema.InitiateData()
+	if err != nil {
+		return err
+	}
+	if actualSchemaVersion == "" {

Review Comment:
   We don't want to have the schema version as empty string because of developer's mistake



##########
playground/backend/build.gradle.kts:
##########
@@ -40,17 +40,45 @@ task("tidy") {
   }
 }
 
-task("test") {
-  group = "verification"
-  description = "Test the backend"
-  doLast {
-    exec {
-      executable("go")
-      args("test", "./...")
+val startDatastoreEmulator by tasks.registering {
+    doFirst {
+        val process = ProcessBuilder()

Review Comment:
   Using ProcessBuilder allows us to wait the datastore emulator running before the process will be end



##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -0,0 +1,196 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/db/entity"
+	"beam.apache.org/playground/backend/internal/logger"
+	"beam.apache.org/playground/backend/internal/utils"
+	"cloud.google.com/go/datastore"
+	"context"
+	"fmt"
+	"time"
+)
+
+const (
+	Namespace = "Playground"

Review Comment:
   We want to have a separate namespace for playground entities



##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -0,0 +1,196 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/db/entity"
+	"beam.apache.org/playground/backend/internal/logger"
+	"beam.apache.org/playground/backend/internal/utils"
+	"cloud.google.com/go/datastore"
+	"context"
+	"fmt"
+	"time"
+)
+
+const (
+	Namespace = "Playground"
+
+	SnippetKind = "pg_snippets"
+	SchemaKind  = "pg_schema_versions"
+	SdkKind     = "pg_sdks"
+	FileKind    = "pg_files"
+)
+
+type Datastore struct {
+	Client *datastore.Client
+}
+
+func New(ctx context.Context, projectId string) (*Datastore, error) {
+	client, err := datastore.NewClient(ctx, projectId)
+	if err != nil {
+		logger.Errorf("Datastore: connection to store: error during connection, err: %s\n", err.Error())
+		return nil, err
+	}
+
+	return &Datastore{Client: client}, nil
+}
+
+// PutSnippet puts the snippet entity to datastore
+func (d *Datastore) PutSnippet(ctx context.Context, snipId string, snip *entity.Snippet) error {
+	if snip == nil {
+		logger.Errorf("Datastore: PutSnippet(): snippet is nil")
+		return nil
+	}
+	snipKey := utils.GetNameKey(SnippetKind, snipId, Namespace, nil)
+	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 {
+		fileId := fmt.Sprintf("%s_%d", snipId, index)
+		fileKeys = append(fileKeys, utils.GetNameKey(FileKind, fileId, Namespace, nil))
+	}
+
+	if _, err = tx.PutMulti(fileKeys, snip.Files); err != nil {
+		if rollBackErr := tx.Rollback(); rollBackErr != nil {
+			err = rollBackErr
+		}
+		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("Datastore: PutSnippet(): error during the transaction committing, err: %s\n", err.Error())
+		return err
+	}
+
+	return nil
+}
+
+// GetSnippet returns the snippet entity by identifier
+func (d *Datastore) GetSnippet(ctx context.Context, id string) (*entity.SnippetEntity, error) {
+	key := utils.GetNameKey(SnippetKind, id, Namespace, nil)
+	snip := new(entity.SnippetEntity)
+	tx, err := d.Client.NewTransaction(ctx)

Review Comment:
   Datastore transaction helps to provide extra data consistency and atomicity



##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -0,0 +1,196 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/db/entity"
+	"beam.apache.org/playground/backend/internal/logger"
+	"beam.apache.org/playground/backend/internal/utils"
+	"cloud.google.com/go/datastore"
+	"context"
+	"fmt"
+	"time"
+)
+
+const (
+	Namespace = "Playground"
+
+	SnippetKind = "pg_snippets"
+	SchemaKind  = "pg_schema_versions"
+	SdkKind     = "pg_sdks"
+	FileKind    = "pg_files"
+)
+
+type Datastore struct {
+	Client *datastore.Client
+}
+
+func New(ctx context.Context, projectId string) (*Datastore, error) {
+	client, err := datastore.NewClient(ctx, projectId)
+	if err != nil {
+		logger.Errorf("Datastore: connection to store: error during connection, err: %s\n", err.Error())
+		return nil, err
+	}
+
+	return &Datastore{Client: client}, nil
+}
+
+// PutSnippet puts the snippet entity to datastore
+func (d *Datastore) PutSnippet(ctx context.Context, snipId string, snip *entity.Snippet) error {
+	if snip == nil {
+		logger.Errorf("Datastore: PutSnippet(): snippet is nil")
+		return nil
+	}
+	snipKey := utils.GetNameKey(SnippetKind, snipId, Namespace, nil)
+	tx, err := d.Client.NewTransaction(ctx)

Review Comment:
   Datastore transaction helps to provide extra data consistency



##########
playground/backend/internal/db/datastore/datastore_db.go:
##########
@@ -0,0 +1,196 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package datastore
+
+import (
+	"beam.apache.org/playground/backend/internal/db/entity"
+	"beam.apache.org/playground/backend/internal/logger"
+	"beam.apache.org/playground/backend/internal/utils"
+	"cloud.google.com/go/datastore"
+	"context"
+	"fmt"
+	"time"
+)
+
+const (
+	Namespace = "Playground"
+
+	SnippetKind = "pg_snippets"
+	SchemaKind  = "pg_schema_versions"
+	SdkKind     = "pg_sdks"
+	FileKind    = "pg_files"
+)
+
+type Datastore struct {
+	Client *datastore.Client
+}
+
+func New(ctx context.Context, projectId string) (*Datastore, error) {
+	client, err := datastore.NewClient(ctx, projectId)
+	if err != nil {
+		logger.Errorf("Datastore: connection to store: error during connection, err: %s\n", err.Error())
+		return nil, err
+	}
+
+	return &Datastore{Client: client}, nil
+}
+
+// PutSnippet puts the snippet entity to datastore
+func (d *Datastore) PutSnippet(ctx context.Context, snipId string, snip *entity.Snippet) error {
+	if snip == nil {
+		logger.Errorf("Datastore: PutSnippet(): snippet is nil")
+		return nil
+	}
+	snipKey := utils.GetNameKey(SnippetKind, snipId, Namespace, nil)
+	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 {
+		fileId := fmt.Sprintf("%s_%d", snipId, index)
+		fileKeys = append(fileKeys, utils.GetNameKey(FileKind, fileId, Namespace, nil))
+	}
+
+	if _, err = tx.PutMulti(fileKeys, snip.Files); err != nil {
+		if rollBackErr := tx.Rollback(); rollBackErr != nil {
+			err = rollBackErr
+		}
+		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("Datastore: PutSnippet(): error during the transaction committing, err: %s\n", err.Error())
+		return err
+	}
+
+	return nil
+}
+
+// GetSnippet returns the snippet entity by identifier
+func (d *Datastore) GetSnippet(ctx context.Context, id string) (*entity.SnippetEntity, error) {
+	key := utils.GetNameKey(SnippetKind, id, Namespace, nil)
+	snip := new(entity.SnippetEntity)
+	tx, err := d.Client.NewTransaction(ctx)
+	if err != nil {
+		logger.Errorf("Datastore: GetSnippet(): error during the transaction creating, err: %s\n", err.Error())
+		return nil, err
+	}
+	if err = tx.Get(key, snip); err != nil {
+		if rollBackErr := tx.Rollback(); rollBackErr != nil {
+			err = rollBackErr
+		}
+		logger.Errorf("Datastore: GetSnippet(): error during snippet getting, err: %s\n", err.Error())
+		return nil, err
+	}
+	snip.LVisited = time.Now()
+	snip.VisitCount += 1
+	if _, err = tx.Put(key, snip); err != nil {
+		if rollBackErr := tx.Rollback(); rollBackErr != nil {
+			err = rollBackErr
+		}
+		logger.Errorf("Datastore: GetSnippet(): error during snippet setting, err: %s\n", err.Error())
+		return nil, err
+	}
+	if _, err = tx.Commit(); err != nil {
+		logger.Errorf("Datastore: GetSnippet(): error during the transaction committing, err: %s\n", err.Error())
+		return nil, err
+	}
+	return snip, nil
+}
+
+// PutSchemaVersion puts the schema entity to datastore
+func (d *Datastore) PutSchemaVersion(ctx context.Context, id string, schema *entity.SchemaEntity) error {
+	if schema == nil {
+		logger.Errorf("Datastore: PutSchemaVersion(): schema version is nil")
+		return nil
+	}
+	key := utils.GetNameKey(SchemaKind, id, Namespace, nil)
+	if _, err := d.Client.Put(ctx, key, schema); err != nil {
+		logger.Errorf("Datastore: PutSchemaVersion(): error during entity saving, err: %s\n", err.Error())
+		return err
+	}
+	return nil
+}
+
+// PutSDKs puts the SDK entity to datastore
+func (d *Datastore) PutSDKs(ctx context.Context, sdks []*entity.SDKEntity) error {
+	if sdks == nil || len(sdks) == 0 {
+		logger.Errorf("Datastore: PutSDKs(): sdks are empty")
+		return nil
+	}
+	var keys []*datastore.Key
+	for _, sdk := range sdks {
+		keys = append(keys, utils.GetNameKey(SdkKind, sdk.Name, Namespace, nil))
+	}
+	if _, err := d.Client.PutMulti(ctx, keys, sdks); err != nil {
+		logger.Errorf("Datastore: PutSDK(): error during entity saving, err: %s\n", err.Error())
+		return err
+	}
+	return nil
+}
+
+//GetFiles returns the file entities by a snippet identifier
+func (d *Datastore) GetFiles(ctx context.Context, snipId string, numberOfFiles int) ([]*entity.FileEntity, error) {
+	if numberOfFiles == 0 {
+		logger.Errorf("The number of files must be more than zero")
+		return []*entity.FileEntity{}, nil
+	}
+	tx, err := d.Client.NewTransaction(ctx, datastore.ReadOnly)

Review Comment:
   Since we use transaction only for reading, the ReadOnly mode is used . It will be better for [performance](https://cloud.google.com/datastore/docs/concepts/transactions#read-only_transactions)



##########
playground/backend/build.gradle.kts:
##########
@@ -40,17 +40,45 @@ task("tidy") {
   }
 }
 
-task("test") {
-  group = "verification"
-  description = "Test the backend"
-  doLast {
-    exec {
-      executable("go")
-      args("test", "./...")
+val startDatastoreEmulator by tasks.registering {
+    doFirst {
+        val process = ProcessBuilder()
+            .directory(projectDir)
+            .inheritIO()
+            .command("sh", "start_datastore_emulator.sh")
+            .start()
+            .waitFor()
+        if (process == 0) {
+            println("Datastore emulator started")
+        } else {
+            println("Failed to start datastore emulator")
+        }
+    }
+}
+
+val stopDatastoreEmulator by tasks.registering {
+    doLast {
+        exec {
+            executable("sh")
+            args("stop_datastore_emulator.sh")
+        }
+    }
+}
+
+val test by tasks.registering {
+    group = "verification"
+    description = "Test the backend"
+    doLast {
+        exec {
+            executable("go")
+            args("test", "./...")
+        }
     }
-  }
 }
 
+test { dependsOn(startDatastoreEmulator) }

Review Comment:
   It needs to start the datastore emulator before running tests and stop that at the end of testing



##########
playground/infrastructure/proxy/allow_list.py:
##########
@@ -18,7 +18,9 @@
 ALLOWED_LIST = [
     "localhost",
     "127.0.0.1",
-    "logging.googleapis.com"
+    "logging.googleapis.com",

Review Comment:
   Since router server in charge of datastore interaction, we added more allowed addresses for proxy



-- 
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] pabloem commented on pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #22140:
URL: https://github.com/apache/beam/pull/22140#issuecomment-1183771474

   hm this PR is very large : ( - maybe in the future we can do smaller PRs so that it's less difficult to review?
   
   no need to change it for now, as I want to make sure we're making progress, but for following PRs, it would be great to avoid so much code in a single one


-- 
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] pabloem commented on a diff in pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
pabloem commented on code in PR #22140:
URL: https://github.com/apache/beam/pull/22140#discussion_r920556221


##########
playground/backend/build.gradle.kts:
##########
@@ -40,17 +40,45 @@ task("tidy") {
   }
 }
 
-task("test") {
-  group = "verification"
-  description = "Test the backend"
-  doLast {
-    exec {
-      executable("go")
-      args("test", "./...")
+val startDatastoreEmulator by tasks.registering {
+    doFirst {
+        val process = ProcessBuilder()

Review Comment:
   huh cool. Thanks.



##########
playground/backend/internal/db/entity/snippet.go:
##########
@@ -0,0 +1,71 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package entity
+
+import (
+	"beam.apache.org/playground/backend/internal/utils"
+	"cloud.google.com/go/datastore"
+	"fmt"
+	"sort"
+	"strings"
+	"time"
+)
+
+type FileEntity struct {
+	Name     string `datastore:"name"`
+	Content  string `datastore:"content,noindex"`
+	CntxLine int32  `datastore:"cntxLine"`
+	IsMain   bool   `datastore:"isMain"`
+}
+
+type SnippetEntity struct {
+	OwnerId       string         `datastore:"ownerId"`
+	Sdk           *datastore.Key `datastore:"sdk"`
+	PipeOpts      string         `datastore:"pipeOpts"`
+	Created       time.Time      `datastore:"created"`
+	LVisited      time.Time      `datastore:"lVisited"`
+	Origin        string         `datastore:"origin"`
+	VisitCount    int            `datastore:"visitCount"`
+	SchVer        *datastore.Key `datastore:"schVer"`
+	NumberOfFiles int            `datastore:"numberOfFiles"`
+}
+
+type Snippet struct {
+	*IDMeta
+	Snippet *SnippetEntity
+	Files   []*FileEntity
+}
+
+// ID generates id according to content of the entity
+func (s *Snippet) ID() (string, error) {
+	var files []string
+	for _, v := range s.Files {
+		files = append(files, strings.TrimSpace(v.Content)+strings.TrimSpace(v.Name))
+	}
+	sort.Strings(files)
+	var contentBuilder strings.Builder
+	for i, file := range files {
+		contentBuilder.WriteString(file)
+		if i == len(files)-1 {
+			contentBuilder.WriteString(fmt.Sprintf("%v%s", s.Snippet.Sdk, strings.TrimSpace(s.Snippet.PipeOpts)))
+		}
+	}
+	id, err := utils.ID(s.Salt, contentBuilder.String(), s.IdLength)

Review Comment:
   makes sense - so if a user accidentally clicks the button twice, will we generate the same ID twice?



-- 
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] pabloem commented on pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
pabloem commented on PR #22140:
URL: https://github.com/apache/beam/pull/22140#issuecomment-1185681852

   ok LGTM


-- 
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] pabloem merged pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
pabloem merged PR #22140:
URL: https://github.com/apache/beam/pull/22140


-- 
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] vchunikhin commented on a diff in pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
vchunikhin commented on code in PR #22140:
URL: https://github.com/apache/beam/pull/22140#discussion_r922140397


##########
.github/workflows/build_playground_backend.yml:
##########
@@ -51,6 +51,7 @@ jobs:
         uses: google-github-actions/setup-gcloud@v0
         with:
             install_components: 'beta,cloud-datastore-emulator'
+            version: '389.0.0'

Review Comment:
   It was necessary because the last version causes some bad effects, e.g. it requires Java 11+ in lieu of Java 8



-- 
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] vchunikhin commented on a diff in pull request #22140: [Playground Task] Sharing any code API

Posted by GitBox <gi...@apache.org>.
vchunikhin commented on code in PR #22140:
URL: https://github.com/apache/beam/pull/22140#discussion_r921788159


##########
playground/backend/internal/db/entity/snippet.go:
##########
@@ -0,0 +1,71 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package entity
+
+import (
+	"beam.apache.org/playground/backend/internal/utils"
+	"cloud.google.com/go/datastore"
+	"fmt"
+	"sort"
+	"strings"
+	"time"
+)
+
+type FileEntity struct {
+	Name     string `datastore:"name"`
+	Content  string `datastore:"content,noindex"`
+	CntxLine int32  `datastore:"cntxLine"`
+	IsMain   bool   `datastore:"isMain"`
+}
+
+type SnippetEntity struct {
+	OwnerId       string         `datastore:"ownerId"`
+	Sdk           *datastore.Key `datastore:"sdk"`
+	PipeOpts      string         `datastore:"pipeOpts"`
+	Created       time.Time      `datastore:"created"`
+	LVisited      time.Time      `datastore:"lVisited"`
+	Origin        string         `datastore:"origin"`
+	VisitCount    int            `datastore:"visitCount"`
+	SchVer        *datastore.Key `datastore:"schVer"`
+	NumberOfFiles int            `datastore:"numberOfFiles"`
+}
+
+type Snippet struct {
+	*IDMeta
+	Snippet *SnippetEntity
+	Files   []*FileEntity
+}
+
+// ID generates id according to content of the entity
+func (s *Snippet) ID() (string, error) {
+	var files []string
+	for _, v := range s.Files {
+		files = append(files, strings.TrimSpace(v.Content)+strings.TrimSpace(v.Name))
+	}
+	sort.Strings(files)
+	var contentBuilder strings.Builder
+	for i, file := range files {
+		contentBuilder.WriteString(file)
+		if i == len(files)-1 {
+			contentBuilder.WriteString(fmt.Sprintf("%v%s", s.Snippet.Sdk, strings.TrimSpace(s.Snippet.PipeOpts)))
+		}
+	}
+	id, err := utils.ID(s.Salt, contentBuilder.String(), s.IdLength)

Review Comment:
   @pabloem hi! Yes, it will be always the same ID since this method depends on a snippet content.
   When you change a file name, a file content, pipeline options or SDK it will be other ID.
   In addition, if a user accidentally clicks the button twice, the same object will not be created in the Cloud Datastore.



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