This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new d4babb2 [ISSUE-4065] Reduce JSONPayloadFormatter#format deserialize
measurements times (#4066)
d4babb2 is described below
commit d4babb28236c10fdf525eb253314eedafd7109fe
Author: Wenjun Ruan <[email protected]>
AuthorDate: Sat Oct 2 17:28:40 2021 +0800
[ISSUE-4065] Reduce JSONPayloadFormatter#format deserialize measurements
times (#4066)
---
.../server/CustomizedJsonPayloadFormatter.java | 2 +-
.../apache/iotdb/db/mqtt/JSONPayloadFormatter.java | 51 +++++++++++++++-------
.../org/apache/iotdb/db/mqtt/PayloadFormatter.java | 8 +++-
...rmatTest.java => JSONPayloadFormatterTest.java} | 2 +-
4 files changed, 45 insertions(+), 18 deletions(-)
diff --git
a/example/mqtt-customize/src/main/java/org/apache/iotdb/mqtt/server/CustomizedJsonPayloadFormatter.java
b/example/mqtt-customize/src/main/java/org/apache/iotdb/mqtt/server/CustomizedJsonPayloadFormatter.java
index 3ba7d55..51c4fe8 100644
---
a/example/mqtt-customize/src/main/java/org/apache/iotdb/mqtt/server/CustomizedJsonPayloadFormatter.java
+++
b/example/mqtt-customize/src/main/java/org/apache/iotdb/mqtt/server/CustomizedJsonPayloadFormatter.java
@@ -39,7 +39,7 @@ public class CustomizedJsonPayloadFormatter implements
PayloadFormatter {
String json = payload.toString(StandardCharsets.UTF_8);
- // parse data from the json and generate Messages and put them into
List<Meesage> ret
+ // parse data from the json and generate Messages and put them into
List<Message> ret
List<Message> ret = new ArrayList<>();
// this is just an example, so we just generate some Messages directly
for (int i = 0; i < 2; i++) {
diff --git
a/server/src/main/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatter.java
b/server/src/main/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatter.java
index ac332ec..5600980 100644
--- a/server/src/main/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatter.java
+++ b/server/src/main/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatter.java
@@ -20,8 +20,8 @@ package org.apache.iotdb.db.mqtt;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
-import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import io.netty.buffer.ByteBuf;
@@ -50,28 +50,49 @@ public class JSONPayloadFormatter implements
PayloadFormatter {
return null;
}
String txt = payload.toString(StandardCharsets.UTF_8);
-
JsonObject jsonObject = GSON.fromJson(txt, JsonObject.class);
- Object timestamp = jsonObject.get(JSON_KEY_TIMESTAMP);
- if (timestamp != null) {
- return Lists.newArrayList(GSON.fromJson(txt, Message.class));
+
+ if (jsonObject.get(JSON_KEY_TIMESTAMP) != null) {
+ return formatJson(jsonObject);
}
+ if (jsonObject.get(JSON_KEY_TIMESTAMPS) != null) {
+ return formatBatchJson(jsonObject);
+ }
+ throw new JsonParseException("payload is invalidate");
+ }
+ private List<Message> formatJson(JsonObject jsonObject) {
+ Message message = new Message();
+ message.setDevice(jsonObject.get(JSON_KEY_DEVICE).getAsString());
+ message.setTimestamp(jsonObject.get(JSON_KEY_TIMESTAMP).getAsLong());
+ message.setMeasurements(
+ GSON.fromJson(
+ jsonObject.get(JSON_KEY_MEASUREMENTS), new
TypeToken<List<String>>() {}.getType()));
+ message.setValues(
+ GSON.fromJson(jsonObject.get(JSON_KEY_VALUES), new
TypeToken<List<String>>() {}.getType()));
+ return Lists.newArrayList(message);
+ }
+
+ private List<Message> formatBatchJson(JsonObject jsonObject) {
String device = jsonObject.get(JSON_KEY_DEVICE).getAsString();
- JsonArray timestamps = jsonObject.getAsJsonArray(JSON_KEY_TIMESTAMPS);
- JsonArray measurements = jsonObject.getAsJsonArray(JSON_KEY_MEASUREMENTS);
- JsonArray values = jsonObject.getAsJsonArray(JSON_KEY_VALUES);
+ List<String> measurements =
+ GSON.fromJson(
+ jsonObject.getAsJsonArray(JSON_KEY_MEASUREMENTS),
+ new TypeToken<List<String>>() {}.getType());
+ List<Long> timestamps =
+ GSON.fromJson(
+ jsonObject.get(JSON_KEY_TIMESTAMPS), new TypeToken<List<Long>>()
{}.getType());
+ List<List<String>> values =
+ GSON.fromJson(
+ jsonObject.get(JSON_KEY_VALUES), new
TypeToken<List<List<String>>>() {}.getType());
- List<Message> ret = new ArrayList<>();
+ List<Message> ret = new ArrayList<>(timestamps.size());
for (int i = 0; i < timestamps.size(); i++) {
- Long ts = timestamps.get(i).getAsLong();
-
Message message = new Message();
message.setDevice(device);
- message.setTimestamp(ts);
- message.setMeasurements(
- GSON.fromJson(measurements, new TypeToken<List<String>>()
{}.getType()));
- message.setValues(GSON.fromJson(values.get(i), new
TypeToken<List<String>>() {}.getType()));
+ message.setTimestamp(timestamps.get(i));
+ message.setMeasurements(measurements);
+ message.setValues(values.get(i));
ret.add(message);
}
return ret;
diff --git
a/server/src/main/java/org/apache/iotdb/db/mqtt/PayloadFormatter.java
b/server/src/main/java/org/apache/iotdb/db/mqtt/PayloadFormatter.java
index 4bc1384..4d7c49f 100644
--- a/server/src/main/java/org/apache/iotdb/db/mqtt/PayloadFormatter.java
+++ b/server/src/main/java/org/apache/iotdb/db/mqtt/PayloadFormatter.java
@@ -21,7 +21,13 @@ import io.netty.buffer.ByteBuf;
import java.util.List;
-/** PayloadFormatter format the payload to the messages. */
+/**
+ * PayloadFormatter format the payload to the messages.
+ *
+ * <p>This is a SPI interface.
+ *
+ * @see JSONPayloadFormatter
+ */
public interface PayloadFormatter {
/**
* format a payload to a list of messages
diff --git
a/server/src/test/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatTest.java
b/server/src/test/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatterTest.java
similarity index 98%
rename from
server/src/test/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatTest.java
rename to
server/src/test/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatterTest.java
index 9132baf..cdda17f 100644
--- a/server/src/test/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatTest.java
+++
b/server/src/test/java/org/apache/iotdb/db/mqtt/JSONPayloadFormatterTest.java
@@ -25,7 +25,7 @@ import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
-public class JSONPayloadFormatTest {
+public class JSONPayloadFormatterTest {
@Test
public void formatJson() {