add index for tags-to-documents

Now we can find writer much faster, because we don't have to execute
a query for documents that match the tags. We can just look up the 
documents in the map.
Speedup: 2-4ms -> 0.002-0.01ms
This commit is contained in:
ahr
2018-01-14 09:51:37 +01:00
parent 64613ce43c
commit d98c45e8bd
5 changed files with 60 additions and 33 deletions

View File

@@ -39,4 +39,8 @@ public class PdbDB {
return proposer.propose(query, caretIndex);
}
public List<Doc> getByTags(Tags tags) {
return dataStore.getByTags(tags);
}
}

View File

@@ -48,8 +48,10 @@ public class DataStore {
// to be guarded by itself
private final List<Doc> docIdToDoc = new ArrayList<>();
private final ConcurrentHashMap<Tags, List<Doc>> tagsToDocs = new ConcurrentHashMap<>();
private final Map<String, Map<String, IntList>> keyToValueToDocId = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Map<String, IntList>> keyToValueToDocId = new ConcurrentHashMap<>();
private final StringCompressor stringCompressor;
private final FolderStorage folderStorage;
@@ -81,11 +83,18 @@ public class DataStore {
private void cacheTagToFileMapping(final Tags tags, final Path path) {
final int docId;
final Doc newDoc = new Doc(tags, path);
synchronized (docIdToDoc) {
docId = docIdToDoc.size();
docIdToDoc.add(new Doc(tags, path));
docIdToDoc.add(newDoc);
}
tagsToDocs.compute(tags, (t, listOfDocs) -> {
final List<Doc> result = listOfDocs != null ? listOfDocs : new ArrayList<>(2);
result.add(newDoc);
return result;
});
for (final String key : tags.getKeys()) {
final Map<String, IntList> valueToDocIds = keyToValueToDocId.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
@@ -280,4 +289,9 @@ public class DataStore {
}
return result;
}
public List<Doc> getByTags(Tags tags) {
final List<Doc> result = tagsToDocs.getOrDefault(tags, new ArrayList<>(0));
return result;
}
}