/// <summary>
        /// Save a single segment's info. </summary>
        public override void Write(Directory dir, SegmentInfo si, FieldInfos fis, IOContext ioContext)
        {
            string fileName = IndexFileNames.SegmentFileName(si.Name, "", Lucene40SegmentInfoFormat.SI_EXTENSION);

            si.AddFile(fileName);

            IndexOutput output = dir.CreateOutput(fileName, ioContext);

            bool success = false;

            try
            {
                CodecUtil.WriteHeader(output, Lucene40SegmentInfoFormat.CODEC_NAME, Lucene40SegmentInfoFormat.VERSION_CURRENT);
                // Write the Lucene version that created this segment, since 3.1
                output.WriteString(si.Version);
                output.WriteInt32(si.DocCount);

                output.WriteByte((byte)(sbyte)(si.UseCompoundFile ? SegmentInfo.YES : SegmentInfo.NO));
                output.WriteStringStringMap(si.Diagnostics);
                output.WriteStringStringMap(Collections.EmptyMap <string, string>());
                output.WriteStringSet(si.GetFiles());

                success = true;
            }
            finally
            {
                if (!success)
                {
                    IOUtils.DisposeWhileHandlingException(output);
                    si.Dir.DeleteFile(fileName);
                }
                else
                {
                    output.Dispose();
                }
            }
        }
예제 #2
0
        public override void Write(Directory dir, SegmentInfo si, FieldInfos fis, IOContext ioContext)
        {
            var segFileName = IndexFileNames.SegmentFileName(si.Name, "", SimpleTextSegmentInfoFormat.SI_EXTENSION);

            si.AddFile(segFileName);

            var success = false;
            var output  = dir.CreateOutput(segFileName, ioContext);

            try
            {
                var scratch = new BytesRef();

                SimpleTextUtil.Write(output, SI_VERSION);
                SimpleTextUtil.Write(output, si.Version, scratch);
                SimpleTextUtil.WriteNewline(output);

                SimpleTextUtil.Write(output, SI_DOCCOUNT);
                SimpleTextUtil.Write(output, Convert.ToString(si.DocCount, CultureInfo.InvariantCulture), scratch);
                SimpleTextUtil.WriteNewline(output);

                SimpleTextUtil.Write(output, SI_USECOMPOUND);
                SimpleTextUtil.Write(output, Convert.ToString(si.UseCompoundFile, CultureInfo.InvariantCulture).ToLowerInvariant(), scratch);
                SimpleTextUtil.WriteNewline(output);

                IDictionary <string, string> diagnostics = si.Diagnostics;
                int numDiagnostics = diagnostics is null ? 0 : diagnostics.Count;
                SimpleTextUtil.Write(output, SI_NUM_DIAG);
                SimpleTextUtil.Write(output, Convert.ToString(numDiagnostics, CultureInfo.InvariantCulture), scratch);
                SimpleTextUtil.WriteNewline(output);

                if (numDiagnostics > 0)
                {
                    foreach (var diagEntry in diagnostics)
                    {
                        SimpleTextUtil.Write(output, SI_DIAG_KEY);
                        SimpleTextUtil.Write(output, diagEntry.Key, scratch);
                        SimpleTextUtil.WriteNewline(output);

                        SimpleTextUtil.Write(output, SI_DIAG_VALUE);
                        SimpleTextUtil.Write(output, diagEntry.Value, scratch);
                        SimpleTextUtil.WriteNewline(output);
                    }
                }

                var files    = si.GetFiles();
                var numFiles = files is null ? 0 : files.Count;
                SimpleTextUtil.Write(output, SI_NUM_FILES);
                SimpleTextUtil.Write(output, Convert.ToString(numFiles, CultureInfo.InvariantCulture), scratch);
                SimpleTextUtil.WriteNewline(output);

                if (numFiles > 0)
                {
                    foreach (var fileName in files)
                    {
                        SimpleTextUtil.Write(output, SI_FILE);
                        SimpleTextUtil.Write(output, fileName, scratch);
                        SimpleTextUtil.WriteNewline(output);
                    }
                }

                SimpleTextUtil.WriteChecksum(output, scratch);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    IOUtils.DisposeWhileHandlingException(output);
                    try
                    {
                        dir.DeleteFile(segFileName);
                    }
                    catch (Exception t) when(t.IsThrowable())
                    {
                        //Esnure we throw original exeception
                    }
                }
                else
                {
                    output.Dispose();
                }
            }
        }