private void Flush() { if (Count == 0) { return; } string filePath = Path.Combine(WorkingPath, $"{BlockCount}.idx"); using (SearchIndexSliceWriter writer = new SearchIndexSliceWriter(new BufferedWriter(File.Create(filePath), WriterBuffer), WordCount)) { for (int wordIndex = 0; wordIndex < WordCount; ++wordIndex) { int matchIndex = FirstWordMatch[wordIndex]; while (matchIndex != -1) { writer.WritePosition(MatchPositions[matchIndex]); matchIndex = NextMatchIndex[matchIndex]; } writer.NextWord(); } } BlockCount++; Reset(); }
private void Merge() { if (BlockCount == 1) { if (File.Exists(OutputPath)) { File.Delete(OutputPath); } File.Move(Path.Combine(WorkingPath, "0.idx"), OutputPath); Directory.Delete(WorkingPath, true); return; } SearchIndexSliceWriter writer = new SearchIndexSliceWriter(new BufferedWriter(File.Create(OutputPath), WriterBuffer), WordCount); SearchIndexReader[] readers = new SearchIndexReader[BlockCount]; try { for (int i = 0; i < readers.Length; ++i) { readers[i] = new SearchIndexReader(Path.Combine(WorkingPath, $"{i}.idx")); } long[] positions = new long[256]; for (int wordIndex = 0; wordIndex < WordCount; ++wordIndex) { for (int readerIndex = 0; readerIndex < readers.Length; ++readerIndex) { SearchResult result = readers[readerIndex].Find(wordIndex); while (!result.Done) { int count = result.Page(ref positions); for (int i = 0; i < count; ++i) { writer.WritePosition(positions[i]); } } } writer.NextWord(); } } finally { for (int i = 0; i < readers.Length; ++i) { if (readers[i] != null) { readers[i].Dispose(); readers[i] = null; } } if (writer != null) { writer.Dispose(); writer = null; } Directory.Delete(WorkingPath, true); } }