diff --git a/hugegraph-store/hg-store-common/pom.xml b/hugegraph-store/hg-store-common/pom.xml new file mode 100644 index 000000000..7746c7615 --- /dev/null +++ b/hugegraph-store/hg-store-common/pom.xml @@ -0,0 +1,33 @@ + + + + + + 4.0.0 + + + org.apache.hugegraph + hugegraph-store + ${revision} + ../pom.xml + + + hg-store-common + diff --git a/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java new file mode 100644 index 000000000..25a5f1814 --- /dev/null +++ b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java @@ -0,0 +1,63 @@ +/* + * 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.store.buffer; + +import java.nio.ByteBuffer; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +public class ByteBufferAllocator { + + // size of each Buffer + final int capacity; + // max num of Buffers + final int maxCount; + final BlockingQueue freeQueue = new LinkedBlockingQueue<>(); + // current num of Buffers in queue + AtomicInteger totalCount; + + public ByteBufferAllocator(int cap, int count) { + this.capacity = cap; + this.maxCount = count; + this.totalCount = new AtomicInteger(0); + } + + public ByteBuffer get() throws InterruptedException { + ByteBuffer buffer = null; + while (buffer == null) { + if (freeQueue.size() > 0) { + buffer = freeQueue.poll(); + } else if (totalCount.get() < maxCount) { + buffer = ByteBuffer.allocate(capacity); + totalCount.incrementAndGet(); + } else { + buffer = freeQueue.poll(1, TimeUnit.SECONDS); + } + } + return buffer; + } + + public void release(ByteBuffer buffer) { + if (freeQueue.size() < maxCount) { + buffer.clear(); + freeQueue.add(buffer); + } + } +} diff --git a/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/KVByteBuffer.java b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/KVByteBuffer.java new file mode 100644 index 000000000..524a0f58f --- /dev/null +++ b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/KVByteBuffer.java @@ -0,0 +1,98 @@ +/* + * 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.store.buffer; + +import java.nio.ByteBuffer; + +public class KVByteBuffer { + + ByteBuffer buffer; + + public KVByteBuffer(int capacity) { + buffer = ByteBuffer.allocate(capacity); + } + + public KVByteBuffer(byte[] buffer) { + this.buffer = ByteBuffer.wrap(buffer); + } + + public KVByteBuffer(ByteBuffer buffer) { + this.buffer = buffer; + } + + public void clear() { + this.buffer.clear(); + } + + public KVByteBuffer flip() { + buffer.flip(); + return this; + } + + public ByteBuffer getBuffer() { + return buffer; + } + + public ByteBuffer copyBuffer() { + byte[] buf = new byte[buffer.position()]; + System.arraycopy(buffer.array(), 0, buf, 0, buffer.position()); + return ByteBuffer.wrap(buf); + } + + public void put(byte data) { + buffer.put(data); + } + + public void put(byte[] data) { + if (data != null) { + buffer.putInt(data.length); + buffer.put(data); + } + } + + public byte[] getBytes() { + int len = buffer.getInt(); + byte[] data = new byte[len]; + buffer.get(data); + return data; + } + + public byte get() { + return buffer.get(); + } + + public void putInt(int data) { + buffer.putInt(data); + } + + public int getInt() { + return buffer.getInt(); + } + + public byte[] array() { + return this.buffer.array(); + } + + public int position() { + return this.buffer.position(); + } + + public final boolean hasRemaining() { + return this.buffer.hasRemaining(); + } +} diff --git a/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/Bits.java b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/Bits.java new file mode 100644 index 000000000..2b78a22b8 --- /dev/null +++ b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/Bits.java @@ -0,0 +1,66 @@ +/* + * 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.store.term; + +import java.nio.ByteBuffer; + +public class Bits { + + /** + * 大头字节序写入short + */ + public static void putShort(byte[] buf, int offSet, int x) { + buf[offSet] = (byte) (x >> 8); + buf[offSet + 1] = (byte) (x); + } + + public static void putInt(byte[] buf, int offSet, int x) { + buf[offSet] = (byte) (x >> 24); + buf[offSet + 1] = (byte) (x >> 16); + buf[offSet + 2] = (byte) (x >> 8); + buf[offSet + 3] = (byte) (x); + } + + /** + * 大头字节序读取short + */ + public static int getShort(byte[] buf, int offSet) { + int x = buf[offSet] & 0xff; + x = (x << 8) + (buf[offSet + 1] & 0xff); + return x; + } + + public static int getInt(byte[] buf, int offSet) { + int x = (buf[offSet] << 24) + + ((buf[offSet + 1] & 0xff) << 16) + + ((buf[offSet + 2] & 0xff) << 8) + + (buf[offSet + 3] & 0xff); + return x; + } + + public static void put(byte[] buf, int offSet, byte[] srcBuf) { + System.arraycopy(srcBuf, 0, buf, offSet, srcBuf.length); + } + + public static int toInt(byte[] bytes) { + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + buffer.put(bytes); + buffer.flip();//need flip + return buffer.getInt(); + } +} diff --git a/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/HgPair.java b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/HgPair.java new file mode 100644 index 000000000..84aa00b9a --- /dev/null +++ b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/HgPair.java @@ -0,0 +1,142 @@ +/* + * 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.store.term; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Copy from javafx.util:Pair + * TODO: refer license later, ?% match, maybe refer to avafx.util:Pair + *

+ * created on 2021/10/15 + */ +public class HgPair implements Serializable { + + /** + * Key of this Pair. + */ + private K key; + /** + * Value of this Pair. + */ + private V value; + + public HgPair() { + + } + + /** + * Creates a new pair + * + * @param key The key for this pair + * @param value The value to use for this pair + */ + public HgPair(K key, V value) { + this.key = key; + this.value = value; + } + + /** + * Gets the key for this pair. + * + * @return key for this pair + */ + public K getKey() { + return key; + } + + public void setKey(K key) { + this.key = key; + } + + /** + * Gets the value for this pair. + * + * @return value for this pair + */ + public V getValue() { + return value; + } + + public void setValue(V value) { + this.value = value; + } + + /** + *

String representation of this + * Pair.

+ * + *

The default name/value delimiter '=' is always used.

+ * + * @return String representation of this Pair + */ + @Override + public String toString() { + return key + "=" + value; + } + + /** + *

Generate a hash code for this Pair.

+ * + *

The hash code is calculated using both the name and + * the value of the Pair.

+ * + * @return hash code for this Pair + */ + @Override + public int hashCode() { + // name's hashCode is multiplied by an arbitrary prime number (13) + // in order to make sure there is a difference in the hashCode between + // these two parameters: + // name: a value: aa + // name: aa value: a + return key.hashCode() * 13 + (value == null ? 0 : value.hashCode()); + } + + /** + *

Test this Pair for equality with another + * Object.

+ * + *

If the Object to be tested is not a + * Pair or is null, then this method + * returns false.

+ * + *

Two Pairs are considered equal if and only if + * both the names and values are equal.

+ * + * @param o the Object to test for + * equality with this Pair + * @return true if the given Object is + * equal to this Pair else false + */ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o instanceof HgPair) { + HgPair pair = (HgPair) o; + if (!Objects.equals(key, pair.key)) { + return false; + } + return Objects.equals(value, pair.value); + } + return false; + } +} diff --git a/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/HgTriple.java b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/HgTriple.java new file mode 100644 index 000000000..5206b2444 --- /dev/null +++ b/hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/term/HgTriple.java @@ -0,0 +1,79 @@ +/* + * 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.store.term; + +import java.util.Objects; + +/** + * created on 2021/10/19 + */ +public class HgTriple { + + private final X x; + private final Y y; + private final Z z; + private int hash = -1; + + public HgTriple(X x, Y y, Z z) { + this.x = x; + this.y = y; + this.z = z; + } + + public X getX() { + return x; + } + + public Y getY() { + return y; + } + + public Z getZ() { + return z; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + HgTriple hgTriple = (HgTriple) o; + return Objects.equals(x, hgTriple.x) && Objects.equals(y, hgTriple.y) && + Objects.equals(z, hgTriple.z); + } + + @Override + public int hashCode() { + if (hash == -1) { + hash = Objects.hash(x, y, z); + } + return this.hash; + } + + @Override + public String toString() { + return "HgTriple{" + + "x=" + x + + ", y=" + y + + ", z=" + z + + '}'; + } +}