Skip to content

Commit c7d14bd

Browse files
committed
Refactor synchronisation to InvertedIndex to make it threadsafe
1 parent 7614d19 commit c7d14bd

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

src/main/java/in/ntsh/FileSearchEngine/DirectoryIndexer.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
public class DirectoryIndexer {
1515

1616
private final Path path;
17-
private InvertedIndex index = new InvertedIndex();
18-
private final Object lock = new Object();
17+
private InvertedIndex index;
1918

2019
/**
2120
* @param directoryPath : path of the directory to be indexed
@@ -45,15 +44,9 @@ private void indexFile(final Path file) {
4544
try (Stream<String> lines = Files.lines(file)) { // Get all lines
4645
lines.flatMap(Pattern.compile("\\W+")::splitAsStream) // Get all words
4746
.map(word -> word.toLowerCase())
48-
.forEach(word -> this.indexWordForFile(word, file.toString())); // index word
47+
.forEach(word -> this.index.indexWordInFile(word, file.toString())); // index word
4948
} catch (final Exception e) {
5049
return; // Ignore files which can't be read as text
5150
}
5251
}
53-
54-
private void indexWordForFile(final String word, final String file) {
55-
synchronized (this.lock) {
56-
this.index.indexWordInFile(word, file);
57-
}
58-
}
5952
}

src/main/java/in/ntsh/FileSearchEngine/InvertedIndex.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class InvertedIndex {
99

1010
private final Map<String, Map<String, Integer>> index;
11+
private final Object lock = new Object();
1112

1213
public InvertedIndex() {
1314
this.index = new HashMap<String, Map<String, Integer>>();
@@ -20,35 +21,39 @@ public InvertedIndex() {
2021
* @param file
2122
*/
2223
public void indexWordInFile(final String word, final String file) {
23-
// Get Existing index for the word or create a new list
24-
Map<String, Integer> fileMap = this.index.get(word);
25-
if (fileMap == null) {
26-
fileMap = new HashMap<String, Integer>();
27-
fileMap.put(file, 1);
28-
} else {
29-
// Add file and count to index of the word
30-
Integer countOfWords = fileMap.get(file);
31-
if (countOfWords == null) {
32-
countOfWords = 0;
24+
synchronized(lock) {
25+
// Get Existing index for the word or create a new list
26+
Map<String, Integer> fileMap = this.index.get(word);
27+
if (fileMap == null) {
28+
fileMap = new HashMap<String, Integer>();
29+
fileMap.put(file, 1);
30+
} else {
31+
// Add file and count to index of the word
32+
Integer countOfWords = fileMap.get(file);
33+
if (countOfWords == null) {
34+
countOfWords = 0;
35+
}
36+
fileMap.put(file, countOfWords + 1);
3337
}
34-
fileMap.put(file, countOfWords + 1);
38+
// Put back word's index to Directory's index
39+
this.index.put(word, fileMap);
3540
}
36-
// Put back word's index to Directory's index
37-
this.index.put(word, fileMap);
3841
}
3942

4043
/**
4144
* Returns the List of postings for a given word from the index
4245
* @param word
4346
*/
4447
public List<Posting> getPostingsForWord(final String word) {
45-
final Map<String, Integer> fileMap = this.index.get(word);
48+
synchronized(lock) {
49+
final Map<String, Integer> fileMap = this.index.get(word);
4650

47-
final List<Posting> postings = new ArrayList<Posting>();
48-
if(fileMap != null) {
49-
fileMap.forEach((file, frequency) -> postings.add(new Posting(file, frequency)));
50-
}
51+
final List<Posting> postings = new ArrayList<Posting>();
52+
if(fileMap != null) {
53+
fileMap.forEach((file, frequency) -> postings.add(new Posting(file, frequency)));
54+
}
5155

52-
return postings;
56+
return postings;
57+
}
5358
}
5459
}

0 commit comments

Comments
 (0)