public void TestReadWrite() { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); BinaryReader reader = new BinaryReader(stream); PostingListEncoder encoder = new PostingListEncoder(); List<Posting> postings = new List<Posting>(); postings.Add(new Posting(DocA, 23)); postings.Add(new Posting(DocB, 23)); encoder.write(writer, postings); stream.Seek(0, SeekOrigin.Begin); IList<Posting> readPostings = encoder.read(reader); Assert.AreEqual(postings.Count, readPostings.Count); for (int postingIndex = 0; postingIndex < postings.Count; postingIndex++) { Assert.AreEqual(postings[postingIndex].Frequency, readPostings[postingIndex].Frequency); Assert.AreEqual(postings[postingIndex].DocumentId, readPostings[postingIndex].DocumentId); } }
public string FlushToFile() { string filename = Path.GetTempFileName(); SortedList<string, SortedList<long, Posting>> orderedEntries = index.Entries; using (FileStream fs = File.Open(filename, FileMode.Append)) { PostingListEncoder encoder = new PostingListEncoder(); BinaryWriter writer = new BinaryWriter(fs); writer.Write((Int32)orderedEntries.Count); foreach (KeyValuePair<string, SortedList<long, Posting>> termPostingsPair in orderedEntries) { writer.Write((string)termPostingsPair.Key); encoder.write(writer, termPostingsPair.Value.Values); } } this.index = new MemoryIndex(); return filename; }