Skip to content

Commit

Permalink
Support using more recent datastore emulator versions via gcloud emul…
Browse files Browse the repository at this point in the history
…ators (#1303)
  • Loading branch information
mziccard authored Oct 5, 2016
1 parent 88344a2 commit 46f808b
Showing 1 changed file with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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");
}
Expand All @@ -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();
Expand All @@ -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]);
}
}
}
Expand All @@ -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.

Copy link
@pauloleitemoreira

pauloleitemoreira Oct 5, 2016

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+)

This comment has been minimized.

Copy link
@mziccard

mziccard Oct 5, 2016

Author Contributor

Thanks for spotting this. I was sloppy writing this regex. I'll take care of fixing.


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;
Expand Down

0 comments on commit 46f808b

Please sign in to comment.