Skip to content

Commit 0e29b6d

Browse files
committed
Implement Issue #389 : Support for MessagePack
checking of version support at querying time
1 parent 0bbef73 commit 0e29b6d

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/main/java/org/influxdb/impl/InfluxDBImpl.java

+24-16
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class InfluxDBImpl implements InfluxDB {
9393
private String database;
9494
private String retentionPolicy = "autogen";
9595
private ConsistencyLevel consistency = ConsistencyLevel.ONE;
96+
private final boolean messagePack;
9697
private final ChunkProccesor chunkProccesor;
9798

9899
/**
@@ -112,6 +113,7 @@ public class InfluxDBImpl implements InfluxDB {
112113
*/
113114
public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client,
114115
final ResponseFormat responseFormat) {
116+
this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
115117
this.hostAddress = parseHostAddress(url);
116118
this.username = username;
117119
this.password = password;
@@ -148,15 +150,6 @@ public InfluxDBImpl(final String url, final String username, final String passwo
148150
.build();
149151
this.influxDBService = this.retrofit.create(InfluxDBService.class);
150152

151-
if (ResponseFormat.MSGPACK.equals(responseFormat)) {
152-
String[] versionNumbers = version().split("\\.");
153-
final int major = Integer.parseInt(versionNumbers[0]);
154-
final int minor = Integer.parseInt(versionNumbers[1]);
155-
final int fromMinor = 4;
156-
if ((major < 2) && ((major != 1) || (minor < fromMinor))) {
157-
throw new InfluxDBException("MessagePack format is only supported from InfluxDB version 1.4 and later");
158-
}
159-
}
160153
}
161154

162155
public InfluxDBImpl(final String url, final String username, final String password,
@@ -168,6 +161,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo
168161
InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client,
169162
final InfluxDBService influxDBService, final JsonAdapter<QueryResult> adapter) {
170163
super();
164+
this.messagePack = false;
171165
this.hostAddress = parseHostAddress(url);
172166
this.username = username;
173167
this.password = password;
@@ -505,7 +499,7 @@ public void write(final int udpPort, final List<String> records) {
505499
*/
506500
@Override
507501
public QueryResult query(final Query query) {
508-
return execute(callQuery(query));
502+
return executeQuery(callQuery(query));
509503
}
510504

511505
/**
@@ -591,7 +585,7 @@ public QueryResult query(final Query query, final TimeUnit timeUnit) {
591585
call = this.influxDBService.query(this.username, this.password, query.getDatabase(),
592586
TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded());
593587
}
594-
return execute(call);
588+
return executeQuery(call);
595589
}
596590

597591
/**
@@ -604,15 +598,15 @@ public void createDatabase(final String name) {
604598
if (this.version().startsWith("0.")) {
605599
createDatabaseQueryString = String.format("CREATE DATABASE IF NOT EXISTS \"%s\"", name);
606600
}
607-
execute(this.influxDBService.postQuery(this.username, this.password, Query.encode(createDatabaseQueryString)));
601+
executeQuery(this.influxDBService.postQuery(this.username, this.password, Query.encode(createDatabaseQueryString)));
608602
}
609603

610604
/**
611605
* {@inheritDoc}
612606
*/
613607
@Override
614608
public void deleteDatabase(final String name) {
615-
execute(this.influxDBService.postQuery(this.username, this.password,
609+
executeQuery(this.influxDBService.postQuery(this.username, this.password,
616610
Query.encode("DROP DATABASE \"" + name + "\"")));
617611
}
618612

@@ -621,7 +615,7 @@ public void deleteDatabase(final String name) {
621615
*/
622616
@Override
623617
public List<String> describeDatabases() {
624-
QueryResult result = execute(this.influxDBService.query(this.username,
618+
QueryResult result = executeQuery(this.influxDBService.query(this.username,
625619
this.password, SHOW_DATABASE_COMMAND_ENCODED));
626620
// {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]}
627621
// Series [name=databases, columns=[name], values=[[mydb], [unittest_1433605300968]]]
@@ -675,6 +669,20 @@ static class ErrorMessage {
675669
public String error;
676670
}
677671

672+
private QueryResult executeQuery(final Call<QueryResult> call) {
673+
if (messagePack) {
674+
String[] versionNumbers = version().split("\\.");
675+
final int major = Integer.parseInt(versionNumbers[0]);
676+
final int minor = Integer.parseInt(versionNumbers[1]);
677+
final int fromMinor = 4;
678+
if ((major < 2) && ((major != 1) || (minor < fromMinor))) {
679+
throw new UnsupportedOperationException(
680+
"MessagePack format is only supported from InfluxDB version 1.4 and later");
681+
}
682+
}
683+
return execute(call);
684+
}
685+
678686
private <T> T execute(final Call<T> call) {
679687
try {
680688
Response<T> response = call.execute();
@@ -762,7 +770,7 @@ public void createRetentionPolicy(final String rpName, final String database, fi
762770
if (isDefault) {
763771
queryBuilder.append(" DEFAULT");
764772
}
765-
execute(this.influxDBService.postQuery(this.username, this.password, Query.encode(queryBuilder.toString())));
773+
executeQuery(this.influxDBService.postQuery(this.username, this.password, Query.encode(queryBuilder.toString())));
766774
}
767775

768776
/**
@@ -797,7 +805,7 @@ public void dropRetentionPolicy(final String rpName, final String database) {
797805
.append("\" ON \"")
798806
.append(database)
799807
.append("\"");
800-
execute(this.influxDBService.postQuery(this.username, this.password,
808+
executeQuery(this.influxDBService.postQuery(this.username, this.password,
801809
Query.encode(queryBuilder.toString())));
802810
}
803811

src/test/java/org/influxdb/InfluxDBTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,9 @@ public void testIsBatchEnabledWithConsistency() {
886886
@Test
887887
@EnabledIfEnvironmentVariable(named = "INFLUXDB_VERSION", matches = "1\\.3|1\\.2|1\\.1")
888888
public void testMessagePackOnOldDbVersion() {
889-
Assertions.assertThrows(InfluxDBException.class, () -> {
890-
TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK);
889+
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
890+
InfluxDB influxDB = TestUtils.connectToInfluxDB(ResponseFormat.MSGPACK);
891+
influxDB.describeDatabases();
891892
});
892893
}
893894

0 commit comments

Comments
 (0)