|
| 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