private IndexWriter InitIndex(Func <IConcurrentMergeScheduler> newScheduler, Random random, MockDirectoryWrapper dir, bool initialCommit) { dir.SetLockFactory(NoLockFactory.GetNoLockFactory()); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)) .SetMaxBufferedDocs(10) .SetMergeScheduler(newScheduler())); IConcurrentMergeScheduler scheduler = writer.Config.MergeScheduler as IConcurrentMergeScheduler; if (scheduler != null) { scheduler.SetSuppressExceptions(); } if (initialCommit) { writer.Commit(); } Document doc = new Document(); doc.Add(NewTextField("content", "aaa", Field.Store.NO)); doc.Add(NewTextField("id", "0", Field.Store.NO)); for (int i = 0; i < 157; i++) { writer.AddDocument(doc); } return(writer); }
public virtual void TestCustomLockFactory() { Directory dir = new MockDirectoryWrapper(Random(), new RAMDirectory()); MockLockFactory lf = new MockLockFactory(this); dir.SetLockFactory(lf); // Lock prefix should have been set: Assert.IsTrue(lf.LockPrefixSet, "lock prefix was not set by the RAMDirectory"); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); // add 100 documents (so that commit lock is used) for (int i = 0; i < 100; i++) { AddDoc(writer); } // Both write lock and commit lock should have been created: Assert.AreEqual(1, lf.LocksCreated.Count, "# of unique locks created (after instantiating IndexWriter)"); Assert.IsTrue(lf.MakeLockCount >= 1, "# calls to makeLock is 0 (after instantiating IndexWriter)"); foreach (String lockName in lf.LocksCreated.Keys) { MockLockFactory.MockLock @lock = (MockLockFactory.MockLock)lf.LocksCreated[lockName]; Assert.IsTrue(@lock.LockAttempts > 0, "# calls to Lock.obtain is 0 (after instantiating IndexWriter)"); } writer.Dispose(); }
public virtual void TestRAMDirectoryNoLocking() { MockDirectoryWrapper dir = new MockDirectoryWrapper(Random(), new RAMDirectory()); dir.SetLockFactory(NoLockFactory.GetNoLockFactory()); dir.WrapLockFactory = false; // we are gonna explicitly test we get this back Assert.IsTrue(typeof(NoLockFactory).IsInstanceOfType(dir.LockFactory), "RAMDirectory.setLockFactory did not take"); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); writer.Commit(); // required so the second open succeed // Create a 2nd IndexWriter. this is normally not allowed but it should run through since we're not // using any locks: IndexWriter writer2 = null; try { writer2 = new IndexWriter(dir, (new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))).SetOpenMode(OpenMode.APPEND)); } catch (Exception e) { Console.Out.Write(e.StackTrace); Assert.Fail("Should not have hit an IOException with no locking"); } writer.Dispose(); if (writer2 != null) { writer2.Dispose(); } }
public virtual void TestFailIfIndexWriterNotClosedChangeLockFactory() { MockDirectoryWrapper dir = NewMockDirectory(); dir.SetLockFactory(new SingleInstanceLockFactory()); IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null)); try { dir.Dispose(); Assert.Fail(); } catch (Exception expected) when(expected.IsException()) { Assert.IsTrue(expected.Message.Contains("there are still open locks")); } iw.Dispose(); dir.Dispose(); }