-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support using more recent datastore emulator versions via gcloud emul…
…ators (#1303)
- Loading branch information
Showing
1 changed file
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,9 +55,11 @@ | |
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
@@ -67,7 +69,7 @@ | |
*/ | ||
public class LocalDatastoreHelper { | ||
private static final Logger log = Logger.getLogger(LocalDatastoreHelper.class.getName()); | ||
private static final String GCD_VERSION = "1.2.0"; | ||
private static final Version GCD_VERSION = Version.fromString("1.2.0"); | ||
private static final double DEFAULT_CONSISTENCY = 0.9; | ||
private static final String GCD_BASENAME = "cloud-datastore-emulator-" + GCD_VERSION; | ||
private static final String GCD_FILENAME = GCD_BASENAME + ".zip"; | ||
|
@@ -120,8 +122,8 @@ private static Path installedGcdPath() { | |
Path installedGcdPath = gcloudPath.resolve("platform").resolve("cloud-datastore-emulator"); | ||
if (Files.exists(installedGcdPath)) { | ||
try { | ||
String installedVersion = installedGcdVersion(); | ||
if (installedVersion != null && installedVersion.startsWith(GCD_VERSION)) { | ||
Version installedVersion = installedGcdVersion(); | ||
if (installedVersion != null && installedVersion.compareTo(GCD_VERSION) >= 0) { | ||
if (log.isLoggable(Level.FINE)) { | ||
log.fine("SDK datastore emulator found"); | ||
} | ||
|
@@ -131,14 +133,14 @@ private static Path installedGcdPath() { | |
log.fine("SDK datastore emulator found but version mismatch"); | ||
} | ||
} | ||
} catch (IOException | InterruptedException ignore) { | ||
} catch (IOException | InterruptedException | IllegalArgumentException ignore) { | ||
// ignore | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static String installedGcdVersion() throws IOException, InterruptedException { | ||
private static Version installedGcdVersion() throws IOException, InterruptedException { | ||
Process process = | ||
CommandWrapper.create().command("gcloud", "version").redirectErrorStream().start(); | ||
process.waitFor(); | ||
|
@@ -148,7 +150,7 @@ private static String installedGcdVersion() throws IOException, InterruptedExcep | |
if (line.startsWith(GCD_VERSION_PREFIX)) { | ||
String[] lineComponents = line.split(" "); | ||
if (lineComponents.length > 1) { | ||
return lineComponents[1]; | ||
return Version.fromString(lineComponents[1]); | ||
} | ||
} | ||
} | ||
|
@@ -171,6 +173,59 @@ private static Path executablePath(String cmd) { | |
return null; | ||
} | ||
|
||
private static class Version implements Comparable<Version> { | ||
|
||
private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+).(\\d+).(\\d+)"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mziccard
Author
Contributor
|
||
|
||
final int major; | ||
final int minor; | ||
final int patch; | ||
|
||
Version(int major, int minor, int patch) { | ||
this.major = major; | ||
this.minor = minor; | ||
this.patch = patch; | ||
} | ||
|
||
@Override | ||
public int compareTo(Version version) { | ||
int result = major - version.major; | ||
if (result == 0) { | ||
result = minor - version.minor; | ||
if (result == 0) { | ||
result = patch - version.patch; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%d.%d.%d", major, minor, patch); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other || other instanceof Version && compareTo((Version) other) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(major, minor, patch); | ||
} | ||
|
||
static Version fromString(String version) { | ||
Matcher matcher = VERSION_PATTERN.matcher(version); | ||
if (matcher.matches()) { | ||
return new Version( | ||
Integer.valueOf(matcher.group(1)), | ||
Integer.valueOf(matcher.group(2)), | ||
Integer.valueOf(matcher.group(3))); | ||
} | ||
throw new IllegalArgumentException("Invalid version format"); | ||
} | ||
} | ||
|
||
private static class ProcessStreamReader extends Thread { | ||
private final BufferedReader reader; | ||
private volatile boolean terminated; | ||
|
The character dot will match any character. This regex will match for example
1a2a0
. If you want to match the literal dot, you should escape it with\\.
:(\\d+)\\.(\\d+)\\.(\\d+)