Пример #1
0
 public void TestWrite()
 {
     PostingListEncoder decoder = new PostingListEncoder();
     SpimiBlockWriter writer = new SpimiBlockWriter();
     writer.AddPosting("bTerm", DocA);
     writer.AddPosting("aTerm", DocA);
     writer.AddPosting("aTerm", DocB);
     string filePath = writer.FlushToFile();
     using (FileStream file = File.Open(filePath, FileMode.Open))
     {
         BinaryReader reader = new BinaryReader(file);
         Assert.AreEqual(2, reader.ReadInt32());
         Assert.AreEqual("aTerm", reader.ReadString());
         IList<Posting> postings = new List<Posting>();
         postings.Add(new Posting(DocA, 1));
         postings.Add(new Posting(DocB, 1));
         IList<Posting> readPostings = decoder.read(reader);
         for (int i = 0; i < postings.Count; i++ )
         {
             readPostings[i].Equals(postings[i]);
         }
         Assert.AreEqual("bTerm", reader.ReadString());
         readPostings = decoder.read(reader);
         Assert.AreEqual(new Posting(DocA, 1), readPostings[0]);
     }
 }
Пример #2
0
        public void TestWrite()
        {
            MemoryStream stream = new MemoryStream();

            // FileIndex should support a stream starting at any point
            stream.Seek(10, SeekOrigin.Begin);

            PostingListEncoder decoder = new PostingListEncoder();
            PerformWrite(stream);

            BinaryReader reader = new BinaryReader(stream);

            // Term count
            Assert.AreEqual(2, reader.ReadInt64());

            long ptr1 = reader.ReadInt64();
            long ptr2 = reader.ReadInt64();

            Assert.AreEqual(stream.Position, ptr1);
            Assert.AreEqual("aTerm", reader.ReadString());
            IList<Posting> postings = decoder.read(reader);
            Assert.AreEqual(2, postings.Count);
            Assert.AreEqual(new Posting(DocA, 1), postings[0]);
            Assert.AreEqual(new Posting(DocB, 1), postings[1]);

            Assert.AreEqual(stream.Position, ptr2);
            Assert.AreEqual("bTerm", reader.ReadString());
            postings = decoder.read(reader);
            Assert.AreEqual(3, postings.Count);
            Assert.AreEqual(new Posting(DocA, 1), postings[0]);
            Assert.AreEqual(new Posting(DocZ, 1), postings[1]);
            Assert.AreEqual(new Posting(DocT, 1), postings[2]);
        }
Пример #3
0
        public void TestReadWrite()
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);
            BinaryReader reader = new BinaryReader(stream);
            PostingListEncoder encoder = new PostingListEncoder();
            List<Posting> postings = new List<Posting>();
            postings.Add(new Posting(DocA, 23));
            postings.Add(new Posting(DocB, 23));

            encoder.write(writer, postings);

            stream.Seek(0, SeekOrigin.Begin);
            IList<Posting> readPostings = encoder.read(reader);

            Assert.AreEqual(postings.Count, readPostings.Count);
            for (int postingIndex = 0; postingIndex < postings.Count; postingIndex++)
            {
                Assert.AreEqual(postings[postingIndex].Frequency, readPostings[postingIndex].Frequency);
                Assert.AreEqual(postings[postingIndex].DocumentId, readPostings[postingIndex].DocumentId);
            }
        }
Пример #4
0
 /// <remarks> Keeps file open until all postings have been read</remarks>
 public IEnumerable<PostingList> Read(string filepath)
 {
     using (FileStream stream = File.Open(filepath, FileMode.Open))
     {
         PostingListEncoder decoder = new PostingListEncoder();
         BinaryReader reader = new BinaryReader(stream);
         int termCount = reader.ReadInt32();
         for (int termIndex = 0; termIndex < termCount; termIndex++)
         {
             string term = reader.ReadString();
             yield return new PostingList(term, decoder.read(reader));
         }
     }
 }