Lucene.Net.Index.DirectoryReader.ListCommits C# (CSharp) Метод

ListCommits() публичный статический Метод

public static ListCommits ( Lucene.Net.Store.Directory dir ) : ICollection
dir Lucene.Net.Store.Directory
Результат ICollection
        public static new ICollection<IndexCommit> ListCommits(Directory dir)
        {
            String[] files = dir.ListAll();

            ICollection<IndexCommit> commits = new  List<IndexCommit>();
            
            var latest = new SegmentInfos();
            latest.Read(dir);
            long currentGen = latest.Generation;
            
            commits.Add(new ReaderCommit(latest, dir));
            
            foreach (string fileName in files)
            {
            	if (fileName.StartsWith(IndexFileNames.SEGMENTS) && !fileName.Equals(IndexFileNames.SEGMENTS_GEN) && SegmentInfos.GenerationFromSegmentsFileName(fileName) < currentGen)
            	{
                    
            		var sis = new SegmentInfos();
            		try
            		{
            			// IOException allowed to throw there, in case
            			// segments_N is corrupt
            			sis.Read(dir, fileName);
            		}
            		catch (System.IO.FileNotFoundException)
            		{
            			// LUCENE-948: on NFS (and maybe others), if
            			// you have writers switching back and forth
            			// between machines, it's very likely that the
            			// dir listing will be stale and will claim a
            			// file segments_X exists when in fact it
            			// doesn't.  So, we catch this and handle it
            			// as if the file does not exist
            			sis = null;
            		}
                    
            		if (sis != null)
            			commits.Add(new ReaderCommit(sis, dir));
            	}
            }
            
            return commits;
        }
        

Usage Example

Пример #1
0
        public virtual void TestReopenOnCommit()
        {
            Directory   dir    = NewDirectory();
            IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetIndexDeletionPolicy(new KeepAllCommits <IndexCommit>()).SetMaxBufferedDocs(-1).SetMergePolicy(NewLogMergePolicy(10)));

            for (int i = 0; i < 4; i++)
            {
                Document doc = new Document();
                doc.Add(NewStringField("id", "" + i, Field.Store.NO));
                writer.AddDocument(doc);
                IDictionary <string, string> data = new Dictionary <string, string>();
                data["index"]     = i + "";
                writer.CommitData = data;
                writer.Commit();
            }
            for (int i = 0; i < 4; i++)
            {
                writer.DeleteDocuments(new Term("id", "" + i));
                IDictionary <string, string> data = new Dictionary <string, string>();
                data["index"]     = (4 + i) + "";
                writer.CommitData = data;
                writer.Commit();
            }
            writer.Dispose();

            DirectoryReader r = DirectoryReader.Open(dir);

            Assert.AreEqual(0, r.NumDocs);

            ICollection <IndexCommit> commits = DirectoryReader.ListCommits(dir);

            foreach (IndexCommit commit in commits)
            {
                DirectoryReader r2 = DirectoryReader.OpenIfChanged(r, commit);
                Assert.IsNotNull(r2);
                Assert.IsTrue(r2 != r);

                IDictionary <string, string> s = commit.UserData;
                int v;
                if (s.Count == 0)
                {
                    // First commit created by IW
                    v = -1;
                }
                else
                {
                    v = Convert.ToInt32(s["index"]);
                }
                if (v < 4)
                {
                    Assert.AreEqual(1 + v, r2.NumDocs);
                }
                else
                {
                    Assert.AreEqual(7 - v, r2.NumDocs);
                }
                r.Dispose();
                r = r2;
            }
            r.Dispose();
            dir.Dispose();
        }
All Usage Examples Of Lucene.Net.Index.DirectoryReader::ListCommits