35
35
import java .util .List ;
36
36
import java .util .UUID ;
37
37
import java .util .concurrent .TimeoutException ;
38
- import java .util .logging .Level ;
39
38
import java .util .logging .Logger ;
40
39
import org .threeten .bp .Duration ;
41
40
@@ -50,6 +49,7 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
50
49
private final List <EmulatorRunner > emulatorRunners ;
51
50
private final double consistency ;
52
51
private final Path gcdPath ;
52
+ private boolean storeOnDisk ;
53
53
54
54
// Gcloud emulator settings
55
55
private static final String GCLOUD_CMD_TEXT = "gcloud beta emulators datastore start" ;
@@ -78,39 +78,87 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper<DatastoreOptions> {
78
78
}
79
79
}
80
80
81
- private LocalDatastoreHelper (double consistency , int port ) {
81
+ /** A builder for {@code LocalDatastoreHelper} objects. */
82
+ public static class Builder {
83
+ private double consistency ;
84
+ private int port ;
85
+ private Path dataDir ;
86
+ private boolean storeOnDisk = true ;
87
+
88
+ private Builder () {}
89
+
90
+ private Builder (LocalDatastoreHelper helper ) {
91
+ this .consistency = helper .consistency ;
92
+ this .dataDir = helper .gcdPath ;
93
+ this .storeOnDisk = helper .storeOnDisk ;
94
+ }
95
+
96
+ public Builder setConsistency (double consistency ) {
97
+ this .consistency = consistency ;
98
+ return this ;
99
+ }
100
+
101
+ public Builder setPort (int port ) {
102
+ this .port = port ;
103
+ return this ;
104
+ }
105
+
106
+ public Builder setDataDir (Path dataDir ) {
107
+ this .dataDir = dataDir ;
108
+ return this ;
109
+ }
110
+
111
+ public Builder setStoreOnDisk (boolean storeOnDisk ) {
112
+ this .storeOnDisk = storeOnDisk ;
113
+ return this ;
114
+ }
115
+
116
+ /** Creates a {@code LocalDatastoreHelper} object. */
117
+ public LocalDatastoreHelper build () {
118
+ return new LocalDatastoreHelper (this );
119
+ }
120
+ }
121
+
122
+ private LocalDatastoreHelper (Builder builder ) {
82
123
super (
83
124
"datastore" ,
84
- port > 0 ? port : BaseEmulatorHelper .findAvailablePort (DEFAULT_PORT ),
125
+ builder . port > 0 ? builder . port : BaseEmulatorHelper .findAvailablePort (DEFAULT_PORT ),
85
126
PROJECT_ID_PREFIX + UUID .randomUUID ().toString ());
86
- Path tmpDirectory = null ;
87
- try {
88
- tmpDirectory = Files .createTempDirectory ("gcd" );
89
- } catch (IOException ex ) {
90
- getLogger ().log (Level .WARNING , "Failed to create temporary directory" );
91
- }
92
- this .gcdPath = tmpDirectory ;
93
- this .consistency = consistency ;
127
+ this .consistency = builder .consistency > 0 ? builder .consistency : DEFAULT_CONSISTENCY ;
128
+ this .gcdPath = builder .dataDir ;
129
+ this .storeOnDisk = builder .storeOnDisk ;
94
130
String binName = BIN_NAME ;
95
131
if (isWindows ()) {
96
132
binName = BIN_NAME .replace ("/" , "\\ " );
97
133
}
98
134
List <String > gcloudCommand = new ArrayList <>(Arrays .asList (GCLOUD_CMD_TEXT .split (" " )));
99
135
gcloudCommand .add (GCLOUD_CMD_PORT_FLAG + "localhost:" + getPort ());
100
- gcloudCommand .add (CONSISTENCY_FLAG + consistency );
101
- gcloudCommand .add ("--no-store-on-disk" );
136
+ gcloudCommand .add (CONSISTENCY_FLAG + builder .consistency );
137
+ if (!builder .storeOnDisk ) {
138
+ gcloudCommand .add ("--no-store-on-disk" );
139
+ }
102
140
GcloudEmulatorRunner gcloudRunner =
103
141
new GcloudEmulatorRunner (gcloudCommand , VERSION_PREFIX , MIN_VERSION );
104
142
List <String > binCommand = new ArrayList <>(Arrays .asList (binName , "start" ));
105
143
binCommand .add ("--testing" );
106
144
binCommand .add (BIN_CMD_PORT_FLAG + getPort ());
107
- binCommand .add (CONSISTENCY_FLAG + consistency );
108
- if (gcdPath != null ) {
109
- gcloudCommand .add ("--data-dir=" + gcdPath . toString ());
145
+ binCommand .add (CONSISTENCY_FLAG + getConsistency () );
146
+ if (builder . dataDir != null ) {
147
+ gcloudCommand .add ("--data-dir=" + getGcdPath ());
110
148
}
111
149
DownloadableEmulatorRunner downloadRunner =
112
150
new DownloadableEmulatorRunner (binCommand , EMULATOR_URL , MD5_CHECKSUM );
113
- emulatorRunners = ImmutableList .of (gcloudRunner , downloadRunner );
151
+ this .emulatorRunners = ImmutableList .of (gcloudRunner , downloadRunner );
152
+ }
153
+
154
+ /** Returns a builder for {@code LocalDatastoreHelper} object. */
155
+ public LocalDatastoreHelper .Builder toBuilder () {
156
+ return new Builder (this );
157
+ }
158
+
159
+ /** Returns a builder for {@code LocalDatastoreHelper} object. */
160
+ public static LocalDatastoreHelper .Builder newBuilder () {
161
+ return new LocalDatastoreHelper .Builder ();
114
162
}
115
163
116
164
@ Override
@@ -153,6 +201,16 @@ public double getConsistency() {
153
201
return consistency ;
154
202
}
155
203
204
+ /** Returns the data directory path of the local Datastore emulator. */
205
+ public Path getGcdPath () {
206
+ return gcdPath ;
207
+ }
208
+
209
+ /** Returns {@code true} data persist on disk, otherwise {@code false} data not store on disk. */
210
+ public boolean isStoreOnDisk () {
211
+ return storeOnDisk ;
212
+ }
213
+
156
214
/**
157
215
* Creates a local Datastore helper with the specified settings for project ID and consistency.
158
216
*
@@ -162,7 +220,7 @@ public double getConsistency() {
162
220
* consistency of non-ancestor queries; non-ancestor queries are eventually consistent.
163
221
*/
164
222
public static LocalDatastoreHelper create (double consistency ) {
165
- return create ( consistency , 0 );
223
+ return LocalDatastoreHelper . newBuilder (). setConsistency ( consistency ). setPort ( 0 ). build ( );
166
224
}
167
225
168
226
/**
@@ -176,7 +234,7 @@ public static LocalDatastoreHelper create(double consistency) {
176
234
* emulator will search for a free random port.
177
235
*/
178
236
public static LocalDatastoreHelper create (double consistency , int port ) {
179
- return new LocalDatastoreHelper ( consistency , port );
237
+ return LocalDatastoreHelper . newBuilder (). setConsistency ( consistency ). setPort ( port ). build ( );
180
238
}
181
239
182
240
/**
@@ -187,7 +245,10 @@ public static LocalDatastoreHelper create(double consistency, int port) {
187
245
* emulator will search for a free random port.
188
246
*/
189
247
public static LocalDatastoreHelper create (int port ) {
190
- return new LocalDatastoreHelper (DEFAULT_CONSISTENCY , port );
248
+ return LocalDatastoreHelper .newBuilder ()
249
+ .setConsistency (DEFAULT_CONSISTENCY )
250
+ .setPort (port )
251
+ .build ();
191
252
}
192
253
193
254
/**
@@ -197,7 +258,7 @@ public static LocalDatastoreHelper create(int port) {
197
258
* all writes are immediately visible.
198
259
*/
199
260
public static LocalDatastoreHelper create () {
200
- return create ( DEFAULT_CONSISTENCY );
261
+ return LocalDatastoreHelper . newBuilder (). setConsistency ( DEFAULT_CONSISTENCY ). build ( );
201
262
}
202
263
203
264
/**
@@ -254,7 +315,7 @@ public void stop() throws IOException, InterruptedException, TimeoutException {
254
315
stop (Duration .ofSeconds (20 ));
255
316
}
256
317
257
- private static void deleteRecursively (Path path ) throws IOException {
318
+ static void deleteRecursively (Path path ) throws IOException {
258
319
if (path == null || !Files .exists (path )) {
259
320
return ;
260
321
}
0 commit comments