fileListCache is now an immutable Map, which is thread safe. Thanks to catbref for this idea.

This commit is contained in:
CalDescent 2022-03-11 10:59:07 +00:00
parent b764172500
commit 6aad6a1618

View File

@ -63,7 +63,7 @@ public class BlockArchiveReader {
map.put(filename, new Triple(startHeight, endHeight, range)); map.put(filename, new Triple(startHeight, endHeight, range));
} }
} }
this.fileListCache = map; this.fileListCache = Map.copyOf(map);
} }
public Triple<BlockData, List<TransactionData>, List<ATStateData>> fetchBlockAtHeight(int height) { public Triple<BlockData, List<TransactionData>, List<ATStateData>> fetchBlockAtHeight(int height) {
@ -145,22 +145,20 @@ public class BlockArchiveReader {
} }
private String getFilenameForHeight(int height) { private String getFilenameForHeight(int height) {
synchronized (this.fileListCache) { Iterator it = this.fileListCache.entrySet().iterator();
Iterator it = this.fileListCache.entrySet().iterator(); while (it.hasNext()) {
while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next();
Map.Entry pair = (Map.Entry) it.next(); if (pair == null && pair.getKey() == null && pair.getValue() == null) {
if (pair == null && pair.getKey() == null && pair.getValue() == null) { continue;
continue; }
} Triple<Integer, Integer, Integer> heightInfo = (Triple<Integer, Integer, Integer>) pair.getValue();
Triple<Integer, Integer, Integer> heightInfo = (Triple<Integer, Integer, Integer>) pair.getValue(); Integer startHeight = heightInfo.getA();
Integer startHeight = heightInfo.getA(); Integer endHeight = heightInfo.getB();
Integer endHeight = heightInfo.getB();
if (height >= startHeight && height <= endHeight) { if (height >= startHeight && height <= endHeight) {
// Found the correct file // Found the correct file
String filename = (String) pair.getKey(); String filename = (String) pair.getKey();
return filename; return filename;
}
} }
} }