From 2b5f07169e6fb2d267b47511762a1d183f8928a2 Mon Sep 17 00:00:00 2001 From: laura-ding <48548375+laura-ding@users.noreply.github.com> Date: Tue, 6 Apr 2021 18:01:00 +0800 Subject: [PATCH] fix encoder without string col --- .../com/vesoft/nebula/encoder/RowWriterImpl.java | 8 ++++---- .../vesoft/nebula/encoder/MetaCacheImplTest.java | 14 ++++++++++++++ .../com/vesoft/nebula/encoder/TestEncoder.java | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/com/vesoft/nebula/encoder/RowWriterImpl.java b/client/src/main/java/com/vesoft/nebula/encoder/RowWriterImpl.java index 8e8a5cf39..a1bd1f4d2 100644 --- a/client/src/main/java/com/vesoft/nebula/encoder/RowWriterImpl.java +++ b/client/src/main/java/com/vesoft/nebula/encoder/RowWriterImpl.java @@ -82,7 +82,7 @@ public RowWriterImpl(SchemaProviderImpl schema, ByteOrder byteOrder) throws Runt if (numNullables > 0) { numNullBytes = ((numNullables - 1) >> 3) + 1; } - buf = ByteBuffer.allocate(headerLen + numNullBytes + schema.size()); + buf = ByteBuffer.allocate(headerLen + numNullBytes + schema.size() + Long.BYTES); buf.order(this.byteOrder); buf.put(header); if (ver > 0) { @@ -767,9 +767,9 @@ public ByteBuffer processOutOfSpace() { // Reserve enough space to avoid memory re-allocation // Copy the data except the strings - temp = temp.put(buf.array()); + temp = temp.put(buf.array(), 0, buf.array().length - Long.BYTES); - int strOffset = buf.array().length; + int strOffset = buf.array().length - Long.BYTES; // Now let's process all strings int strNum = 0; @@ -812,7 +812,7 @@ public void finish() { buf = processOutOfSpace(); } // Save the timestamp to the tail of buf - buf.putLong(getTimestamp()); + buf.putLong(buf.array().length - Long.BYTES, getTimestamp()); } private long getTimestamp() { diff --git a/client/src/test/java/com/vesoft/nebula/encoder/MetaCacheImplTest.java b/client/src/test/java/com/vesoft/nebula/encoder/MetaCacheImplTest.java index fb75ff688..35bcb2b22 100644 --- a/client/src/test/java/com/vesoft/nebula/encoder/MetaCacheImplTest.java +++ b/client/src/test/java/com/vesoft/nebula/encoder/MetaCacheImplTest.java @@ -158,6 +158,14 @@ public Schema genEmptyString() { return new Schema(columns, null); } + public Schema genWithoutString() { + List columns = new ArrayList<>(); + ColumnDef columnDef = new ColumnDef(("Col01").getBytes(), + new ColumnTypeDef(PropertyType.INT64)); + columns.add(columnDef); + return new Schema(columns, null); + } + public MetaCacheImplTest() { spaceItem.space_id = 1; SpaceDesc spaceDesc = new SpaceDesc("test_space".getBytes(), @@ -190,6 +198,12 @@ public MetaCacheImplTest() { tagItem3.schema = genEmptyString(); this.tagItems.put(new String(tagItem3.tag_name), tagItem3); + TagItem tagItem4 = new TagItem(); + tagItem4.tag_name = "tag_without_string".getBytes(); + tagItem4.version = 7; + tagItem4.schema = genWithoutString(); + this.tagItems.put(new String(tagItem4.tag_name), tagItem4); + EdgeItem edgeItem1 = new EdgeItem(); edgeItem1.edge_name = "edge_no_default".getBytes(); edgeItem1.schema = genNoDefaultVal(); diff --git a/client/src/test/java/com/vesoft/nebula/encoder/TestEncoder.java b/client/src/test/java/com/vesoft/nebula/encoder/TestEncoder.java index 09394696d..fd6c1aedd 100644 --- a/client/src/test/java/com/vesoft/nebula/encoder/TestEncoder.java +++ b/client/src/test/java/com/vesoft/nebula/encoder/TestEncoder.java @@ -210,6 +210,7 @@ public void testEncodeVertexValue() { TagItem tagItem1 = cacheImplTest.getTag("test", "tag_no_default"); TagItem tagItem2 = cacheImplTest.getTag("test", "tag_with_empty_string"); TagItem tagItem3 = cacheImplTest.getTag("test", "tag_with_default"); + TagItem tagItem4 = cacheImplTest.getTag("test", "tag_without_string"); try { codec.encodeTag(tagItem1, colNames, colVals); Assert.fail(); @@ -253,6 +254,19 @@ public void testEncodeVertexValue() { "Unsupported default value yet".getBytes()); assert (true); } + + // test without string type + try { + byte[] encodeStr = codec.encodeTag( + tagItem4, Arrays.asList("Col01"), Arrays.asList(1024)); + String hexStr = Hex.encodeHexString(encodeStr); + String expectResult = "09070004000000000000"; + Assert.assertArrayEquals(expectResult.getBytes(), + hexStr.substring(0, hexStr.length() - 16).getBytes()); + } catch (Exception exception) { + exception.printStackTrace(); + Assert.fail(exception.getMessage()); + } } @Test()