BytesKey 및 기타 코드 세부 사항
4699 단어 bytes
public class BytesKey implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -6296965387124592707L;
private byte[] data;
public BytesKey(byte[] data) {
this.data = data;
}
/**
* @return the data
*/
public byte[] getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(byte[] data) {
this.data = data;
}
@Override
public int hashCode() {
int h = 0;
if (null != this.data) {
for (int i = 0; i < this.data.length; i++) {
h = 31*h + data[i++];
}
}
return h;
}
@Override
public boolean equals(Object o) {
if (null == o || !(o instanceof BytesKey)) {
return false;
}
BytesKey k = (BytesKey)o;
if (null == k.getData() && null == this.getData()) {
return true;
}
if (null == k.getData() || null == this.getData()) {
return false;
}
if (k.getData().length != this.getData().length) {
return false;
}
for (int i = 0; i < this.data.length; ++i) {
if (this.data[i] != k.getData()[i]) {
return false;
}
}
return true;
}
}
BytesKey를 사용한 간단한 메모리 KV 캐시
public class MemStore implements Store {
private Map<BytesKey, byte[]> datas = new ConcurrentHashMap<BytesKey, byte[]>();
/*
* (non-Javadoc)
*
* @see com.taobao.common.store.Store#add(byte[], byte[])
*/
public void add(byte[] key, byte[] data) throws IOException {
datas.put(new BytesKey(key), data);
}
/*
* (non-Javadoc)
*
* @see com.taobao.common.store.Store#get(byte[])
*/
public byte[] get(byte[] key) throws IOException {
return datas.get(new BytesKey(key));
}
/*
* (non-Javadoc)
*
* @see com.taobao.common.store.Store#iterator()
*/
public Iterator<byte[]> iterator() throws IOException {
final Iterator<BytesKey> it = datas.keySet().iterator();
return new Iterator<byte[]>() {
public boolean hasNext() {
return it.hasNext();
}
public byte[] next() {
BytesKey key = it.next();
if (null == key) {
return null;
}
return key.getData();
}
public void remove() {
it.remove();
}
};
}
/*
* (non-Javadoc)
*
* @see com.taobao.common.store.Store#remove(byte[])
*/
public boolean remove(byte[] key) throws IOException {
return null != datas.remove(new BytesKey(key));
}
/*
* (non-Javadoc)
*
* @see com.taobao.common.store.Store#size()
*/
public int size() {
return datas.size();
}
/*
* (non-Javadoc)
*
* @see com.taobao.common.store.Store#update(byte[], byte[])
*/
public boolean update(byte[] key, byte[] data) throws IOException {
datas.put(new BytesKey(key), data);
return true;
}
public void close() throws IOException {
// nodo
}
public long getMaxFileCount() {
return Long.MAX_VALUE;
}
public void setMaxFileCount(long maxFileCount) {
}
}
이중hash를 사용하여 키를 계산하고,
private int hash(int keyHash, int i) {
return abs(hash1(keyHash) + i * hash2(keyHash)) % table.length; //
}
BitSet을 사용하여 키의 존재 여부를 신속하게 판단하고 존재하면 다음 라운드의 이중hash로 키를 계산합니다
while (this.table[j] != null && !isEntryDeleted(j) && this.bitSet.get(offset) && i < m) {
j = hash(keyHash, i++);
offset = calcOffset(j);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
BytesKey 및 기타 코드 세부 사항byte 수조는 어떻게 키로 합니까?다음은 하나의 실현이다. BytesKey를 사용한 간단한 메모리 KV 캐시 이중hash를 사용하여 키를 계산하고, BitSet을 사용하여 키의 존재 여부를 신속하게 판단하고 존재하...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.