Пример #1
0
        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);
            }
        }
Пример #2
0
        private void EnsureIndexVersionMatches(FSDirectory directory)
        {
            if (directory.FileExists(indexVersionFilename) == false)
            {
                throw new InvalidOperationException("Could not find " + indexVersionFilename + " for '" + this.name + "', resetting index");
            }

            using (var indexInput = directory.OpenInput(indexVersionFilename))
            {
                var versionToCheck  = IndexVersion;
                var versionFromDisk = indexInput.ReadString();
                if (versionFromDisk != versionToCheck)
                {
                    throw new InvalidOperationException("Index for " + this.name + " is of version " + versionFromDisk +
                                                        " which is not compatible with " + versionToCheck + ", resetting index");
                }
            }
        }
Пример #3
0
 /// <summary>Returns true if a file with the given name exists. </summary>
 public override bool FileExists(string name)
 {
     //always from the real dir
     return(_realDirectory.FileExists(name));
 }