예제 #1
0
 private void  DeleteFiles(System.Collections.ArrayList files, Directory directory)
 {
     for (int i = 0; i < files.Count; i++)
     {
         directory.DeleteFile((System.String)files[i]);
     }
 }
예제 #2
0
        public void Optimize(Directory directory)
        {
            string[] files = directory.List();

            System.Collections.ArrayList segment_names = new System.Collections.ArrayList();

            foreach (SegmentInfo si in this)
            {
                segment_names.Add(si.name);
            }

            foreach (string file in files)
            {
                string basename = System.IO.Path.GetFileNameWithoutExtension(file);
                if (segment_names.Contains(basename))
                {
                    continue;
                }

                // Allowed files deletable, segments, segments.gen, segments_N
                if (basename == IndexFileNames.DELETABLE || basename.StartsWith(IndexFileNames.SEGMENTS))
                {
                    continue;
                }

                Console.WriteLine("WARNING! Deleting stale data {0}", file);
                try {
                    directory.DeleteFile(file);
                } catch { /* Could be already deleted. */ }
            }
        }
예제 #3
0
        public void Optimize(Directory directory)
        {
            string[] files = directory.List();

            System.Collections.ArrayList segment_names = new System.Collections.ArrayList();
            foreach (SegmentInfo si in this)
            {
                segment_names.Add(si.name);
            }

            foreach (string file in files)
            {
                string basename = System.IO.Path.GetFileNameWithoutExtension(file);
                if (segment_names.Contains(basename))
                {
                    continue;
                }

                if (basename == IndexFileNames.DELETABLE || basename == IndexFileNames.SEGMENTS)
                {
                    continue;
                }

                Console.WriteLine("WARNING! Deleting stale data {0}", file);
                directory.DeleteFile(file);
            }
        }
예제 #4
0
        internal void  RollbackCommit(Directory dir)
        {
            if (pendingSegnOutput != null)
            {
                try
                {
                    pendingSegnOutput.Close();
                }
                catch (System.Exception t)
                {
                    // Suppress so we keep throwing the original exception
                    // in our caller
                }

                // Must carefully compute fileName from "generation"
                // since lastGeneration isn't incremented:
                try
                {
                    System.String segmentFileName = IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, "", generation);
                    dir.DeleteFile(segmentFileName);
                }
                catch (System.Exception t)
                {
                    // Suppress so we keep throwing the original exception
                    // in our caller
                }
                pendingSegnOutput = null;
            }
        }
예제 #5
0
        private void Write(Directory directory)
        {
            System.String segmentFileName = GetNextSegmentFileName();

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

            ChecksumIndexOutput output = new ChecksumIndexOutput(directory.CreateOutput(segmentFileName));

            bool success = false;

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

            var files = LuceneDirectory.ListAll();

            foreach (var file in files)
            {
                LuceneDirectory.DeleteFile(file);
            }

            using (GetIndexWriter()) { }
        }
예제 #7
0
 internal void  DeleteFile(System.String file)
 {
     try
     {
         directory.DeleteFile(file);                 // try to delete each file
     }
     catch (System.IO.IOException e)
     {
         // if delete fails
         if (directory.FileExists(file))
         {
             if (infoStream != null)
             {
                 infoStream.WriteLine("IndexFileDeleter: unable to remove file \"" + file + "\": " + e.ToString() + "; Will re-try later.");
             }
             AddDeletableFile(file);                     // add to deletable
         }
     }
 }
예제 #8
0
 private void  DeleteFiles(System.Collections.ArrayList files, System.Collections.ArrayList deletable)
 {
     for (int i = 0; i < files.Count; i++)
     {
         System.String file = (System.String)files[i];
         try
         {
             directory.DeleteFile(file);                     // try to delete each file
         }
         catch (System.IO.IOException e)
         {
             // if delete fails
             if (directory.FileExists(file))
             {
                 if (infoStream != null)
                 {
                     infoStream.WriteLine(e.ToString() + "; Will re-try later.");
                 }
                 deletable.Add(file);                         // add to deletable
             }
         }
     }
 }
예제 #9
0
        private void Write(Directory directory)
        {
            System.String segmentFileName = GetNextSegmentFileName();

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

            ChecksumIndexOutput output = new ChecksumIndexOutput(directory.CreateOutput(segmentFileName));

            bool success = false;

            try
            {
                output.WriteInt(CURRENT_FORMAT); // write FORMAT
                output.WriteLong(++version); // every write changes
                // the index
                output.WriteInt(counter); // write counter
                output.WriteInt(Count); // write infos
                for (int i = 0; i < Count; i++)
                {
                    Info(i).Write(output);
                }
                output.PrepareCommit();
                success = true;
                pendingOutput = output;
            }
            finally
            {
                if (!success)
                {
                    // we hit an exception above; try to close the file but suppress any exception:
                    try
                    {
                        output.Close();
                    }
                    catch (System.Exception)
                    {
                        // suppress so we keep throwing the original exception
                    }
                    try
                    {
                        // try not to leave a truncated segments_N file int the index
                        directory.DeleteFile(segmentFileName);
                    }
                    catch (System.Exception)
                    {
                        // suppress so we keep throwing the original exception
                    }
                }
            }
        }
예제 #10
0
        public void RollbackCommit(Directory dir)
        {
            if (pendingOutput != null)
            {
                try
                {
                    pendingOutput.Close();
                }
                catch (System.Exception)
                {
                    // Suppress so we keep throwing the original exception
                    // in our caller
                }

                // Must carefully compute fileName from "generation"
                // since lastGeneration isn't incremented:
                try
                {
                    String segmentFileName = IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, "", generation);
                    dir.DeleteFile(segmentFileName);
                }
                catch (System.Exception)
                {
                    // Suppress so we keep throwing the original exception
                    // in our caller
                }
                pendingOutput = null;
            }
        }
예제 #11
0
        public void FinishCommit(Directory dir)
        {
            if (pendingOutput == null)
                throw new System.Exception("prepareCommit was not called");
            bool success = false;
            try
            {
                pendingOutput.FinishCommit();
                pendingOutput.Close();
                pendingOutput = null;
                success = true;
            }
            finally
            {
                if (!success)
                    RollbackCommit(dir);
            }

            // NOTE: if we crash here, we have left a segments_N
            // file in the directory in a possibly corrupt state (if
            // some bytes made it to stable storage and others
            // didn't).  But, the segments_N file includes checksum
            // at the end, which should catch this case.  So when a
            // reader tries to read it, it will throw a
            // CorruptIndexException, which should cause the retry
            // logic in SegmentInfos to kick in and load the last
            // good (previous) segments_N-1 file.

            String fileName = IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, "", generation);
            success = false;
            try
            {
                dir.Sync(fileName);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    try
                    {
                        dir.DeleteFile(fileName);
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                }
            }

            lastGeneration = generation;

            try
            {
                IndexOutput genOutput = dir.CreateOutput(IndexFileNames.SEGMENTS_GEN);
                try
                {
                    genOutput.WriteInt(FORMAT_LOCKLESS);
                    genOutput.WriteLong(generation);
                    genOutput.WriteLong(generation);
                }
                finally
                {
                    genOutput.Close();
                }
            }
            catch (System.Exception)
            {
                // It's OK if we fail to write this file since it's
                // used only as one of the retry fallbacks.
            }
        }
예제 #12
0
        /// <summary>
        /// NOTE: this method creates a compound file for all files returned by
        /// info.files(). While, generally, this may include separate norms and
        /// deletion files, this SegmentInfo must not reference such files when this
        /// method is called, because they are not allowed within a compound file.
        /// </summary>
        public static ICollection<string> CreateCompoundFile(InfoStream infoStream, Directory directory, CheckAbort checkAbort, SegmentInfo info, IOContext context)
        {
            string fileName = Index.IndexFileNames.SegmentFileName(info.Name, "", Lucene.Net.Index.IndexFileNames.COMPOUND_FILE_EXTENSION);
            if (infoStream.IsEnabled("IW"))
            {
                infoStream.Message("IW", "create compound file " + fileName);
            }
            Debug.Assert(Lucene3xSegmentInfoFormat.GetDocStoreOffset(info) == -1);
            // Now merge all added files
            ICollection<string> files = info.Files;
            CompoundFileDirectory cfsDir = new CompoundFileDirectory(directory, fileName, context, true);
            IOException prior = null;
            try
            {
                foreach (string file in files)
                {
                    directory.Copy(cfsDir, file, file, context);
                    checkAbort.Work(directory.FileLength(file));
                }
            }
            catch (System.IO.IOException ex)
            {
                prior = ex;
            }
            finally
            {
                bool success = false;
                try
                {
                    IOUtils.CloseWhileHandlingException(prior, cfsDir);
                    success = true;
                }
                finally
                {
                    if (!success)
                    {
                        try
                        {
                            directory.DeleteFile(fileName);
                        }
                        catch (Exception)
                        {
                        }
                        try
                        {
                            directory.DeleteFile(Lucene.Net.Index.IndexFileNames.SegmentFileName(info.Name, "", Lucene.Net.Index.IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            // Replace all previous files with the CFS/CFE files:
            HashSet<string> siFiles = new HashSet<string>();
            siFiles.Add(fileName);
            siFiles.Add(Lucene.Net.Index.IndexFileNames.SegmentFileName(info.Name, "", Lucene.Net.Index.IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));
            info.Files = siFiles;

            return files;
        }
예제 #13
0
        public void  Write(Directory directory)
        {
            System.String segmentFileName = GetNextSegmentFileName();

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

            IndexOutput output = directory.CreateOutput(segmentFileName);

            bool success = false;

            try
            {
                output.WriteInt(CURRENT_FORMAT);        // write FORMAT
                output.WriteLong(++version);            // every write changes
                // the index
                output.WriteInt(counter);               // write counter
                output.WriteInt(Count);                 // write infos
                for (int i = 0; i < Count; i++)
                {
                    Info(i).Write(output);
                }
            }
            finally
            {
                try
                {
                    output.Close();
                    success = true;
                }
                finally
                {
                    if (!success)
                    {
                        // Try not to leave a truncated segments_N file in
                        // the index:
                        directory.DeleteFile(segmentFileName);
                    }
                }
            }

            try
            {
                output = directory.CreateOutput(IndexFileNames.SEGMENTS_GEN);
                try
                {
                    output.WriteInt(FORMAT_LOCKLESS);
                    output.WriteLong(generation);
                    output.WriteLong(generation);
                }
                finally
                {
                    output.Close();
                }
            }
            catch (System.IO.IOException e)
            {
                // It's OK if we fail to write this file since it's
                // used only as one of the retry fallbacks.
            }

            lastGeneration = generation;
        }
예제 #14
0
		private void  DeleteFiles(System.Collections.ArrayList files, Directory directory)
		{
			for (int i = 0; i < files.Count; i++)
				directory.DeleteFile((System.String) files[i]);
		}
예제 #15
0
        internal void  FinishCommit(Directory dir)
        {
            if (pendingSegnOutput == null)
            {
                throw new System.SystemException("prepareCommit was not called");
            }
            bool success = false;

            try
            {
                pendingSegnOutput.FinishCommit();
                pendingSegnOutput.Close();
                pendingSegnOutput = null;
                success           = true;
            }
            finally
            {
                if (!success)
                {
                    RollbackCommit(dir);
                }
            }

            // NOTE: if we crash here, we have left a segments_N
            // file in the directory in a possibly corrupt state (if
            // some bytes made it to stable storage and others
            // didn't).  But, the segments_N file includes checksum
            // at the end, which should catch this case.  So when a
            // reader tries to read it, it will throw a
            // CorruptIndexException, which should cause the retry
            // logic in SegmentInfos to kick in and load the last
            // good (previous) segments_N-1 file.

            System.String fileName = IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, "", generation);
            success = false;
            try
            {
                dir.Sync(fileName);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    try
                    {
                        dir.DeleteFile(fileName);
                    }
                    catch (System.Exception t)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                }
            }

            lastGeneration = generation;

            try
            {
                IndexOutput genOutput = dir.CreateOutput(IndexFileNames.SEGMENTS_GEN);
                try
                {
                    genOutput.WriteInt(FORMAT_LOCKLESS);
                    genOutput.WriteLong(generation);
                    genOutput.WriteLong(generation);
                }
                finally
                {
                    genOutput.Close();
                }
            }
            catch (System.Exception t)
            {
                // It's OK if we fail to write this file since it's
                // used only as one of the retry fallbacks.
            }
        }
예제 #16
0
		public void Optimize(Directory directory)
		{
			string[] files = directory.List();

			System.Collections.ArrayList segment_names = new System.Collections.ArrayList();
			foreach (SegmentInfo si in this)
				segment_names.Add (si.name);

			foreach (string file in files) {
				string basename = System.IO.Path.GetFileNameWithoutExtension (file);
				if (segment_names.Contains (basename))
					continue;

				if (basename == IndexFileNames.DELETABLE || basename == IndexFileNames.SEGMENTS)
					continue;

				Console.WriteLine ("WARNING! Deleting stale data {0}", file);
				directory.DeleteFile (file);
			}
		}
예제 #17
0
		public void  Write(Directory directory)
		{
			
			System.String segmentFileName = GetNextSegmentFileName();
			
			// Always advance the generation on write:
			if (generation == - 1)
			{
				generation = 1;
			}
			else
			{
				generation++;
			}
			
			IndexOutput output = directory.CreateOutput(segmentFileName);
			
			bool success = false;
			
			try
			{
				output.WriteInt(CURRENT_FORMAT); // write FORMAT
				output.WriteLong(++version); // every write changes
				// the index
				output.WriteInt(counter); // write counter
				output.WriteInt(Count); // write infos
				for (int i = 0; i < Count; i++)
				{
					Info(i).Write(output);
				}
			}
			finally
			{
				try
				{
					output.Close();
					success = true;
				}
				finally
				{
					if (!success)
					{
						// Try not to leave a truncated segments_N file in
						// the index:
						directory.DeleteFile(segmentFileName);
					}
				}
			}
			
			try
			{
				output = directory.CreateOutput(IndexFileNames.SEGMENTS_GEN);
				try
				{
					output.WriteInt(FORMAT_LOCKLESS);
					output.WriteLong(generation);
					output.WriteLong(generation);
				}
				finally
				{
					output.Close();
				}
			}
			catch (System.IO.IOException e)
			{
				// It's OK if we fail to write this file since it's
				// used only as one of the retry fallbacks.
			}
			
			lastGeneration = generation;
		}
예제 #18
0
		public void Optimize(Directory directory)
		{
			string[] files = directory.List();

			System.Collections.ArrayList segment_names = new System.Collections.ArrayList();

			foreach (SegmentInfo si in this)
				segment_names.Add (si.name);

			foreach (string file in files) {
				string basename = System.IO.Path.GetFileNameWithoutExtension (file);
				if (segment_names.Contains (basename))
					continue;

				// Allowed files deletable, segments, segments.gen, segments_N
				if (basename == IndexFileNames.DELETABLE || basename.StartsWith (IndexFileNames.SEGMENTS))
					continue;

				Console.WriteLine ("WARNING! Deleting stale data {0}", file);
				try {
					directory.DeleteFile (file);
				} catch { /* Could be already deleted. */ }
			}
		}
예제 #19
0
        internal FieldsWriter(Directory d, System.String segment, FieldInfos fn)
        {
            fieldInfos = fn;

            bool   success    = false;
            String fieldsName = segment + "." + IndexFileNames.FIELDS_EXTENSION;

            try
            {
                fieldsStream = d.CreateOutput(fieldsName);
                fieldsStream.WriteInt(FORMAT_CURRENT);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    try
                    {
                        Dispose();
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                    try
                    {
                        d.DeleteFile(fieldsName);
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                }
            }

            success = false;
            String indexName = segment + "." + IndexFileNames.FIELDS_INDEX_EXTENSION;

            try
            {
                indexStream = d.CreateOutput(indexName);
                indexStream.WriteInt(FORMAT_CURRENT);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    try
                    {
                        Dispose();
                    }
                    catch (System.IO.IOException)
                    {
                    }
                    try
                    {
                        d.DeleteFile(fieldsName);
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                    try
                    {
                        d.DeleteFile(indexName);
                    }
                    catch (System.Exception)
                    {
                        // Suppress so we keep throwing the original exception
                    }
                }
            }

            doClose = true;
        }
예제 #20
0
		internal FieldsWriter(Directory d, System.String segment, FieldInfos fn)
		{
			fieldInfos = fn;
			
			bool success = false;
			System.String fieldsName = segment + "." + IndexFileNames.FIELDS_EXTENSION;
			try
			{
				fieldsStream = d.CreateOutput(fieldsName);
				fieldsStream.WriteInt(FORMAT_CURRENT);
				success = true;
			}
			finally
			{
				if (!success)
				{
					try
					{
						Close();
					}
					catch (System.Exception t)
					{
						// Suppress so we keep throwing the original exception
					}
					try
					{
						d.DeleteFile(fieldsName);
					}
					catch (System.Exception t)
					{
						// Suppress so we keep throwing the original exception
					}
				}
			}
			
			success = false;
			System.String indexName = segment + "." + IndexFileNames.FIELDS_INDEX_EXTENSION;
			try
			{
				indexStream = d.CreateOutput(indexName);
				indexStream.WriteInt(FORMAT_CURRENT);
				success = true;
			}
			finally
			{
				if (!success)
				{
					try
					{
						Close();
					}
					catch (System.IO.IOException ioe)
					{
					}
					try
					{
						d.DeleteFile(fieldsName);
					}
					catch (System.Exception t)
					{
						// Suppress so we keep throwing the original exception
					}
					try
					{
						d.DeleteFile(indexName);
					}
					catch (System.Exception t)
					{
						// Suppress so we keep throwing the original exception
					}
				}
			}
			
			doClose = true;
		}
예제 #21
0
        private void Initialize(Directory directory, string segment, FieldInfos fis, int interval, bool isi)
        {
            IndexInterval = interval;
            FieldInfos = fis;
            IsIndex = isi;
            Output = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", (IsIndex ? Lucene3xPostingsFormat.TERMS_INDEX_EXTENSION : Lucene3xPostingsFormat.TERMS_EXTENSION)), IOContext.DEFAULT);
            bool success = false;
            try
            {
                Output.WriteInt(FORMAT_CURRENT); // write format
                Output.WriteLong(0); // leave space for size
                Output.WriteInt(IndexInterval); // write indexInterval
                Output.WriteInt(SkipInterval); // write skipInterval
                Output.WriteInt(MaxSkipLevels); // write maxSkipLevels
                Debug.Assert(InitUTF16Results());
                success = true;
            }
            finally
            {
                if (!success)
                {
                    IOUtils.CloseWhileHandlingException(Output);

                    try
                    {
                        directory.DeleteFile(IndexFileNames.SegmentFileName(segment, "", (IsIndex ? Lucene3xPostingsFormat.TERMS_INDEX_EXTENSION : Lucene3xPostingsFormat.TERMS_EXTENSION)));
                    }
                    catch (IOException ignored)
                    {
                    }
                }
            }
        }
예제 #22
0
        internal TermInfosWriter(Directory directory, string segment, FieldInfos fis, int interval)
        {
            Initialize(directory, segment, fis, interval, false);
            bool success = false;
            try
            {
                Other = new TermInfosWriter(directory, segment, fis, interval, true);
                Other.Other = this;
                success = true;
            }
            finally
            {
                if (!success)
                {
                    IOUtils.CloseWhileHandlingException(Output);

                    try
                    {
                        directory.DeleteFile(IndexFileNames.SegmentFileName(segment, "", (IsIndex ? Lucene3xPostingsFormat.TERMS_INDEX_EXTENSION : Lucene3xPostingsFormat.TERMS_EXTENSION)));
                    }
                    catch (IOException ignored)
                    {
                    }
                }
            }
        }