From cc115bad2333a528f13fe1a987c21f1f3a0230e8 Mon Sep 17 00:00:00 2001 From: Jarno Elovirta Date: Thu, 3 Sep 2020 21:23:30 +0300 Subject: [PATCH] Use concurrent sets or lists Signed-off-by: Jarno Elovirta --- .../dost/module/GenMapAndTopicListModule.java | 23 ++++---- .../module/reader/AbstractReaderModule.java | 57 ++++++++++--------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/dita/dost/module/GenMapAndTopicListModule.java b/src/main/java/org/dita/dost/module/GenMapAndTopicListModule.java index d339222274..de4d306024 100644 --- a/src/main/java/org/dita/dost/module/GenMapAndTopicListModule.java +++ b/src/main/java/org/dita/dost/module/GenMapAndTopicListModule.java @@ -33,6 +33,9 @@ import java.io.*; import java.net.URI; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -99,11 +102,11 @@ public final class GenMapAndTopicListModule extends SourceReaderModule { private final Set relFlagImagesSet; /** List of files waiting for parsing. Values are absolute URI references. */ - private final Queue waitList; + private final NavigableMap waitList; /** List of parsed files */ - private final List doneList; - private final List failureList; + private final Set doneList; + private final Set failureList; /** Set of outer dita files */ private final Set outDitaFilesSet; @@ -168,9 +171,9 @@ public GenMapAndTopicListModule() { htmlSet = SetMultimapBuilder.hashKeys().hashSetValues().build(); hrefTargetSet = new HashSet<>(128); coderefTargetSet = new HashSet<>(16); - waitList = new LinkedList<>(); - doneList = new LinkedList<>(); - failureList = new LinkedList<>(); + waitList = new ConcurrentSkipListMap<>(); + doneList = ConcurrentHashMap.newKeySet(); + failureList = ConcurrentHashMap.newKeySet(); conrefTargetSet = new HashSet<>(128); nonConrefCopytoTargetSet = new HashSet<>(128); outDitaFilesSet = new HashSet<>(128); @@ -344,8 +347,8 @@ private void parseInputParameters(final AbstractPipelineInput input) { } private void processWaitList() throws DITAOTException { - while (!waitList.isEmpty()) { - processFile(waitList.remove()); + for (Map.Entry entry = waitList.pollFirstEntry(); entry != null; entry = waitList.pollFirstEntry()) { + processFile(entry.getValue()); } } @@ -659,11 +662,11 @@ private void updateUplevels(final URI file) { private void addToWaitList(final Reference ref) { final URI file = ref.filename; assert file.isAbsolute() && file.getFragment() == null; - if (doneList.contains(file) || waitList.contains(ref) || file.equals(currentFile)) { + if (doneList.contains(file) || waitList.containsKey(ref.filename) || file.equals(currentFile)) { return; } - waitList.add(ref); + waitList.put(ref.filename, ref); } /** diff --git a/src/main/java/org/dita/dost/module/reader/AbstractReaderModule.java b/src/main/java/org/dita/dost/module/reader/AbstractReaderModule.java index 55c9ed1370..820549c332 100644 --- a/src/main/java/org/dita/dost/module/reader/AbstractReaderModule.java +++ b/src/main/java/org/dita/dost/module/reader/AbstractReaderModule.java @@ -30,6 +30,9 @@ import java.io.*; import java.net.URI; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -52,47 +55,47 @@ public abstract class AbstractReaderModule extends AbstractPipelineModuleImpl { Predicate formatFilter; /** FileInfos keyed by src. */ - private final Map> fileinfos = new HashMap<>(); + private final Map> fileinfos = new ConcurrentHashMap<>(); /** Set of all topic files */ - final Set fullTopicSet = new HashSet<>(128); + final Set fullTopicSet = ConcurrentHashMap.newKeySet(); /** Set of all map files */ - final Set fullMapSet = new HashSet<>(128); + final Set fullMapSet = ConcurrentHashMap.newKeySet(); /** Set of topic files containing href */ - private final Set hrefTopicSet = new HashSet<>(128); + private final Set hrefTopicSet = ConcurrentHashMap.newKeySet(); /** Set of dita files containing conref */ - final Set conrefSet = new HashSet<>(128); + final Set conrefSet = ConcurrentHashMap.newKeySet(); /** Set of topic files containing coderef */ - private final Set coderefSet = new HashSet<>(128); + private final Set coderefSet = ConcurrentHashMap.newKeySet(); /** Set of all images */ - final Set formatSet = new HashSet<>(); + final Set formatSet = ConcurrentHashMap.newKeySet(); /** Set of all images used for flagging */ - private final Set flagImageSet = new LinkedHashSet<>(128); + private final Set flagImageSet = ConcurrentHashMap.newKeySet(); /** Set of all HTML and other non-DITA or non-image files */ final SetMultimap htmlSet = SetMultimapBuilder.hashKeys().hashSetValues().build(); /** Set of all the href targets */ - final Set hrefTargetSet = new HashSet<>(128); + final Set hrefTargetSet = ConcurrentHashMap.newKeySet(); /** Set of all the conref targets */ - Set conrefTargetSet = new HashSet<>(128); + Set conrefTargetSet = ConcurrentHashMap.newKeySet(); /** Set of all targets except conref and copy-to */ - final Set nonConrefCopytoTargetSet = new HashSet<>(128); + final Set nonConrefCopytoTargetSet = ConcurrentHashMap.newKeySet(); /** Set of subsidiary files */ - private final Set coderefTargetSet = new HashSet<>(16); + private final Set coderefTargetSet = ConcurrentHashMap.newKeySet(); /** Set of absolute flag image files */ - private final Set relFlagImagesSet = new LinkedHashSet<>(128); + private final Set relFlagImagesSet = ConcurrentHashMap.newKeySet(); /** List of files waiting for parsing. Values are absolute URI references. */ @VisibleForTesting - final Queue waitList = new LinkedList<>(); + final NavigableMap waitList = new ConcurrentSkipListMap<>(); /** List of parsed files */ - final List doneList = new LinkedList<>(); - final List failureList = new LinkedList<>(); + final Set doneList = ConcurrentHashMap.newKeySet(); + final Set failureList = ConcurrentHashMap.newKeySet(); /** Set of outer dita files */ - final Set outDitaFilesSet = new HashSet<>(128); + final Set outDitaFilesSet = ConcurrentHashMap.newKeySet(); /** Set of sources of conacion */ - final Set conrefpushSet = new HashSet<>(128); + final Set conrefpushSet = ConcurrentHashMap.newKeySet(); /** Set of files containing keyref */ - final Set keyrefSet = new HashSet<>(128); + final Set keyrefSet = ConcurrentHashMap.newKeySet(); /** Set of files with "@processing-role=resource-only" */ - final Set resourceOnlySet = new HashSet<>(128); + final Set resourceOnlySet = ConcurrentHashMap.newKeySet(); /** Absolute basedir for processing */ private URI baseInputDir; GenListModuleReader listFilter; @@ -105,10 +108,10 @@ public abstract class AbstractReaderModule extends AbstractPipelineModuleImpl { URI rootFile; List resources; /** Subject scheme absolute file paths. */ - private final Set schemeSet = new HashSet<>(128); + private final Set schemeSet = ConcurrentHashMap.newKeySet(); /** Subject scheme usage. Key is absolute file path, value is set of applicable subject schemes. */ private final Map> schemeDictionary = new HashMap<>(); - private final Map copyTo = new HashMap<>(); + private final Map copyTo = new ConcurrentHashMap<>(); Mode processingMode; /** Generate {@code xtrf} and {@code xtrc} attributes */ boolean genDebugInfo; @@ -130,7 +133,7 @@ public abstract class AbstractReaderModule extends AbstractPipelineModuleImpl { DitaWriterFilter ditaWriterFilter; TopicFragmentFilter topicFragmentFilter; /** Files found during additional resource crawl. **/ - final Set additionalResourcesSet = new HashSet<>(); + final Set additionalResourcesSet = ConcurrentHashMap.newKeySet(); public abstract void readStartFile() throws DITAOTException; @@ -306,8 +309,8 @@ void parseInputParameters(final AbstractPipelineInput input) { } void processWaitList() throws DITAOTException { - while (!waitList.isEmpty()) { - readFile(waitList.remove(), null); + for (Map.Entry entry = waitList.pollFirstEntry(); entry != null; entry = waitList.pollFirstEntry()) { + readFile(entry.getValue(), null); } } @@ -565,11 +568,11 @@ void categorizeCurrentFile(final Reference ref) { void addToWaitList(final Reference ref) { final URI file = ref.filename; assert file.isAbsolute() && file.getFragment() == null; - if (doneList.contains(file) || waitList.contains(ref) || file.equals(currentFile)) { + if (doneList.contains(file) || waitList.containsKey(ref.filename) || file.equals(currentFile)) { return; } - waitList.add(ref); + waitList.put(ref.filename, ref); } /**