perf[net]: 增加ConcurrentFileChannelMap类型的map

This commit is contained in:
jaysunxiao
2021-08-28 14:14:29 +08:00
parent eafd8bc7a1
commit 575b53142d
3 changed files with 88 additions and 0 deletions
@@ -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<V extends IPacket> implements LpMap<V> {
private FileChannelMap<V> fileChannelMap;
public ConcurrentFileChannelMap(String dbPath, Class<V> 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<Long, V> biConsumer) {
fileChannelMap.forEach(biConsumer);
}
}
@@ -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<V extends IPacket> implements LpMap<V>, Closeable {
return maxIndex;
}
@Override
public void forEach(BiConsumer<Long, V> biConsumer) {
for (var i = 1L; i < getMaxIndex(); i++) {
var value = get(i);
if (value != null) {
biConsumer.accept(i, value);
}
}
}
@Override
public void clear() {
try {
@@ -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<V extends IPacket> implements LpMap<V> {
return heapMap.getIncrementIndex();
}
@Override
public void forEach(BiConsumer<Long, V> biConsumer) {
heapMap.forEach(biConsumer);
}
@Override
public void clear() {
heapMap.clear();