Skip to content

Commit 8344443

Browse files
committed
complete adoption for all id
1 parent 871015e commit 8344443

16 files changed

+1169
-31
lines changed

hugegraph-server/hugegraph-core/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@
9696
<groupId>org.apache.tinkerpop</groupId>
9797
<artifactId>gremlin-driver</artifactId>
9898
</dependency>
99+
<dependency>
100+
<groupId>org.apache.fury</groupId>
101+
<artifactId>fury-core</artifactId>
102+
<version>0.9.0-SNAPSHOT</version>
103+
</dependency>
99104

100105
<!-- jraft -->
101106
<dependency>

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/cache/CachedBackendStore.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,21 @@ public Number queryNumber(Query query) {
215215
/**
216216
* Query as an Id for cache
217217
*/
218-
static class QueryId implements Id {
218+
public static class QueryId implements Id {
219219

220-
private String query;
221-
private int hashCode;
220+
protected String query;
221+
protected int hashCode;
222222

223223
public QueryId(Query q) {
224224
this.query = q.toString();
225225
this.hashCode = q.hashCode();
226226
}
227227

228+
public QueryId(String query, int hashCode) {
229+
this.query = query;
230+
this.hashCode = hashCode;
231+
}
232+
228233
@Override
229234
public IdType type() {
230235
return IdType.UNKNOWN;

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/EdgeId.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
* > sortKeys > target-vertex-id }
3333
* NOTE:
3434
* <p>1. for edges with edgeLabelType = NORMAL: edgeLabelId = parentEdgeLabelId = subEdgeLabelId;
35-
* for edges with edgeLabelType = PARENT: edgeLabelId = subEdgeLabelId, parentEdgeLabelId =
36-
* edgeLabelId.fatherId
35+
* for edges with edgeLabelType = PARENT: edgeLabelId = subEdgeLabelId, parentEdgeLabelId =
36+
* edgeLabelId.fatherId
3737
* <p>2.if we use `entry.type()` which is IN or OUT as a part of id,
3838
* an edge's id will be different due to different directions (belongs
3939
* to 2 owner vertex)
@@ -49,15 +49,14 @@ public class EdgeId implements Id {
4949
HugeKeys.OTHER_VERTEX
5050
};
5151

52-
private final Id ownerVertexId;
53-
private final Directions direction;
54-
private final Id edgeLabelId;
55-
private final Id subLabelId;
56-
private final String sortValues;
57-
private final Id otherVertexId;
58-
59-
private final boolean directed;
60-
private String cache;
52+
protected final Id ownerVertexId;
53+
protected final Id edgeLabelId;
54+
protected final Id subLabelId;
55+
protected final Id otherVertexId;
56+
protected final Directions direction;
57+
protected final boolean directed;
58+
protected String sortValues;
59+
protected String cache;
6160

6261
public EdgeId(HugeVertex ownerVertex, Directions direction,
6362
Id edgeLabelId, Id subLabelId, String sortValues, HugeVertex otherVertex) {

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ public static IdType idType(Id id) {
119119
return IdType.UNKNOWN;
120120
}
121121

122-
private static int compareType(Id id1, Id id2) {
122+
public static int compareType(Id id1, Id id2) {
123123
return idType(id1).ordinal() - idType(id2).ordinal();
124124
}
125125

126126
/****************************** id defines ******************************/
127127

128-
public static final class StringId implements Id {
128+
public static class StringId implements Id {
129129

130-
private final String id;
130+
protected String id;
131131

132132
public StringId(String id) {
133133
E.checkArgument(!id.isEmpty(), "The id can't be empty");
@@ -196,11 +196,11 @@ public String toString() {
196196
}
197197
}
198198

199-
public static final class LongId extends Number implements Id {
199+
public static class LongId extends Number implements Id {
200200

201201
private static final long serialVersionUID = -7732461469037400190L;
202202

203-
private final long id;
203+
protected Long id;
204204

205205
public LongId(long id) {
206206
this.id = id;
@@ -270,7 +270,7 @@ public String toString() {
270270

271271
@Override
272272
public int intValue() {
273-
return (int) this.id;
273+
return this.id.intValue();
274274
}
275275

276276
@Override
@@ -289,9 +289,9 @@ public double doubleValue() {
289289
}
290290
}
291291

292-
public static final class UuidId implements Id {
292+
public static class UuidId implements Id {
293293

294-
private final UUID uuid;
294+
protected UUID uuid;
295295

296296
public UuidId(String string) {
297297
this(StringEncoding.uuid(string));
@@ -379,9 +379,9 @@ public String toString() {
379379
/**
380380
* This class is just used by backend store for wrapper object as Id
381381
*/
382-
public static final class ObjectId implements Id {
382+
public static class ObjectId implements Id {
383383

384-
private final Object object;
384+
protected Object object;
385385

386386
public ObjectId(Object object) {
387387
E.checkNotNull(object, "object");

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BinaryBackendEntry.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public BinaryBackendEntry(HugeType type, byte[] bytes, boolean enablePartition)
4949
}
5050

5151
// FIXME: `enablePartition` is unused here
52-
public BinaryBackendEntry(HugeType type, byte[] bytes, boolean enablePartition, boolean isOlap) {
52+
public BinaryBackendEntry(HugeType type, byte[] bytes, boolean enablePartition,
53+
boolean isOlap) {
5354
this(type, BytesBuffer.wrap(bytes).parseOlapId(type, isOlap));
5455
}
5556

@@ -207,10 +208,10 @@ public int hashCode() {
207208
return this.id().hashCode() ^ this.columns.size();
208209
}
209210

210-
public static final class BinaryId implements Id {
211+
public static class BinaryId implements Id {
211212

212-
private final byte[] bytes;
213-
private final Id id;
213+
protected byte[] bytes;
214+
protected Id id;
214215

215216
public BinaryId(byte[] bytes, Id id) {
216217
this.bytes = bytes;

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/allocator/OnHeapMemoryStrategy.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public OnHeapMemoryStrategy(MemoryManager memoryManager) {
2929
}
3030

3131
@Override
32-
public Object tryToAllocate(long size) {
32+
public byte[] tryToAllocate(long size) {
3333
if (memoryManager.getCurrentOnHeapAllocatedMemory().get() +
3434
memoryManager.getCurrentOffHeapAllocatedMemory().get() + size <
3535
MemoryManager.MAX_MEMORY_CAPACITY_IN_BYTES) {
@@ -41,7 +41,7 @@ public Object tryToAllocate(long size) {
4141
}
4242

4343
@Override
44-
public Object forceAllocate(long size) {
44+
public byte[] forceAllocate(long size) {
4545
int sizeOfByte = (int) (size / Bytes.BASE);
4646
memoryManager.getCurrentOnHeapAllocatedMemory().addAndGet(sizeOfByte);
4747
return new byte[sizeOfByte];

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/MemoryConsumer.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,31 @@
1717

1818
package org.apache.hugegraph.memory.consumer;
1919

20-
// TODO(pjz): integrated it with HG objects such as edges and vertex.
20+
import org.apache.hugegraph.memory.pool.MemoryPool;
21+
22+
/**
23+
* This interface is used by immutable, memory-heavy objects which will be stored in off heap.
24+
*/
2125
public interface MemoryConsumer {
2226

27+
/**
28+
* This method will read Off heap ByteBuf storing binary data of self.
29+
*
30+
* @return self value
31+
*/
32+
Object zeroCopyReadFromByteBuf();
33+
34+
/**
35+
* Serialize to DataOutputStream in stack first, then request an off heap ByteBuf from
36+
* OperatorMemoryPool based on size of DataOutputStream. Finally, serializing it to ByteBuf.
37+
*/
38+
void serializeSelfToByteBuf();
39+
40+
/**
41+
* Called after serializingSelfToByteBuf, pointing all self's on heap vars to null, in order
42+
* to let GC release all its on heap memory.
43+
*/
44+
void releaseOriginalOnHeapVars();
45+
46+
MemoryPool getOperatorMemoryPool();
2347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hugegraph.memory.consumer.impl.id;
19+
20+
import org.apache.hugegraph.backend.id.Id;
21+
import org.apache.hugegraph.backend.serializer.BinaryBackendEntry;
22+
import org.apache.hugegraph.memory.consumer.MemoryConsumer;
23+
import org.apache.hugegraph.memory.pool.MemoryPool;
24+
import org.apache.hugegraph.util.Bytes;
25+
import org.apache.hugegraph.util.E;
26+
27+
import io.netty.buffer.ByteBuf;
28+
import io.netty.buffer.ByteBufUtil;
29+
30+
public class BinaryIdOffHeap extends BinaryBackendEntry.BinaryId implements MemoryConsumer {
31+
32+
private final MemoryPool memoryPool;
33+
private final MemoryConsumer originId;
34+
private ByteBuf bytesOffHeap;
35+
36+
public BinaryIdOffHeap(byte[] bytes, Id id, MemoryPool memoryPool, MemoryConsumer originId) {
37+
super(bytes, id);
38+
this.memoryPool = memoryPool;
39+
this.originId = originId;
40+
serializeSelfToByteBuf();
41+
releaseOriginalOnHeapVars();
42+
}
43+
44+
@Override
45+
public void serializeSelfToByteBuf() {
46+
this.bytesOffHeap = (ByteBuf) memoryPool.requireMemory(bytes.length);
47+
this.bytesOffHeap.markReaderIndex();
48+
this.bytesOffHeap.writeBytes(bytes);
49+
}
50+
51+
@Override
52+
public BinaryBackendEntry.BinaryId zeroCopyReadFromByteBuf() {
53+
return new BinaryBackendEntry.BinaryId(ByteBufUtil.getBytes(bytesOffHeap),
54+
(Id) originId.zeroCopyReadFromByteBuf());
55+
}
56+
57+
@Override
58+
public MemoryPool getOperatorMemoryPool() {
59+
return this.memoryPool;
60+
}
61+
62+
@Override
63+
public void releaseOriginalOnHeapVars() {
64+
this.bytes = null;
65+
this.id = null;
66+
}
67+
68+
@Override
69+
public Object asObject() {
70+
return bytesOffHeap.nioBuffer();
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "0x" + Bytes.toHex(asBytes());
76+
}
77+
78+
@Override
79+
public boolean equals(Object other) {
80+
if (!(other instanceof BinaryIdOffHeap)) {
81+
return false;
82+
}
83+
return bytesOffHeap.equals(((BinaryIdOffHeap) other).bytesOffHeap);
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
return bytesOffHeap.hashCode();
89+
}
90+
91+
@Override
92+
public int length() {
93+
return bytesOffHeap.readableBytes();
94+
}
95+
96+
@Override
97+
public byte[] asBytes(int offset) {
98+
E.checkArgument(offset < this.bytesOffHeap.readableBytes(),
99+
"Invalid offset %s, must be < length %s",
100+
offset, this.bytesOffHeap.readableBytes());
101+
try {
102+
// zero-copy read
103+
byte[] tmpBytes = new byte[offset];
104+
this.bytesOffHeap.readBytes(tmpBytes);
105+
return tmpBytes;
106+
} finally {
107+
this.bytesOffHeap.resetReaderIndex();
108+
}
109+
}
110+
111+
@Override
112+
public byte[] asBytes() {
113+
try {
114+
// zero-copy read
115+
byte[] tmpBytes = new byte[bytesOffHeap.readableBytes()];
116+
this.bytesOffHeap.readBytes(tmpBytes);
117+
return tmpBytes;
118+
} finally {
119+
this.bytesOffHeap.resetReaderIndex();
120+
}
121+
}
122+
123+
@Override
124+
public int compareTo(Id other) {
125+
return bytesOffHeap.compareTo(((BinaryIdOffHeap) other).bytesOffHeap);
126+
}
127+
128+
@Override
129+
public Id origin() {
130+
return (Id) originId.zeroCopyReadFromByteBuf();
131+
}
132+
}

0 commit comments

Comments
 (0)