示例#1
0
        private void  Write(Directory directory)
        {
            System.String segmentFileName = GetNextSegmentFileName();

            // Always advance the generation on write:
            if (generation == -1)
            {
                generation = 1;
            }
            else
            {
                generation++;
            }

            var segnOutput = new ChecksumIndexOutput(directory.CreateOutput(segmentFileName));

            bool success = false;

            try
            {
                segnOutput.WriteInt(CURRENT_FORMAT);        // write FORMAT
                segnOutput.WriteLong(++version);            // every write changes
                // the index
                segnOutput.WriteInt(counter);               // write counter
                segnOutput.WriteInt(Count);                 // write infos
                for (int i = 0; i < Count; i++)
                {
                    Info(i).Write(segnOutput);
                }
                segnOutput.WriteStringStringMap(userData);
                segnOutput.PrepareCommit();
                success           = true;
                pendingSegnOutput = segnOutput;
            }
            finally
            {
                if (!success)
                {
                    // We hit an exception above; try to close the file
                    // but suppress any exception:
                    try
                    {
                        segnOutput.Close();
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                    try
                    {
                        // Try not to leave a truncated segments_N file in
                        // the index:
                        directory.DeleteFile(segmentFileName);
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                }
            }
        }