Commit() private method

Writes & syncs to the Directory dir, taking care to remove the segments file on exception

Note: #changed() should be called prior to this method if changes have been made to this SegmentInfos instance

private Commit ( Directory dir ) : void
dir Directory
return void
Beispiel #1
0
        public virtual void Split(DirectoryInfo destDir, string[] segs)
        {
            destDir.Create();
            FSDirectory  destFSDir = FSDirectory.Open(destDir);
            SegmentInfos destInfos = new SegmentInfos();

            destInfos.Counter = Infos.Counter;
            foreach (string n in segs)
            {
                SegmentCommitInfo infoPerCommit = GetInfo(n);
                SegmentInfo       info          = infoPerCommit.Info;
                // Same info just changing the dir:
                SegmentInfo newInfo = new SegmentInfo(destFSDir, info.Version, info.Name, info.DocCount, info.UseCompoundFile, info.Codec, info.Diagnostics);
                destInfos.Add(new SegmentCommitInfo(newInfo, infoPerCommit.DelCount, infoPerCommit.DelGen, infoPerCommit.FieldInfosGen));
                // now copy files over
                ICollection <string> files = infoPerCommit.GetFiles();
                foreach (string srcName in files)
                {
                    FileInfo srcFile  = new FileInfo(Path.Combine(dir.FullName, srcName));
                    FileInfo destFile = new FileInfo(Path.Combine(destDir.FullName, srcName));
                    CopyFile(srcFile, destFile);
                }
            }
            destInfos.Changed();
            destInfos.Commit(destFSDir);
            // Console.WriteLine("destDir:"+destDir.getAbsolutePath());
        }
Beispiel #2
0
 public virtual void Remove(string[] segs)
 {
     foreach (string n in segs)
     {
         int idx = GetIdx(n);
         infos.Remove(idx);
     }
     infos.Changed();
     infos.Commit(fsDir);
 }
        public virtual void  DoTestUndeleteAll()
        {
            sis.Read(dir);
            IndexReader reader = OpenReader();

            Assert.IsTrue(reader != null);
            Assert.AreEqual(2, reader.NumDocs());
            reader.DeleteDocument(0);
            Assert.AreEqual(1, reader.NumDocs());
            reader.UndeleteAll();
            Assert.AreEqual(2, reader.NumDocs());

            // Ensure undeleteAll survives commit/close/reopen:
            reader.Commit();
            reader.Close();

            if (reader is MultiReader)
            {
                // MultiReader does not "own" the directory so it does
                // not write the changes to sis on commit:
                sis.Commit(dir);
            }

            sis.Read(dir);
            reader = OpenReader();
            Assert.AreEqual(2, reader.NumDocs());

            reader.DeleteDocument(0);
            Assert.AreEqual(1, reader.NumDocs());
            reader.Commit();
            reader.Close();
            if (reader is MultiReader)
            {
                // MultiReader does not "own" the directory so it does
                // not write the changes to sis on commit:
                sis.Commit(dir);
            }
            sis.Read(dir);
            reader = OpenReader();
            Assert.AreEqual(1, reader.NumDocs());
        }
 public virtual void Split(DirectoryInfo destDir, string[] segs)
 {
     destDir.Create();
     FSDirectory destFSDir = FSDirectory.Open(destDir);
     SegmentInfos destInfos = new SegmentInfos();
     destInfos.Counter = infos.Counter;
     foreach (string n in segs)
     {
         SegmentCommitInfo infoPerCommit = GetInfo(n);
         SegmentInfo info = infoPerCommit.Info;
         // Same info just changing the dir:
         SegmentInfo newInfo = new SegmentInfo(destFSDir, info.Version, info.Name, info.DocCount, info.UseCompoundFile, info.Codec, info.Diagnostics);
         destInfos.Add(new SegmentCommitInfo(newInfo, infoPerCommit.DelCount, infoPerCommit.DelGen, infoPerCommit.FieldInfosGen));
         // now copy files over
         ICollection<string> files = infoPerCommit.Files();
         foreach (string srcName in files)
         {
             FileInfo srcFile = new FileInfo(Path.Combine(dir.FullName, srcName));
             FileInfo destFile = new FileInfo(Path.Combine(destDir.FullName, srcName));
             CopyFile(srcFile, destFile);
         }
     }
     destInfos.Changed();
     destInfos.Commit(destFSDir);
     // Console.WriteLine("destDir:"+destDir.getAbsolutePath());
 }
        /// <summary> Commit changes resulting from delete, undeleteAll, or
        /// setNorm operations
        ///
        /// If an exception is hit, then either no changes or all
        /// changes will have been committed to the index
        /// (transactional semantics).
        /// </summary>
        /// <throws>  IOException if there is a low-level IO error </throws>
        protected internal override void DoCommit()
        {
            if (hasChanges)
            {
                if (segmentInfos != null)
                {
                    // Default deleter (for backwards compatibility) is
                    // KeepOnlyLastCommitDeleter:
                    IndexFileDeleter deleter = new IndexFileDeleter(directory, deletionPolicy == null ? new KeepOnlyLastCommitDeletionPolicy() : deletionPolicy, segmentInfos, null, null);

                    // Checkpoint the state we are about to change, in
                    // case we have to roll back:
                    StartCommit();

                    bool success = false;
                    try
                    {
                        CommitChanges();

                        // sync all the files we just wrote
                        for (int i = 0; i < segmentInfos.Count; i++)
                        {
                            SegmentInfo    info  = segmentInfos.Info(i);
                            IList <string> files = info.Files();
                            for (int j = 0; j < files.Count; j++)
                            {
                                string fileName = files[j];
                                if (!synced.ContainsKey(fileName))
                                {
                                    System.Diagnostics.Debug.Assert(directory.FileExists(fileName));
                                    directory.Sync(fileName);
                                    synced[fileName] = fileName;
                                }
                            }
                        }

                        segmentInfos.Commit(directory);
                        success = true;
                    }
                    finally
                    {
                        if (!success)
                        {
                            // Rollback changes that were made to
                            // SegmentInfos but failed to get [fully]
                            // committed.  This way this reader instance
                            // remains consistent (matched to what's
                            // actually in the index):
                            RollbackCommit();

                            // Recompute deletable files & remove them (so
                            // partially written .del files, etc, are
                            // removed):
                            deleter.Refresh();
                        }
                    }

                    // Have the deleter remove any now unreferenced
                    // files due to this commit:
                    deleter.Checkpoint(segmentInfos, true);

                    if (writeLock != null)
                    {
                        writeLock.Release(); // release write lock
                        writeLock = null;
                    }
                }
                else
                {
                    CommitChanges();
                }
            }
            hasChanges = false;
        }