From f209ad4302cdaa95f5e98824457878a09d6f71f9 Mon Sep 17 00:00:00 2001 From: Mridula <66699525+mpeddada1@users.noreply.github.com> Date: Tue, 27 Aug 2024 12:16:23 -0400 Subject: [PATCH] chore: remove datastore native image sample in favor of sample hosted in google-cloud-java (#1519) --- samples/native-image-sample/README.md | 96 ------------ samples/native-image-sample/pom.xml | 141 ------------------ .../datastore/NativeImageDatastoreSample.java | 131 ---------------- .../ITNativeImageDatastoreSample.java | 75 ---------- samples/pom.xml | 1 - 5 files changed, 444 deletions(-) delete mode 100644 samples/native-image-sample/README.md delete mode 100644 samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java delete mode 100644 samples/native-image-sample/src/test/java/com/example/datastore/ITNativeImageDatastoreSample.java diff --git a/samples/native-image-sample/README.md b/samples/native-image-sample/README.md deleted file mode 100644 index 5f2cfbd27..000000000 --- a/samples/native-image-sample/README.md +++ /dev/null @@ -1,96 +0,0 @@ -# Datastore Sample Application with Native Image - -This application uses the [Google Cloud Datastore client library](https://cloud.google.com/datastore/docs/reference/libraries) and is compatible with Native Image compilation. - -This sample runs through some basic operations of creating/deleting entities, running queries, and running transaction code. - -## Setup Instructions - -You will need to follow these prerequisite steps in order to run the samples: - -1. If you have not already, [create a Google Cloud Platform Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project). - -2. Install the [Google Cloud SDK](https://cloud.google.com/sdk/) which will allow you to run the sample with your project's credentials. - - Once installed, log in with Application Default Credentials using the following command: - - ``` - gcloud auth application-default login - ``` - - **Note:** Authenticating with Application Default Credentials is convenient to use during development, but we recommend [alternate methods of authentication](https://cloud.google.com/docs/authentication/production) during production use. - -3. Install the native image compiler. - - You can follow the [installation instructions](https://www.graalvm.org/docs/getting-started/#install-graalvm). - After following the instructions, ensure that you install the native image extension installed by running: - - ``` - gu install native-image - ``` - - Once you finish following the instructions, verify that the default version of Java is set to the correct version by running `java -version` in a terminal. - - You will see something similar to the below output: - - ``` - $ java -version - - openjdk version "17.0.3" 2022-04-19 - OpenJDK Runtime Environment GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06) - OpenJDK 64-Bit Server VM GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing) - ``` -## Sample -1. **(Optional)** If you wish to run the application against the [Datastore emulator](https://cloud.google.com/sdk/gcloud/reference/beta/emulators/datastore), ensure that you have the [Google Cloud SDK](https://cloud.google.com/sdk) installed. - - In a new terminal window, start the emulator via `gcloud`: - - ``` - gcloud beta emulators datastore start --host-port=localhost:9010 - ``` - - Leave the emulator running in this terminal for now. - In the next section, we will run the sample application against the Datastore emulator instance. - -2. Navigate to this directory and compile the application with the native image compiler. - - ``` - mvn package -P native -DskipTests - ``` - -3. **(Optional)** If you're using the emulator, export the `DATASTORE_EMULATOR_HOST` as an environment variable in your terminal. - - ``` - export DATASTORE_EMULATOR_HOST=localhost:9010 - ``` - - The Datastore Client Libraries will detect this environment variable and automatically connect to the emulator instance if this variable is set. - -4. Run the application. - - ``` - ./target/native-image-sample - ``` - -5. The application will run through some basic Datastore operations and log some output statements. - - ``` - Successfully added entity. - Reading entity: 1cf34cc1-2b8a-4945-9fc4-058f03dcd08e - Successfully deleted entity: 1cf34cc1-2b8a-4945-9fc4-058f03dcd08e - Run fake transaction code. - Found entity: - name=de4f36f4-3936-4252-98d3-e0d56d485254 - kind=test-kind - namespace=nativeimage-test-namespace - properties={description=StringValue{valueType=STRING, excludeFromIndexes=false, meaning=0, value=hello world}} - Ran transaction callable. - ``` - -### Sample Integration test with Native Image Support - -In order to run the sample integration test as a native image, call the following command: - - ``` - mvn test -Pnative - ``` diff --git a/samples/native-image-sample/pom.xml b/samples/native-image-sample/pom.xml index 5a0ee2386..e69de29bb 100644 --- a/samples/native-image-sample/pom.xml +++ b/samples/native-image-sample/pom.xml @@ -1,141 +0,0 @@ - - - 4.0.0 - com.example.datastore - native-image-sample - Native Image Sample - https://github.com/googleapis/java-datastore - - - - com.google.cloud.samples - shared-configuration - 1.2.0 - - - - - 1.8 - 1.8 - UTF-8 - - - - - - com.google.cloud - libraries-bom - 26.43.0 - pom - import - - - - - - - com.google.cloud - google-cloud-datastore - - - - junit - junit - 4.13.2 - test - - - com.google.truth - truth - 1.4.3 - test - - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - com.example.datastore.NativeImageDatastoreSample - - - - - - - - - - - native - - - - org.junit.vintage - junit-vintage-engine - 5.10.2 - test - - - org.graalvm.buildtools - junit-platform-native - 0.10.2 - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - 3.2.5 - - - **/IT* - - - - - org.graalvm.buildtools - native-maven-plugin - 0.10.2 - true - - com.example.datastore.NativeImageDatastoreSample - - --no-fallback - --no-server - - - - - build-native - - build - test - - package - - - test-native - - test - - test - - - - - - - - \ No newline at end of file diff --git a/samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java b/samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java deleted file mode 100644 index 7ce5c900a..000000000 --- a/samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright 2020-2021 Google LLC - * - * Licensed 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 - * - * https://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 com.example.datastore; - -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreOptions; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; -import com.google.cloud.datastore.Query; -import com.google.cloud.datastore.QueryResults; -import com.google.cloud.datastore.StructuredQuery; -import com.google.cloud.datastore.Transaction; -import java.time.Duration; -import java.time.Instant; -import java.util.UUID; - -/** Sample Datastore Application. */ -public class NativeImageDatastoreSample { - - /* Datastore namespace where entities will be created. */ - private static final String TEST_NAMESPACE = "nativeimage-test-namespace"; - - /* Datastore kind used. */ - private static final String TEST_KIND = "test-kind"; - - /** Entrypoint to the Datastore sample application. */ - public static void main(String[] args) { - Instant startTime = Instant.now(); - Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); - - String testId = UUID.randomUUID().toString(); - - addEntity(datastore, testId); - getEntity(datastore, testId); - deleteEntity(datastore, testId); - - runTransaction(datastore); - - String id = UUID.randomUUID().toString(); - Key key = createKey(datastore, id); - runTransactionCallable(datastore, key); - Instant endTime = Instant.now(); - Duration duration = Duration.between(startTime, endTime); - System.out.println("Duration: " + duration.toString()); - } - - static void addEntity(Datastore datastore, String id) { - Key key = createKey(datastore, id); - Entity entity = Entity.newBuilder(key).set("description", "hello world").build(); - datastore.add(entity); - System.out.println("Successfully added entity."); - } - - static void getEntity(Datastore datastore, String id) { - Key key = createKey(datastore, id); - Entity entity = datastore.get(key); - System.out.println("Reading entity: " + entity.getKey().getName()); - } - - static void deleteEntity(Datastore datastore, String id) { - Key key = createKey(datastore, id); - datastore.delete(key); - - Entity entity = datastore.get(key); - if (entity == null) { - System.out.println("Successfully deleted entity: " + id); - } else { - throw new RuntimeException("Failed to delete entity: " + id); - } - } - - static void runTransactionCallable(Datastore datastore, Key entityKey) { - datastore.runInTransaction( - client -> { - Entity entity = Entity.newBuilder(entityKey).set("description", "hello world").build(); - datastore.add(entity); - - StructuredQuery query = - Query.newEntityQueryBuilder().setNamespace(TEST_NAMESPACE).setKind(TEST_KIND).build(); - - QueryResults results = datastore.run(query); - while (results.hasNext()) { - Entity result = results.next(); - String name = result.getKey().getName(); - String kind = result.getKey().getKind(); - String namespace = result.getKey().getNamespace(); - System.out.println( - "Found entity:" - + "\n\t\tname=" - + name - + "\n\t\tkind=" - + kind - + "\n\t\tnamespace=" - + namespace - + "\n\t\tproperties=" - + result.getProperties().toString()); - } - - datastore.delete(entityKey); - return null; - }); - - System.out.println("Ran transaction callable."); - } - - private static void runTransaction(Datastore datastore) { - Transaction transaction = datastore.newTransaction(); - transaction.commit(); - transaction = datastore.newTransaction(); - transaction.rollback(); - System.out.println("Run fake transaction code."); - } - - static Key createKey(Datastore datastore, String id) { - return datastore.newKeyFactory().setNamespace(TEST_NAMESPACE).setKind(TEST_KIND).newKey(id); - } -} diff --git a/samples/native-image-sample/src/test/java/com/example/datastore/ITNativeImageDatastoreSample.java b/samples/native-image-sample/src/test/java/com/example/datastore/ITNativeImageDatastoreSample.java deleted file mode 100644 index 710f18367..000000000 --- a/samples/native-image-sample/src/test/java/com/example/datastore/ITNativeImageDatastoreSample.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * Licensed 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 - * - * https://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 com.example.datastore; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreOptions; -import com.google.cloud.datastore.Key; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.junit.Before; -import org.junit.Test; - -/** Tests for {@link com.example.datastore.NativeImageDatastoreSample} */ -public class ITNativeImageDatastoreSample { - - private Datastore datastore; - private ByteArrayOutputStream bout; - private PrintStream out; - - @Before - public void setUp() { - datastore = DatastoreOptions.getDefaultInstance().getService(); - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @Test - public void testAddAndGetEntity() { - bout.reset(); - String testId = "test-id-" + UUID.randomUUID(); - NativeImageDatastoreSample.addEntity(datastore, testId); - NativeImageDatastoreSample.getEntity(datastore, testId); - assertThat(bout.toString()).contains("Reading entity: " + testId); - - NativeImageDatastoreSample.deleteEntity(datastore, testId); - } - - @Test - public void testRunTransactionalCallable() { - bout.reset(); - String testId = "test-id-" + UUID.randomUUID(); - Key key = NativeImageDatastoreSample.createKey(datastore, testId); - NativeImageDatastoreSample.runTransactionCallable(datastore, key); - assertThat(bout.toString()) - .contains( - "Found entity:" - + "\n\t\tname=" - + testId - + "\n\t\tkind=test-kind" - + "\n\t\tnamespace=nativeimage-test-namespace" - + "\n\t\tproperties={description=StringValue{valueType=STRING, excludeFromIndexes=false," - + " meaning=0, value=hello world}}\n" - + "Ran transaction callable."); - - NativeImageDatastoreSample.deleteEntity(datastore, "test-id"); - } -} diff --git a/samples/pom.xml b/samples/pom.xml index e81bec450..2e970d081 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -31,7 +31,6 @@ install-without-bom snapshot snippets - native-image-sample