Skip to content

Commit

Permalink
Added version of query method (influxdata#687)
Browse files Browse the repository at this point in the history
* Added version of query method 

InfluxDBResultMapper was recently updated with this version of .toPojo:
  public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName)
      throws InfluxDBMapperException {
    return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS);
  }

Adding  InfluxDBMapper.query(final Query query, final Class<T> clazz, String measurementName) to take advantage of the new toPojo variation to map a db with variable measurement names to a POJO. Without this pull, mapping Query to Pojo requires both an InfluxDB instance to fetch the query result and an influxDBMapper instance to map to call toPojo directly.
  • Loading branch information
Ahimsaka authored Jul 21, 2020
1 parent 93c0093 commit 125a9ca
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

### Features
- Add an option in `BatchOption` to prevent `InfluxDB#write` from blocking when actions queue is exhausted. [Issue #668](https://github.com/influxdata/influxdb-java/issues/688)
- Added new signature to InfluxDBMapper.query() with params final Query query, final Class<T> clazz, final String measurementName to leverage InfluxDBResultMapper.toPojo method with identical signature.

### Improvements

- Test: Added test for new InfluxDBMapper.query() signature, as well as test for existing InfluxDBMapper.query(Class clazz) signature (previously only InfluxDBMapper.query(Query query, Class clazz) was tested).

## 2.19 [2020-05-18]

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/influxdb/impl/InfluxDBMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public InfluxDBMapper(final InfluxDB influxDB) {
this.influxDB = influxDB;
}

public <T> List<T> query(final Query query, final Class<T> clazz, final String measurementName) {
QueryResult queryResult = influxDB.query(query);
return toPOJO(queryResult, clazz, measurementName);
}

public <T> List<T> query(final Query query, final Class<T> clazz) {
throwExceptionIfMissingAnnotation(clazz);
QueryResult queryResult = influxDB.query(query);
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/org/influxdb/impl/InfluxDBMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ public void testQuery() {
Assert.assertTrue(persistedMeasures.size()>0);
}

@Test
public void testQueryWhenCalledWithClassOnly() {
ServerMeasure serverMeasure = createMeasure();
influxDBMapper.save(serverMeasure);

List<ServerMeasure> persistedMeasures = influxDBMapper.query(ServerMeasure.class);
Assert.assertTrue(persistedMeasures.size()>0);
}

@Test
public void testQueryWhenCalledWithQuery_Class_MeasurementName() {
ServerMeasure serverMeasure = createMeasure();
influxDBMapper.save(serverMeasure);

List<NonAnnotatedServerMeasure> persistedMeasures = influxDBMapper.query(new Query("SELECT * FROM server_measure",UDP_DATABASE), NonAnnotatedServerMeasure.class, "server_measure");
Assert.assertTrue(persistedMeasures.size()>0);
}

@Test
public void testIllegalField() {
InvalidMeasure invalidMeasure = new InvalidMeasure();
Expand Down Expand Up @@ -185,6 +203,87 @@ public void setIp(String ip) {
}
}

static class NonAnnotatedServerMeasure {

/** Check the instant conversions */
@Column(name = "time")
private Instant time;

@Column(name = "name", tag = true)
private String name;

@Column(name = "cpu")
private double cpu;

@Column(name = "healthy")
private boolean healthy;

@Column(name = "min")
private long uptime;

@Column(name = "memory_utilization")
private Double memoryUtilization;

@Column(name = "ip")
private String ip;

public Instant getTime() {
return time;
}

public void setTime(Instant time) {
this.time = time;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getCpu() {
return cpu;
}

public void setCpu(double cpu) {
this.cpu = cpu;
}

public boolean isHealthy() {
return healthy;
}

public void setHealthy(boolean healthy) {
this.healthy = healthy;
}

public long getUptime() {
return uptime;
}

public void setUptime(long uptime) {
this.uptime = uptime;
}

public Double getMemoryUtilization() {
return memoryUtilization;
}

public void setMemoryUtilization(Double memoryUtilization) {
this.memoryUtilization = memoryUtilization;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}
}

@Measurement(name = "invalid_measure", database = UDP_DATABASE)
static class InvalidMeasure {

Expand Down

0 comments on commit 125a9ca

Please sign in to comment.