From 1a7f6cbdbff3f076c8763d3577e2484210d17625 Mon Sep 17 00:00:00 2001 From: VGalaxies Date: Mon, 11 Sep 2023 19:12:37 +0800 Subject: [PATCH] fix: index serialization and deserialization logic --- .../hugegraph/backend/serializer/BytesBuffer.java | 8 ++++---- .../org/apache/hugegraph/structure/HugeIndex.java | 15 +++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java index 38250b43fe..52534bf0bd 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java @@ -756,11 +756,11 @@ public BytesBuffer writeIndexId(Id id, HugeType type, boolean withEnding) { public BinaryId readIndexId(HugeType type) { byte[] id; if (type.isRange4Index()) { - // IndexLabel 4 bytes + fieldValue 4 bytes - id = this.read(8); + // HugeTypeCode 1 byte + IndexLabel 4 bytes + fieldValue 4 bytes + id = this.read(9); } else if (type.isRange8Index()) { - // IndexLabel 4 bytes + fieldValue 8 bytes - id = this.read(12); + // HugeTypeCode 1 byte + IndexLabel 4 bytes + fieldValue 8 bytes + id = this.read(13); } else { assert type.isStringIndex(); id = this.readBytesWithEnding(); diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeIndex.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeIndex.java index 4f5d8d04c4..d1c7b1bf06 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeIndex.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/structure/HugeIndex.java @@ -44,6 +44,7 @@ public class HugeIndex implements GraphType, Cloneable { private Object fieldValues; private IndexLabel indexLabel; private Set elementIds; + private static final int HUGE_TYPE_CODE_LENGTH = 1; public HugeIndex(HugeGraph graph, IndexLabel indexLabel) { E.checkNotNull(graph, "graph"); @@ -210,11 +211,12 @@ public static Id formatIndexId(HugeType type, Id indexLabelId, * index label in front(hugegraph-1317) */ String strIndexLabelId = IdGenerator.asStoredString(indexLabelId); - return SplicingIdGenerator.splicing(strIndexLabelId, value); + return SplicingIdGenerator.splicing(type.string(), strIndexLabelId, value); } else { assert type.isRangeIndex(); int length = type.isRange4Index() ? 4 : 8; - BytesBuffer buffer = BytesBuffer.allocate(4 + length); + BytesBuffer buffer = BytesBuffer.allocate(HUGE_TYPE_CODE_LENGTH + 4 + length); + buffer.write(type.code()); buffer.writeInt(SchemaElement.schemaId(indexLabelId)); if (fieldValues != null) { E.checkState(fieldValues instanceof Number, @@ -234,15 +236,16 @@ public static HugeIndex parseIndexId(HugeGraph graph, HugeType type, if (type.isStringIndex()) { Id idObject = IdGenerator.of(id, IdType.STRING); String[] parts = SplicingIdGenerator.parse(idObject); - E.checkState(parts.length == 2, "Invalid secondary index id"); - Id label = IdGenerator.ofStoredString(parts[0], IdType.LONG); + E.checkState(parts.length == 3, "Invalid secondary index id"); + Id label = IdGenerator.ofStoredString(parts[1], IdType.LONG); indexLabel = IndexLabel.label(graph, label); - values = parts[1]; + values = parts[2]; } else { assert type.isRange4Index() || type.isRange8Index(); final int labelLength = 4; E.checkState(id.length > labelLength, "Invalid range index id"); BytesBuffer buffer = BytesBuffer.wrap(id); + buffer.read(HUGE_TYPE_CODE_LENGTH); Id label = IdGenerator.of(buffer.readInt()); indexLabel = IndexLabel.label(graph, label); List fields = indexLabel.indexFields(); @@ -252,7 +255,7 @@ public static HugeIndex parseIndexId(HugeGraph graph, HugeType type, "Invalid range index field type"); Class clazz = dataType.isNumber() ? dataType.clazz() : DataType.LONG.clazz(); - values = bytes2number(buffer.read(id.length - labelLength), clazz); + values = bytes2number(buffer.read(id.length - labelLength - HUGE_TYPE_CODE_LENGTH), clazz); } HugeIndex index = new HugeIndex(graph, indexLabel); index.fieldValues(values);