public bool TryLoad(string path, IndexLoadOptions options) { try { _directory = FSDirectory.Open(path); _reader = IndexReader.Open(_directory, options.ReadOnly); if (options.ForceUnlock && _directory.FileExists(IndexWriter.WRITE_LOCK_NAME)) { _directory.ClearLock(IndexWriter.WRITE_LOCK_NAME); } var fields = _reader.GetFieldNames(IndexReader.FieldOption.ALL); _numFields = fields.Count; CountTerms(); foreach (var file in _directory.ListAll()) { try { string fpath = Path.Combine(_directory.Directory.ToString(), file); if (_lastModified == null) { _lastModified = File.GetLastWriteTimeUtc(fpath); } else { var mod = File.GetLastWriteTimeUtc(fpath); if (mod > _lastModified.Value) { _lastModified = mod; } } } catch { // ignore } } _loaded = true; return(true); } catch { _directory = null; _reader = null; _numFields = 0; _numTerms = 0; _loaded = false; _lastModified = null; return(false); } }
public bool TryLoad(string path, IndexLoadOptions options) { try { _directory = FSDirectory.Open(path); _reader = IndexReader.Open(_directory, options.ReadOnly); if (options.ForceUnlock && _directory.FileExists(IndexWriter.WRITE_LOCK_NAME)) { _directory.ClearLock(IndexWriter.WRITE_LOCK_NAME); } var fields = _reader.GetFieldNames(IndexReader.FieldOption.ALL); _numFields = fields.Count; CountTerms(); foreach (var file in _directory.ListAll()) { try { string fpath = Path.Combine(_directory.Directory.ToString(), file); if (_lastModified == null) _lastModified = File.GetLastWriteTimeUtc(fpath); else { var mod = File.GetLastWriteTimeUtc(fpath); if (mod > _lastModified.Value) _lastModified = mod; } } catch { // ignore } } _loaded = true; return true; } catch { _directory = null; _reader = null; _numFields = 0; _numTerms = 0; _loaded = false; _lastModified = null; return false; } }
void LoadIndex(object sender, EventArgs e) { var vm = sender as OpenIndexViewModel; var options = new IndexLoadOptions(); options.ForceUnlock = vm.ForceUnlock; options.ReadOnly = vm.OpenReadOnly; bool success = _indexService.TryLoad(vm.PathToIndex, options); if (!success) { vm.LoadFailed(); return; } IndexPath = vm.PathToIndex; vm.TryClose(); }