Lucene.Net.Index.SegmentInfos.GenerationFromSegmentsFileName C# (CSharp) Method

GenerationFromSegmentsFileName() public static method

Parse the generation off the segments file name and return it.
public static GenerationFromSegmentsFileName ( string fileName ) : long
fileName string
return long
        public static long GenerationFromSegmentsFileName(string fileName)
        {
            if (fileName.Equals(IndexFileNames.SEGMENTS))
            {
                return 0;
            }
            else if (fileName.StartsWith(IndexFileNames.SEGMENTS))
            {
                return Convert.ToInt64(fileName.Substring(1 + IndexFileNames.SEGMENTS.Length));
            }
            else
            {
                throw new System.ArgumentException("fileName \"" + fileName + "\" is not a segments file");
            }
        }

Usage Example

        /** @see IndexReader#listCommits */
        public new static ICollection <IndexCommitPoint> ListCommits(Directory dir)
        {
            string[] files = dir.List();
            if (files == null)
            {
                throw new System.IO.IOException("cannot read directory " + dir + ": list() returned null");
            }

            ICollection <IndexCommitPoint> commits = new List <IndexCommitPoint>();

            SegmentInfos latest = new SegmentInfos();

            latest.Read(dir);
            long currentGen = latest.GetGeneration();

            commits.Add(new ReaderCommit(latest, dir));

            for (int i = 0; i < files.Length; i++)
            {
                String fileName = files[i];

                if (fileName.StartsWith(IndexFileNames.SEGMENTS) &&
                    !fileName.Equals(IndexFileNames.SEGMENTS_GEN) &&
                    SegmentInfos.GenerationFromSegmentsFileName(fileName) < currentGen)
                {
                    SegmentInfos sis = new SegmentInfos();
                    try
                    {
                        // IOException allowed to throw there, in case
                        // segments_N is corrupt
                        sis.Read(dir, fileName);
                    }
                    catch (System.Exception)
                    {
                        // 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);
        }
All Usage Examples Of Lucene.Net.Index.SegmentInfos::GenerationFromSegmentsFileName