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

StringEncoding: improve empty bytes decode #1788

Merged
merged 1 commit into from
Apr 16, 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 @@ -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 @@ -123,7 +121,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 @@ -19,9 +19,12 @@

package com.baidu.hugegraph.backend.serializer;

import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baidu.hugegraph.config.HugeConfig;
import org.apache.commons.lang.NotImplementedException;

import com.baidu.hugegraph.HugeGraph;
Expand All @@ -39,6 +42,7 @@
import com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId;
import com.baidu.hugegraph.backend.store.BackendEntry;
import com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.schema.PropertyKey;
Expand Down Expand Up @@ -71,8 +75,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 @@ -415,7 +417,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 @@ -427,16 +430,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 @@ -493,8 +499,7 @@ protected void parseVertexOlap(byte[] value, HugeVertex vertex) {
public BackendEntry writeEdge(HugeEdge edge) {
BinaryBackendEntry entry = newBackendEntry(edge);
byte[] name = this.keyWithIdPrefix ?
entry.id().asBytes() : EMPTY_BYTES;

entry.id().asBytes() : BytesBuffer.BYTES_EMPTY;
byte[] value = this.formatEdgeValue(edge);
entry.column(name, value);

Expand All @@ -515,8 +520,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 @@ -908,7 +910,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 @@ -919,6 +922,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 @@ -35,7 +35,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 @@ -208,10 +208,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