Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support java records as measurement classes #983

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public class Cpu {
}
```

2. Add @Measurement,@TimeColumn and @Column annotations (column names default to field names unless otherwise specified):
2. Add @Measurement, @TimeColumn and @Column annotations (column names default to field names unless otherwise specified):

```Java
@Measurement(name = "cpu")
Expand Down Expand Up @@ -364,6 +364,24 @@ public class Cpu {
}
```

Or (if you're on JDK14+ and/or [Android SDK34+](https://android-developers.googleblog.com/2023/06/records-in-android-studio-flamingo.html)):

```Java
@Measurement(name = "cpu", allFields = true)
public record Cpu(
@TimeColumn
Instant time,
@Column(name = "host", tag = true)
String hostname,
@Column(tag = true)
String region,
Double idle,
Boolean happydevop,
@Column(name = "uptimesecs")
Long uptimeSecs
) {}
```

3. Call _InfluxDBResultMapper.toPOJO(...)_ to map the QueryResult to your POJO:

```java
Expand Down
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<!-- Needed for Android desugared record tests -->
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -404,5 +408,50 @@
</plugins>
</build>
</profile>
<profile>
<!-- A different setup for JDK 17 -->
<id>java17</id>
<activation>
<jdk>17</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test-jdk17/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<!-- Enable Java 17 for all sources so that Intellij picks the right language level -->
<source>17</source>
<release>17</release>
<compilerArgs>
<!-- Needed for record tests -->
<arg>-parameters</arg>
<arg>--add-opens=java.base/java.lang=ALL-UNNAMED</arg>
<arg>--add-opens=java.base/java.util=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
3 changes: 3 additions & 0 deletions src/main/java/org/influxdb/annotation/Exclude.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
/**
* When a POJO annotated with {@code @Measurement(allFields = true)} is loaded or saved,
* this annotation can be used to exclude some of its fields.
* <p>
* Note: this is not considered when loading record measurements.
*
* @see Measurement#allFields()
*
* @author Eran Leshem
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/influxdb/annotation/Measurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
/**
* If {@code true}, then all non-static fields of this measurement will be loaded or saved,
* regardless of any {@code @Column} annotations.
* <p>
* Note: When loading record measurements, this is always implied to be true,
* since the record's canonical constructor is used to populate the record.
*
* @see Exclude
*/
boolean allFields() default false;
Expand Down
Loading
Loading