Skip to content

Commit b826220

Browse files
committed
feat: Add firestoreInDatastoreMode for datastore emulator
1 parent 9b63580 commit b826220

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
5353
private final double consistency;
5454
private final Path gcdPath;
5555
private boolean storeOnDisk;
56+
private boolean firestoreInDatastoreMode;
5657

5758
// Gcloud emulator settings
5859
private static final String GCLOUD_CMD_TEXT = "gcloud beta emulators datastore start";
@@ -74,6 +75,8 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
7475
private static final String PROJECT_FLAG = "--project=";
7576
private static final double DEFAULT_CONSISTENCY = 0.9;
7677
private static final String DEFAULT_PROJECT_ID = PROJECT_ID_PREFIX + UUID.randomUUID();
78+
private static final String FIRESTORE_IN_DATASTORE_MODE_FLAG =
79+
"--use-firestore-in-datastore-mode";
7780

7881
private static final Logger LOGGER = Logger.getLogger(LocalDatastoreHelper.class.getName());
7982

@@ -102,6 +105,7 @@ public static class Builder {
102105
private int port;
103106
private Path dataDir;
104107
private boolean storeOnDisk = true;
108+
private boolean firestoreInDatastoreMode = false;
105109
private String projectId;
106110

107111
private Builder() {}
@@ -110,6 +114,7 @@ private Builder(LocalDatastoreHelper helper) {
110114
this.consistency = helper.consistency;
111115
this.dataDir = helper.gcdPath;
112116
this.storeOnDisk = helper.storeOnDisk;
117+
this.firestoreInDatastoreMode = helper.firestoreInDatastoreMode;
113118
}
114119

115120
public Builder setConsistency(double consistency) {
@@ -136,6 +141,10 @@ public Builder setStoreOnDisk(boolean storeOnDisk) {
136141
this.storeOnDisk = storeOnDisk;
137142
return this;
138143
}
144+
public Builder setFirestoreInDatastoreMode(boolean firestoreInDatastoreMode) {
145+
this.firestoreInDatastoreMode = firestoreInDatastoreMode;
146+
return this;
147+
}
139148

140149
/** Creates a {@code LocalDatastoreHelper} object. */
141150
public LocalDatastoreHelper build() {
@@ -152,14 +161,21 @@ private LocalDatastoreHelper(Builder builder) {
152161
this.consistency = builder.consistency > 0 ? builder.consistency : DEFAULT_CONSISTENCY;
153162
this.gcdPath = builder.dataDir;
154163
this.storeOnDisk = builder.storeOnDisk;
164+
this.firestoreInDatastoreMode = builder.firestoreInDatastoreMode;
155165
String binName = BIN_NAME;
156166
if (isWindows()) {
157167
binName = BIN_NAME.replace("/", "\\");
158168
}
159169
List<String> gcloudCommand = new ArrayList<>(Arrays.asList(GCLOUD_CMD_TEXT.split(" ")));
160170
gcloudCommand.add(GCLOUD_CMD_PORT_FLAG + "localhost:" + getPort());
161-
gcloudCommand.add(CONSISTENCY_FLAG + builder.consistency);
162171
gcloudCommand.add(PROJECT_FLAG + projectId);
172+
if (builder.firestoreInDatastoreMode) {
173+
gcloudCommand.add(FIRESTORE_IN_DATASTORE_MODE_FLAG);
174+
} else {
175+
// At most one of --consistency | --use-firestore-in-datastore-mode can be specified.
176+
// --consistency will be ignored with --use-firestore-in-datastore-mode.
177+
gcloudCommand.add(CONSISTENCY_FLAG + builder.consistency);
178+
}
163179
if (!builder.storeOnDisk) {
164180
gcloudCommand.add("--no-store-on-disk");
165181
}
@@ -170,8 +186,14 @@ private LocalDatastoreHelper(Builder builder) {
170186
new GcloudEmulatorRunner(gcloudCommand, VERSION_PREFIX, MIN_VERSION);
171187
List<String> binCommand = new ArrayList<>(Arrays.asList(binName, "start"));
172188
binCommand.add("--testing");
189+
if (builder.firestoreInDatastoreMode) {
190+
binCommand.add(FIRESTORE_IN_DATASTORE_MODE_FLAG);
191+
} else {
192+
// At most one of --consistency | --use-firestore-in-datastore-mode can be specified.
193+
// --consistency will be ignored with --use-firestore-in-datastore-mode.
194+
binCommand.add(CONSISTENCY_FLAG + getConsistency());
195+
}
173196
binCommand.add(BIN_CMD_PORT_FLAG + getPort());
174-
binCommand.add(CONSISTENCY_FLAG + getConsistency());
175197
DownloadableEmulatorRunner downloadRunner =
176198
new DownloadableEmulatorRunner(binCommand, EMULATOR_URL, MD5_CHECKSUM, ACCESS_TOKEN);
177199
this.emulatorRunners = ImmutableList.of(gcloudRunner, downloadRunner);
@@ -235,6 +257,11 @@ public boolean isStoreOnDisk() {
235257
return storeOnDisk;
236258
}
237259

260+
/** Returns {@code true} use firestore-in-datastore-mode, otherwise {@code false} use native mode. */
261+
public boolean isFirestoreInDatastoreMode() {
262+
return firestoreInDatastoreMode;
263+
}
264+
238265
/**
239266
* Creates a local Datastore helper with the specified settings for project ID and consistency.
240267
*

google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ public void testCreateWithBuilder() {
7676
.setConsistency(0.75)
7777
.setPort(8081)
7878
.setStoreOnDisk(false)
79+
.setFirestoreInDatastoreMode(true)
7980
.setDataDir(dataDir)
8081
.build();
8182
assertTrue(Math.abs(0.75 - helper.getConsistency()) < TOLERANCE);
8283
assertTrue(helper.getProjectId().startsWith(PROJECT_ID_PREFIX));
8384
assertFalse(helper.isStoreOnDisk());
85+
assertTrue(helper.isFirestoreInDatastoreMode());
8486
assertEquals(8081, helper.getPort());
8587
assertEquals(dataDir, helper.getGcdPath());
8688
LocalDatastoreHelper incompleteHelper = LocalDatastoreHelper.newBuilder().build();
@@ -103,11 +105,13 @@ public void testCreateWithToBuilder() throws IOException {
103105
.setConsistency(0.75)
104106
.setPort(8081)
105107
.setStoreOnDisk(false)
108+
.setFirestoreInDatastoreMode(true)
106109
.setDataDir(dataDir)
107110
.build();
108111
assertTrue(Math.abs(0.75 - helper.getConsistency()) < TOLERANCE);
109112
assertTrue(helper.getProjectId().startsWith(PROJECT_ID_PREFIX));
110113
assertFalse(helper.isStoreOnDisk());
114+
assertTrue(helper.isFirestoreInDatastoreMode());
111115
assertEquals(8081, helper.getPort());
112116
assertEquals(dataDir, helper.getGcdPath());
113117
LocalDatastoreHelper actualHelper = helper.toBuilder().build();
@@ -119,10 +123,12 @@ public void testCreateWithToBuilder() throws IOException {
119123
.setConsistency(0.85)
120124
.setPort(9091)
121125
.setStoreOnDisk(true)
126+
.setFirestoreInDatastoreMode(false)
122127
.setDataDir(dataDir)
123128
.build();
124129
assertTrue(Math.abs(0.85 - actualHelper.getConsistency()) < TOLERANCE);
125130
assertTrue(actualHelper.isStoreOnDisk());
131+
assertFalse(actualHelper.isFirestoreInDatastoreMode());
126132
assertEquals(9091, actualHelper.getPort());
127133
assertEquals(dataDir, actualHelper.getGcdPath());
128134
LocalDatastoreHelper.deleteRecursively(dataDir);
@@ -205,11 +211,27 @@ public void testStartStopResetWithBuilder()
205211
assertNotNull(ex.getMessage());
206212
}
207213
}
214+
@Test
215+
public void testCreateWithFirestoreInDatastoreMode()
216+
throws IOException, InterruptedException, TimeoutException {
217+
LocalDatastoreHelper helper = LocalDatastoreHelper.newBuilder().setFirestoreInDatastoreMode(true).build();
218+
assertTrue(helper.isFirestoreInDatastoreMode());
219+
helper.start();
220+
Datastore datastore = helper.getOptions().getService();
221+
Key key = datastore.newKeyFactory().setKind("kind").newKey("name");
222+
Entity expected = Entity.newBuilder(key).build();
223+
datastore.put(expected);
224+
assertNotNull(datastore.get(key));
225+
Entity actual = datastore.get(key);
226+
assertEquals(expected, actual);
227+
helper.stop();
228+
}
208229

209230
public void assertLocalDatastoreHelpersEquivelent(
210231
LocalDatastoreHelper expected, LocalDatastoreHelper actual) {
211232
assertEquals(expected.getConsistency(), actual.getConsistency(), 0);
212233
assertEquals(expected.isStoreOnDisk(), actual.isStoreOnDisk());
234+
assertEquals(expected.isFirestoreInDatastoreMode(), actual.isFirestoreInDatastoreMode());
213235
assertEquals(expected.getGcdPath(), actual.getGcdPath());
214236
}
215237
}

0 commit comments

Comments
 (0)