Skip to content

Commit

Permalink
StringEncoding: improve empty bytes decode
Browse files Browse the repository at this point in the history
Change-Id: I6649c70ef63305acb9eada4abf307a099cf4a997
  • Loading branch information
javeme committed Mar 18, 2022
1 parent 730f332 commit 61d7798
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

public class BinaryBackendEntry implements BackendEntry {

private static final byte[] EMPTY_BYTES = new byte[]{};

private final HugeType type;
private final BinaryId id;
private Id subId;
Expand Down Expand Up @@ -119,7 +117,7 @@ public void column(BackendColumn column) {

public void column(byte[] name, byte[] value) {
E.checkNotNull(name, "name");
value = value != null ? value : EMPTY_BYTES;
value = value != null ? value : BytesBuffer.BYTES_EMPTY;
this.columns.add(BackendColumn.of(name, value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@

public class BinarySerializer extends AbstractSerializer {

public static final byte[] EMPTY_BYTES = new byte[0];

/*
* Id is stored in column name if keyWithIdPrefix=true like RocksDB,
* else stored in rowkey like HBase.
Expand Down Expand Up @@ -399,7 +397,8 @@ public BackendEntry writeVertex(HugeVertex vertex) {
}

// Fill column
byte[] name = this.keyWithIdPrefix ? entry.id().asBytes() : EMPTY_BYTES;
byte[] name = this.keyWithIdPrefix ?
entry.id().asBytes() : BytesBuffer.BYTES_EMPTY;
entry.column(name, buffer.bytes());

return entry;
Expand All @@ -411,16 +410,19 @@ public BackendEntry writeOlapVertex(HugeVertex vertex) {
BytesBuffer buffer = BytesBuffer.allocate(8 + 16);

Collection<HugeProperty<?>> properties = vertex.getProperties();
E.checkArgument(properties.size() == 1,
"Expect only 1 property for olap vertex, but got %s",
properties.size());
if (properties.size() != 1) {
E.checkArgument(false,
"Expect 1 property for olap vertex, but got %s",
properties.size());
}
HugeProperty<?> property = properties.iterator().next();
PropertyKey propertyKey = property.propertyKey();
buffer.writeVInt(SchemaElement.schemaId(propertyKey.id()));
buffer.writeProperty(propertyKey, property.value());

// Fill column
byte[] name = this.keyWithIdPrefix ? entry.id().asBytes() : EMPTY_BYTES;
byte[] name = this.keyWithIdPrefix ?
entry.id().asBytes() : BytesBuffer.BYTES_EMPTY;
entry.column(name, buffer.bytes());
entry.subId(propertyKey.id());
entry.olap(true);
Expand Down Expand Up @@ -477,7 +479,7 @@ protected void parseVertexOlap(byte[] value, HugeVertex vertex) {
public BackendEntry writeEdge(HugeEdge edge) {
BinaryBackendEntry entry = newBackendEntry(edge);
byte[] name = this.keyWithIdPrefix ?
this.formatEdgeName(edge) : EMPTY_BYTES;
this.formatEdgeName(edge) : BytesBuffer.BYTES_EMPTY;
byte[] value = this.formatEdgeValue(edge);
entry.column(name, value);

Expand All @@ -498,8 +500,11 @@ public BackendEntry writeEdgeProperty(HugeEdgeProperty<?> prop) {
public HugeEdge readEdge(HugeGraph graph, BackendEntry bytesEntry) {
HugeVertex vertex = this.readVertex(graph, bytesEntry);
Collection<HugeEdge> edges = vertex.getEdges();
E.checkState(edges.size() == 1,
"Expect one edge in vertex, but got %s", edges.size());
if (edges.size() != 1) {
E.checkState(false,
"Expect 1 edge in vertex, but got %s",
edges.size());
}
return edges.iterator().next();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public final class BytesBuffer extends OutputStream {
public static final int BUF_EDGE_ID = 128;
public static final int BUF_PROPERTY = 64;

public static final byte[] BYTES_EMPTY = new byte[0];

private ByteBuffer buffer;
private final boolean resize;

Expand Down Expand Up @@ -906,7 +908,8 @@ private long readNumber(byte b) {
private byte[] readBytesWithEnding() {
int start = this.buffer.position();
boolean foundEnding = false;
while (this.remaining() > 0) {
int remaining = this.remaining();
for (int i = 0; i < remaining; i++) {
byte current = this.read();
if (current == STRING_ENDING_BYTE) {
foundEnding = true;
Expand All @@ -917,6 +920,9 @@ private byte[] readBytesWithEnding() {
Bytes.toHex(STRING_ENDING_BYTE));
int end = this.buffer.position() - 1;
int len = end - start;
if (len <= 0) {
return BYTES_EMPTY;
}
byte[] bytes = new byte[len];
System.arraycopy(this.array(), start, bytes, 0, len);
return bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ public void assignId(Id id, boolean force) {
protected void checkIdLength() {
assert this.id != null;
int len = this.id.asBytes().length;
E.checkArgument(len <= BytesBuffer.ID_LEN_MAX,
if (len <= BytesBuffer.ID_LEN_MAX) {
return;
}
E.checkArgument(false,
"The max length of vertex id is %s, but got %s {%s}",
BytesBuffer.ID_LEN_MAX, len, this.id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class StringEncoding {

private static final MessageDigest DIGEST;
private static final byte[] BYTES_EMPTY = new byte[0];
private static final String STRING_EMPTY = "";
private static final int BLOCK_SIZE = 4096;

static {
Expand Down Expand Up @@ -117,6 +118,9 @@ public static byte[] encode(String value) {
}

public static String decode(byte[] bytes) {
if (bytes.length == 0) {
return STRING_EMPTY;
}
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
Expand All @@ -125,6 +129,9 @@ public static String decode(byte[] bytes) {
}

public static String decode(byte[] bytes, int offset, int length) {
if (length == 0) {
return STRING_EMPTY;
}
try {
return new String(bytes, offset, length, "UTF-8");
} catch (UnsupportedEncodingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.backend.serializer.BinaryBackendEntry;
import com.baidu.hugegraph.backend.serializer.BinaryEntryIterator;
import com.baidu.hugegraph.backend.serializer.BinarySerializer;
import com.baidu.hugegraph.backend.serializer.BytesBuffer;
import com.baidu.hugegraph.backend.store.BackendEntry;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn;
import com.baidu.hugegraph.backend.store.BackendEntryIterator;
Expand Down Expand Up @@ -200,10 +200,10 @@ public void insert(Session session, BackendEntry entry) {
long ttl = entry.ttl();
if (ttl == 0L) {
session.put(this.table(), CF, col.name,
BinarySerializer.EMPTY_BYTES, col.value);
BytesBuffer.BYTES_EMPTY, col.value);
} else {
session.put(this.table(), CF, col.name,
BinarySerializer.EMPTY_BYTES, col.value, ttl);
BytesBuffer.BYTES_EMPTY, col.value, ttl);
}
}

Expand Down

0 comments on commit 61d7798

Please sign in to comment.