-
Notifications
You must be signed in to change notification settings - Fork 41
/
NativeImageDatastoreSample.java
131 lines (110 loc) · 4.46 KB
/
NativeImageDatastoreSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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<Entity> 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);
}
}