Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
tellet-q committed Jan 2, 2025
1 parent e56c800 commit c70a00e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 deletions.
46 changes: 9 additions & 37 deletions src/main/java/io/qdrant/client/VersionsCompatibilityChecker.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package io.qdrant.client;

import java.util.ArrayList;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Version {
private final int major;
private final int minor;
private final String rest;

public Version(int major, int minor, String rest) {
public Version(int major, int minor) {
this.major = major;
this.minor = minor;
this.rest = rest;
}

public int getMajor() {
Expand All @@ -23,10 +19,6 @@ public int getMajor() {
public int getMinor() {
return minor;
}

public String getRest() {
return rest;
}
}

/** Utility class to check compatibility between server's and client's versions. */
Expand All @@ -45,15 +37,11 @@ private static Version parseVersion(String version) throws IllegalArgumentExcept
String[] parts = version.split("\\.");
int major = parts.length > 0 ? Integer.parseInt(parts[0]) : 0;
int minor = parts.length > 1 ? Integer.parseInt(parts[1]) : 0;
String rest =
parts.length > 2
? String.join(".", new ArrayList<>(Arrays.asList(parts).subList(2, parts.length)))
: "";

return new Version(major, minor, rest);
return new Version(major, minor);
} catch (Exception e) {
throw new IllegalArgumentException(
"Unable to parse version, expected format: x.y.z, found: " + version, e);
"Unable to parse version, expected format: x.y[.z], found: " + version, e);
}
}

Expand All @@ -65,31 +53,15 @@ private static Version parseVersion(String version) throws IllegalArgumentExcept
* @return True if the versions are compatible, false otherwise.
*/
public static boolean isCompatible(String clientVersion, String serverVersion) {
if (clientVersion.isEmpty()) {
logger.warn("Unable to compare with client version {}", clientVersion);
return false;
}

if (serverVersion.isEmpty()) {
logger.warn("Unable to compare with server version {}", serverVersion);
return false;
}

if (clientVersion.equals(serverVersion)) {
return true;
}

try {
Version parsedServerVersion = parseVersion(serverVersion);
Version parsedClientVersion = parseVersion(clientVersion);
Version client = parseVersion(clientVersion);
Version server = parseVersion(serverVersion);

if (client.getMajor() != server.getMajor()) return false;
return Math.abs(client.getMinor() - server.getMinor()) <= 1;

int majorDiff = Math.abs(parsedServerVersion.getMajor() - parsedClientVersion.getMajor());
if (majorDiff >= 1) {
return false;
}
return Math.abs(parsedServerVersion.getMinor() - parsedClientVersion.getMinor()) <= 1;
} catch (IllegalArgumentException e) {
logger.warn("Unable to compare versions: {}", e.getMessage());
logger.warn("Version comparison failed: {}", e.getMessage());
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@
public class VersionsCompatibilityCheckerTest {
private static Stream<Object[]> validVersionProvider() {
return Stream.of(
new Object[] {"1.2.3", 1, 2, "3"},
new Object[] {"1.2.3-alpha", 1, 2, "3-alpha"},
new Object[] {"1.2", 1, 2, ""},
new Object[] {"1", 1, 0, ""},
new Object[] {"1.", 1, 0, ""});
new Object[] {"1.2.3", 1, 2},
new Object[] {"1.2.3-alpha", 1, 2},
new Object[] {"1.2", 1, 2},
new Object[] {"1", 1, 0},
new Object[] {"1.", 1, 0});
}

@ParameterizedTest
@MethodSource("validVersionProvider")
public void testParseVersion_validVersion(
String versionStr, int expectedMajor, int expectedMinor, String expectedRest)
public void testParseVersion_validVersion(String versionStr, int expectedMajor, int expectedMinor)
throws Exception {
Method method =
VersionsCompatibilityChecker.class.getDeclaredMethod("parseVersion", String.class);
method.setAccessible(true);
Version version = (Version) method.invoke(null, versionStr);
assertEquals(expectedMajor, version.getMajor());
assertEquals(expectedMinor, version.getMinor());
assertEquals(expectedRest, version.getRest());
}

private static Stream<String> invalidVersionProvider() {
Expand Down

0 comments on commit c70a00e

Please sign in to comment.