Skip to content

Commit ee33a4f

Browse files
committed
implement read/write functionality of VarintPostingsList #19
1 parent cfdd07c commit ee33a4f

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/InformationRetrieval.Test/Indexing/PostingsList/VarintPostingsListTest.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Linq;
34

45
using Xunit;
56

67
using Corpus;
78
using InformationRetrieval.Indexing.PostingsList;
8-
using InformationRetrieval.Utility;
99

1010

1111
namespace InformationRetrieval.Test.Indexing.PostingsList
1212
{
1313
public class VarintPostingsListTest
1414
{
1515
[Fact]
16-
public void VarintPostingsListSmokeTest()
16+
public void CreationTest()
1717
{
1818
var postingsList = new VarintPostingsList()
1919
{
@@ -26,5 +26,28 @@ public void VarintPostingsListSmokeTest()
2626

2727
Assert.Equal(PostingsListTest.GetDocIds(1, 2, 11, 15, 119), postingsList.ToArray());
2828
}
29+
30+
[Fact]
31+
public void ReadWriteTest()
32+
{
33+
var postingsList = new VarintPostingsList()
34+
{
35+
new DocumentId(1),
36+
new DocumentId(2),
37+
new DocumentId(11),
38+
new DocumentId(15),
39+
new DocumentId(119),
40+
};
41+
42+
int size = postingsList.GetReadOnlySpan().Length;
43+
var stream = new MemoryStream();
44+
stream.Write(postingsList.GetReadOnlySpan());
45+
stream.Seek(0, SeekOrigin.Begin);
46+
var buffer = new byte[size];
47+
stream.Read(buffer.AsSpan());
48+
var restored = new VarintPostingsList(buffer);
49+
50+
Assert.Equal(postingsList, restored);
51+
}
2952
}
3053
}

src/InformationRetrieval/Indexing/PostingsList/VarintPostingsList.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
using Corpus;
56
using InformationRetrieval.Utility;
@@ -16,14 +17,26 @@ public class VarintPostingsList : IReadOnlyCollection<DocumentId>
1617
private int length;
1718
private uint prevInserted;
1819

19-
public VarintPostingsList()
20+
public VarintPostingsList() : this(0) { }
21+
22+
public VarintPostingsList(int capacity)
2023
{
21-
data = new byte[32];
24+
data = capacity > 0 ? new byte[capacity] : Array.Empty<byte>();
2225
length = 0;
2326
prevInserted = 0;
2427
Count = 0;
2528
}
2629

30+
public VarintPostingsList(byte[] buffer) : this(buffer, buffer.Length) { }
31+
32+
public VarintPostingsList(byte[] buffer, int length)
33+
{
34+
data = buffer;
35+
this.length = length;
36+
Count = VarintEncoder.GetIntegerCount(buffer.AsSpan(0, length));
37+
prevInserted = this.LastOrDefault().Id;
38+
}
39+
2740
public int Count { get; private set; }
2841

2942
public void Add(DocumentId docId)
@@ -55,14 +68,13 @@ public IEnumerator<DocumentId> GetEnumerator()
5568
}
5669
}
5770

58-
IEnumerator IEnumerable.GetEnumerator()
59-
{
60-
return GetEnumerator();
61-
}
71+
IEnumerator IEnumerable.GetEnumerator()
72+
=> GetEnumerator();
6273

63-
private void Resize(int newSize)
64-
{
65-
Array.Resize(ref data, newSize);
66-
}
74+
public ReadOnlySpan<byte> GetReadOnlySpan()
75+
=> data.AsSpan(0, length);
76+
77+
private void Resize(int newSize)
78+
=> Array.Resize(ref data, newSize);
6779
}
6880
}

0 commit comments

Comments
 (0)