private bool TryReusePreviousCommitPointsToRecoverIndex(Lucene.Net.Store.Directory directory, IndexDefinition indexDefinition, string indexStoragePath, out IndexCommitPoint indexCommit, out string[] keysToDelete) { indexCommit = null; keysToDelete = null; if (indexDefinition.IsMapReduce) return false; var indexFullPath = Path.Combine(indexStoragePath, indexDefinition.IndexId.ToString()); var allCommitPointsFullPath = IndexCommitPointDirectory.GetAllCommitPointsFullPath(indexFullPath); if (Directory.Exists(allCommitPointsFullPath) == false) return false; var filesInIndexDirectory = Directory.GetFiles(indexFullPath).Select(Path.GetFileName); var existingCommitPoints = IndexCommitPointDirectory.ScanAllCommitPointsDirectory(indexFullPath); Array.Reverse(existingCommitPoints); // start from the highest generation foreach (var commitPointDirectoryName in existingCommitPoints) { try { var commitPointDirectory = new IndexCommitPointDirectory(indexStoragePath, indexDefinition.IndexId.ToString(), commitPointDirectoryName); if (TryGetCommitPoint(commitPointDirectory, out indexCommit) == false) { IOExtensions.DeleteDirectory(commitPointDirectory.FullPath); continue; // checksum is invalid, try another commit point } var missingFile = indexCommit.SegmentsInfo.ReferencedFiles.Any( referencedFile => filesInIndexDirectory.Contains(referencedFile) == false); if (missingFile) { IOExtensions.DeleteDirectory(commitPointDirectory.FullPath); continue; // there are some missing files, try another commit point } var storedSegmentsFile = indexCommit.SegmentsInfo.SegmentsFileName; // here there should be only one segments_N file, however remove all if there is more foreach (var currentSegmentsFile in Directory.GetFiles(commitPointDirectory.IndexFullPath, "segments_*")) { File.Delete(currentSegmentsFile); } // copy old segments_N file File.Copy(Path.Combine(commitPointDirectory.FullPath, storedSegmentsFile), Path.Combine(commitPointDirectory.IndexFullPath, storedSegmentsFile), true); try { // update segments.gen file using (var genOutput = directory.CreateOutput(IndexFileNames.SEGMENTS_GEN)) { genOutput.WriteInt(SegmentInfos.FORMAT_LOCKLESS); genOutput.WriteLong(indexCommit.SegmentsInfo.Generation); genOutput.WriteLong(indexCommit.SegmentsInfo.Generation); } } catch (Exception) { // here we can ignore, segments.gen is used only as fallback } if (File.Exists(commitPointDirectory.DeletedKeysFile)) keysToDelete = File.ReadLines(commitPointDirectory.DeletedKeysFile).ToArray(); return true; } catch (Exception ex) { startupLog.WarnException("Could not recover an index named '" + indexDefinition.IndexId + "'from segments of the following generation " + commitPointDirectoryName, ex); } } return false; }
private static void WriteIndexVersion(Lucene.Net.Store.Directory directory) { using(var indexOutput = directory.CreateOutput("index.version")) { indexOutput.WriteString(IndexVersion); indexOutput.Flush(); } }
public static void WriteIndexVersion(Lucene.Net.Store.Directory directory, IndexDefinition indexDefinition) { var version = IndexVersion; if (indexDefinition.IsMapReduce) { version = MapReduceIndexVersion; } using (var indexOutput = directory.CreateOutput(IndexVersionFileName(indexDefinition))) { indexOutput.WriteString(version); indexOutput.Flush(); } }
private static void WriteIndexVersion(Lucene.Net.Store.Directory directory) { var indexOutput = directory.CreateOutput("index.version"); try { indexOutput.WriteString(IndexVersion); indexOutput.Flush(); } finally { indexOutput.Close(); } }