Ejemplo n.º 1
0
		/// <summary> Read a particular segmentFileName.  Note that this may
		/// throw an IOException if a commit is in process.
		/// 
		/// </summary>
		/// <param name="directory">-- directory containing the segments file
		/// </param>
		/// <param name="segmentFileName">-- segment file to load
		/// </param>
		/// <throws>  CorruptIndexException if the index is corrupt </throws>
		/// <throws>  IOException if there is a low-level IO error </throws>
		public void  Read(Directory directory, System.String segmentFileName)
		{
			bool success = false;
			
			// Clear any previous segments:
			Clear();
			
			ChecksumIndexInput input = new ChecksumIndexInput(directory.OpenInput(segmentFileName));
			
			generation = GenerationFromSegmentsFileName(segmentFileName);
			
			lastGeneration = generation;
			
			try
			{
				int format = input.ReadInt();
				if (format < 0)
				{
					// file contains explicit format info
					// check that it is a format we can understand
					if (format < CURRENT_FORMAT)
						throw new CorruptIndexException("Unknown format version: " + format);
					version = input.ReadLong(); // read version
					counter = input.ReadInt(); // read counter
				}
				else
				{
					// file is in old format without explicit format info
					counter = format;
				}
				
				for (int i = input.ReadInt(); i > 0; i--)
				{
					// read segmentInfos
					Add(new SegmentInfo(directory, format, input));
				}
				
				if (format >= 0)
				{
					// in old format the version number may be at the end of the file
					if (input.GetFilePointer() >= input.Length())
						version = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
					// old file format without version number
					else
						version = input.ReadLong(); // read version
				}
				
				if (format <= FORMAT_USER_DATA)
				{
					if (format <= FORMAT_DIAGNOSTICS)
					{
						userData = input.ReadStringStringMap();
					}
					else if (0 != input.ReadByte())
					{
                        userData = new System.Collections.Generic.Dictionary<string,string>();
						userData.Add("userData", input.ReadString());
					}
					else
					{
                        userData = new System.Collections.Generic.Dictionary<string, string>();
					}
				}
				else
				{
                    userData = new System.Collections.Generic.Dictionary<string, string>();
				}
				
				if (format <= FORMAT_CHECKSUM)
				{
					long checksumNow = input.GetChecksum();
					long checksumThen = input.ReadLong();
					if (checksumNow != checksumThen)
						throw new CorruptIndexException("checksum mismatch in segments file");
				}
				success = true;
			}
			finally
			{
				input.Close();
				if (!success)
				{
					// Clear any segment infos we had loaded so we
					// have a clean slate on retry:
					Clear();
				}
			}
		}
Ejemplo n.º 2
0
        /// <summary> Read a particular segmentFileName.  Note that this may
        /// throw an IOException if a commit is in process.
        ///
        /// </summary>
        /// <param name="directory">-- directory containing the segments file
        /// </param>
        /// <param name="segmentFileName">-- segment file to load
        /// </param>
        /// <throws>  CorruptIndexException if the index is corrupt </throws>
        /// <throws>  IOException if there is a low-level IO error </throws>
        public void  Read(Directory directory, System.String segmentFileName)
        {
            bool success = false;

            // Clear any previous segments:
            Clear();

            ChecksumIndexInput input = new ChecksumIndexInput(directory.OpenInput(segmentFileName));

            generation = GenerationFromSegmentsFileName(segmentFileName);

            lastGeneration = generation;

            try
            {
                int format = input.ReadInt();
                if (format < 0)
                {
                    // file contains explicit format info
                    // check that it is a format we can understand
                    if (format < CURRENT_FORMAT)
                    {
                        throw new CorruptIndexException("Unknown format version: " + format);
                    }
                    version = input.ReadLong();                    // read version
                    counter = input.ReadInt();                     // read counter
                }
                else
                {
                    // file is in old format without explicit format info
                    counter = format;
                }

                for (int i = input.ReadInt(); i > 0; i--)
                {
                    // read segmentInfos
                    Add(new SegmentInfo(directory, format, input));
                }

                if (format >= 0)
                {
                    // in old format the version number may be at the end of the file
                    if (input.GetFilePointer() >= input.Length())
                    {
                        version = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                    }
                    // old file format without version number
                    else
                    {
                        version = input.ReadLong();                         // read version
                    }
                }

                if (format <= FORMAT_USER_DATA)
                {
                    if (format <= FORMAT_DIAGNOSTICS)
                    {
                        userData = input.ReadStringStringMap();
                    }
                    else if (0 != input.ReadByte())
                    {
                        userData = new System.Collections.Generic.Dictionary <string, string>();
                        userData.Add("userData", input.ReadString());
                    }
                    else
                    {
                        userData = new System.Collections.Generic.Dictionary <string, string>();
                    }
                }
                else
                {
                    userData = new System.Collections.Generic.Dictionary <string, string>();
                }

                if (format <= FORMAT_CHECKSUM)
                {
                    long checksumNow  = input.GetChecksum();
                    long checksumThen = input.ReadLong();
                    if (checksumNow != checksumThen)
                    {
                        throw new CorruptIndexException("checksum mismatch in segments file");
                    }
                }
                success = true;
            }
            finally
            {
                input.Close();
                if (!success)
                {
                    // Clear any segment infos we had loaded so we
                    // have a clean slate on retry:
                    Clear();
                }
            }
        }