feat(store): integrate `store-common` submodule

This commit is contained in:
Peng Junzhi 2024-04-05 11:53:46 +08:00 committed by imbajin
parent 52f3d64b6a
commit e605f732e2
6 changed files with 481 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.hugegraph</groupId>
<artifactId>hugegraph-store</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>hg-store-common</artifactId>
</project>

View File

@ -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<ByteBuffer> 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);
}
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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
* <p>
* created on 2021/10/15
*/
public class HgPair<K, V> implements Serializable {
/**
* Key of this <code>Pair</code>.
*/
private K key;
/**
* Value of this <code>Pair</code>.
*/
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;
}
/**
* <p><code>String</code> representation of this
* <code>Pair</code>.</p>
*
* <p>The default name/value delimiter '=' is always used.</p>
*
* @return <code>String</code> representation of this <code>Pair</code>
*/
@Override
public String toString() {
return key + "=" + value;
}
/**
* <p>Generate a hash code for this <code>Pair</code>.</p>
*
* <p>The hash code is calculated using both the name and
* the value of the <code>Pair</code>.</p>
*
* @return hash code for this <code>Pair</code>
*/
@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());
}
/**
* <p>Test this <code>Pair</code> for equality with another
* <code>Object</code>.</p>
*
* <p>If the <code>Object</code> to be tested is not a
* <code>Pair</code> or is <code>null</code>, then this method
* returns <code>false</code>.</p>
*
* <p>Two <code>Pair</code>s are considered equal if and only if
* both the names and values are equal.</p>
*
* @param o the <code>Object</code> to test for
* equality with this <code>Pair</code>
* @return <code>true</code> if the given <code>Object</code> is
* equal to this <code>Pair</code> else <code>false</code>
*/
@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;
}
}

View File

@ -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<X, Y, Z> {
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 +
'}';
}
}