Example #1
0
        /// <summary> Tries to acquire the WriteLock on this directory.
        /// this method is only valid if this IndexReader is directory owner.
        ///
        /// </summary>
        /// <throws>  IOException If WriteLock cannot be acquired. </throws>
        private void  AquireWriteLock()
        {
            if (stale)
            {
                throw new System.IO.IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");
            }

            if (this.writeLock == null)
            {
                Lock writeLock = directory.MakeLock(IndexWriter.WRITE_LOCK_NAME);
                if (!writeLock.Obtain(IndexWriter.WRITE_LOCK_TIMEOUT))
                // obtain write lock
                {
                    throw new System.IO.IOException("Index locked for write: " + writeLock);
                }
                this.writeLock = writeLock;

                // we have to check whether index has changed since this reader was opened.
                // if so, this reader is no longer valid for deletion
                if (SegmentInfos.ReadCurrentVersion(directory) > segmentInfos.GetVersion())
                {
                    stale = true;
                    this.writeLock.Release();
                    this.writeLock = null;
                    throw new System.IO.IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");
                }
            }
        }
Example #2
0
        private IndexWriter(Directory d, Analyzer a, bool create, bool closeDir)
        {
            InitBlock();
            this.closeDir = closeDir;
            directory     = d;
            analyzer      = a;

            Lock writeLock = directory.MakeLock(IndexWriter.WRITE_LOCK_NAME);

            if (!writeLock.Obtain(writeLockTimeout))
            // obtain write lock
            {
                throw new System.IO.IOException("Index locked for write: " + writeLock);
            }
            this.writeLock = writeLock;             // save it

            lock (directory)
            {
                // in- & inter-process sync
                new AnonymousClassWith(create, this, directory.MakeLock(IndexWriter.COMMIT_LOCK_NAME), commitLockTimeout).Run();
            }
        }
Example #3
0
        /// <summary> Check whether this IndexReader still works on a current version of the index.
        /// If this is not the case you will need to re-open the IndexReader to
        /// make sure you see the latest changes made to the index.
        ///
        /// </summary>
        /// <throws>  IOException </throws>
        public virtual bool IsCurrent()
        {
            lock (directory)
            {
                // in- & inter-process sync
                Lock commitLock = directory.MakeLock(IndexWriter.COMMIT_LOCK_NAME);

                bool locked = false;

                try
                {
                    locked = commitLock.Obtain(IndexWriter.COMMIT_LOCK_TIMEOUT);

                    return(SegmentInfos.ReadCurrentVersion(directory) == segmentInfos.GetVersion());
                }
                finally
                {
                    if (locked)
                    {
                        commitLock.Release();
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Constructs a new IndexWriter per the settings given in <code>conf</code>.
        /// If you want to make "live" changes to this writer instance, use
        /// <seealso cref="#getConfig()"/>.
        ///
        /// <p>
        /// <b>NOTE:</b> after ths writer is created, the given configuration instance
        /// cannot be passed to another writer. If you intend to do so, you should
        /// <seealso cref="IndexWriterConfig#clone() clone"/> it beforehand.
        /// </summary>
        /// <param name="d">
        ///          the index directory. The index is either created or appended
        ///          according <code>conf.getOpenMode()</code>. </param>
        /// <param name="conf">
        ///          the configuration settings according to which IndexWriter should
        ///          be initialized. </param>
        /// <exception cref="IOException">
        ///           if the directory cannot be read/written to, or if it does not
        ///           exist and <code>conf.getOpenMode()</code> is
        ///           <code>OpenMode.APPEND</code> or if there is any other low-level
        ///           IO error </exception>
        public IndexWriter(Directory d, IndexWriterConfig conf)
        {
            /*if (!InstanceFieldsInitialized)
            {
                InitializeInstanceFields();
                InstanceFieldsInitialized = true;
            }*/
            readerPool = new ReaderPool(this);
            conf.SetIndexWriter(this); // prevent reuse by other instances
            Config_Renamed = new LiveIndexWriterConfig(conf);
            directory = d;
            analyzer = Config_Renamed.Analyzer;
            infoStream = Config_Renamed.InfoStream;
            mergePolicy = Config_Renamed.MergePolicy;
            mergePolicy.IndexWriter = this;
            mergeScheduler = Config_Renamed.MergeScheduler;
            Codec = Config_Renamed.Codec;

            BufferedUpdatesStream = new BufferedUpdatesStream(infoStream);
            PoolReaders = Config_Renamed.ReaderPooling;

            WriteLock = directory.MakeLock(WRITE_LOCK_NAME);

            if (!WriteLock.Obtain(Config_Renamed.WriteLockTimeout)) // obtain write lock
            {
                throw new LockObtainFailedException("Index locked for write: " + WriteLock);
            }

            bool success = false;
            try
            {
                OpenMode_e? mode = Config_Renamed.OpenMode;
                bool create;
                if (mode == OpenMode_e.CREATE)
                {
                    create = true;
                }
                else if (mode == OpenMode_e.APPEND)
                {
                    create = false;
                }
                else
                {
                    // CREATE_OR_APPEND - create only if an index does not exist
                    create = !DirectoryReader.IndexExists(directory);
                }

                // If index is too old, reading the segments will throw
                // IndexFormatTooOldException.
                segmentInfos = new SegmentInfos();

                bool initialIndexExists = true;

                if (create)
                {
                    // Try to read first.  this is to allow create
                    // against an index that's currently open for
                    // searching.  In this case we write the next
                    // segments_N file with no segments:
                    try
                    {
                        segmentInfos.Read(directory);
                        segmentInfos.Clear();
                    }
                    catch (IOException)
                    {
                        // Likely this means it's a fresh directory
                        initialIndexExists = false;
                    }

                    // Record that we have a change (zero out all
                    // segments) pending:
                    Changed();
                }
                else
                {
                    segmentInfos.Read(directory);

                    IndexCommit commit = Config_Renamed.IndexCommit;
                    if (commit != null)
                    {
                        // Swap out all segments, but, keep metadata in
                        // SegmentInfos, like version & generation, to
                        // preserve write-once.  this is important if
                        // readers are open against the future commit
                        // points.
                        if (commit.Directory != directory)
                        {
                            throw new ArgumentException(string.Format("IndexCommit's directory doesn't match my directory (mine: {0}, commit's: {1})", directory, commit.Directory));
                        }
                        SegmentInfos oldInfos = new SegmentInfos();
                        oldInfos.Read(directory, commit.SegmentsFileName);
                        segmentInfos.Replace(oldInfos);
                        Changed();
                        if (infoStream.IsEnabled("IW"))
                        {
                            infoStream.Message("IW", "init: loaded commit \"" + commit.SegmentsFileName + "\"");
                        }
                    }
                }

                RollbackSegments = segmentInfos.CreateBackupSegmentInfos();

                // start with previous field numbers, but new FieldInfos
                GlobalFieldNumberMap = FieldNumberMap;
                Config_Renamed.FlushPolicy.Init(Config_Renamed);
                DocWriter = new DocumentsWriter(this, Config_Renamed, directory);
                eventQueue = DocWriter.EventQueue();

                // Default deleter (for backwards compatibility) is
                // KeepOnlyLastCommitDeleter:
                lock (this)
                {
                    Deleter = new IndexFileDeleter(directory, Config_Renamed.DelPolicy, segmentInfos, infoStream, this, initialIndexExists);
                }

                if (Deleter.StartingCommitDeleted)
                {
                    // Deletion policy deleted the "head" commit point.
                    // We have to mark ourself as changed so that if we
                    // are closed w/o any further changes we write a new
                    // segments_N file.
                    Changed();
                }

                if (infoStream.IsEnabled("IW"))
                {
                    infoStream.Message("IW", "init: create=" + create);
                    MessageState();
                }

                success = true;
            }
            finally
            {
                if (!success)
                {
                    if (infoStream.IsEnabled("IW"))
                    {
                        infoStream.Message("IW", "init: hit exception on init; releasing write lock");
                    }
                    WriteLock.Release();
                    IOUtils.CloseWhileHandlingException(WriteLock);
                    WriteLock = null;
                }
            }
        }