mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 02:16:31 +00:00
feat[lpmap]: 增加ConcurrentHeapMap
This commit is contained in:
1 parent
14a7e69c7c
commit
caf93644c4
4 files changed
+59
-3
No files matched your search
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed 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 com.zfoo.orm.lpmap;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import java.util.concurrent.ConcurrentNavigableMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ConcurrentHeapMap<V extends IPacket> implements LpMap<V> {
|
||||
|
||||
private ConcurrentNavigableMap<Long, V> map = new ConcurrentSkipListMap<>();
|
||||
|
||||
private AtomicLong index = new AtomicLong(0);
|
||||
|
||||
@Override
|
||||
public long insert(V packet) {
|
||||
var key = index.incrementAndGet();
|
||||
map.put(key, packet);
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(long key, V packet) {
|
||||
checkKey(key);
|
||||
return map.put(key, packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V delete(long key) {
|
||||
checkKey(key);
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(long key) {
|
||||
checkKey(key);
|
||||
return map.get(key);
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class FileHeapMap<V extends IPacket> implements LpMap<V> {
|
||||
buffer.writeBytes(fileChannel, 0L, (int) file.length());
|
||||
|
||||
var size = ByteBufUtils.readLong(buffer);
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (var i = 0; i < size; i++) {
|
||||
var key = ByteBufUtils.readLong(buffer);
|
||||
var value = (V) protocolRegistration.read(buffer);
|
||||
put(key, value);
|
||||
|
||||
@@ -29,9 +29,9 @@ public class HeapMap<V extends IPacket> implements LpMap<V> {
|
||||
/**
|
||||
* 没有被使用的key
|
||||
*/
|
||||
protected Queue<Long> freeKeyQueue = new LinkedList<>();
|
||||
private Queue<Long> freeKeyQueue = new LinkedList<>();
|
||||
|
||||
protected long index = 0;
|
||||
private long index = 0;
|
||||
|
||||
public HeapMap(int initialCapacity) {
|
||||
map = new LongObjectHashMap<>(initialCapacity);
|
||||
|
||||
@@ -70,5 +70,6 @@ public class FileHeapMapTest {
|
||||
public void readTest() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
var map = new FileHeapMap<MyPacket>(new File("myPacket.db"), 10, MyPacket.class);
|
||||
Assert.assertNotNull(map.get(5));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user