public virtual void TestIllegalEOF() { RAMDirectory dir = new RAMDirectory(); IndexOutput o = dir.CreateOutput("out", NewIOContext(Random)); var b = new byte[1024]; o.WriteBytes(b, 0, 1024); o.Dispose(); IndexInput i = dir.OpenInput("out", NewIOContext(Random)); i.Seek(1024); i.Dispose(); dir.Dispose(); }
/// <summary> /// Close this directory, which flushes any cached files /// to the delegate and then closes the delegate. /// </summary> public override void Dispose() { // NOTE: technically we shouldn't have to do this, ie, // IndexWriter should have sync'd all files, but we do // it for defensive reasons... or in case the app is // doing something custom (creating outputs directly w/o // using IndexWriter): foreach (string fileName in Cache.ListAll()) { UnCache(fileName); } Cache.Dispose(); @delegate.Dispose(); }
public void Test_Store_RAMDirectory() { Lucene.Net.Store.RAMDirectory ramDIR = new Lucene.Net.Store.RAMDirectory(); //Index 1 Doc Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); var conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); Lucene.Net.Index.IndexWriter wr = new Lucene.Net.Index.IndexWriter(ramDIR, conf /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED*/); Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); wr.AddDocument(doc); wr.Dispose(); //now serialize it System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); serializer.Serialize(memoryStream, ramDIR); //Close DIR ramDIR.Dispose(); ramDIR = null; //now deserialize memoryStream.Seek(0, System.IO.SeekOrigin.Begin); Lucene.Net.Store.RAMDirectory ramDIR2 = (Lucene.Net.Store.RAMDirectory)serializer.Deserialize(memoryStream); //Add 1 more doc Lucene.Net.Analysis.Analyzer analyzer2 = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); var conf2 = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); wr = new Lucene.Net.Index.IndexWriter(ramDIR2, conf2 /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED*/); doc = new Lucene.Net.Documents.Document(); doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); wr.AddDocument(doc); wr.Dispose(); Lucene.Net.Search.TopDocs topDocs; //Search using (var reader = DirectoryReader.Open(ramDIR2)) { Lucene.Net.Search.IndexSearcher s = new Lucene.Net.Search.IndexSearcher(reader); Lucene.Net.QueryParsers.Classic.QueryParser qp = new Lucene.Net.QueryParsers.Classic.QueryParser(Version.LUCENE_CURRENT, "field1", new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_CURRENT)); Lucene.Net.Search.Query q = qp.Parse("value1"); topDocs = s.Search(q, 100); } Assert.AreEqual(topDocs.TotalHits, 2, "See the issue: LUCENENET-174"); }
public virtual void TestSeekToEOFThenBack() { RAMDirectory dir = new RAMDirectory(); IndexOutput o = dir.CreateOutput("out", NewIOContext(Random)); var bytes = new byte[3 * RAMInputStream.BUFFER_SIZE]; o.WriteBytes(bytes, 0, bytes.Length); o.Dispose(); IndexInput i = dir.OpenInput("out", NewIOContext(Random)); i.Seek(2 * RAMInputStream.BUFFER_SIZE - 1); i.Seek(3 * RAMInputStream.BUFFER_SIZE); i.Seek(RAMInputStream.BUFFER_SIZE); i.ReadBytes(bytes, 0, 2 * RAMInputStream.BUFFER_SIZE); i.Dispose(); dir.Dispose(); }
public virtual void TestCaching() { Directory dir = new RAMDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, Similarity, TimeZone); Document doc = new Document(); TokenStream stream = new TokenStreamAnonymousInnerClassHelper(this); stream = new CachingTokenFilter(stream); doc.Add(new TextField("preanalyzed", stream)); // 1) we consume all tokens twice before we add the doc to the index CheckTokens(stream); stream.Reset(); CheckTokens(stream); // 2) now add the document to the index and verify if all tokens are indexed // don't reset the stream here, the DocumentWriter should do that implicitly writer.AddDocument(doc); IndexReader reader = writer.Reader; DocsAndPositionsEnum termPositions = MultiFields.GetTermPositionsEnum(reader, MultiFields.GetLiveDocs(reader), "preanalyzed", new BytesRef("term1")); Assert.IsTrue(termPositions.NextDoc() != DocIdSetIterator.NO_MORE_DOCS); Assert.AreEqual(1, termPositions.Freq()); Assert.AreEqual(0, termPositions.NextPosition()); termPositions = MultiFields.GetTermPositionsEnum(reader, MultiFields.GetLiveDocs(reader), "preanalyzed", new BytesRef("term2")); Assert.IsTrue(termPositions.NextDoc() != DocIdSetIterator.NO_MORE_DOCS); Assert.AreEqual(2, termPositions.Freq()); Assert.AreEqual(1, termPositions.NextPosition()); Assert.AreEqual(3, termPositions.NextPosition()); termPositions = MultiFields.GetTermPositionsEnum(reader, MultiFields.GetLiveDocs(reader), "preanalyzed", new BytesRef("term3")); Assert.IsTrue(termPositions.NextDoc() != DocIdSetIterator.NO_MORE_DOCS); Assert.AreEqual(1, termPositions.Freq()); Assert.AreEqual(2, termPositions.NextPosition()); reader.Dispose(); writer.Dispose(); // 3) reset stream and consume tokens again stream.Reset(); CheckTokens(stream); dir.Dispose(); }
public virtual void TestSeekToEOFThenBack() { RAMDirectory dir = new RAMDirectory(); IndexOutput o = dir.CreateOutput("out", NewIOContext(Random())); var bytes = new byte[3 * RAMInputStream.BUFFER_SIZE]; o.WriteBytes(bytes, 0, bytes.Length); o.Dispose(); IndexInput i = dir.OpenInput("out", NewIOContext(Random())); i.Seek(2 * RAMInputStream.BUFFER_SIZE - 1); i.Seek(3 * RAMInputStream.BUFFER_SIZE); i.Seek(RAMInputStream.BUFFER_SIZE); i.ReadBytes(bytes, 0, 2 * RAMInputStream.BUFFER_SIZE); i.Dispose(); dir.Dispose(); }
public virtual void TestIllegalEOF() { RAMDirectory dir = new RAMDirectory(); IndexOutput o = dir.CreateOutput("out", NewIOContext(Random())); var b = new byte[1024]; o.WriteBytes(b, 0, 1024); o.Dispose(); IndexInput i = dir.OpenInput("out", NewIOContext(Random())); i.Seek(1024); i.Dispose(); dir.Dispose(); }