ListCommits() public static method

Returns all commit points that exist in the Directory. Normally, because the default is KeepOnlyLastCommitDeletionPolicy , there would be only one commit point. But if you're using a custom IndexDeletionPolicy then there could be many commits. Once you have a given commit, you can open a reader on it by calling IndexReader.Open(IndexCommit,bool) There must be at least one commit in the Directory, else this method throws System.IO.IOException. Note that if a commit is in progress while this method is running, that commit may or may not be returned array.
public static ListCommits ( Directory dir ) : System.Collections.Generic.ICollection
dir Directory
return System.Collections.Generic.ICollection
Esempio n. 1
0
        //Rolls back index to a chosen ID
        private void  RollBackLast(int id)
        {
            // System.out.println("Attempting to rollback to "+id);
            System.String ids  = "-" + id;
            IndexCommit   last = null;

            System.Collections.ICollection commits = IndexReader.ListCommits(dir);
            for (System.Collections.IEnumerator iterator = commits.GetEnumerator(); iterator.MoveNext();)
            {
                IndexCommit commit = (IndexCommit)iterator.Current;
                System.Collections.Generic.IDictionary <string, string> ud = commit.GetUserData();
                if (ud.Count > 0)
                {
                    if (((System.String)ud["index"]).EndsWith(ids))
                    {
                        last = commit;
                    }
                }
            }

            if (last == null)
            {
                throw new System.SystemException("Couldn't find commit point " + id);
            }

            IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), new RollbackDeletionPolicy(this, id), MaxFieldLength.UNLIMITED, last);

            System.Collections.Generic.IDictionary <string, string> data = new System.Collections.Generic.Dictionary <string, string>();
            data["index"] = "Rolled back to 1-" + id;
            w.Commit(data);
            w.Close();
        }
Esempio n. 2
0
        public virtual void  TestOpenPriorSnapshot()
        {
            // Never deletes a commit
            KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy(this);

            Directory dir = new MockRAMDirectory();

            policy.dir = dir;

            IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), policy, IndexWriter.MaxFieldLength.LIMITED);

            writer.SetMaxBufferedDocs(2);
            for (int i = 0; i < 10; i++)
            {
                AddDoc(writer);
                if ((1 + i) % 2 == 0)
                {
                    writer.Commit();
                }
            }
            writer.Close();

            System.Collections.ICollection commits = IndexReader.ListCommits(dir);
            Assert.AreEqual(6, commits.Count);
            IndexCommit lastCommit = null;

            System.Collections.IEnumerator it = commits.GetEnumerator();
            while (it.MoveNext())
            {
                IndexCommit commit = (IndexCommit)it.Current;
                if (lastCommit == null || commit.GetGeneration() > lastCommit.GetGeneration())
                {
                    lastCommit = commit;
                }
            }
            Assert.IsTrue(lastCommit != null);

            // Now add 1 doc and optimize
            writer = new IndexWriter(dir, new WhitespaceAnalyzer(), policy, IndexWriter.MaxFieldLength.LIMITED);
            AddDoc(writer);
            Assert.AreEqual(11, writer.NumDocs());
            writer.Optimize();
            writer.Close();

            Assert.AreEqual(7, IndexReader.ListCommits(dir).Count);

            // Now open writer on the commit just before optimize:
            writer = new IndexWriter(dir, new WhitespaceAnalyzer(), policy, IndexWriter.MaxFieldLength.LIMITED, lastCommit);
            Assert.AreEqual(10, writer.NumDocs());

            // Should undo our rollback:
            writer.Rollback();

            IndexReader r = IndexReader.Open(dir);

            // Still optimized, still 11 docs
            Assert.IsTrue(r.IsOptimized());
            Assert.AreEqual(11, r.NumDocs());
            r.Close();

            writer = new IndexWriter(dir, new WhitespaceAnalyzer(), policy, IndexWriter.MaxFieldLength.LIMITED, lastCommit);
            Assert.AreEqual(10, writer.NumDocs());
            // Commits the rollback:
            writer.Close();

            // Now 8 because we made another commit
            Assert.AreEqual(8, IndexReader.ListCommits(dir).Count);

            r = IndexReader.Open(dir);
            // Not optimized because we rolled it back, and now only
            // 10 docs
            Assert.IsTrue(!r.IsOptimized());
            Assert.AreEqual(10, r.NumDocs());
            r.Close();

            // Reoptimize
            writer = new IndexWriter(dir, new WhitespaceAnalyzer(), policy, IndexWriter.MaxFieldLength.LIMITED);
            writer.Optimize();
            writer.Close();

            r = IndexReader.Open(dir);
            Assert.IsTrue(r.IsOptimized());
            Assert.AreEqual(10, r.NumDocs());
            r.Close();

            // Now open writer on the commit just before optimize,
            // but this time keeping only the last commit:
            writer = new IndexWriter(dir, new WhitespaceAnalyzer(), new KeepOnlyLastCommitDeletionPolicy(), IndexWriter.MaxFieldLength.LIMITED, lastCommit);
            Assert.AreEqual(10, writer.NumDocs());

            // Reader still sees optimized index, because writer
            // opened on the prior commit has not yet committed:
            r = IndexReader.Open(dir);
            Assert.IsTrue(r.IsOptimized());
            Assert.AreEqual(10, r.NumDocs());
            r.Close();

            writer.Close();

            // Now reader sees unoptimized index:
            r = IndexReader.Open(dir);
            Assert.IsTrue(!r.IsOptimized());
            Assert.AreEqual(10, r.NumDocs());
            r.Close();

            dir.Close();
        }
Esempio n. 3
0
        public virtual void  TestKeepAllDeletionPolicy()
        {
            for (int pass = 0; pass < 4; pass++)
            {
                bool autoCommit      = pass < 2;
                bool useCompoundFile = (pass % 2) > 0;

                // Never deletes a commit
                KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy(this);

                Directory dir = new RAMDirectory();
                policy.dir = dir;

                IndexWriter writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true, policy);
                writer.SetMaxBufferedDocs(10);
                writer.SetUseCompoundFile(useCompoundFile);
                writer.SetMergeScheduler(new SerialMergeScheduler());
                for (int i = 0; i < 107; i++)
                {
                    AddDoc(writer);
                    if (autoCommit && i % 10 == 0)
                    {
                        writer.Commit();
                    }
                }
                writer.Close();

                writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), false, policy);
                writer.SetUseCompoundFile(useCompoundFile);
                writer.Optimize();
                writer.Close();

                Assert.AreEqual(2, policy.numOnInit);
                if (!autoCommit)
                {
                    // If we are not auto committing then there should
                    // be exactly 2 commits (one per close above):
                    Assert.AreEqual(2, policy.numOnCommit);
                }

                // Test listCommits
                System.Collections.ICollection commits = IndexReader.ListCommits(dir);
                if (!autoCommit)
                {
                    // 1 from opening writer + 2 from closing writer
                    Assert.AreEqual(3, commits.Count);
                }
                // 1 from opening writer + 2 from closing writer +
                // 11 from calling writer.commit() explicitly above
                else
                {
                    Assert.AreEqual(14, commits.Count);
                }

                System.Collections.IEnumerator it = commits.GetEnumerator();
                // Make sure we can open a reader on each commit:
                while (it.MoveNext())
                {
                    IndexCommit commit = (IndexCommit)it.Current;
                    IndexReader r      = IndexReader.Open(commit, null);
                    r.Close();
                }

                // Simplistic check: just verify all segments_N's still
                // exist, and, I can open a reader on each:
                dir.DeleteFile(IndexFileNames.SEGMENTS_GEN);
                long gen = SegmentInfos.GetCurrentSegmentGeneration(dir);
                while (gen > 0)
                {
                    IndexReader reader = IndexReader.Open(dir);
                    reader.Close();
                    dir.DeleteFile(IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, "", gen));
                    gen--;

                    if (gen > 0)
                    {
                        // Now that we've removed a commit point, which
                        // should have orphan'd at least one index file.
                        // Open & close a writer and assert that it
                        // actually removed something:
                        int preCount = dir.ListAll().Length;
                        writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, policy, IndexWriter.MaxFieldLength.LIMITED);
                        writer.Close();
                        int postCount = dir.ListAll().Length;
                        Assert.IsTrue(postCount < preCount);
                    }
                }

                dir.Close();
            }
        }