Base class for Directory implementations that store index files in the file system. There are currently three core subclasses: SimpleFSDirectory is a straightforward implementation using java.io.RandomAccessFile. However, it has poor concurrent performance (multiple threads will bottleneck) as it synchronizes when multiple threads read from the same file. NIOFSDirectory uses java.nio's FileChannel's positional io when reading to avoid synchronization when reading from the same file. Unfortunately, due to a Windows-only Sun JRE bug this is a poor choice for Windows, but on all other platforms this is the preferred choice. Applications using System.Threading.Thread.Interrupt() or Future#cancel(boolean) (on Java 1.5) should use SimpleFSDirectory instead. See NIOFSDirectory java doc for details. MMapDirectory uses memory-mapped IO when reading. This is a good choice if you have plenty of virtual memory relative to your index size, eg if you are running on a 64 bit JRE, or you are running on a 32 bit JRE but your index sizes are small enough to fit into the virtual memory space. Java has currently the limitation of not being able to unmap files from user code. The files are unmapped, when GC releases the byte buffers. Due to this bug in Sun's JRE, MMapDirectory's IndexInput.Close is unable to close the underlying OS file handle. Only when GC finally collects the underlying objects, which could be quite some time later, will the file handle be closed. This will consume additional transient disk usage: on Windows, attempts to delete or overwrite the files will result in an exception; on other platforms, which typically have a "delete on last close" semantics, while such operations will succeed, the bytes are still consuming space on disk. For many applications this limitation is not a problem (e.g. if you have plenty of disk space, and you don't rely on overwriting files on Windows) but it's still an important limitation to be aware of. This class supplies a (possibly dangerous) workaround mentioned in the bug report, which may fail on non-Sun JVMs. Applications using System.Threading.Thread.Interrupt() or Future#cancel(boolean) (on Java 1.5) should use SimpleFSDirectory instead. See MMapDirectory java doc for details. Unfortunately, because of system peculiarities, there is no single overall best implementation. Therefore, we've added the Open(System.IO.DirectoryInfo) method, to allow Lucene to choose the best FSDirectory implementation given your environment, and the known limitations of each implementation. For users who have no reason to prefer a specific implementation, it's best to simply use FSDirectory.Open(System.IO.DirectoryInfo) . For all others, you should instantiate the desired implementation directly.

The locking implementation is by default NativeFSLockFactory , but can be changed by passing in a custom LockFactory instance.

Inheritance: Directory
示例#1
0
 protected IndexWriter GetIndexWriter(FSDirectory directory)
 {
     return new IndexWriter(
         this.GetDirectory(),
         LuceneEngineBase.GetAnalyzer(),
         IndexWriter.MaxFieldLength.UNLIMITED);
 }
示例#2
0
 public LuceneWriter(string folder)
 {
     _folder = folder;
     _indexDirectory = FSDirectory.Open(new DirectoryInfo(folder));
     _standardAnalyzer = new StandardAnalyzer(Version.LUCENE_30);
     _indexWriter = new IndexWriter(_indexDirectory, _standardAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);
 }
        protected override Directory GetDirectory()
        {
            if(this._directory == null)
                this._directory = FSDirectory.Open(new System.IO.DirectoryInfo(this._path));

            return this._directory;
        }
示例#4
0
        /// <summary>
        ///  Constructor.
        /// </summary>
        /// <param name="index">Index path.</param>
        public LuceneUpdater(FSDirectory index)
        {
            this.index = index;

            // Debug messages
            log.DebugFormat("Testing if '{0}' index exists...",this.index.Directory.Name);

            if (!IndexReader.IndexExists(this.index))
            {
                // Debug messages
                log.Debug("Index doesn't exist!");
                log.Debug("Creating one...");

                try
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();

                    this.CreateIndex();

                    stopwatch.Stop();

                    // Debug messages
                    log.DebugFormat("Index created at {0} in {1:00}:{2:00}:{3:00}!", index, stopwatch.Elapsed.Hours, stopwatch.Elapsed.Minutes,stopwatch.Elapsed.Seconds);
                }
                catch (Exception e)
                {
                    log.Fatal(string.Format("Error creating index '{0}'!",this.index.Directory.Name), e);
                }
            }

            // Debug messages
            log.Debug("Lucene updater initialized!");
        }
        public void Initialize(String directoryProviderName, IDictionary<string, string> properties, ISearchFactoryImplementor searchFactory)
        {
            DirectoryInfo indexDir = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary) properties);
            try
            {
                bool create = !IndexReader.IndexExists(indexDir.FullName);
                indexName = indexDir.FullName;
                directory = FSDirectory.GetDirectory(indexName, create);

                if (create)
                {
                    IndexWriter iw = new IndexWriter(directory,
                                                     new StandardAnalyzer(),
                                                     create,
                                                     new KeepOnlyLastCommitDeletionPolicy(),
                                                     IndexWriter.MaxFieldLength.UNLIMITED);
                    iw.Close();
                }

                //searchFactory.RegisterDirectoryProviderForLocks(this);
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }
        }
        /// <summary>
        /// Performs the explanation.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="searchQuery">The search query.</param>
        /// <param name="resultId">The result identifier.</param>
        /// <returns></returns>
        protected virtual string PerformExplain(Version luceneVersion, FSDirectory fsDirectory, string searchQuery, int resultId)
		{
			/*
			 * The obvious problem here is that we're not using the exact same search as the real one.
			 */

			var explanation = string.Empty;

			using (var indexSearcher = new IndexSearcher(fsDirectory, false))
			{
				var analyzer = new StandardAnalyzer(luceneVersion);
				
				var queryParser = new MultiFieldQueryParser(luceneVersion, new[] { "Id".ToLowerInvariant() }, analyzer)
									{
										DefaultOperator = QueryParser.Operator.AND
									};
				
				var query = this.searchQueryParser.ParseQuery(searchQuery, queryParser);

				explanation = indexSearcher.Explain(query, resultId).ToHtml();

				analyzer.Close();
			}

			return explanation;
		}
示例#7
0
 public MultiIndexLockFactory(FSDirectory master, FSDirectory child)
 {
     if (master == null) throw new ArgumentNullException("master");
     if (child == null) throw new ArgumentNullException("child");
     _master = master;
     _child = child;
 }
示例#8
0
        static void Main(string[] args)
        {
            //Analyzer analyzer = new StandardAnalyzer();
            Analyzer  analyzer  = new StandardAnalyzer(Version.LUCENE_23);
            Directory directory = null;

            //directory.CreateOutput(@"D:\workspace\C#\lucene\lucene\lucene\IndexDirectory");

            while (true)
            {
                Console.Write("id:");
                string id = Console.ReadLine().ToString();
                Console.Write("title:");
                string title = Console.ReadLine().ToString();
                Console.Write("content:");
                string content = Console.ReadLine().ToString();
                Console.WriteLine("=================================");
                string indexPath = @"D:\workspace\C#\lucene\lucene\lucene\IndexDirectory";
                directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath));

                IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
                AddDocument(writer, id, title, content);
                writer.Optimize();
                writer.Dispose();
            }
            //AddDocument(writer,"1","name","ada");
            //AddDocument(writer, "2","SQL Server 2008 的发布ada", "SQL Server 2008 的新特性");
            //AddDocument(writer, "3","ASP.Net MVC框架配置与分析", "而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions");
        }
示例#9
0
 public ContentSearcher(string indexPath)
 {
     this._indexPath = indexPath;
     this._directory = FSDirectory.Open(new DirectoryInfo(indexPath));
     this._indexReader = IndexReader.Open(this._directory, readOnly: true);
     this._analyzer = new StandardAnalyzer(global::Lucene.Net.Util.Version.LUCENE_29);
     this._indexSearcher = new IndexSearcher(this._indexReader);
 }
				public LuceneSearcher(string searchIndex)
				{
						DirectoryInfo indexDirectory = GetIndexPath(searchIndex);
						directory = FSDirectory.Open(indexDirectory);
						reader = IndexReader.Open(directory, true);
						searcher = new IndexSearcher(reader);	
						searchFields = GetSearchFields(reader);
				}
示例#11
0
 public SearchIndexer(FSDirectory directory, Analyzer analyzer) {
     _directory = directory;
     
     EnsureThatIndexIsUnlocked();
     
     _analyzer = analyzer;
     _indexWriter = new IndexWriter(directory, _analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
     _open = true;
 }
        public TwitterFeedSearcher(string directoryPath)
        {
            dir = FSDirectory.GetDirectory(directoryPath);
            searcher = new IndexSearcher(dir);
            var positiveReviews = new Evidence("Positive", "Repository\\Positive.Evidence.csv");
            var negativeReviews = new Evidence("Negative", "Repository\\Negative.Evidence.csv");

            classifier = new Classifier(positiveReviews, negativeReviews);
        }
 protected AbstractSearchDirectory(string dataFolder)
 {
     this.dataFolder = dataFolder;
     var di = new DirectoryInfo(Path.Combine(dataFolder,LuceneIndexFolder));
     if (!di.Exists)
     {
         di.Create();
     }
     luceneDirectory = FSDirectory.Open(di.FullName);
 }
示例#14
0
        public void Dispose()
        {
            Log.App.DebugFormat("Fechando indice");

            writer.Dispose();
            directory.Dispose();

            writer = null;
            directory = null;
        }
        /// <summary>
        /// Optimizes the Lucene index.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="maxFieldLength">Maximum length of the field.</param>
		public virtual void Optimize(Version luceneVersion, FSDirectory fsDirectory, IndexWriter.MaxFieldLength maxFieldLength)
		{
			var analyzer = new StandardAnalyzer(luceneVersion);

			using (var indexWriter = new IndexWriter(fsDirectory, analyzer, maxFieldLength))
			{
				analyzer.Close();

				indexWriter.Optimize();
			}
		}
        public LocalTempStorageDirectory GetDirectory(DirectoryInfo dir, FSDirectory realDir, bool disable = false)
        {
            var resolved = _directories.GetOrAdd(dir.FullName, s => new LocalTempStorageDirectory(dir, realDir));

            if (disable)
            {
                resolved.Enabled = false;
            }

            return resolved;
        }
        /// <summary>
        /// Clears the entire index.
        /// </summary>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="maxFieldLength">Maximum length of the field.</param>
		public virtual void ClearIndex(Version luceneVersion, FSDirectory fsDirectory, IndexWriter.MaxFieldLength maxFieldLength)
		{
			var analyzer = new StandardAnalyzer(luceneVersion);

			using (var indexWriter = new IndexWriter(fsDirectory, analyzer, maxFieldLength))
			{
				indexWriter.DeleteAll();

				analyzer.Close();
			}
		}
示例#18
0
		public void Initialize()
		{
			directory = FSDirectory.Open(new DirectoryInfo(path));
			if (IndexWriter.IsLocked(directory))
				IndexWriter.Unlock(directory);

			analyzer = new LowerCaseKeywordAnalyzer();
			writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
			writer.SetMergeScheduler(new ErrorLoggingConcurrentMergeScheduler());
			currentIndexSearcherHolder.SetIndexSearcher(new IndexSearcher(directory, true));
		}
示例#19
0
        /// <summary>
        /// Before doing anything with the search index, first make sure there's no existing
        /// write lock on the directory. (This can happen if the application crashes and restarts)
        /// </summary>
        static SearchIndex()
        {
            _directory = FSDirectory.Open(new DirectoryInfo(_luceneDir));

            if (IndexWriter.IsLocked(_directory))
                IndexWriter.Unlock(_directory);

            var lockFilePath = Path.Combine(_luceneDir, "write.lock");

            if (File.Exists(lockFilePath))
                File.Delete(lockFilePath);
        }
示例#20
0
        public static long CalcTotalFileSize(FSDirectory dir)
        {
            long totalFileSize = 0;
            foreach (string file in dir.ListAll())
            {
                totalFileSize += dir.FileLength(file);
                //				FileInfo fi = new FileInfo(file);
                //				totalFileSize += fi.Length;
            }

            return totalFileSize;
        }
示例#21
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;
            }
        }
        /// <summary>
        /// Clears an item from the index.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="luceneVersion">The lucene version.</param>
        /// <param name="fsDirectory">The fs directory.</param>
        /// <param name="maxFieldLength">Maximum length of the field.</param>
        public void ClearIndex(string id, Version luceneVersion, FSDirectory fsDirectory, IndexWriter.MaxFieldLength maxFieldLength)
        {
            var analyzer = new StandardAnalyzer(luceneVersion);

            using (var indexWriter = new IndexWriter(fsDirectory, analyzer, maxFieldLength))
            {
                var searchQuery = new TermQuery(new Term("Key", id));

                indexWriter.DeleteDocuments(searchQuery);

                analyzer.Close();
            }
        }
示例#23
0
        public LuceneIndexerSession Begin(string indexDirectory)
        {
            Log.App.DebugFormat("Abrindo indice em {0}", indexDirectory);

            directory = FSDirectory.Open(new DirectoryInfo(indexDirectory));

            writer = new IndexWriter(
                directory,
                LuceneEngineBase.GetAnalyzer(),
                IndexWriter.MaxFieldLength.UNLIMITED);

            return this;
        }
        public LocalTempStorageDirectory(
            DirectoryInfo tempStorageDir,
            FSDirectory realDirectory)
        {
            if (tempStorageDir == null) throw new ArgumentNullException("tempStorageDir");
            if (realDirectory == null) throw new ArgumentNullException("realDirectory");

            _tempStorageDir = new SimpleFSDirectory(tempStorageDir);
            _realDirectory = realDirectory;
            _lockFactory = new MultiIndexLockFactory(_realDirectory, _tempStorageDir);

            Enabled = true;
        }
示例#25
0
 public Searcher(string indexDirectory)
 {
     _indexDirectory = indexDirectory;
     if (!Directory.Exists(indexDirectory)) Directory.CreateDirectory(indexDirectory);
     try
     {
         _directory = FSDirectory.GetDirectory(_indexDirectory, false);
         searcher = new IndexSearcher(_directory);
     }
     catch (Exception)
     {
         IsOperational = false;
     }
 }
        public void Setup()
        {
            var dirProvider = new Mock<IDataDirectoryProvider>();
            dirProvider.Setup(x => x.DataDirectoryPath).Returns(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MyJobLeadsTestIndex");

            _provider = new LuceneSearchProvider(dirProvider.Object);

            // Load the index and delete all documents so it's fresh for the test
            _indexDirectory = Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo(_provider.LuceneIndexBaseDirectory));
            bool createNewIndex = !IndexReader.IndexExists(_indexDirectory);
            var writer = new IndexWriter(_indexDirectory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), createNewIndex, IndexWriter.MaxFieldLength.UNLIMITED);
            writer.DeleteAll();
            writer.Close();
        }
示例#27
0
		public void Initialize()
		{
			if (System.IO.Directory.Exists(path) == false)
				System.IO.Directory.CreateDirectory(path);
			directory = FSDirectory.Open(new DirectoryInfo(path));
			if (IndexWriter.IsLocked(directory))
				IndexWriter.Unlock(directory);

			analyzer = new LowerCaseKeywordAnalyzer();
            snapshotter = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
			writer = new IndexWriter(directory, analyzer, snapshotter, IndexWriter.MaxFieldLength.UNLIMITED);
			writer.SetMergeScheduler(new ErrorLoggingConcurrentMergeScheduler());
			currentIndexSearcherHolder.SetIndexSearcher(new IndexSearcher(directory, true));
		}
示例#28
0
 private LuceneStore()
 {
     string indexPath = Path.Combine(
         Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
         "LuceneIndex");
     this.luceneIndexDirectory = FSDirectory.Open(indexPath);
     this.analyzer = new StandardAnalyzer(Version);
     this.writer = new IndexWriter(
         luceneIndexDirectory,
         this.analyzer,
         !System.IO.Directory.Exists(indexPath),
         IndexWriter.MaxFieldLength.UNLIMITED);
     this.searcher = new IndexSearcher(this.luceneIndexDirectory);
 }
示例#29
0
        public SearchManager(string indexLocation)
        {
            if (string.IsNullOrEmpty(indexLocation))
                throw new FileNotFoundException("The lucene index could not be found.");

            _luceneVersion = Version.LUCENE_30;

            var resolvedServerLocation = HttpContext.Current.Server.MapPath(string.Format("~{0}", indexLocation));
            _directory = FSDirectory.Open(new DirectoryInfo(resolvedServerLocation));

            var createIndex = !IndexReader.IndexExists(_directory);

            _writer = new IndexWriter(_directory, new StandardAnalyzer(_luceneVersion), createIndex, IndexWriter.MaxFieldLength.UNLIMITED);

            _analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(_luceneVersion));
        }
示例#30
0
        /// <summary>
        /// This will load a file based index into RAM
        /// </summary>
        protected virtual RAMDirectory CreateDirectory(string folder)
        {
            FileUtil.EnsureFolder(folder);
            Lucene.Net.Store.FSDirectory tempDirectory = Lucene.Net.Store.FSDirectory.GetDirectory(folder, false);
            var directory = new RAMDirectory(tempDirectory);

            using (new IndexLocker(directory.MakeLock("write.lock")))
            {
                if (!IndexReader.IndexExists(directory))
                {
                    new IndexWriter(directory, this._analyzer, true).Close();
                }
            }

            return(directory);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MoreUmbracoDocsLikeThis"/> class.
 /// </summary>
 /// <param name="DocId">The doc id.</param>
 /// <param name="IndexToSearch">The index to search.</param>
 /// <param name="MaxNo">The max no.</param>
 /// <param name="FieldsToSearch">The fields to search.</param>
 public MoreUmbracoDocsLikeThis(int DocId, string IndexToSearch, int MaxNo, IEnumerable<string> FieldsToSearch)
 {
     docId = DocId;
     indexToSearch = IndexToSearch;
     maxNo = MaxNo;
     fieldsToSearch = FieldsToSearch;
     try
     {
         directory = FSDirectory.Open(new DirectoryInfo(GetIndexPath(indexToSearch)));
         reader = IndexReader.Open(directory, true);
         searcher = new IndexSearcher(reader);
     }
     catch (Exception ex)
     {
         ex.ToString();
     }
 }
示例#32
0
            protected override void Dispose(bool disposing)
            {
                if (isDisposed)
                {
                    return;
                }

                if (disposing)
                {
                    if (fsDir != null)
                    {
                        fsDir.Close();
                    }
                }

                fsDir      = null;
                isDisposed = true;
            }
        //--- Constructors ---
        public SearchInstanceData(string indexPath, Analyzer analyzer, UpdateDelayQueue queue) {
            _analyzer = analyzer;
            _directory = FSDirectory.GetDirectory(indexPath);

            // Note (arnec): Needed with 2.4.0 SimpleFSLock, since a hard shutdown will have left the lock dangling
            IndexWriter.Unlock(_directory);
            try {
                _writer = new IndexWriter(_directory, _analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
            } catch(CorruptIndexException e) {
                _log.WarnFormat("The Search index at {0} is corrupt. You must repair or delete it before restarting the service. If you delete it, you must rebuild your index after service restart.", indexPath);
                if(e.Message.StartsWith("Unknown format version")) {
                    _log.Warn("The index is considered corrupt because it's an unknown version. Did you accidentally downgrade your install?");
                }
                throw;
            }
            _reader = IndexReader.Open(_directory);
            _searcher = new IndexSearcher(_reader);
            _queue = queue;
        }
示例#34
0
        static void Main(string[] args)
        {
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_23);

            //IndexWriter writer = new IndexWriter("IndexDirectory", analyzer, true);
            //AddDocument(writer, "SQL Server 2008 的发布", "SQL Server 2008 的新特性");
            //AddDocument(writer, "ASP.Net MVC框架配置与分析", "而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions");
            //writer.Optimize();
            //writer.Close();

            while (true)
            {
                Console.Write("key:");

                string key = Console.ReadLine().ToString();

                string    indexPath = @"D:\workspace\C#\lucene\lucene\lucene\IndexDirectory";
                Directory directory = null;
                directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath));
                IndexSearcher         searcher = new IndexSearcher(directory);
                MultiFieldQueryParser parser   = new MultiFieldQueryParser(Version.LUCENE_23, new string[] { "title", "content" }, analyzer);
                Query   query = parser.Parse(key);
                TopDocs hits  = searcher.Search(query, (Filter)null, 1000);


                foreach (ScoreDoc hit in hits.ScoreDocs)
                {
                    Document doc     = searcher.Doc(hit.Doc);
                    string   id      = doc.Get("id");
                    string   title   = doc.Get("title");
                    string   content = doc.Get("content");

                    Console.WriteLine("title:{0},content:{1},id:{2}", title, content, id);
                }
                searcher.Dispose();
                Console.WriteLine("=================================");
            }


            Console.ReadKey();
        }
        public virtual void  TestNorms()
        {
            // tmp dir
            System.String tempDir = System.IO.Path.GetTempPath();
            if (tempDir == null)
            {
                throw new System.IO.IOException("java.io.tmpdir undefined, cannot run test");
            }

            // test with a single index: index1
            System.IO.DirectoryInfo indexDir1 = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempDir, "lucenetestindex1"));
            Directory dir1 = FSDirectory.Open(indexDir1);

            IndexWriter.Unlock(dir1);

            norms         = new System.Collections.ArrayList();
            modifiedNorms = new System.Collections.ArrayList();

            CreateIndex(dir1);
            DoTestNorms(dir1);

            // test with a single index: index2
            System.Collections.ArrayList norms1         = norms;
            System.Collections.ArrayList modifiedNorms1 = modifiedNorms;
            int numDocNorms1 = numDocNorms;

            norms         = new System.Collections.ArrayList();
            modifiedNorms = new System.Collections.ArrayList();
            numDocNorms   = 0;

            System.IO.DirectoryInfo indexDir2 = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempDir, "lucenetestindex2"));
            Directory dir2 = FSDirectory.Open(indexDir2);

            CreateIndex(dir2);
            DoTestNorms(dir2);

            // add index1 and index2 to a third index: index3
            System.IO.DirectoryInfo indexDir3 = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempDir, "lucenetestindex3"));
            Directory dir3 = FSDirectory.Open(indexDir3);

            CreateIndex(dir3);
            IndexWriter iw = new IndexWriter(dir3, anlzr, false, IndexWriter.MaxFieldLength.LIMITED);

            iw.SetMaxBufferedDocs(5);
            iw.MergeFactor = 3;
            iw.AddIndexesNoOptimize(new Directory[] { dir1, dir2 });
            iw.Optimize();
            iw.Close();

            norms1.AddRange(norms);
            norms = norms1;
            modifiedNorms1.AddRange(modifiedNorms);
            modifiedNorms = modifiedNorms1;
            numDocNorms  += numDocNorms1;

            // test with index3
            VerifyIndex(dir3);
            DoTestNorms(dir3);

            // now with optimize
            iw = new IndexWriter(dir3, anlzr, false, IndexWriter.MaxFieldLength.LIMITED);
            iw.SetMaxBufferedDocs(5);
            iw.MergeFactor = 3;
            iw.Optimize();
            iw.Close();
            VerifyIndex(dir3);

            dir1.Close();
            dir2.Close();
            dir3.Close();
        }
示例#36
0
        public virtual void TestDirectInstantiation()
        {
            DirectoryInfo path = CreateTempDir("testDirectInstantiation");

            byte[] largeBuffer = new byte[Random.Next(256 * 1024)], largeReadBuffer = new byte[largeBuffer.Length];
            for (int i = 0; i < largeBuffer.Length; i++)
            {
                largeBuffer[i] = (byte)i; // automatically loops with modulo
            }

            var dirs = new FSDirectory[] { new SimpleFSDirectory(path, null), new NIOFSDirectory(path, null), new MMapDirectory(path, null) };

            for (int i = 0; i < dirs.Length; i++)
            {
                FSDirectory dir = dirs[i];
                dir.EnsureOpen();
                string      fname    = "foo." + i;
                string      lockname = "foo" + i + ".lck";
                IndexOutput @out     = dir.CreateOutput(fname, NewIOContext(Random));
                @out.WriteByte((byte)(sbyte)i);
                @out.WriteBytes(largeBuffer, largeBuffer.Length);
                @out.Dispose();

                for (int j = 0; j < dirs.Length; j++)
                {
                    FSDirectory d2 = dirs[j];
                    d2.EnsureOpen();
                    Assert.IsTrue(SlowFileExists(d2, fname));
                    Assert.AreEqual(1 + largeBuffer.Length, d2.FileLength(fname));

                    // LUCENENET specific - unmap hack not needed
                    //// don't do read tests if unmapping is not supported!
                    //if (d2 is MMapDirectory && !((MMapDirectory)d2).UseUnmap)
                    //{
                    //    continue;
                    //}

                    IndexInput input = d2.OpenInput(fname, NewIOContext(Random));
                    Assert.AreEqual((byte)i, input.ReadByte());
                    // read array with buffering enabled
                    Arrays.Fill(largeReadBuffer, (byte)0);
                    input.ReadBytes(largeReadBuffer, 0, largeReadBuffer.Length, true);
                    Assert.AreEqual(largeBuffer, largeReadBuffer);
                    // read again without using buffer
                    input.Seek(1L);
                    Arrays.Fill(largeReadBuffer, (byte)0);
                    input.ReadBytes(largeReadBuffer, 0, largeReadBuffer.Length, false);
                    Assert.AreEqual(largeBuffer, largeReadBuffer);
                    input.Dispose();
                }

                // delete with a different dir
                dirs[(i + 1) % dirs.Length].DeleteFile(fname);

                for (int j = 0; j < dirs.Length; j++)
                {
                    FSDirectory d2 = dirs[j];
                    Assert.IsFalse(SlowFileExists(d2, fname));
                }

                Lock @lock = dir.MakeLock(lockname);
                Assert.IsTrue(@lock.Obtain());

                for (int j = 0; j < dirs.Length; j++)
                {
                    FSDirectory d2    = dirs[j];
                    Lock        lock2 = d2.MakeLock(lockname);
                    try
                    {
                        Assert.IsFalse(lock2.Obtain(1));
                    }
#pragma warning disable 168
                    catch (LockObtainFailedException e)
#pragma warning restore 168
                    {
                        // OK
                    }
                }

                @lock.Dispose();

                // now lock with different dir
                @lock = dirs[(i + 1) % dirs.Length].MakeLock(lockname);
                Assert.IsTrue(@lock.Obtain());
                @lock.Dispose();
            }

            for (int i = 0; i < dirs.Length; i++)
            {
                FSDirectory dir = dirs[i];
                dir.EnsureOpen();
                dir.Dispose();
                Assert.IsFalse(dir.IsOpen);
            }
        }
示例#37
0
 public virtual void  TestFSDirectoryFilter()
 {
     CheckDirectoryFilter(FSDirectory.Open(new System.IO.FileInfo("test")));
 }
示例#38
0
        public static int Main(System.String[] args)
        {
            bool doFix        = false;
            var  onlySegments = new List <string>();

            System.String indexPath = null;
            int           i         = 0;

            while (i < args.Length)
            {
                if (args[i].Equals("-fix"))
                {
                    doFix = true;
                    i++;
                }
                else if (args[i].Equals("-segment"))
                {
                    if (i == args.Length - 1)
                    {
                        System.Console.Out.WriteLine("ERROR: missing name for -segment option");
                        return(1);
                    }
                    onlySegments.Add(args[i + 1]);
                    i += 2;
                }
                else
                {
                    if (indexPath != null)
                    {
                        System.Console.Out.WriteLine("ERROR: unexpected extra argument '" + args[i] + "'");
                        return(1);
                    }
                    indexPath = args[i];
                    i++;
                }
            }

            if (indexPath == null)
            {
                System.Console.Out.WriteLine("\nERROR: index path not specified");
                System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Index.CheckIndex pathToIndex [-fix] [-segment X] [-segment Y]\n" + "\n" + "  -fix: actually write a new segments_N file, removing any problematic segments\n" + "  -segment X: only check the specified segments.  This can be specified multiple\n" + "              times, to check more than one segment, eg '-segment _2 -segment _a'.\n" + "              You can't use this with the -fix option\n" + "\n" + "**WARNING**: -fix should only be used on an emergency basis as it will cause\n" + "documents (perhaps many) to be permanently removed from the index.  Always make\n" + "a backup copy of your index before running this!  Do not run this tool on an index\n" + "that is actively being written to.  You have been warned!\n" + "\n" + "Run without -fix, this tool will open the index, report version information\n" + "and report any exceptions it hits and what action it would take if -fix were\n" + "specified.  With -fix, this tool will remove any segments that have issues and\n" + "write a new segments_N file.  This means all documents contained in the affected\n" + "segments will be removed.\n" + "\n" + "This tool exits with exit code 1 if the index cannot be opened or has any\n" + "corruption, else 0.\n");
                return(1);
            }

            if (!AssertsOn())
            {
                System.Console.Out.WriteLine("\nNOTE: testing will be more thorough if you run java with '-ea:Lucene.Net...', so assertions are enabled");
            }

            if (onlySegments.Count == 0)
            {
                onlySegments = null;
            }
            else if (doFix)
            {
                System.Console.Out.WriteLine("ERROR: cannot specify both -fix and -segment");
                return(1);
            }

            System.Console.Out.WriteLine("\nOpening index @ " + indexPath + "\n");
            Directory dir = null;

            try
            {
                dir = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath));
            }
            catch (Exception t)
            {
                Console.Out.WriteLine("ERROR: could not open directory \"" + indexPath + "\"; exiting");
                Console.Out.WriteLine(t.StackTrace);
                return(1);
            }

            var checker    = new CheckIndex(dir);
            var tempWriter = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Console.Out.Encoding)
            {
                AutoFlush = true
            };

            checker.SetInfoStream(tempWriter);

            Status result = checker.CheckIndex_Renamed_Method(onlySegments, null);

            if (result.missingSegments)
            {
                return(1);
            }

            if (!result.clean)
            {
                if (!doFix)
                {
                    System.Console.Out.WriteLine("WARNING: would write new segments file, and " + result.totLoseDocCount + " documents would be lost, if -fix were specified\n");
                }
                else
                {
                    Console.Out.WriteLine("WARNING: " + result.totLoseDocCount + " documents will be lost\n");
                    Console.Out.WriteLine("NOTE: will write new segments file in 5 seconds; this will remove " + result.totLoseDocCount + " docs from the index. THIS IS YOUR LAST CHANCE TO CTRL+C!");
                    for (var s = 0; s < 5; s++)
                    {
                        System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 1000));
                        System.Console.Out.WriteLine("  " + (5 - s) + "...");
                    }
                    Console.Out.WriteLine("Writing...");
                    checker.FixIndex(result, null);
                    Console.Out.WriteLine("OK");
                    Console.Out.WriteLine("Wrote new segments file \"" + result.newSegments.GetCurrentSegmentFileName() + "\"");
                }
            }
            System.Console.Out.WriteLine("");

            int exitCode;

            if (result != null && result.clean == true)
            {
                exitCode = 0;
            }
            else
            {
                exitCode = 1;
            }

            return(exitCode);
        }
示例#39
0
 public AnonymousClassLock(System.IO.FileInfo lockFile, FSDirectory enclosingInstance)
 {
     InitBlock(lockFile, enclosingInstance);
 }
示例#40
0
 public RAMDirectory(System.String dir) : this(FSDirectory.GetDirectory(dir), true)
 {
 }
示例#41
0
        public virtual void  TestLazyPerformance()
        {
            System.String           tmpIODir = AppSettings.Get("tempDir", Path.GetTempPath());
            System.String           path     = tmpIODir + System.IO.Path.DirectorySeparatorChar.ToString() + "lazyDir" + Guid.NewGuid();
            System.IO.DirectoryInfo file     = new System.IO.DirectoryInfo(path);
            _TestUtil.RmDir(file);
            FSDirectory tmpDir = FSDirectory.Open(file);

            Assert.IsTrue(tmpDir != null);

            IndexWriter writer = new IndexWriter(tmpDir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED, null);

            writer.UseCompoundFile = false;
            writer.AddDocument(testDoc, null);
            writer.Close();

            Assert.IsTrue(fieldInfos != null);
            FieldsReader  reader;
            long          lazyTime       = 0;
            long          regularTime    = 0;
            int           length         = 50;
            ISet <string> lazyFieldNames = Support.Compatibility.SetFactory.CreateHashSet <string>();

            lazyFieldNames.Add(DocHelper.LARGE_LAZY_FIELD_KEY);
            SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(Support.Compatibility.SetFactory.CreateHashSet <string>(), lazyFieldNames);

            for (int i = 0; i < length; i++)
            {
                reader = new FieldsReader(tmpDir, TEST_SEGMENT_NAME, fieldInfos, null);
                Assert.IsTrue(reader != null);
                Assert.IsTrue(reader.Size() == 1);

                Document doc;
                doc = reader.Doc(0, null, null);                 //Load all of them
                Assert.IsTrue(doc != null, "doc is null and it shouldn't be");
                IFieldable field = doc.GetFieldable(DocHelper.LARGE_LAZY_FIELD_KEY);
                Assert.IsTrue(field.IsLazy == false, "field is lazy");
                System.String value_Renamed;
                long          start;
                long          finish;
                start = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                //On my machine this was always 0ms.
                value_Renamed = field.StringValue(null);
                finish        = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be");
                Assert.IsTrue(field != null, "field is null and it shouldn't be");
                regularTime += (finish - start);
                reader.Dispose();
                reader = null;
                doc    = null;
                //Hmmm, are we still in cache???
                System.GC.Collect();
                reader = new FieldsReader(tmpDir, TEST_SEGMENT_NAME, fieldInfos, null);
                doc    = reader.Doc(0, fieldSelector, null);
                field  = doc.GetFieldable(DocHelper.LARGE_LAZY_FIELD_KEY);
                Assert.IsTrue(field.IsLazy == true, "field is not lazy");
                start = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                //On my machine this took around 50 - 70ms
                value_Renamed = field.StringValue(null);
                finish        = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be");
                lazyTime += (finish - start);
                reader.Dispose();
            }
            System.Console.Out.WriteLine("Average Non-lazy time (should be very close to zero): " + regularTime / length + " ms for " + length + " reads");
            System.Console.Out.WriteLine("Average Lazy Time (should be greater than zero): " + lazyTime / length + " ms for " + length + " reads");
        }
示例#42
0
 public FaultyFSDirectory(System.IO.DirectoryInfo dir)
 {
     fsDir = FSDirectory.Open(dir);
     interalLockFactory = fsDir.LockFactory;
 }
示例#43
0
 public RAMDirectory(System.IO.FileInfo dir) : this(FSDirectory.GetDirectory(dir), true)
 {
 }
示例#44
0
 private void  InitBlock(System.IO.FileInfo lockFile, FSDirectory enclosingInstance)
 {
     this.lockFile          = lockFile;
     this.enclosingInstance = enclosingInstance;
 }