From 6aad6a161863a8ee2bddaf27b04fb30e7583fed5 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 11 Mar 2022 10:59:07 +0000 Subject: [PATCH] fileListCache is now an immutable Map, which is thread safe. Thanks to catbref for this idea. --- .../qortal/repository/BlockArchiveReader.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/qortal/repository/BlockArchiveReader.java b/src/main/java/org/qortal/repository/BlockArchiveReader.java index b6d7cdd6..47e4e7fc 100644 --- a/src/main/java/org/qortal/repository/BlockArchiveReader.java +++ b/src/main/java/org/qortal/repository/BlockArchiveReader.java @@ -63,7 +63,7 @@ public class BlockArchiveReader { map.put(filename, new Triple(startHeight, endHeight, range)); } } - this.fileListCache = map; + this.fileListCache = Map.copyOf(map); } public Triple, List> fetchBlockAtHeight(int height) { @@ -145,22 +145,20 @@ public class BlockArchiveReader { } private String getFilenameForHeight(int height) { - synchronized (this.fileListCache) { - Iterator it = this.fileListCache.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - if (pair == null && pair.getKey() == null && pair.getValue() == null) { - continue; - } - Triple heightInfo = (Triple) pair.getValue(); - Integer startHeight = heightInfo.getA(); - Integer endHeight = heightInfo.getB(); + Iterator it = this.fileListCache.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = (Map.Entry) it.next(); + if (pair == null && pair.getKey() == null && pair.getValue() == null) { + continue; + } + Triple heightInfo = (Triple) pair.getValue(); + Integer startHeight = heightInfo.getA(); + Integer endHeight = heightInfo.getB(); - if (height >= startHeight && height <= endHeight) { - // Found the correct file - String filename = (String) pair.getKey(); - return filename; - } + if (height >= startHeight && height <= endHeight) { + // Found the correct file + String filename = (String) pair.getKey(); + return filename; } }