synchronize docIdToDoc list
When we parallelized the initialization we forgot to synchronize the docIdToDoc list. Luckily there is a high probability, that queries return results, that are obviously wrong.
This commit is contained in:
@@ -45,6 +45,7 @@ public class DataStore {
|
|||||||
|
|
||||||
private static final Pattern EXTRACT_TAGS_PATTERN = Pattern.compile(REGEX_STORAGE_FILE);
|
private static final Pattern EXTRACT_TAGS_PATTERN = Pattern.compile(REGEX_STORAGE_FILE);
|
||||||
|
|
||||||
|
// to be guarded by itself
|
||||||
private final List<Doc> docIdToDoc = new ArrayList<>();
|
private final List<Doc> docIdToDoc = new ArrayList<>();
|
||||||
|
|
||||||
private final Map<String, Map<String, IntList>> keyToValueToDocId = new ConcurrentHashMap<>();
|
private final Map<String, Map<String, IntList>> keyToValueToDocId = new ConcurrentHashMap<>();
|
||||||
@@ -70,13 +71,18 @@ public class DataStore {
|
|||||||
|
|
||||||
});
|
});
|
||||||
trimIntLists();
|
trimIntLists();
|
||||||
((ArrayList<Doc>)docIdToDoc).trimToSize();
|
synchronized (docIdToDoc) {
|
||||||
|
((ArrayList<Doc>)docIdToDoc).trimToSize();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cacheTagToFileMapping(final Tags tags, final Path path) {
|
private void cacheTagToFileMapping(final Tags tags, final Path path) {
|
||||||
|
|
||||||
final int docId = docIdToDoc.size();
|
final int docId;
|
||||||
docIdToDoc.add(new Doc(tags, path));
|
synchronized (docIdToDoc) {
|
||||||
|
docId = docIdToDoc.size();
|
||||||
|
docIdToDoc.add(new Doc(tags, path));
|
||||||
|
}
|
||||||
|
|
||||||
for (final String key : tags.getKeys()) {
|
for (final String key : tags.getKeys()) {
|
||||||
final Map<String, IntList> valueToDocIds = keyToValueToDocId.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
|
final Map<String, IntList> valueToDocIds = keyToValueToDocId.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
|
||||||
@@ -231,23 +237,35 @@ public class DataStore {
|
|||||||
|
|
||||||
private IntList executeQuery(final String query) {
|
private IntList executeQuery(final String query) {
|
||||||
final long start = System.nanoTime();
|
final long start = System.nanoTime();
|
||||||
final Expression expression = QueryLanguageParser.parse(query);
|
synchronized (docIdToDoc) {
|
||||||
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(keyToValueToDocId,
|
final Expression expression = QueryLanguageParser.parse(query);
|
||||||
new AllDocIds(docIdToDoc));
|
final ExpressionToDocIdVisitor visitor = new ExpressionToDocIdVisitor(
|
||||||
final IntList docIdsList = expression.visit(visitor);
|
keyToValueToDocId, new AllDocIds(docIdToDoc));
|
||||||
EXECUTE_QUERY_LOGGER.debug("executeQuery({}) took {}ms returned {} results " , query, (System.nanoTime() - start) / 1_000_000.0, docIdsList.size());
|
final IntList docIdsList = expression.visit(visitor);
|
||||||
return docIdsList;
|
EXECUTE_QUERY_LOGGER.debug(
|
||||||
|
"executeQuery({}) took {}ms returned {} results ", query,
|
||||||
|
(System.nanoTime() - start) / 1_000_000.0,
|
||||||
|
docIdsList.size());
|
||||||
|
return docIdsList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Doc> mapDocIdsToDocs(final IntList docIdsList) {
|
private List<Doc> mapDocIdsToDocs(final IntList docIdsList) {
|
||||||
final List<Doc> result = new ArrayList<>(docIdsList.size());
|
final List<Doc> result = new ArrayList<>(docIdsList.size());
|
||||||
|
|
||||||
|
synchronized (docIdToDoc) {
|
||||||
final int[] intDocIds = docIdsList.toArray();
|
final int[] intDocIds = docIdsList.toArray();
|
||||||
for (int i = 0; i < intDocIds.length; i++) {
|
for (int i = 0; i < intDocIds.length; i++) {
|
||||||
final int docId = intDocIds[i];
|
final int docId = intDocIds[i];
|
||||||
|
|
||||||
final Doc doc = docIdToDoc.get(docId);
|
final Doc doc = docIdToDoc.get(docId);
|
||||||
result.add(doc);
|
|
||||||
|
if (!doc.getTags().getValue("pod").equals("vadtrans01")){
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
result.add(doc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user