From c526c94f460caa027a926b0d264b845af892ce69 Mon Sep 17 00:00:00 2001 From: "xvyang.xy" Date: Tue, 28 Feb 2023 16:29:49 +0800 Subject: [PATCH 1/4] feat: columns APi in Row --- .../src/test/java/io/ceresdb/CeresDBTest.java | 34 +++--- .../src/test/java/io/ceresdb/ReadmeTest.java | 10 +- .../src/main/java/io/ceresdb/WriteClient.java | 2 +- .../src/main/java/io/ceresdb/models/Row.java | 101 +++++++++++++++--- .../main/java/io/ceresdb/models/Value.java | 2 +- .../src/main/java/io/ceresdb/util/Utils.java | 71 ++++++------ .../test/java/io/ceresdb/QueryClientTest.java | 32 +++--- .../test/java/io/ceresdb/models/RowTest.java | 37 +++++++ 8 files changed, 202 insertions(+), 87 deletions(-) create mode 100644 ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java diff --git a/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java b/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java index f4a4c4d..a7365f1 100644 --- a/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java +++ b/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java @@ -162,22 +162,22 @@ public void comprehensiveTest() throws ExecutionException, InterruptedException "Data: ts={}, tString={}, tInt64={}, fString={}, fBool={}, fDouble={}, fFloat={}, fInt64={}, fInt32={}, fInt16={}," + // "fInt8={}, fUint64={}, fUint32={}, fUint16={}, fUint8={}, fTimestamp={}, fVarbinary={}", // - row.getColumnValue("ts").getTimestamp(), // - row.getColumnValue("tString").getString(), // - row.getColumnValue("tInt64").getInt64(), // - row.getColumnValue("fString").getString(), // - row.getColumnValue("fBool").getBoolean(), // - row.getColumnValue("fDouble").getDouble(), // - row.getColumnValue("fFloat").getFloat(), // - row.getColumnValue("fInt64").getInt64(), // - row.getColumnValue("fInt32").getInt32(), // - row.getColumnValue("fInt16").getInt16(), // - row.getColumnValue("fInt8").getInt8(), // - row.getColumnValue("fUint64").getUInt64(), // - row.getColumnValue("fUint32").getUInt32(), // - row.getColumnValue("fUint16").getUInt16(), // - row.getColumnValue("fUint8").getUInt8(), // - row.getColumnValue("fTimestamp").getTimestamp() // + row.getColumn("ts").value().getTimestamp(), // + row.getColumn("tString").value().getString(), // + row.getColumn("tInt64").value().getInt64(), // + row.getColumn("fString").value().getString(), // + row.getColumn("fBool").value().getBoolean(), // + row.getColumn("fDouble").value().getDouble(), // + row.getColumn("fFloat").value().getFloat(), // + row.getColumn("fInt64").value().getInt64(), // + row.getColumn("fInt32").value().getInt32(), // + row.getColumn("fInt16").value().getInt16(), // + row.getColumn("fInt8").value().getInt8(), // + row.getColumn("fUint64").value().getUInt64(), // + row.getColumn("fUint32").value().getUInt32(), // + row.getColumn("fUint16").value().getUInt16(), // + row.getColumn("fUint8").value().getUInt8(), // + row.getColumn("fTimestamp").value().getTimestamp() // //row.getColumnValue("fVarbinary").getVarbinary()) ); }); @@ -303,7 +303,7 @@ public void streamQueryTest() { int i = 0; while (it.hasNext()) { - LOG.info("The {} row, timestamp={}", ++i, it.next().getColumnValue("ts")); + LOG.info("The {} row, timestamp={}", ++i, it.next().getColumn("ts")); } Assert.assertEquals(1000, i); diff --git a/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java b/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java index 8ebeefc..028ed17 100644 --- a/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java +++ b/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java @@ -101,11 +101,11 @@ public void readmeTest() throws ExecutionException, InterruptedException { // get rows as list final List rows = queryOk.getRowList(); - Assert.assertEquals(timestamp, rows.get(0).getColumnValue("ts").getTimestamp()); - Assert.assertEquals("Singapore", rows.get(0).getColumnValue("city").getString()); - Assert.assertEquals("10.0.0.1", rows.get(0).getColumnValue("ip").getString()); - Assert.assertEquals(0.23, rows.get(0).getColumnValue("cpu").getDouble(), 0.0000001); - Assert.assertEquals(0.55, rows.get(0).getColumnValue("mem").getDouble(), 0.0000001); + Assert.assertEquals(timestamp, rows.get(0).getColumn("ts").value().getTimestamp()); + Assert.assertEquals("Singapore", rows.get(0).getColumn("city").value().getString()); + Assert.assertEquals("10.0.0.1", rows.get(0).getColumn("ip").value().getString()); + Assert.assertEquals(0.23, rows.get(0).getColumn("cpu").value().getDouble(), 0.0000001); + Assert.assertEquals(0.55, rows.get(0).getColumn("mem").value().getDouble(), 0.0000001); // get rows as stream final Stream rowStream = queryOk.stream(); diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java b/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java index af2d27b..e42df0d 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java @@ -456,7 +456,7 @@ public Storage.WriteRequest toWriteRequestObj(final RequestContext reqCtx, final tagDict.toOrdered().forEach((tagK) -> { Value tagV = point.getTags().get(tagK); if (!Value.isNull(tagV)) { - seriesKeyBuffer.append(tagV.getValue().toString()); + seriesKeyBuffer.append(tagV.getAnyValue().toString()); } }); Storage.WriteSeriesEntry.Builder seriesEntryBuilder = tp3.getSeriesBuilders() diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java b/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java index 3e86dfc..c9efbdd 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java @@ -3,39 +3,114 @@ */ package io.ceresdb.models; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.stream.Collectors; public class Row { - private Map values; + protected String[] fields; + protected Value[] values; - public Row() { - this.values = new HashMap<>(); + protected Row() { } - public Value getColumnValue(String column) { - return this.values.get(column); + public boolean hasColumn(String name) { + return getColumnIdx(name) > -1; } - public void setColumnValue(String column, Value value) { - this.values.put(column, value); + public Column getColumn(String name) { + int columnIdx = getColumnIdx(name); + if (columnIdx > 0) { + return Column.of(name, values[columnIdx]); + } + return null; + } + + public List getColumns() { + if (fields == null) { + return Collections.emptyList(); + } + List columns = new ArrayList<>(getColumnCount()); + for (int idx = 0; idx < fields.length; idx++) { + columns.add(Column.of(fields[idx], values[idx])); + } + return columns; } public int getColumnCount() { - if (this.values == null) { + if (fields == null) { return 0; } - return this.values.size(); + return fields.length; + } + + private int getColumnIdx(String name) { + if (fields == null) { + return -1; + } + for (int idx = 0; idx < fields.length; idx++) { + if (fields[idx].equals(name)) { + return idx; + } + } + return -1; } @Override public String toString() { - if (this.values == null || this.values.isEmpty()) { + if (this.fields == null || this.fields.length == 0) { return "[Empty Row]"; } - return this.values.entrySet().stream().map(entry -> entry.getKey() + ":" + entry.getValue().toString()) + return getColumns().stream().map(column -> column.name + ":" + column.value.toString()) .collect(Collectors.joining("|")); } + + public static class Column { + public Column(String name, Value value) { + this.name = name; + this.value = value; + } + + private String name; + private Value value; + + public String name() { + return name; + } + + public Value value() { + return value; + } + + public static Column of(String name, Value value) { + return new Column(name, value); + } + } + + public static RowBuilder newRowBuilder(int size) { + return new RowBuilder(size); + } + + public static class RowBuilder { + private Row row; + + public RowBuilder(int size) { + this.row = new Row(); + this.row.values = new Value[size]; + } + + public void setFields(String[] fields) { + this.row.fields = fields; + } + + public void setValue(int colIdx, Value value) { + this.row.values[colIdx] = value; + } + + public Row build() { + return this.row; + } + } } diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java b/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java index 0f049c2..4ab550c 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java @@ -72,7 +72,7 @@ public DataType getDataType() { return type; } - public Object getValue() { + public Object getAnyValue() { return value; } diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java b/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java index eb88933..ca3395b 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java @@ -7,6 +7,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -468,20 +469,23 @@ private static List parseArrowBatch(ByteString batch, Storage.ArrowPayload. } private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) { - List rows = Stream.generate(Row::new).limit(root.getRowCount()).collect(Collectors.toList()); + List builders = Collections.nCopies(root.getRowCount(), + Row.newRowBuilder(schema.getFields().size())); + String[] fields = new String[schema.getFields().size()]; for (int fieldIdx = 0; fieldIdx < schema.getFields().size(); fieldIdx++) { Field field = schema.getFields().get(fieldIdx); - FieldVector vector = root.getVector(fieldIdx); + fields[fieldIdx] = field.getName(); + FieldVector vector = root.getVector(fieldIdx); switch (Types.getMinorTypeForArrowType(field.getType())) { case VARCHAR: VarCharVector varCharVector = (VarCharVector) vector; for (int rowIdx = 0; rowIdx < varCharVector.getValueCount(); rowIdx++) { if (varCharVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withStringOrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withStringOrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), + builders.get(rowIdx).setValue(fieldIdx, Value.withString(new String(varCharVector.get(rowIdx)))); } } @@ -490,10 +494,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) BitVector bitVector = (BitVector) vector; for (int rowIdx = 0; rowIdx < bitVector.getValueCount(); rowIdx++) { if (bitVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withBooleanOrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withBooleanOrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), - Value.withBoolean(bitVector.get(rowIdx) > 0)); + builders.get(rowIdx).setValue(fieldIdx, Value.withBoolean(bitVector.get(rowIdx) > 0)); } } break; @@ -501,10 +504,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) Float8Vector float8Vector = (Float8Vector) vector; for (int rowIdx = 0; rowIdx < float8Vector.getValueCount(); rowIdx++) { if (float8Vector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withDoubleOrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withDoubleOrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), - Value.withDouble(float8Vector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withDouble(float8Vector.get(rowIdx))); } } break; @@ -512,9 +514,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) Float4Vector float4Vector = (Float4Vector) vector; for (int rowIdx = 0; rowIdx < float4Vector.getValueCount(); rowIdx++) { if (float4Vector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withFloatOrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withFloatOrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withFloat(float4Vector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withFloat(float4Vector.get(rowIdx))); } } break; @@ -522,9 +524,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) BigIntVector bigIntVector = (BigIntVector) vector; for (int rowIdx = 0; rowIdx < bigIntVector.getValueCount(); rowIdx++) { if (bigIntVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt64OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt64OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt64(bigIntVector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt64(bigIntVector.get(rowIdx))); } } break; @@ -532,9 +534,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) IntVector intVector = (IntVector) vector; for (int rowIdx = 0; rowIdx < intVector.getValueCount(); rowIdx++) { if (intVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt32OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt32OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt32(intVector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt32(intVector.get(rowIdx))); } } break; @@ -542,10 +544,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) SmallIntVector smallIntVector = (SmallIntVector) vector; for (int rowIdx = 0; rowIdx < smallIntVector.getValueCount(); rowIdx++) { if (smallIntVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt16OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt16OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), - Value.withInt16(smallIntVector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt16(smallIntVector.get(rowIdx))); } } break; @@ -553,9 +554,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) TinyIntVector tinyIntVector = (TinyIntVector) vector; for (int rowIdx = 0; rowIdx < tinyIntVector.getValueCount(); rowIdx++) { if (tinyIntVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt8OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt8OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withInt8(tinyIntVector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withInt8(tinyIntVector.get(rowIdx))); } } break; @@ -563,9 +564,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) UInt8Vector uInt8Vector = (UInt8Vector) vector; for (int rowIdx = 0; rowIdx < uInt8Vector.getValueCount(); rowIdx++) { if (uInt8Vector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt64OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt64OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt64(uInt8Vector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt64(uInt8Vector.get(rowIdx))); } } break; @@ -573,9 +574,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) UInt4Vector uInt4Vector = (UInt4Vector) vector; for (int rowIdx = 0; rowIdx < uInt4Vector.getValueCount(); rowIdx++) { if (uInt4Vector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt32OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt32OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt32(uInt4Vector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt32(uInt4Vector.get(rowIdx))); } } break; @@ -583,9 +584,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) UInt2Vector uInt2Vector = (UInt2Vector) vector; for (int rowIdx = 0; rowIdx < uInt2Vector.getValueCount(); rowIdx++) { if (uInt2Vector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt16OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt16OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt16(uInt2Vector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt16(uInt2Vector.get(rowIdx))); } } break; @@ -593,9 +594,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) UInt1Vector uInt1Vector = (UInt1Vector) vector; for (int rowIdx = 0; rowIdx < uInt1Vector.getValueCount(); rowIdx++) { if (uInt1Vector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt8OrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt8OrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withUInt8(uInt1Vector.get(rowIdx))); + builders.get(rowIdx).setValue(fieldIdx, Value.withUInt8(uInt1Vector.get(rowIdx))); } } break; @@ -603,9 +604,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) TimeStampMilliVector timeStampMilliVector = (TimeStampMilliVector) vector; for (int rowIdx = 0; rowIdx < timeStampMilliVector.getValueCount(); rowIdx++) { if (timeStampMilliVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withTimestampOrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withTimestampOrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), + builders.get(rowIdx).setValue(fieldIdx, Value.withTimestamp(timeStampMilliVector.get(rowIdx))); } } @@ -614,9 +615,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) VarBinaryVector varBinaryVector = (VarBinaryVector) vector; for (int rowIdx = 0; rowIdx < varBinaryVector.getValueCount(); rowIdx++) { if (varBinaryVector.isNull(rowIdx)) { - rows.get(rowIdx).setColumnValue(field.getName(), Value.withVarbinaryOrNull(null)); + builders.get(rowIdx).setValue(fieldIdx, Value.withVarbinaryOrNull(null)); } else { - rows.get(rowIdx).setColumnValue(field.getName(), + builders.get(rowIdx).setValue(fieldIdx, Value.withVarbinaryOrNull(varBinaryVector.get(rowIdx))); } } @@ -626,7 +627,9 @@ private static List parseArrowRecord(Schema schema, VectorSchemaRoot root) } } - return rows; + + builders.stream().forEach(builder -> builder.setFields(fields)); + return builders.stream().map(builder -> builder.build()).collect(Collectors.toList()); } private Utils() { diff --git a/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java b/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java index 6537a6f..f33c910 100644 --- a/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java +++ b/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java @@ -209,20 +209,20 @@ public void queryByArrowFullTypeTest() throws ExecutionException, InterruptedExc private void checkFullTypeRow(final Row row) { Assert.assertEquals(14, row.getColumnCount()); - Assert.assertEquals("test", row.getColumnValue("fString").getString()); - Assert.assertEquals(Boolean.TRUE, row.getColumnValue("fBool").getBoolean()); - Assert.assertEquals(0.64, row.getColumnValue("fDouble").getDouble(), 0.000001); - Assert.assertEquals(0.32f, row.getColumnValue("fFloat").getFloat(), 0.000001); - Assert.assertEquals(-64, row.getColumnValue("fInt64").getInt64()); - Assert.assertEquals(-32, row.getColumnValue("fInt32").getInt32()); - Assert.assertEquals(-16, row.getColumnValue("fInt16").getInt16()); - Assert.assertEquals(-8, row.getColumnValue("fInt8").getInt8()); - Assert.assertEquals(64, row.getColumnValue("fUint64").getUInt64()); - Assert.assertEquals(32, row.getColumnValue("fUint32").getUInt32()); - Assert.assertEquals(16, row.getColumnValue("fUint16").getUInt16()); - Assert.assertEquals(8, row.getColumnValue("fUint8").getUInt8()); - Assert.assertEquals(1675345488158L, row.getColumnValue("fTimestamp").getTimestamp()); - Assert.assertArrayEquals(new byte[] { 1, 2, 3 }, row.getColumnValue("fVarbinary").getVarbinary()); + Assert.assertEquals("test", row.getColumn("fString").value().getString()); + Assert.assertEquals(Boolean.TRUE, row.getColumn("fBool").value().getBoolean()); + Assert.assertEquals(0.64, row.getColumn("fDouble").value().getDouble(), 0.000001); + Assert.assertEquals(0.32f, row.getColumn("fFloat").value().getFloat(), 0.000001); + Assert.assertEquals(-64, row.getColumn("fInt64").value().getInt64()); + Assert.assertEquals(-32, row.getColumn("fInt32").value().getInt32()); + Assert.assertEquals(-16, row.getColumn("fInt16").value().getInt16()); + Assert.assertEquals(-8, row.getColumn("fInt8").value().getInt8()); + Assert.assertEquals(64, row.getColumn("fUint64").value().getUInt64()); + Assert.assertEquals(32, row.getColumn("fUint32").value().getUInt32()); + Assert.assertEquals(16, row.getColumn("fUint16").value().getUInt16()); + Assert.assertEquals(8, row.getColumn("fUint8").value().getUInt8()); + Assert.assertEquals(1675345488158L, row.getColumn("fTimestamp").value().getTimestamp()); + Assert.assertArrayEquals(new byte[] { 1, 2, 3 }, row.getColumn("fVarbinary").value().getVarbinary()); } @Test @@ -249,8 +249,8 @@ public void queryByArrowNullTypeTest() throws ExecutionException, InterruptedExc final SqlQueryOk queryOk = r.getOk(); Row row = queryOk.stream().findFirst().get(); - Assert.assertEquals("tvtest", row.getColumnValue("t1").getString()); - Assert.assertTrue(row.getColumnValue("f1").isNull()); + Assert.assertEquals("tvtest", row.getColumn("t1").value().getString()); + Assert.assertTrue(row.getColumn("f1").value().isNull()); } private Result queryByArrow() throws IOException, ExecutionException, InterruptedException { diff --git a/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java b/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java new file mode 100644 index 0000000..77c01ac --- /dev/null +++ b/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2023 CeresDB Project Authors. Licensed under Apache-2.0. + */ +package io.ceresdb.models; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +public class RowTest { + @Test + public void RowBuilderTest() { + Row.RowBuilder builder = new Row.RowBuilder(5); + + String[] fields = new String[] { "timestamp", "tagA", "tagB", "valueA", "valueB" }; + builder.setValue(4, Value.withInt64(123)); + builder.setValue(2, Value.withString("foo")); + builder.setValue(0, Value.withTimestamp(12345678L)); + builder.setValue(1, Value.withString("bar")); + builder.setValue(3, Value.withString("haha")); + builder.setFields(fields); + + Row row = builder.build(); + + Assert.assertEquals(row.getColumnCount(), 5); + Assert.assertTrue(row.hasColumn("tagA")); + Assert.assertFalse(row.hasColumn("notExist")); + Assert.assertEquals(row.getColumn("tagA").value().getString(), "bar"); + Assert.assertNull(row.getColumn("notExist")); + + List columns = row.getColumns(); + Assert.assertEquals(columns.size(), 5); + Assert.assertEquals(columns.get(1).value().getString(), "bar"); + Assert.assertEquals(columns.get(4).value().getInt64(), 123); + } +} From 6850f173793fa206f968b34a5df215a8c13ce7f7 Mon Sep 17 00:00:00 2001 From: "xvyang.xy" Date: Tue, 28 Feb 2023 16:31:31 +0800 Subject: [PATCH 2/4] doc: update doc for API columns --- ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java | 2 -- docs/read.md | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java b/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java index ca3395b..274c829 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/util/Utils.java @@ -7,7 +7,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -22,7 +21,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; -import java.util.stream.Stream; import io.ceresdb.Route; import io.ceresdb.common.Display; diff --git a/docs/read.md b/docs/read.md index 8cc4fef..1b52224 100644 --- a/docs/read.md +++ b/docs/read.md @@ -87,7 +87,7 @@ CompletableFuture> sqlQuery(SqlQueryRequest req, Context - When processing the results returned by the query, the user can directly obtain `List`, or process it through stream. - Row is a collection of Value, which is a very simple data structure - Note: When Value gets the java primitive value, you need to pass in the type method that matches the table creation, otherwise an error will be reported -- Example to use `Row`: `row.getColumnValue("cpu_util").getDouble() +- Example to use `Row`: `row.getColumn("cpu_util").value().getDouble() ` `Err` From 80b9fd34be51f29659654688e312fbc801dbd3f8 Mon Sep 17 00:00:00 2001 From: "xvyang.xy" Date: Tue, 28 Feb 2023 17:42:19 +0800 Subject: [PATCH 3/4] feat: rename Value.getAnyValue to Value.getObject --- .../src/test/java/io/ceresdb/CeresDBTest.java | 32 +++++++++---------- .../src/test/java/io/ceresdb/ReadmeTest.java | 10 +++--- .../src/main/java/io/ceresdb/WriteClient.java | 2 +- .../src/main/java/io/ceresdb/models/Row.java | 4 +-- .../main/java/io/ceresdb/models/Value.java | 2 +- .../test/java/io/ceresdb/QueryClientTest.java | 32 +++++++++---------- .../test/java/io/ceresdb/models/RowTest.java | 6 ++-- 7 files changed, 44 insertions(+), 44 deletions(-) diff --git a/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java b/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java index a7365f1..885a8ca 100644 --- a/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java +++ b/ceresdb-example/src/test/java/io/ceresdb/CeresDBTest.java @@ -162,22 +162,22 @@ public void comprehensiveTest() throws ExecutionException, InterruptedException "Data: ts={}, tString={}, tInt64={}, fString={}, fBool={}, fDouble={}, fFloat={}, fInt64={}, fInt32={}, fInt16={}," + // "fInt8={}, fUint64={}, fUint32={}, fUint16={}, fUint8={}, fTimestamp={}, fVarbinary={}", // - row.getColumn("ts").value().getTimestamp(), // - row.getColumn("tString").value().getString(), // - row.getColumn("tInt64").value().getInt64(), // - row.getColumn("fString").value().getString(), // - row.getColumn("fBool").value().getBoolean(), // - row.getColumn("fDouble").value().getDouble(), // - row.getColumn("fFloat").value().getFloat(), // - row.getColumn("fInt64").value().getInt64(), // - row.getColumn("fInt32").value().getInt32(), // - row.getColumn("fInt16").value().getInt16(), // - row.getColumn("fInt8").value().getInt8(), // - row.getColumn("fUint64").value().getUInt64(), // - row.getColumn("fUint32").value().getUInt32(), // - row.getColumn("fUint16").value().getUInt16(), // - row.getColumn("fUint8").value().getUInt8(), // - row.getColumn("fTimestamp").value().getTimestamp() // + row.getColumn("ts").getValue().getTimestamp(), // + row.getColumn("tString").getValue().getString(), // + row.getColumn("tInt64").getValue().getInt64(), // + row.getColumn("fString").getValue().getString(), // + row.getColumn("fBool").getValue().getBoolean(), // + row.getColumn("fDouble").getValue().getDouble(), // + row.getColumn("fFloat").getValue().getFloat(), // + row.getColumn("fInt64").getValue().getInt64(), // + row.getColumn("fInt32").getValue().getInt32(), // + row.getColumn("fInt16").getValue().getInt16(), // + row.getColumn("fInt8").getValue().getInt8(), // + row.getColumn("fUint64").getValue().getUInt64(), // + row.getColumn("fUint32").getValue().getUInt32(), // + row.getColumn("fUint16").getValue().getUInt16(), // + row.getColumn("fUint8").getValue().getUInt8(), // + row.getColumn("fTimestamp").getValue().getTimestamp() // //row.getColumnValue("fVarbinary").getVarbinary()) ); }); diff --git a/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java b/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java index 028ed17..86e42c9 100644 --- a/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java +++ b/ceresdb-example/src/test/java/io/ceresdb/ReadmeTest.java @@ -101,11 +101,11 @@ public void readmeTest() throws ExecutionException, InterruptedException { // get rows as list final List rows = queryOk.getRowList(); - Assert.assertEquals(timestamp, rows.get(0).getColumn("ts").value().getTimestamp()); - Assert.assertEquals("Singapore", rows.get(0).getColumn("city").value().getString()); - Assert.assertEquals("10.0.0.1", rows.get(0).getColumn("ip").value().getString()); - Assert.assertEquals(0.23, rows.get(0).getColumn("cpu").value().getDouble(), 0.0000001); - Assert.assertEquals(0.55, rows.get(0).getColumn("mem").value().getDouble(), 0.0000001); + Assert.assertEquals(timestamp, rows.get(0).getColumn("ts").getValue().getTimestamp()); + Assert.assertEquals("Singapore", rows.get(0).getColumn("city").getValue().getString()); + Assert.assertEquals("10.0.0.1", rows.get(0).getColumn("ip").getValue().getString()); + Assert.assertEquals(0.23, rows.get(0).getColumn("cpu").getValue().getDouble(), 0.0000001); + Assert.assertEquals(0.55, rows.get(0).getColumn("mem").getValue().getDouble(), 0.0000001); // get rows as stream final Stream rowStream = queryOk.stream(); diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java b/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java index e42df0d..21b9b91 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/WriteClient.java @@ -456,7 +456,7 @@ public Storage.WriteRequest toWriteRequestObj(final RequestContext reqCtx, final tagDict.toOrdered().forEach((tagK) -> { Value tagV = point.getTags().get(tagK); if (!Value.isNull(tagV)) { - seriesKeyBuffer.append(tagV.getAnyValue().toString()); + seriesKeyBuffer.append(tagV.getObject().toString()); } }); Storage.WriteSeriesEntry.Builder seriesEntryBuilder = tp3.getSeriesBuilders() diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java b/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java index c9efbdd..f98058d 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java @@ -76,11 +76,11 @@ public Column(String name, Value value) { private String name; private Value value; - public String name() { + public String getName() { return name; } - public Value value() { + public Value getValue() { return value; } diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java b/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java index 4ab550c..b9a29c4 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/models/Value.java @@ -72,7 +72,7 @@ public DataType getDataType() { return type; } - public Object getAnyValue() { + public Object getObject() { return value; } diff --git a/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java b/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java index f33c910..f356e80 100644 --- a/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java +++ b/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java @@ -209,20 +209,20 @@ public void queryByArrowFullTypeTest() throws ExecutionException, InterruptedExc private void checkFullTypeRow(final Row row) { Assert.assertEquals(14, row.getColumnCount()); - Assert.assertEquals("test", row.getColumn("fString").value().getString()); - Assert.assertEquals(Boolean.TRUE, row.getColumn("fBool").value().getBoolean()); - Assert.assertEquals(0.64, row.getColumn("fDouble").value().getDouble(), 0.000001); - Assert.assertEquals(0.32f, row.getColumn("fFloat").value().getFloat(), 0.000001); - Assert.assertEquals(-64, row.getColumn("fInt64").value().getInt64()); - Assert.assertEquals(-32, row.getColumn("fInt32").value().getInt32()); - Assert.assertEquals(-16, row.getColumn("fInt16").value().getInt16()); - Assert.assertEquals(-8, row.getColumn("fInt8").value().getInt8()); - Assert.assertEquals(64, row.getColumn("fUint64").value().getUInt64()); - Assert.assertEquals(32, row.getColumn("fUint32").value().getUInt32()); - Assert.assertEquals(16, row.getColumn("fUint16").value().getUInt16()); - Assert.assertEquals(8, row.getColumn("fUint8").value().getUInt8()); - Assert.assertEquals(1675345488158L, row.getColumn("fTimestamp").value().getTimestamp()); - Assert.assertArrayEquals(new byte[] { 1, 2, 3 }, row.getColumn("fVarbinary").value().getVarbinary()); + Assert.assertEquals("test", row.getColumn("fString").getValue().getString()); + Assert.assertEquals(Boolean.TRUE, row.getColumn("fBool").getValue().getBoolean()); + Assert.assertEquals(0.64, row.getColumn("fDouble").getValue().getDouble(), 0.000001); + Assert.assertEquals(0.32f, row.getColumn("fFloat").getValue().getFloat(), 0.000001); + Assert.assertEquals(-64, row.getColumn("fInt64").getValue().getInt64()); + Assert.assertEquals(-32, row.getColumn("fInt32").getValue().getInt32()); + Assert.assertEquals(-16, row.getColumn("fInt16").getValue().getInt16()); + Assert.assertEquals(-8, row.getColumn("fInt8").getValue().getInt8()); + Assert.assertEquals(64, row.getColumn("fUint64").getValue().getUInt64()); + Assert.assertEquals(32, row.getColumn("fUint32").getValue().getUInt32()); + Assert.assertEquals(16, row.getColumn("fUint16").getValue().getUInt16()); + Assert.assertEquals(8, row.getColumn("fUint8").getValue().getUInt8()); + Assert.assertEquals(1675345488158L, row.getColumn("fTimestamp").getValue().getTimestamp()); + Assert.assertArrayEquals(new byte[] { 1, 2, 3 }, row.getColumn("fVarbinary").getValue().getVarbinary()); } @Test @@ -249,8 +249,8 @@ public void queryByArrowNullTypeTest() throws ExecutionException, InterruptedExc final SqlQueryOk queryOk = r.getOk(); Row row = queryOk.stream().findFirst().get(); - Assert.assertEquals("tvtest", row.getColumn("t1").value().getString()); - Assert.assertTrue(row.getColumn("f1").value().isNull()); + Assert.assertEquals("tvtest", row.getColumn("t1").getValue().getString()); + Assert.assertTrue(row.getColumn("f1").getValue().isNull()); } private Result queryByArrow() throws IOException, ExecutionException, InterruptedException { diff --git a/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java b/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java index 77c01ac..3be78f4 100644 --- a/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java +++ b/ceresdb-protocol/src/test/java/io/ceresdb/models/RowTest.java @@ -26,12 +26,12 @@ public void RowBuilderTest() { Assert.assertEquals(row.getColumnCount(), 5); Assert.assertTrue(row.hasColumn("tagA")); Assert.assertFalse(row.hasColumn("notExist")); - Assert.assertEquals(row.getColumn("tagA").value().getString(), "bar"); + Assert.assertEquals(row.getColumn("tagA").getValue().getString(), "bar"); Assert.assertNull(row.getColumn("notExist")); List columns = row.getColumns(); Assert.assertEquals(columns.size(), 5); - Assert.assertEquals(columns.get(1).value().getString(), "bar"); - Assert.assertEquals(columns.get(4).value().getInt64(), 123); + Assert.assertEquals(columns.get(1).getValue().getString(), "bar"); + Assert.assertEquals(columns.get(4).getValue().getInt64(), 123); } } From 5fe65e1327bff42c57b1815b341ed4f9d7140d32 Mon Sep 17 00:00:00 2001 From: "xvyang.xy" Date: Tue, 28 Feb 2023 18:09:53 +0800 Subject: [PATCH 4/4] fix row.getColumn bug --- ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java | 2 +- .../src/test/java/io/ceresdb/QueryClientTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java b/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java index f98058d..39295c3 100644 --- a/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java +++ b/ceresdb-protocol/src/main/java/io/ceresdb/models/Row.java @@ -21,7 +21,7 @@ public boolean hasColumn(String name) { public Column getColumn(String name) { int columnIdx = getColumnIdx(name); - if (columnIdx > 0) { + if (columnIdx > -1) { return Column.of(name, values[columnIdx]); } return null; diff --git a/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java b/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java index f356e80..6e3a487 100644 --- a/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java +++ b/ceresdb-protocol/src/test/java/io/ceresdb/QueryClientTest.java @@ -115,7 +115,7 @@ public void queryOkNoRouteTest() throws ExecutionException, InterruptedException final Stream strs = queryOk.map(Row::toString); Assert.assertEquals( - Collections.singletonList("f1:Value{type=Int32,value=123}|t1:Value{type=String,value=tvtest}"), + Collections.singletonList("t1:Value{type=String,value=tvtest}|f1:Value{type=Int32,value=123}"), strs.collect(Collectors.toList())); } @@ -150,7 +150,7 @@ public void queryOkByValidRouteTest() throws ExecutionException, InterruptedExce final Stream strs = queryOk.map(Row::toString); Assert.assertEquals( - Collections.singletonList("f1:Value{type=Int32,value=123}|t1:Value{type=String,value=tvtest}"), + Collections.singletonList("t1:Value{type=String,value=tvtest}|f1:Value{type=Int32,value=123}"), strs.collect(Collectors.toList())); }