Skip to content
Open
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
@@ -0,0 +1,118 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package org.apache.hugegraph.serializer.direct;

import org.apache.hugegraph.driver.HugeClient;
import org.apache.hugegraph.serializer.direct.struct.HugeType;
import org.apache.hugegraph.serializer.direct.util.BytesBuffer;
import org.apache.hugegraph.serializer.direct.util.GraphSchema;
import org.apache.hugegraph.serializer.direct.util.IdGenerator;
import org.apache.hugegraph.structure.GraphElement;
import org.apache.hugegraph.structure.graph.Edge;
import org.apache.hugegraph.structure.schema.PropertyKey;

import java.util.Map;

/**
* Directly implement BinarySerializer to generate point-edge Key-Value
*/
public class HStoreSerializer {

private HugeClient client;
private GraphSchema graphSchema;

public HStoreSerializer(HugeClient client) {
this.client = client;
this.graphSchema = new GraphSchema(client);
}

public byte[] getKeyBytes(GraphElement e) {
byte[] array = null;
if (e.type() == "vertex" && e.id() != null) {
BytesBuffer buffer = BytesBuffer.allocate(2 + 1 + e.id().toString().length());
buffer.writeId(IdGenerator.of(e.id()));
array = buffer.bytes();
} else if (e.type() == "edge") {
BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
Edge edge = (Edge)e;
buffer.writeId(IdGenerator.of(edge.sourceId()));
buffer.write(HugeType.EDGE_OUT.code());
buffer.writeId(IdGenerator.of(graphSchema.getEdgeLabel(e.label()).id()));
buffer.writeStringWithEnding("");
buffer.writeId(IdGenerator.of(edge.targetId()));
array = buffer.bytes();
}
return array;
}

public byte[] getKeyBytesSwitchDirection(GraphElement e) {
byte[] array = null;
BytesBuffer buffer = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
Edge edge = (Edge)e;
buffer.writeId(IdGenerator.of(edge.targetId()));
buffer.write(HugeType.EDGE_IN.code());
buffer.writeId(IdGenerator.of(graphSchema.getEdgeLabel(e.label()).id()));
buffer.writeStringWithEnding("");
buffer.writeId(IdGenerator.of(edge.sourceId()));
array = buffer.bytes();
return array;
}

public byte[] getValueBytes(GraphElement e) {
byte[] array = null;
if (e.type() == "vertex") {
int propsCount = e.properties().size();
BytesBuffer buffer = BytesBuffer.allocate(8 + 16 * propsCount);
buffer.writeId(IdGenerator.of(graphSchema.getVertexLabel(e.label()).id()));
buffer.writeVInt(propsCount);
for (Map.Entry<String, Object> entry : e.properties().entrySet()) {
PropertyKey propertyKey = graphSchema.getPropertyKey(entry.getKey());
buffer.writeVInt(propertyKey.id().intValue());
buffer.writeProperty(propertyKey.dataType(),entry.getValue());
}
array = buffer.bytes();
} else if (e.type() == "edge") {
int propsCount = e.properties().size();
BytesBuffer buffer = BytesBuffer.allocate(4 + 16 * propsCount);
buffer.writeVInt(propsCount);
for (Map.Entry<String, Object> entry : e.properties().entrySet()) {
PropertyKey propertyKey = graphSchema.getPropertyKey(entry.getKey());
buffer.writeVInt(propertyKey.id().intValue());
buffer.writeProperty(propertyKey.dataType(),entry.getValue());
}
array = buffer.bytes();
}
return array;
}

public byte[] getOwnerKeyBytes(GraphElement e) {
byte[] array = null;
if (e.type() == "vertex" && e.id() != null) {
array = IdGenerator.of(e.id()).asBytes();
} else if (e.type() == "edge") {
Edge edge = (Edge)e;
array = IdGenerator.of(edge.sourceId()).asBytes();
}
return array;
}

public void close() {
this.client.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import org.apache.hugegraph.driver.HugeClient;
import org.apache.hugegraph.driver.SchemaManager;
import org.apache.hugegraph.serializer.direct.HBaseSerializer;
import org.apache.hugegraph.serializer.direct.HStoreSerializer;
import org.apache.hugegraph.serializer.direct.RocksDBSerializer;
import org.apache.hugegraph.structure.graph.Edge;
import org.apache.hugegraph.structure.graph.Vertex;

/**
* This class is a demo for rocksdb/HBase put(rowkey, values) which use Client-Side's graph struct
* This class is a demo for Rocksdb/HBase/HStore put(rowkey, values) which use Client-Side's graph struct
* And we don't need to construct the graph element, just use it and transfer them to bytes array
* instead of json format
*/
Expand All @@ -39,7 +40,8 @@ public class BytesDemo {
static HugeClient client;
boolean bypassServer = true;
RocksDBSerializer ser;
HBaseSerializer hBaseSer;
HStoreSerializer hStoreSerializer;
HBaseSerializer HBaseSer;

public static void main(String[] args) {
BytesDemo ins = new BytesDemo();
Expand Down Expand Up @@ -96,7 +98,7 @@ void initGraph() {
.ifNotExist()
.create();

hBaseSer = new HBaseSerializer(client, vertexLogicPartitions, edgeLogicPartitions);
HBaseSer = new HBaseSerializer(client, vertexLogicPartitions, edgeLogicPartitions);
writeGraphElements();

client.close();
Expand Down Expand Up @@ -146,14 +148,14 @@ private void writeGraphElements() {
* */
void writeDirectly(List<Vertex> vertices, List<Edge> edges) {
for (Vertex vertex : vertices) {
byte[] rowkey = hBaseSer.getKeyBytes(vertex);
byte[] values = hBaseSer.getValueBytes(vertex);
byte[] rowkey = HBaseSer.getKeyBytes(vertex);
byte[] values = HBaseSer.getValueBytes(vertex);
sendRpcToHBase("vertex", rowkey, values);
}

for (Edge edge : edges) {
byte[] rowkey = hBaseSer.getKeyBytes(edge);
byte[] values = hBaseSer.getValueBytes(edge);
byte[] rowkey = HBaseSer.getKeyBytes(edge);
byte[] values = HBaseSer.getValueBytes(edge);
sendRpcToHBase("edge", rowkey, values);
}
}
Expand Down Expand Up @@ -183,8 +185,18 @@ boolean sendRpcToHBase(String type, byte[] rowkey, byte[] values) {
return flag;
}

boolean sendRpcToHStore(String type, byte[] rowkey, byte[] values) {
boolean flag = false;
try {
flag = put(type, rowkey, values);
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}

boolean put(String type, byte[] rowkey, byte[] values) throws IOException {
// TODO: put to HBase
// TODO: put to HBase/HStore
return true;
}
}