io.github.shanqiang.offheap.datastructure.HashMapOffheap Maven / Gradle / Ivy
The newest version!
package io.github.shanqiang.offheap.datastructure;
import io.github.shanqiang.offheap.InternalUnsafe;
import io.github.shanqiang.offheap.interfazz.Offheap;
import io.github.shanqiang.offheap.interfazz.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import static io.github.shanqiang.offheap.InternalUnsafe.getAndAddLong;
import static io.github.shanqiang.offheap.InternalUnsafe.getInt;
import static io.github.shanqiang.offheap.InternalUnsafe.getLong;
import static io.github.shanqiang.offheap.InternalUnsafe.getShort;
import static io.github.shanqiang.offheap.InternalUnsafe.putInt;
import static io.github.shanqiang.offheap.InternalUnsafe.putLong;
import static io.github.shanqiang.offheap.InternalUnsafe.putShort;
import static io.github.shanqiang.offheap.InternalUnsafe.setMemory;
public class HashMapOffheap, V extends Serializer>
implements Iterable, Serializer
{
private static final Logger logger = LoggerFactory.getLogger(HashMapOffheap.class);
private static class PreKey {
long next;
int hash;
short keyClassId;
long value;
short valueClassId;
private static final Property[] properties = new Property[5];
private static final int INSTANCE_SIZE = (int) Serializer.initProperties(PreKey.class, properties);
private static long nextOffset;
private static long hashOffset;
private static long keyClassIdOffset;
private static long valueOffset;
private static long valueClassIdOffset;
static {
for (int i = 0; i < properties.length; i++) {
long offset = properties[i].offsetOffheap;
switch (properties[i].name) {
case "next":
nextOffset = offset;
break;
case "hash":
hashOffset = offset;
break;
case "keyClassId":
keyClassIdOffset = offset;
break;
case "value":
valueOffset = offset;
break;
case "valueClassId":
valueClassIdOffset = offset;
break;
}
}
}
static long getNext(long addr) {
return getLong(addr + nextOffset);
}
static void setNext(long addr, long next) {
putLong(addr + nextOffset, next);
}
static int getHash(long addr) {
return getInt(addr + hashOffset);
}
static void setHash(long addr, int hash) {
putInt(addr + hashOffset, hash);
}
static short getKeyClassId(long addr) {
return getShort(addr + keyClassIdOffset);
}
static void setKeyClassId(long addr, short keyClassId) {
putShort(addr + keyClassIdOffset, keyClassId);
}
static long getValue(long addr) {
return getLong(addr + valueOffset);
}
static void setValue(long addr, long value) {
putLong(addr + valueOffset, value);
}
static short getValueClassId(long addr) {
return getShort(addr + valueClassIdOffset);
}
static void setValueClassId(long addr, short valueClassId) {
putShort(addr + valueClassIdOffset, valueClassId);
}
}
private static class Handle {
private long table = 0L;
private int size = 0;
private int capacity = 0;
private int threshold = 0;
private long refCount;
private long finalizeCount;
private final long addr;
private static final Property[] properties = new Property[6];
private static final long INSTANCE_SIZE = Serializer.initProperties(Handle.class, properties);
private static long tableOffset;
private static long sizeOffset;
private static long capacityOffset;
private static long thresholdOffset;
private static long refCountOffset;
private static long finalizeCountOffset;
static {
for (int i = 0; i < properties.length; i++) {
long offset = properties[i].offsetOffheap;
switch (properties[i].name) {
case "table":
tableOffset = offset;
break;
case "size":
sizeOffset = offset;
break;
case "capacity":
capacityOffset = offset;
break;
case "threshold":
thresholdOffset = offset;
break;
case "refCount":
refCountOffset = offset;
break;
case "finalizeCount":
finalizeCountOffset = offset;
break;
}
}
}
private Handle(long addr) {
this.addr = addr;
}
void free() {
InternalUnsafe.free(table());
InternalUnsafe.free(addr);
}
long table() {
return getLong(addr + tableOffset);
}
void table(long newTable) {
putLong(addr + tableOffset, newTable);
}
int size() {
return getInt(addr + sizeOffset);
}
void size(int size) {
putInt(addr + sizeOffset, size);
}
int capacity() {
return getInt(addr + capacityOffset);
}
void capacity(int capacity) {
putInt(addr + capacityOffset, capacity);
}
int threshold() {
return getInt(addr + thresholdOffset);
}
void threshold(int threshold) {
putInt(addr + thresholdOffset, threshold);
}
long getRefCount() {
return getLong(addr + refCountOffset);
}
long getAndAddRefCount(long l) {
return getAndAddLong(addr + refCountOffset, l);
}
long getFinalizeCount() {
return getLong(addr + finalizeCountOffset);
}
long getAndAddFinalizeCount(long l) {
return getAndAddLong(addr + finalizeCountOffset, l);
}
}
private final Handle handle;
private volatile boolean released;
// 大部分情况下key和value的类型加起来只有2个
// 但可以是任意多的key类型和value类型只要满足派生条件
private final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy