Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vertexKey without tag for encoder & add test #433

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ byte[] vertexKey(int vidLen,
byte[] vertexId,
int tagId);

byte[] orphanVertexKey(int vidLen,
int partitionId,
byte[] vertexId);

byte[] edgeKey(int vidLen,
int partitionId,
byte[] srcId,
Expand Down
100 changes: 64 additions & 36 deletions client/src/main/java/com/vesoft/nebula/encoder/NebulaCodecImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import java.util.List;

/**
* NebulaCodecImpl is an encoder to generate the given data.
* If the schema with default value, and the filed without given data, it will throw error.
* TODO: Support default value
* NebulaCodecImpl is an encoder to generate the given data.
* If the schema with default value, and the filed without given data, it will throw error.
* TODO: Support default value
*/
public class NebulaCodecImpl implements NebulaCodec {
private static final int PARTITION_ID_SIZE = 4;
Expand All @@ -29,10 +29,11 @@ public class NebulaCodecImpl implements NebulaCodec {
private static final int EDGE_VER_PLACE_HOLDER_SIZE = 1;
private static final int VERTEX_SIZE = PARTITION_ID_SIZE + TAG_ID_SIZE;
private static final int EDGE_SIZE = PARTITION_ID_SIZE + EDGE_TYPE_SIZE
+ EDGE_RANKING_SIZE + EDGE_VER_PLACE_HOLDER_SIZE;
+ EDGE_RANKING_SIZE + EDGE_VER_PLACE_HOLDER_SIZE;

private static final int VERTEX_KEY_TYPE = 0x00000001;
private static final int EDGE_KEY_TYPE = 0x00000002;
private static final int ORPHAN_VERTEX_KEY_TYPE = 0x00000007;
private static final int SEEK = 0xc70f6907;
private final ByteOrder byteOrder;

Expand All @@ -41,10 +42,10 @@ public NebulaCodecImpl() {
}

/**
* @param vidLen the vidLen from the space description
* @param vidLen the vidLen from the space description
* @param partitionId the partitionId
* @param vertexId the vertex id
* @param tagId the tag id
* @param vertexId the vertex id
* @param tagId the tag id
* @return
*/
@Override
Expand All @@ -60,7 +61,7 @@ public byte[] vertexKey(int vidLen,
buffer.order(this.byteOrder);
partitionId = (partitionId << 8) | VERTEX_KEY_TYPE;
buffer.putInt(partitionId)
.put(vertexId);
.put(vertexId);
if (vertexId.length < vidLen) {
ByteBuffer complementVid = ByteBuffer.allocate(vidLen - vertexId.length);
Arrays.fill(complementVid.array(), (byte) '\0');
Expand All @@ -71,12 +72,39 @@ public byte[] vertexKey(int vidLen,
}

/**
* @param vidLen the vidLen from the space description
* @param vidLen the vidLen from the space description
* @param partitionId the partitionId
* @param srcId the src id
* @param edgeType the edge type
* @param edgeRank the ranking
* @param dstId the dstId
* @param vertexId the vertex id
* @return
*/
@Override
public byte[] orphanVertexKey(int vidLen,
int partitionId,
byte[] vertexId) {
if (vertexId.length > vidLen) {
throw new RuntimeException(
"The length of vid size is out of the range, expected vidLen less then " + vidLen);
}
ByteBuffer buffer = ByteBuffer.allocate(PARTITION_ID_SIZE + vidLen);
buffer.order(this.byteOrder);
partitionId = (partitionId << 8) | ORPHAN_VERTEX_KEY_TYPE;
buffer.putInt(partitionId)
.put(vertexId);
if (vertexId.length < vidLen) {
ByteBuffer complementVid = ByteBuffer.allocate(vidLen - vertexId.length);
Arrays.fill(complementVid.array(), (byte) '\0');
buffer.put(complementVid);
}
return buffer.array();
}

/**
* @param vidLen the vidLen from the space description
* @param partitionId the partitionId
* @param srcId the src id
* @param edgeType the edge type
* @param edgeRank the ranking
* @param dstId the dstId
* @return byte[]
*/
@Override
Expand All @@ -86,16 +114,16 @@ public byte[] edgeKeyByDefaultVer(int vidLen,
int edgeType,
long edgeRank,
byte[] dstId) {
return edgeKey(vidLen, partitionId, srcId, edgeType, edgeRank, dstId, (byte)1);
return edgeKey(vidLen, partitionId, srcId, edgeType, edgeRank, dstId, (byte) 1);
}

/**
* @param vidLen the vidLen from the space description
* @param partitionId the partitionId
* @param srcId the src id
* @param edgeType the edge type
* @param edgeRank the ranking
* @param dstId the dstId
* @param vidLen the vidLen from the space description
* @param partitionId the partitionId
* @param srcId the src id
* @param edgeType the edge type
* @param edgeRank the ranking
* @param dstId the dstId
* @param edgeVerHolder the edgeVerHolder
* @return byte[]
*/
Expand Down Expand Up @@ -134,16 +162,16 @@ public byte[] edgeKey(int vidLen,
}

/**
* @param tag the TagItem
* @param names the property names
* @param tag the TagItem
* @param names the property names
* @param values the property values
* @return the encode byte[]
* @throws RuntimeException expection
*/
@Override
public byte[] encodeTag(TagItem tag,
List<String> names,
List<Object> values) throws RuntimeException {
List<Object> values) throws RuntimeException {
if (tag == null) {
throw new RuntimeException("TagItem is null");
}
Expand All @@ -152,16 +180,16 @@ public byte[] encodeTag(TagItem tag,
}

/**
* @param edge the EdgeItem
* @param names the property names
* @param edge the EdgeItem
* @param names the property names
* @param values the property values
* @return the encode byte[]
* @throws RuntimeException expection
*/
@Override
public byte[] encodeEdge(EdgeItem edge,
List<String> names,
List<Object> values) throws RuntimeException {
List<Object> values) throws RuntimeException {
if (edge == null) {
throw new RuntimeException("EdgeItem is null");
}
Expand All @@ -171,8 +199,8 @@ public byte[] encodeEdge(EdgeItem edge,

/**
* @param schema the schema
* @param ver the version of tag or edge
* @param names the property names
* @param ver the version of tag or edge
* @param names the property names
* @param values the property values
* @return the encode byte[]
* @throws RuntimeException expection
Expand All @@ -181,11 +209,11 @@ private byte[] encode(Schema schema,
long ver,
List<String> names,
List<Object> values)
throws RuntimeException {
throws RuntimeException {
if (names.size() != values.size()) {
throw new RuntimeException(
String.format("The names' size no equal with values' size, [%d] != [%d]",
names.size(), values.size()));
String.format("The names' size no equal with values' size, [%d] != [%d]",
names.size(), values.size()));
}
RowWriterImpl writer = new RowWriterImpl(genSchemaProvider(ver, schema), this.byteOrder);
for (int i = 0; i < names.size(); i++) {
Expand All @@ -204,11 +232,11 @@ private SchemaProviderImpl genSchemaProvider(long ver, Schema schema) {
int len = type.isSetType_length() ? type.getType_length() : 0;
GeoShape geoShape = type.isSetGeo_shape() ? type.getGeo_shape() : GeoShape.ANY;
schemaProvider.addField(new String(col.getName()),
type.type.getValue(),
len,
nullable,
hasDefault ? col.getDefault_value() : null,
geoShape.getValue());
type.type.getValue(),
len,
nullable,
hasDefault ? col.getDefault_value() : null,
geoShape.getValue());
}
return schemaProvider;
}
Expand Down
Loading