From 294a9e3334ca85f080968aabd2326db32c564b9c Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sat, 28 Aug 2021 12:05:33 +0800 Subject: [PATCH] =?UTF-8?q?perf[orm]:=20FileChannelMap=E4=BD=BF=E7=94=A8lo?= =?UTF-8?q?ng=E8=A1=A8=E7=A4=BA=E6=9C=80=E5=A4=A7=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E9=AB=98=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zfoo/orm/lpmap/FileChannelMap.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) 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 9ccd7773..4cddba71 100644 --- a/orm/src/main/java/com/zfoo/orm/lpmap/FileChannelMap.java +++ b/orm/src/main/java/com/zfoo/orm/lpmap/FileChannelMap.java @@ -44,6 +44,7 @@ public class FileChannelMap implements LpMap, Closeable { protected RandomAccessFile indexFileRandomAccess; protected FileChannel indexFileChannel; + protected long maxIndex; protected IProtocolRegistration protocolRegistration; @@ -65,6 +66,8 @@ public class FileChannelMap implements LpMap, Closeable { indexFileRandomAccess.writeLong(0L); } + maxIndex = readMaxIndex(); + var protocolId = ProtocolAnalysis.getProtocolIdByClass(clazz); protocolRegistration = ProtocolManager.getProtocol(protocolId); @@ -80,7 +83,6 @@ public class FileChannelMap implements LpMap, Closeable { public V put(long key, V packet) { checkKey(key); - var maxIndex = getMaxIndex(); V previousValue = null; if (key <= maxIndex) { previousValue = get(key); @@ -99,7 +101,6 @@ public class FileChannelMap implements LpMap, Closeable { @Override public V delete(long key) { checkKey(key); - var maxIndex = getMaxIndex(); if (key <= maxIndex) { var previousValue = get(key); @@ -114,7 +115,6 @@ public class FileChannelMap implements LpMap, Closeable { public V get(long key) { checkKey(key); - var maxIndex = getMaxIndex(); if (key > maxIndex) { return null; } @@ -137,20 +137,12 @@ public class FileChannelMap implements LpMap, Closeable { @Override public long getMaxIndex() { - try { - clearByteBuf(); - indexBuffer.writeBytes(indexFileChannel, 0, 8); - return indexBuffer.readLong(); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - clearByteBuf(); - } + return maxIndex; } @Override public long getIncrementIndex() { - var maxIndex = getMaxIndex() + 1; + maxIndex++; // index索引文件的头16个字节是当前index的大小 setMaxIndex(maxIndex); @@ -178,6 +170,18 @@ public class FileChannelMap implements LpMap, Closeable { } + private long readMaxIndex() { + try { + clearByteBuf(); + indexBuffer.writeBytes(indexFileChannel, 0, 8); + return indexBuffer.readLong(); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + clearByteBuf(); + } + } + protected void setKeyValue(long key, V value) { try { clearByteBuf();