You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by di...@apache.org on 2011/03/17 11:57:59 UTC
[Lucene.Net] svn commit: r1082446 [3/3] - in /incubator/lucene.net/trunk/C#/src:
Lucene.Net/Index/ Lucene.Net/Store/ Test/Index/ Test/Store/
Modified: incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterDelete.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Index/TestIndexWriterDelete.cs?rev=1082446&r1=1082445&r2=1082446&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterDelete.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterDelete.cs Thu Mar 17 10:57:58 2011
@@ -699,8 +699,11 @@ namespace Lucene.Net.Index
// If the close() succeeded, make sure there are
// no unreferenced files.
- if (success)
- TestIndexWriter.AssertNoUnreferencedFiles(dir, "after writer.close");
+ if (success)
+ {
+ Lucene.Net.Util._TestUtil.CheckIndex(dir);
+ TestIndexWriter.AssertNoUnreferencedFiles(dir, "after writer.close");
+ }
// Finally, verify index is not corrupt, and, if
// we succeeded, we see all docs changed, and if
Modified: incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterExceptions.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Index/TestIndexWriterExceptions.cs?rev=1082446&r1=1082445&r2=1082446&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterExceptions.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterExceptions.cs Thu Mar 17 10:57:58 2011
@@ -22,6 +22,7 @@ using NUnit.Framework;
using Analyzer = Lucene.Net.Analysis.Analyzer;
using WhitespaceAnalyzer = Lucene.Net.Analysis.WhitespaceAnalyzer;
using Document = Lucene.Net.Documents.Document;
+using TermVector = Lucene.Net.Documents.Field.TermVector;
using Field = Lucene.Net.Documents.Field;
using Directory = Lucene.Net.Store.Directory;
using MockRAMDirectory = Lucene.Net.Store.MockRAMDirectory;
@@ -34,8 +35,18 @@ namespace Lucene.Net.Index
[TestFixture]
public class TestIndexWriterExceptions:LuceneTestCase
{
-
+ Random random;
private const bool DEBUG = false;
+
+ static TermVector[] tvSettings = new TermVector[] {
+ TermVector.NO, TermVector.YES, TermVector.WITH_OFFSETS,
+ TermVector.WITH_POSITIONS, TermVector.WITH_POSITIONS_OFFSETS
+ };
+
+ private TermVector RandomTVSetting(Random random)
+ {
+ return tvSettings[random.Next(tvSettings.Length)];
+ }
private class IndexerThread:SupportClass.ThreadClass
{
@@ -69,18 +80,18 @@ namespace Lucene.Net.Index
{
Document doc = new Document();
-
- doc.Add(new Field("content1", "aaa bbb ccc ddd", Field.Store.YES, Field.Index.ANALYZED));
- doc.Add(new Field("content6", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
- doc.Add(new Field("content2", "aaa bbb ccc ddd", Field.Store.YES, Field.Index.NOT_ANALYZED));
+
+ doc.Add(new Field("content1", "aaa bbb ccc ddd", Field.Store.YES, Field.Index.ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random)));
+ doc.Add(new Field("content6", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random)));
+ doc.Add(new Field("content2", "aaa bbb ccc ddd", Field.Store.YES, Field.Index.NOT_ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random)));
doc.Add(new Field("content3", "aaa bbb ccc ddd", Field.Store.YES, Field.Index.NO));
-
- doc.Add(new Field("content4", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.ANALYZED));
- doc.Add(new Field("content5", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.NOT_ANALYZED));
-
- doc.Add(new Field("content7", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
-
- Field idField = new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
+
+ doc.Add(new Field("content4", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random)));
+ doc.Add(new Field("content5", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.NOT_ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random)));
+
+ doc.Add(new Field("content7", "aaa bbb ccc ddd", Field.Store.NO, Field.Index.NOT_ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random)));
+
+ Field idField = new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED, enclosingInstance.RandomTVSetting(enclosingInstance.random));
doc.Add(idField);
long stopTime = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) + 3000;
@@ -99,7 +110,7 @@ namespace Lucene.Net.Index
{
if (Lucene.Net.Index.TestIndexWriterExceptions.DEBUG)
{
- System.Console.Out.WriteLine("EXC: ");
+ System.Console.Out.WriteLine(SupportClass.ThreadClass.CurrentThread().Name + ": EXC: ");
System.Console.Out.WriteLine(re.StackTrace);
}
try
@@ -185,7 +196,7 @@ namespace Lucene.Net.Index
public virtual void TestRandomExceptions()
{
MockRAMDirectory dir = new MockRAMDirectory();
-
+ random = new Random((int)(DateTime.Now.Ticks&0x7fffffff));
MockIndexWriter writer = new MockIndexWriter(this, dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
((ConcurrentMergeScheduler) writer.GetMergeScheduler()).SetSuppressExceptions();
//writer.setMaxBufferedDocs(10);
@@ -233,7 +244,7 @@ namespace Lucene.Net.Index
[Test]
public virtual void TestRandomExceptionsThreads()
{
-
+ random = new Random((int)(DateTime.Now.Ticks & 0x7fffffff));
MockRAMDirectory dir = new MockRAMDirectory();
MockIndexWriter writer = new MockIndexWriter(this, dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
((ConcurrentMergeScheduler) writer.GetMergeScheduler()).SetSuppressExceptions();
Modified: incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterMergePolicy.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Index/TestIndexWriterMergePolicy.cs?rev=1082446&r1=1082445&r2=1082446&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterMergePolicy.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterMergePolicy.cs Thu Mar 17 10:57:58 2011
@@ -234,7 +234,7 @@ namespace Lucene.Net.Index
private void CheckInvariants(IndexWriter writer)
{
- _TestUtil.SyncConcurrentMerges(writer);
+ writer.WaitForMerges();
int maxBufferedDocs = writer.GetMaxBufferedDocs();
int mergeFactor = writer.GetMergeFactor();
int maxMergeDocs = writer.GetMaxMergeDocs();
@@ -276,28 +276,6 @@ namespace Lucene.Net.Index
{
Assert.IsTrue(numSegments < mergeFactor);
}
-
- System.String[] files = writer.GetDirectory().ListAll();
- int segmentCfsCount = 0;
- for (int i = 0; i < files.Length; i++)
- {
- if (files[i].EndsWith(".cfs"))
- {
- segmentCfsCount++;
- }
- }
- Assert.AreEqual(segmentCount, segmentCfsCount);
- }
-
- /*
- private void printSegmentDocCounts(IndexWriter writer) {
- int segmentCount = writer.getSegmentCount();
- System.out.println("" + segmentCount + " segments total");
- for (int i = 0; i < segmentCount; i++) {
- System.out.println(" segment " + i + " has " + writer.getDocCount(i)
- + " docs");
- }
}
- */
}
}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterReader.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Index/TestIndexWriterReader.cs?rev=1082446&r1=1082445&r2=1082446&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterReader.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Index/TestIndexWriterReader.cs Thu Mar 17 10:57:58 2011
@@ -116,13 +116,13 @@ namespace Lucene.Net.Index
{
try
{
- for (int i = 0; i < 10; i++)
+ for (int docUpto = 0; docUpto < 10; docUpto++)
{
- writer.AddDocument(Lucene.Net.Index.TestIndexWriterReader.CreateDocument(10 * count + i, "test", 4));
+ writer.AddDocument(Lucene.Net.Index.TestIndexWriterReader.CreateDocument(10 * count + docUpto, "test", 4));
}
count++;
int limit = count * 10;
- for (int i = 0; i < 5; i++)
+ for (int delUpto = 0; delUpto < 5; delUpto++)
{
int x = r.Next(limit);
writer.DeleteDocuments(new Term("field3", "b" + x));
@@ -274,7 +274,7 @@ namespace Lucene.Net.Index
Assert.IsTrue(r1.IsCurrent());
writer.Commit();
- Assert.IsTrue(r1.IsCurrent());
+ Assert.IsFalse(r1.IsCurrent());
Assert.AreEqual(200, r1.MaxDoc());
@@ -333,6 +333,7 @@ namespace Lucene.Net.Index
Directory dir1 = new MockRAMDirectory();
IndexWriter writer = new IndexWriter(dir1, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
+ writer.SetReaderTermsIndexDivisor(2);
writer.SetInfoStream(infoStream);
// create the index
CreateIndexNoClose(!optimize, "index1", writer);
@@ -853,7 +854,7 @@ namespace Lucene.Net.Index
writer.SetMergeFactor(2);
writer.SetMaxBufferedDocs(2);
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 100; i++)
{
writer.AddDocument(CreateDocument(i, "test", 4));
}
@@ -944,7 +945,7 @@ namespace Lucene.Net.Index
[Test]
public virtual void TestDuringAddIndexes()
{
- Directory dir1 = new MockRAMDirectory();
+ MockRAMDirectory dir1 = new MockRAMDirectory();
IndexWriter writer = new IndexWriter(dir1, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
writer.SetInfoStream(infoStream);
writer.SetMergeFactor(2);
@@ -996,10 +997,12 @@ namespace Lucene.Net.Index
}
Assert.AreEqual(0, excs.Count);
+ r.Close();
+ Assert.AreEqual(0, dir1.GetOpenDeletedFiles().Count);
writer.Close();
_TestUtil.CheckIndex(dir1);
- r.Close();
+
dir1.Close();
}
@@ -1116,5 +1119,35 @@ namespace Lucene.Net.Index
w.Close();
dir.Close();
}
+
+ class AnonymousIndexReaderWarmer : IndexWriter.IndexReaderWarmer
+ {
+ public override void Warm(IndexReader r)
+ {
+ IndexSearcher s = new IndexSearcher(r);
+ Lucene.Net.Search.TopDocs hits = s.Search(new TermQuery(new Term("foo", "bar")), 10);
+ Assert.AreEqual(20, hits.totalHits);
+ }
+ }
+
+ [Test]
+ public void TestSegmentWarmer()
+ {
+ Directory dir = new MockRAMDirectory();
+ IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
+ w.SetMaxBufferedDocs(2);
+ w.GetReader().Close();
+ w.SetMergedSegmentWarmer(new AnonymousIndexReaderWarmer());
+
+ Document doc = new Document();
+ doc.Add(new Field("foo", "bar", Field.Store.YES, Field.Index.NOT_ANALYZED));
+ for (int i = 0; i < 20; i++)
+ {
+ w.AddDocument(doc);
+ }
+ w.WaitForMerges();
+ w.Close();
+ dir.Close();
+ }
}
}
\ No newline at end of file
Modified: incubator/lucene.net/trunk/C#/src/Test/Store/MockRAMDirectory.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Store/MockRAMDirectory.cs?rev=1082446&r1=1082445&r2=1082446&view=diff
==============================================================================
--- incubator/lucene.net/trunk/C#/src/Test/Store/MockRAMDirectory.cs (original)
+++ incubator/lucene.net/trunk/C#/src/Test/Store/MockRAMDirectory.cs Thu Mar 17 10:57:58 2011
@@ -21,415 +21,433 @@ using NUnit.Framework;
namespace Lucene.Net.Store
{
-
- /// <summary> This is a subclass of RAMDirectory that adds methods
- /// intended to be used only by unit tests.
- /// </summary>
- /// <version> $Id: RAMDirectory.java 437897 2006-08-29 01:13:10Z yonik $
- /// </version>
-
- [Serializable]
- public class MockRAMDirectory:RAMDirectory
- {
- internal long maxSize;
-
- // Max actual bytes used. This is set by MockRAMOutputStream:
- internal long maxUsedSize;
- internal double randomIOExceptionRate;
- internal System.Random randomState;
- internal bool noDeleteOpenFile = true;
- internal bool preventDoubleWrite = true;
- private System.Collections.Hashtable unSyncedFiles;
- private System.Collections.Hashtable createdFiles;
- internal volatile bool crashed;
-
- // NOTE: we cannot initialize the Map here due to the
- // order in which our constructor actually does this
- // member initialization vs when it calls super. It seems
- // like super is called, then our members are initialized:
- internal System.Collections.IDictionary openFiles;
-
- private void Init()
- {
- lock (this)
- {
- if (openFiles == null)
- {
- openFiles = new System.Collections.Hashtable();
- }
- if (createdFiles == null)
- {
- createdFiles = new System.Collections.Hashtable();
- }
- if (unSyncedFiles == null)
- {
- unSyncedFiles = new System.Collections.Hashtable();
- }
- }
- }
-
- public MockRAMDirectory():base()
- {
- Init();
- }
- public MockRAMDirectory(System.String dir):base(dir)
- {
- Init();
- }
- public MockRAMDirectory(Directory dir):base(dir)
- {
- Init();
- }
- public MockRAMDirectory(System.IO.FileInfo dir):base(dir)
- {
- Init();
- }
-
- /// <summary>If set to true, we throw an IOException if the same
- /// file is opened by createOutput, ever.
- /// </summary>
- public virtual void SetPreventDoubleWrite(bool value_Renamed)
- {
- preventDoubleWrite = value_Renamed;
- }
-
- public override void Sync(System.String name)
- {
- lock (this)
- {
- MaybeThrowDeterministicException();
- if (crashed)
- throw new System.IO.IOException("cannot sync after crash");
- if (unSyncedFiles.Contains(name))
- unSyncedFiles.Remove(name);
- }
- }
-
- /// <summary>Simulates a crash of OS or machine by overwriting
- /// unsynced files.
- /// </summary>
- public virtual void Crash()
- {
- lock (this)
- {
- crashed = true;
- openFiles = new System.Collections.Hashtable();
- System.Collections.IEnumerator it = unSyncedFiles.GetEnumerator();
- unSyncedFiles = new System.Collections.Hashtable();
- int count = 0;
- while (it.MoveNext())
- {
- System.String name = (System.String) ((System.Collections.DictionaryEntry)it.Current).Value;
- RAMFile file = (RAMFile) fileMap_ForNUnit[name];
- if (count % 3 == 0)
- {
- DeleteFile(name, true);
- }
- else if (count % 3 == 1)
- {
- // Zero out file entirely
- int numBuffers = file.NumBuffers();
- for (int i = 0; i < numBuffers; i++)
- {
- byte[] buffer = file.GetBuffer(i);
- for (int j = 0; j < buffer.Length; j++) buffer[j] = (byte) 0;
- }
- }
- else if (count % 3 == 2)
- {
- // Truncate the file:
- file.SetLength(file.GetLength() / 2);
- }
- count++;
- }
- }
- }
-
- public virtual void ClearCrash()
- {
- lock (this)
- {
- crashed = false;
- }
- }
-
- public virtual void SetMaxSizeInBytes(long maxSize)
- {
- this.maxSize = maxSize;
- }
- public virtual long GetMaxSizeInBytes()
- {
- return this.maxSize;
- }
-
- /// <summary> Returns the peek actual storage used (bytes) in this
- /// directory.
- /// </summary>
- public virtual long GetMaxUsedSizeInBytes()
- {
- return this.maxUsedSize;
- }
- public virtual void ResetMaxUsedSizeInBytes()
- {
- this.maxUsedSize = GetRecomputedActualSizeInBytes();
- }
-
- /// <summary> Emulate windows whereby deleting an open file is not
- /// allowed (raise IOException).
- /// </summary>
- public virtual void SetNoDeleteOpenFile(bool value_Renamed)
- {
- this.noDeleteOpenFile = value_Renamed;
- }
- public virtual bool GetNoDeleteOpenFile()
- {
- return noDeleteOpenFile;
- }
-
- /// <summary> If 0.0, no exceptions will be thrown. Else this should
- /// be a double 0.0 - 1.0. We will randomly throw an
- /// IOException on the first write to an OutputStream based
- /// on this probability.
- /// </summary>
- public virtual void SetRandomIOExceptionRate(double rate, long seed)
- {
- randomIOExceptionRate = rate;
- // seed so we have deterministic behaviour:
- randomState = new System.Random((System.Int32) seed);
- }
- public virtual double GetRandomIOExceptionRate()
- {
- return randomIOExceptionRate;
- }
-
- internal virtual void MaybeThrowIOException()
- {
- if (randomIOExceptionRate > 0.0)
- {
- int number = System.Math.Abs(randomState.Next() % 1000);
- if (number < randomIOExceptionRate * 1000)
- {
- throw new System.IO.IOException("a random IOException");
- }
- }
- }
-
- public override void DeleteFile(System.String name)
- {
- lock (this)
- {
- DeleteFile(name, false);
- }
- }
-
- private void DeleteFile(System.String name, bool forced)
- {
- lock (this)
- {
-
- MaybeThrowDeterministicException();
-
- if (crashed && !forced)
- throw new System.IO.IOException("cannot delete after crash");
-
- if (unSyncedFiles.Contains(name))
- unSyncedFiles.Remove(name);
- if (!forced)
- {
- if (noDeleteOpenFile && openFiles.Contains(name))
- {
- throw new System.IO.IOException("MockRAMDirectory: file \"" + name + "\" is still open: cannot delete");
- }
- }
- base.DeleteFile(name);
- }
- }
-
- public override IndexOutput CreateOutput(System.String name)
- {
- lock (this)
- {
- if (crashed)
- throw new System.IO.IOException("cannot createOutput after crash");
- Init();
- if (preventDoubleWrite && createdFiles.Contains(name) && !name.Equals("segments.gen"))
- throw new System.IO.IOException("file \"" + name + "\" was already written to");
- if (noDeleteOpenFile && openFiles.Contains(name))
- throw new System.IO.IOException("MockRAMDirectory: file \"" + name + "\" is still open: cannot overwrite");
- RAMFile file = new RAMFile(this);
- if (crashed)
- throw new System.IO.IOException("cannot createOutput after crash");
- SupportClass.CollectionsHelper.AddIfNotContains(unSyncedFiles, name);
- SupportClass.CollectionsHelper.AddIfNotContains(createdFiles, name);
- RAMFile existing = (RAMFile) fileMap_ForNUnit[name];
- // Enforce write once:
- if (existing != null && !name.Equals("segments.gen") && preventDoubleWrite)
- throw new System.IO.IOException("file " + name + " already exists");
- else
- {
- if (existing != null)
- {
- sizeInBytes_ForNUnitTest -= existing.sizeInBytes_ForNUnit;
- existing.directory_ForNUnit = null;
- }
-
- fileMap_ForNUnit[name] = file;
- }
-
- return new MockRAMOutputStream(this, file, name);
- }
- }
-
- public override IndexInput OpenInput(System.String name)
- {
- lock (this)
- {
- RAMFile file = (RAMFile) fileMap_ForNUnit[name];
- if (file == null)
- throw new System.IO.FileNotFoundException(name);
- else
- {
- if (openFiles.Contains(name))
- {
- System.Int32 v = (System.Int32) openFiles[name];
- v = (System.Int32) (v + 1);
- openFiles[name] = v;
- }
- else
- {
- openFiles[name] = 1;
- }
- }
- return new MockRAMInputStream(this, name, file);
- }
- }
-
- /// <summary>Provided for testing purposes. Use sizeInBytes() instead. </summary>
- public long GetRecomputedSizeInBytes()
- {
- lock (this)
- {
- long size = 0;
- System.Collections.IEnumerator it = fileMap_ForNUnit.Values.GetEnumerator();
- while (it.MoveNext())
- {
- size += ((RAMFile) it.Current).GetSizeInBytes();
- }
- return size;
- }
- }
-
- /// <summary>Like getRecomputedSizeInBytes(), but, uses actual file
- /// lengths rather than buffer allocations (which are
- /// quantized up to nearest
- /// RAMOutputStream.BUFFER_SIZE (now 1024) bytes.
- /// </summary>
-
- public long GetRecomputedActualSizeInBytes()
- {
- lock (this)
- {
- long size = 0;
- System.Collections.IEnumerator it = fileMap_ForNUnit.Values.GetEnumerator();
- while (it.MoveNext())
- {
- size += ((RAMFile) it.Current).length_ForNUnit;
- }
- return size;
- }
- }
-
- public override void Close()
- {
- lock (this)
- {
- if (openFiles == null)
- {
- openFiles = new System.Collections.Hashtable();
- }
- if (noDeleteOpenFile && openFiles.Count > 0)
- {
- // RuntimeException instead of IOException because
- // super() does not throw IOException currently:
- throw new System.SystemException("MockRAMDirectory: cannot close: there are still open files: " + SupportClass.CollectionsHelper.CollectionToString(openFiles));
- }
- }
- }
-
- /// <summary> Objects that represent fail-able conditions. Objects of a derived
- /// class are created and registered with the mock directory. After
- /// register, each object will be invoked once for each first write
- /// of a file, giving the object a chance to throw an IOException.
- /// </summary>
- public class Failure
- {
- /// <summary> eval is called on the first write of every new file.</summary>
- public virtual void Eval(MockRAMDirectory dir)
- {
- }
-
- /// <summary> reset should set the state of the failure to its default
- /// (freshly constructed) state. Reset is convenient for tests
- /// that want to create one failure object and then reuse it in
- /// multiple cases. This, combined with the fact that Failure
- /// subclasses are often anonymous classes makes reset difficult to
- /// do otherwise.
- ///
- /// A typical example of use is
- /// Failure failure = new Failure() { ... };
- /// ...
- /// mock.failOn(failure.reset())
- /// </summary>
- public virtual Failure Reset()
- {
- return this;
- }
-
- protected internal bool doFail;
-
- public virtual void SetDoFail()
- {
- doFail = true;
- }
-
- public virtual void ClearDoFail()
- {
- doFail = false;
- }
- }
-
- internal System.Collections.ArrayList failures;
-
- /// <summary> add a Failure object to the list of objects to be evaluated
- /// at every potential failure point
- /// </summary>
- public virtual void FailOn(Failure fail)
- {
- lock (this)
- {
- if (failures == null)
- {
- failures = new System.Collections.ArrayList();
- }
- failures.Add(fail);
- }
- }
-
- /// <summary> Iterate through the failures list, giving each object a
- /// chance to throw an IOE
- /// </summary>
- internal virtual void MaybeThrowDeterministicException()
- {
- lock (this)
- {
- if (failures != null)
- {
- for (int i = 0; i < failures.Count; i++)
- {
- ((Failure) failures[i]).Eval(this);
- }
- }
- }
- }
- }
+
+ /// <summary> This is a subclass of RAMDirectory that adds methods
+ /// intended to be used only by unit tests.
+ /// </summary>
+ /// <version> $Id: RAMDirectory.java 437897 2006-08-29 01:13:10Z yonik $
+ /// </version>
+
+ [Serializable]
+ public class MockRAMDirectory : RAMDirectory
+ {
+ internal long maxSize;
+
+ // Max actual bytes used. This is set by MockRAMOutputStream:
+ internal long maxUsedSize;
+ internal double randomIOExceptionRate;
+ Random randomState;
+ internal bool noDeleteOpenFile = true;
+ internal bool preventDoubleWrite = true;
+ private System.Collections.Hashtable unSyncedFiles;
+ private System.Collections.Hashtable createdFiles;
+ internal volatile bool crashed;
+
+ // NOTE: we cannot initialize the Map here due to the
+ // order in which our constructor actually does this
+ // member initialization vs when it calls super. It seems
+ // like super is called, then our members are initialized:
+ internal System.Collections.IDictionary openFiles;
+
+ // Only tracked if noDeleteOpenFile is true: if an attempt
+ // is made to delete an open file, we enroll it here.
+ internal System.Collections.Hashtable openFilesDeleted;
+
+ private void Init()
+ {
+ lock (this)
+ {
+ if (openFiles == null)
+ {
+ openFiles = new System.Collections.Hashtable();
+ openFilesDeleted = new System.Collections.Hashtable();
+ }
+
+ if (createdFiles == null)
+ createdFiles = new System.Collections.Hashtable();
+ if (unSyncedFiles == null)
+ unSyncedFiles = new System.Collections.Hashtable();
+ }
+ }
+
+ public MockRAMDirectory()
+ : base()
+ {
+ Init();
+ }
+ public MockRAMDirectory(String dir)
+ : base(dir)
+ {
+ Init();
+ }
+ public MockRAMDirectory(Directory dir)
+ : base(dir)
+ {
+ Init();
+ }
+ public MockRAMDirectory(System.IO.FileInfo dir)
+ : base(dir)
+ {
+ Init();
+ }
+
+ /** If set to true, we throw an IOException if the same
+ * file is opened by createOutput, ever. */
+ public virtual void SetPreventDoubleWrite(bool value)
+ {
+ preventDoubleWrite = value;
+ }
+
+ public void Sync(String name)
+ {
+ lock (this)
+ {
+ MaybeThrowDeterministicException();
+ if (crashed)
+ throw new System.IO.IOException("cannot sync after crash");
+ if (unSyncedFiles.Contains(name))
+ unSyncedFiles.Remove(name);
+ }
+ }
+
+ /** Simulates a crash of OS or machine by overwriting
+ * unsynced files. */
+ public virtual void Crash()
+ {
+ lock (this)
+ {
+ crashed = true;
+ openFiles = new System.Collections.Hashtable();
+ openFilesDeleted = new System.Collections.Hashtable();
+ System.Collections.IEnumerator it = unSyncedFiles.GetEnumerator();
+ unSyncedFiles = new System.Collections.Hashtable();
+ int count = 0;
+ while (it.MoveNext())
+ {
+ String name = (String)it.Current;
+ RAMFile file = (RAMFile)fileMap[name];
+ if (count % 3 == 0)
+ {
+ DeleteFile(name, true);
+ }
+ else if (count % 3 == 1)
+ {
+ // Zero out file entirely
+ int numBuffers = file.NumBuffers();
+ for (int i = 0; i < numBuffers; i++)
+ {
+ byte[] buffer = file.GetBuffer(i);
+ Array.Clear(buffer,0,buffer.Length);
+ }
+ }
+ else if (count % 3 == 2)
+ {
+ // Truncate the file:
+ file.SetLength(file.GetLength() / 2);
+ }
+ count++;
+ }
+ }
+ }
+
+ public virtual void ClearCrash()
+ {
+ lock (this)
+ {
+ crashed = false;
+ }
+ }
+
+ public virtual void SetMaxSizeInBytes(long maxSize)
+ {
+ this.maxSize = maxSize;
+ }
+ public virtual long GetMaxSizeInBytes()
+ {
+ return this.maxSize;
+ }
+
+ /**
+ * Returns the peek actual storage used (bytes) in this
+ * directory.
+ */
+ public virtual long GetMaxUsedSizeInBytes()
+ {
+ return this.maxUsedSize;
+ }
+ public virtual void ResetMaxUsedSizeInBytes()
+ {
+ this.maxUsedSize = GetRecomputedActualSizeInBytes();
+ }
+
+ /**
+ * Emulate windows whereby deleting an open file is not
+ * allowed (raise IOException).
+ */
+ public virtual void SetNoDeleteOpenFile(bool value)
+ {
+ this.noDeleteOpenFile = value;
+ }
+ public bool GetNoDeleteOpenFile()
+ {
+ return noDeleteOpenFile;
+ }
+
+ /**
+ * If 0.0, no exceptions will be thrown. Else this should
+ * be a double 0.0 - 1.0. We will randomly throw an
+ * IOException on the first write to an OutputStream based
+ * on this probability.
+ */
+ public virtual void SetRandomIOExceptionRate(double rate, long seed)
+ {
+ randomIOExceptionRate = rate;
+ // seed so we have deterministic behaviour:
+ randomState = new Random((int)(seed&0x7fffffff));
+ }
+ public virtual double GetRandomIOExceptionRate()
+ {
+ return randomIOExceptionRate;
+ }
+
+ internal virtual void MaybeThrowIOException()
+ {
+ if (randomIOExceptionRate > 0.0)
+ {
+ int number = Math.Abs(randomState.Next() % 1000);
+ if (number < randomIOExceptionRate * 1000)
+ {
+ throw new System.IO.IOException("a random IOException");
+ }
+ }
+ }
+
+ public override void DeleteFile(String name)
+ {
+ lock (this)
+ {
+ DeleteFile(name, false);
+ }
+ }
+
+ private void DeleteFile(String name, bool forced)
+ {
+ lock (this)
+ {
+ MaybeThrowDeterministicException();
+
+ if (crashed && !forced)
+ throw new System.IO.IOException("cannot delete after crash");
+
+ if (unSyncedFiles.Contains(name))
+ unSyncedFiles.Remove(name);
+ if (!forced && noDeleteOpenFile)
+ {
+ if (openFiles.Contains(name))
+ {
+ openFilesDeleted[name]=name;
+ throw new System.IO.IOException("MockRAMDirectory: file \"" + name + "\" is still open: cannot delete");
+ }
+ else
+ {
+ openFilesDeleted.Remove(name);
+ }
+ }
+ base.DeleteFile(name);
+ }
+ }
+
+ public System.Collections.IDictionary GetOpenDeletedFiles()
+ {
+ lock (this)
+ {
+ return new System.Collections.Hashtable(openFilesDeleted);
+ }
+ }
+
+ public IndexOutput CreateOutput(String name)
+ {
+ lock (this)
+ {
+ if (crashed)
+ throw new System.IO.IOException("cannot createOutput after crash");
+ Init();
+ if (preventDoubleWrite && createdFiles.Contains(name) && !name.Equals("segments.gen"))
+ throw new System.IO.IOException("file \"" + name + "\" was already written to");
+ if (noDeleteOpenFile && openFiles.Contains(name))
+ throw new System.IO.IOException("MockRAMDirectory: file \"" + name + "\" is still open: cannot overwrite");
+ RAMFile file = new RAMFile(this);
+ if (crashed)
+ throw new System.IO.IOException("cannot createOutput after crash");
+ unSyncedFiles[name]=name;
+ createdFiles[name]=name;
+ RAMFile existing = (RAMFile)fileMap[name];
+ // Enforce write once:
+ if (existing != null && !name.Equals("segments.gen") && preventDoubleWrite)
+ throw new System.IO.IOException("file " + name + " already exists");
+ else
+ {
+ if (existing != null)
+ {
+ sizeInBytes -= existing.sizeInBytes_ForNUnit;
+ existing.directory_ForNUnit = null;
+ }
+
+ fileMap[name]=file;
+ }
+
+ return new MockRAMOutputStream(this, file, name);
+ }
+ }
+
+ public IndexInput OpenInput(String name)
+ {
+ lock (this)
+ {
+ RAMFile file = (RAMFile)fileMap[name];
+ if (file == null)
+ throw new System.IO.FileNotFoundException(name);
+ else
+ {
+ if (openFiles.Contains(name))
+ {
+ int v = (int)openFiles[name];
+ v = (System.Int32)(v + 1);
+ openFiles[name]= v;
+ }
+ else
+ {
+ openFiles[name]=1;
+ }
+ }
+ return new MockRAMInputStream(this, name, file);
+ }
+ }
+
+ /** Provided for testing purposes. Use sizeInBytes() instead. */
+ public long GetRecomputedSizeInBytes()
+ {
+ lock (this)
+ {
+ long size = 0;
+ System.Collections.IEnumerator it = fileMap.Values.GetEnumerator();
+ while (it.MoveNext())
+ size += ((RAMFile)it.Current).GetSizeInBytes();
+ return size;
+ }
+ }
+
+ /** Like getRecomputedSizeInBytes(), but, uses actual file
+ * lengths rather than buffer allocations (which are
+ * quantized up to nearest
+ * RAMOutputStream.BUFFER_SIZE (now 1024) bytes.
+ */
+
+ public long GetRecomputedActualSizeInBytes()
+ {
+ lock (this)
+ {
+ long size = 0;
+ System.Collections.IEnumerator it = fileMap.Values.GetEnumerator();
+ while (it.MoveNext())
+ size += ((RAMFile)it.Current).length_ForNUnit;
+ return size;
+ }
+ }
+
+ public void Close()
+ {
+ lock (this)
+ {
+ if (openFiles == null)
+ {
+ openFiles = new System.Collections.Hashtable();
+ openFilesDeleted = new System.Collections.Hashtable();
+ }
+ if (noDeleteOpenFile && openFiles.Count > 0)
+ {
+ // RuntimeException instead of IOException because
+ // super() does not throw IOException currently:
+ throw new System.SystemException("MockRAMDirectory: cannot close: there are still open files: " + openFiles);
+ }
+ }
+ }
+
+ /**
+ * Objects that represent fail-able conditions. Objects of a derived
+ * class are created and registered with the mock directory. After
+ * register, each object will be invoked once for each first write
+ * of a file, giving the object a chance to throw an IOException.
+ */
+ public class Failure
+ {
+ /**
+ * eval is called on the first write of every new file.
+ */
+ public virtual void Eval(MockRAMDirectory dir) { }
+
+ /**
+ * reset should set the state of the failure to its default
+ * (freshly constructed) state. Reset is convenient for tests
+ * that want to create one failure object and then reuse it in
+ * multiple cases. This, combined with the fact that Failure
+ * subclasses are often anonymous classes makes reset difficult to
+ * do otherwise.
+ *
+ * A typical example of use is
+ * Failure failure = new Failure() { ... };
+ * ...
+ * mock.failOn(failure.reset())
+ */
+ public virtual Failure Reset() { return this; }
+
+ protected internal bool doFail;
+
+ public virtual void SetDoFail()
+ {
+ doFail = true;
+ }
+
+ public virtual void ClearDoFail()
+ {
+ doFail = false;
+ }
+ }
+
+ System.Collections.ArrayList failures;
+
+ /**
+ * add a Failure object to the list of objects to be evaluated
+ * at every potential failure point
+ */
+ public virtual void FailOn(Failure fail)
+ {
+ lock (this)
+ {
+ if (failures == null)
+ {
+ failures = new System.Collections.ArrayList();
+ }
+ failures.Add(fail);
+ }
+ }
+
+ /**
+ * Iterate through the failures list, giving each object a
+ * chance to throw an IOE
+ */
+ internal virtual void MaybeThrowDeterministicException()
+ {
+ lock (this)
+ {
+ if (failures != null)
+ {
+ for (int i = 0; i < failures.Count; i++)
+ {
+ ((Failure)failures[i]).Eval(this);
+ }
+ }
+ }
+ }
+ }
}
\ No newline at end of file