diff --git a/orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentFileChannelMap.java b/orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentFileChannelMap.java new file mode 100644 index 00000000..bc0bdf87 --- /dev/null +++ b/orm/src/main/java/com/zfoo/orm/lpmap/ConcurrentFileChannelMap.java @@ -0,0 +1,71 @@ +/* + * 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.function.BiConsumer; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class ConcurrentFileChannelMap implements LpMap { + + + private FileChannelMap fileChannelMap; + + public ConcurrentFileChannelMap(String dbPath, Class clazz) { + fileChannelMap = new FileChannelMap<>(dbPath, clazz); + } + + @Override + public synchronized V put(long key, V value) { + return fileChannelMap.put(key, value); + } + + @Override + public synchronized V putIfAbsent(long key, V packet) { + return fileChannelMap.put(key, packet); + } + + @Override + public synchronized V delete(long key) { + return fileChannelMap.delete(key); + } + + @Override + public synchronized V get(long key) { + return fileChannelMap.get(key); + } + + @Override + public synchronized long getMaxIndex() { + return fileChannelMap.getMaxIndex(); + } + + @Override + public synchronized long getIncrementIndex() { + return fileChannelMap.getIncrementIndex(); + } + + @Override + public synchronized void clear() { + fileChannelMap.clear(); + } + + @Override + public synchronized void forEach(BiConsumer biConsumer) { + fileChannelMap.forEach(biConsumer); + } +} diff --git a/orm/src/main/java/com/zfoo/orm/lpmap/FileChannelMap.java b/orm/src/main/java/com/zfoo/orm/lpmap/FileChannelMap.java index 11a23ba9..9ccd7773 100644 --- a/orm/src/main/java/com/zfoo/orm/lpmap/FileChannelMap.java +++ b/orm/src/main/java/com/zfoo/orm/lpmap/FileChannelMap.java @@ -28,6 +28,7 @@ import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; +import java.util.function.BiConsumer; /** * @author jaysunxiao @@ -156,6 +157,16 @@ public class FileChannelMap implements LpMap, Closeable { return maxIndex; } + @Override + public void forEach(BiConsumer biConsumer) { + for (var i = 1L; i < getMaxIndex(); i++) { + var value = get(i); + if (value != null) { + biConsumer.accept(i, value); + } + } + } + @Override public void clear() { try { 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 1f2222dd..135a9b9e 100644 --- a/orm/src/main/java/com/zfoo/orm/lpmap/FileHeapMap.java +++ b/orm/src/main/java/com/zfoo/orm/lpmap/FileHeapMap.java @@ -29,6 +29,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; +import java.util.function.BiConsumer; /** * @author jaysunxiao @@ -82,6 +83,11 @@ public class FileHeapMap implements LpMap { return heapMap.getIncrementIndex(); } + @Override + public void forEach(BiConsumer biConsumer) { + heapMap.forEach(biConsumer); + } + @Override public void clear() { heapMap.clear();