Skip to content

Commit

Permalink
fix: index serialization and deserialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
VGalaxies committed Sep 11, 2023
1 parent d818aef commit 1a7f6cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class HugeIndex implements GraphType, Cloneable {
private Object fieldValues;
private IndexLabel indexLabel;
private Set<IdWithExpiredTime> elementIds;
private static final int HUGE_TYPE_CODE_LENGTH = 1;

public HugeIndex(HugeGraph graph, IndexLabel indexLabel) {
E.checkNotNull(graph, "graph");
Expand Down Expand Up @@ -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,
Expand All @@ -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<Id> fields = indexLabel.indexFields();
Expand All @@ -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);
Expand Down

0 comments on commit 1a7f6cb

Please sign in to comment.