Add initial inverted index implementation. It's untested though.

This commit is contained in:
Starbeamrainbowlabs 2018-09-02 18:15:24 +01:00
parent c46a789c28
commit 8cd7156023
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 25 additions and 2 deletions

View file

@ -31,6 +31,7 @@ namespace SearchBox
} }
} }
public Index(string inSource, IndexOptions options) public Index(string inSource, IndexOptions options)
: this(inSource, EmbeddedFiles.EnumerateLines("SearchBox.EmbeddedFiles.Stopwords.txt"), options) : this(inSource, EmbeddedFiles.EnumerateLines("SearchBox.EmbeddedFiles.Stopwords.txt"), options)
{ {
@ -55,6 +56,10 @@ namespace SearchBox
public IEnumerable<string> Tokens()
{
return index.Keys;
}
public IEnumerable<KeyValuePair<string, List<int>>> IterateItems() public IEnumerable<KeyValuePair<string, List<int>>> IterateItems()
{ {
foreach(KeyValuePair<string, List<int>> item in index) foreach(KeyValuePair<string, List<int>> item in index)

View file

@ -1,23 +1,41 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
namespace SearchBox namespace SearchBox
{ {
public class InvertedIndex public class InvertedIndex
{ {
private Dictionary<string, Dictionary<int, List<int>>> invertedIndex = new Dictionary<string, Dictionary<int, List<int>>>(); private ConcurrentDictionary<string, ConcurrentDictionary<int, List<int>>> invertedIndex = new Dictionary<string, Dictionary<int, List<int>>>();
public InvertedIndex() public InvertedIndex()
{ {
} }
public bool AddIndex(Index newIndex) public bool AddIndex(int pageId, Index newIndex)
{ {
foreach (KeyValuePair<string, List<int>> token in newIndex) foreach (KeyValuePair<string, List<int>> token in newIndex)
{ {
if (!invertedIndex.ContainsKey(token.Key) &&
!invertedIndex.TryAdd(token.Key, new ConcurrentDictionary<int, List<int>>()))
return false;
if (!invertedIndex[token.Key].TryAdd(pageId, token.Value))
return false;
} }
return true; return true;
} }
public bool RemoveIndex(int pageId, Index newIndex)
{
foreach (string token in newIndex.Tokens())
{
if (!invertedIndex.ContainsKey(token) || !invertedIndex[token].ContainsKey(pageId)) continue;
if (!invertedIndex[token].TryRemove(pageId, out List<int> noop))
return false;
}
return false;
}
} }
} }