Skip to content

Commit

Permalink
fix: #80 - added Serializable to Flux* structures
Browse files Browse the repository at this point in the history
  • Loading branch information
rhajek committed Jan 23, 2020
1 parent 3978960 commit e90e1e9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
21 changes: 20 additions & 1 deletion client-core/src/main/java/com/influxdb/query/FluxColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package com.influxdb.query;

import java.io.Serializable;
import java.util.Objects;
import java.util.StringJoiner;
import javax.annotation.Nonnull;

Expand All @@ -40,7 +42,7 @@
* <li>"duration" to {@link java.time.Duration}</li>
* </ul>
*/
public final class FluxColumn {
public final class FluxColumn implements Serializable {

/**
* Column index in record.
Expand Down Expand Up @@ -118,4 +120,21 @@ public String toString() {
.add("defaultValue='" + defaultValue + "'")
.toString();
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final FluxColumn that = (FluxColumn) o;
return index == that.index &&
group == that.group &&
Objects.equals(label, that.label) &&
Objects.equals(dataType, that.dataType) &&
Objects.equals(defaultValue, that.defaultValue);
}

@Override
public int hashCode() {
return Objects.hash(index, label, dataType, group, defaultValue);
}
}
19 changes: 18 additions & 1 deletion client-core/src/main/java/com/influxdb/query/FluxRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
*/
package com.influxdb.query;

import java.io.Serializable;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -36,7 +38,7 @@
*
* <a href="http://bit.ly/flux-spec#record">Specification</a>.
*/
public final class FluxRecord {
public final class FluxRecord implements Serializable {

/**
* The Index of the table that the record belongs.
Expand Down Expand Up @@ -154,4 +156,19 @@ public String toString() {
.add("values=" + values.size())
.toString();
}


@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final FluxRecord that = (FluxRecord) o;
return Objects.equals(table, that.table) &&
Objects.equals(values, that.values);
}

@Override
public int hashCode() {
return Objects.hash(table, values);
}
}
3 changes: 2 additions & 1 deletion client-core/src/main/java/com/influxdb/query/FluxTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package com.influxdb.query;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
Expand All @@ -32,7 +33,7 @@
*
* <a href="http://bit.ly/flux-spec#table">Specification</a>.
*/
public final class FluxTable {
public final class FluxTable implements Serializable {

/**
* Table column's labels and types.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.influxdb.query.internal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.Instant;
import java.util.Map;

import com.influxdb.query.FluxColumn;
import com.influxdb.query.FluxRecord;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
class FluxStructureSerializationTest {

@Test
public void testFluxColumnSerialization() throws IOException, ClassNotFoundException {
FluxColumn source = new FluxColumn();
source.setDataType("dateTime:RFC3339");
source.setDefaultValue("val1");
source.setGroup(true);
source.setIndex(1);
source.setLabel("my-column");

ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(source);

Object copy = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).readObject();

System.out.println("source = " + source);
System.out.println("copy = " + copy);

Assertions.assertEquals(source.hashCode(), copy.hashCode());
Assertions.assertEquals(source, copy);
}

@Test
public void testFluxRecordSerialization() throws IOException, ClassNotFoundException {
FluxRecord source = new FluxRecord(1);
Map<String, Object> values = source.getValues();
values.put("val1", 1);
values.put("val2", Instant.now());
values.put("val3", "my-string");
values.put("val4", 3.14f);

ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(source);

FluxRecord copy = (FluxRecord) new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).readObject();

System.out.println("source = " + source);
System.out.println("copy = " + copy);

Assertions.assertEquals(source.getValues(), copy.getValues());
Assertions.assertEquals(source.hashCode(), copy.hashCode());
Assertions.assertEquals(source, copy);
}

}

0 comments on commit e90e1e9

Please sign in to comment.