Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit fecf985

Browse files
committed
fix issue influxdata#517 : missing millis and nanos in MsgPack
1 parent 24c5542 commit fecf985

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/main/java/org/influxdb/msgpack/MessagePackTraverser.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Iterator;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.concurrent.TimeUnit;
1112

1213
import org.influxdb.InfluxDBException;
1314
import org.influxdb.dto.QueryResult;
@@ -27,6 +28,7 @@
2728
*/
2829
public class MessagePackTraverser {
2930

31+
private static final byte MSG_PACK_TIME_EXT_TYPE = 5;
3032
private String lastStringNode;
3133

3234
/**
@@ -229,14 +231,17 @@ void traverse(final MessageUnpacker unpacker, final QueryResultModelPath queryRe
229231
}
230232
break;
231233
case EXTENSION:
232-
final byte msgPackTimeExtType = (byte) 5;
233-
final int timeOffset = 0;
234-
final int timeByteArrayLength = 8;
234+
final int nanosStartIndex = 8;
235235
extension = unpacker.unpackExtensionTypeHeader();
236-
if (extension.getType() == msgPackTimeExtType) {
236+
if (extension.getType() == MSG_PACK_TIME_EXT_TYPE) {
237+
//decode epoch nanos in accordance with https://github.com/tinylib/msgp/blob/master/msgp/write.go#L594
238+
237239
dst = new byte[extension.getLength()];
238240
unpacker.readPayload(dst);
239-
o = ByteBuffer.wrap(dst, timeOffset, timeByteArrayLength).getLong();
241+
ByteBuffer bf = ByteBuffer.wrap(dst, 0, extension.getLength());
242+
long epochSeconds = bf.getLong();
243+
int nanosOffset = bf.getInt(nanosStartIndex);
244+
o = TimeUnit.SECONDS.toNanos(epochSeconds) + nanosOffset;
240245
}
241246
break;
242247

src/test/java/org/influxdb/MessagePackInfluxDBTest.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.mockito.Mockito.spy;
66

77
import java.io.IOException;
8+
import java.time.Instant;
89
import java.util.ArrayList;
910
import java.util.List;
1011
import java.util.concurrent.TimeUnit;
@@ -91,9 +92,17 @@ public void testWriteBatchWithPrecision() throws Exception {
9192

9293
// THEN the measure points have a timestamp with second precision
9394
QueryResult queryResult = this.influxDB.query(new Query("SELECT * FROM " + measurement, dbName));
94-
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(0), t1);
95-
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0), t2);
96-
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0), t3);
95+
long bySecond = TimeUnit.NANOSECONDS.toSeconds(
96+
(Long) queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(0));
97+
Assertions.assertEquals(bySecond, t1);
98+
99+
bySecond = TimeUnit.NANOSECONDS.toSeconds(
100+
(Long) queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0));
101+
Assertions.assertEquals(bySecond, t2);
102+
103+
bySecond = TimeUnit.NANOSECONDS.toSeconds(
104+
(Long) queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0));
105+
Assertions.assertEquals(bySecond, t3);
97106

98107
this.influxDB.deleteDatabase(dbName);
99108
}
@@ -182,9 +191,19 @@ public void testWriteRecordsWithPrecision() throws Exception {
182191
// THEN the measure points have a timestamp with second precision
183192
QueryResult queryResult = this.influxDB.query(new Query("SELECT * FROM " + measurement, dbName));
184193
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().size(), 3);
185-
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(0), timeP1);
186-
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0), timeP2);
187-
Assertions.assertEquals(queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0), timeP3);
194+
195+
long bySecond = TimeUnit.NANOSECONDS.toSeconds(
196+
(Long) queryResult.getResults().get(0).getSeries().get(0).getValues().get(0).get(0));
197+
Assertions.assertEquals(bySecond, timeP1);
198+
199+
bySecond = TimeUnit.NANOSECONDS.toSeconds(
200+
(Long) queryResult.getResults().get(0).getSeries().get(0).getValues().get(1).get(0));
201+
Assertions.assertEquals(bySecond, timeP2);
202+
203+
bySecond = TimeUnit.NANOSECONDS.toSeconds(
204+
(Long) queryResult.getResults().get(0).getSeries().get(0).getValues().get(2).get(0));
205+
Assertions.assertEquals(bySecond, timeP3);
206+
188207
this.influxDB.deleteDatabase(dbName);
189208
}
190209

0 commit comments

Comments
 (0)