public void T02_BasicBlock_Enumerators()
{
// exercize the iscannable interface
string[] testvalues = { "test/1", "test/2", "test/3" };
byte[] databuffer;
// encode a buffer
{
MemoryStream ms = new MemoryStream();
// add some values to the block encoder
SegmentBlockBasicEncoder enc = new SegmentBlockBasicEncoder();
enc.setStream(ms);
for (int i = 0; i < testvalues.Length; i++) {
RecordKey tkey = new RecordKey().appendParsedKey(testvalues[i]);
RecordUpdate tupdate = RecordUpdate.WithPayload("data: " + testvalues[i]);
enc.add(tkey, tupdate);
}
enc.flush();
databuffer = ms.ToArray();
}
// decode and test the buffer, scan enumerators
{
BlockAccessor rs = new BlockAccessor(databuffer);
SegmentBlockBasicDecoder decoder = new SegmentBlockBasicDecoder(rs);
int pos = 0;
IScanner<RecordKey> scanner = ScanRange<RecordKey>.All();
foreach (KeyValuePair<RecordKey, RecordUpdate> row in decoder.scanForward(scanner)) {
RecordKey tkey = new RecordKey().appendParsedKey(testvalues[pos]);
RecordUpdate tupdate = RecordUpdate.WithPayload("data: " + testvalues[pos]);
Assert.AreEqual(tkey, row.Key, "forward, record keys should match");
Assert.AreEqual(tupdate, row.Value, "forward, record values should match:");
pos++;
}
Assert.AreEqual(testvalues.Length, pos, "forward, should return all values we put in");
pos = testvalues.Length - 1;
foreach (KeyValuePair<RecordKey, RecordUpdate> row in decoder.scanBackward(scanner)) {
RecordKey tkey = new RecordKey().appendParsedKey(testvalues[pos]);
RecordUpdate tupdate = RecordUpdate.WithPayload("data: " + testvalues[pos]);
Assert.AreEqual(tkey, row.Key, "backward, record keys should match, pos=" + pos);
Assert.AreEqual(tupdate, row.Value, "backward, record values should match:");
pos--;
}
Assert.AreEqual(-1, pos, "backward, should return all values we put in");
}
}