From caf93644c40168878ad519525d7f9642e6571020 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Tue, 17 Aug 2021 12:26:26 +0800 Subject: [PATCH] =?UTF-8?q?feat[lpmap]:=20=E5=A2=9E=E5=8A=A0ConcurrentHeap?= =?UTF-8?q?Map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zfoo/orm/lpmap/ConcurrentHeapMap.java | 55 +++++++++++++++++++ .../java/com/zfoo/orm/lpmap/FileHeapMap.java | 2 +- .../main/java/com/zfoo/orm/lpmap/HeapMap.java | 4 +- .../com/zfoo/orm/lpmap/FileHeapMapTest.java | 1 + 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentHeapMap.java diff --git a/orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentHeapMap.java b/orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentHeapMap.java new file mode 100644 index 00000000..0a76d56b --- /dev/null +++ b/orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentHeapMap.java @@ -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 implements LpMap { + + private ConcurrentNavigableMap 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); + } +} diff --git a/orm/src/main/java/com/zfoo/orm/lpmap/FileHeapMap.java b/orm/src/main/java/com/zfoo/orm/lpmap/FileHeapMap.java index 7ddbae16..0a5d32b0 100644 --- a/orm/src/main/java/com/zfoo/orm/lpmap/FileHeapMap.java +++ b/orm/src/main/java/com/zfoo/orm/lpmap/FileHeapMap.java @@ -83,7 +83,7 @@ public class FileHeapMap implements LpMap { 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); diff --git a/orm/src/main/java/com/zfoo/orm/lpmap/HeapMap.java b/orm/src/main/java/com/zfoo/orm/lpmap/HeapMap.java index f9294d40..6c0d460b 100644 --- a/orm/src/main/java/com/zfoo/orm/lpmap/HeapMap.java +++ b/orm/src/main/java/com/zfoo/orm/lpmap/HeapMap.java @@ -29,9 +29,9 @@ public class HeapMap implements LpMap { /** * 没有被使用的key */ - protected Queue freeKeyQueue = new LinkedList<>(); + private Queue freeKeyQueue = new LinkedList<>(); - protected long index = 0; + private long index = 0; public HeapMap(int initialCapacity) { map = new LongObjectHashMap<>(initialCapacity); diff --git a/orm/src/test/java/com/zfoo/orm/lpmap/FileHeapMapTest.java b/orm/src/test/java/com/zfoo/orm/lpmap/FileHeapMapTest.java index 693ce071..09ea41ad 100644 --- a/orm/src/test/java/com/zfoo/orm/lpmap/FileHeapMapTest.java +++ b/orm/src/test/java/com/zfoo/orm/lpmap/FileHeapMapTest.java @@ -70,5 +70,6 @@ public class FileHeapMapTest { public void readTest() { ProtocolManager.initProtocol(Set.of(MyPacket.class)); var map = new FileHeapMap(new File("myPacket.db"), 10, MyPacket.class); + Assert.assertNotNull(map.get(5)); } }