public void FindKeyWithPreferPreviousKeyWorks()
{
const int keyCount = 10000;
using (var fileCollection = new InMemoryFileCollection())
using (IKeyValueDB db = new KeyValueDB(fileCollection))
{
using (var tr = db.StartTransaction())
{
var key = new byte[100];
for (int i = 0; i < keyCount; i++)
{
key[0] = (byte)(i / 256);
key[1] = (byte)(i % 256);
tr.CreateKey(key);
}
tr.Commit();
}
using (var tr = db.StartTransaction())
{
var key = new byte[101];
for (int i = 0; i < keyCount; i++)
{
key[0] = (byte)(i / 256);
key[1] = (byte)(i % 256);
var findKeyResult = tr.Find(ByteBuffer.NewSync(key));
Assert.Equal(FindResult.Previous, findKeyResult);
Assert.Equal(i, tr.GetKeyIndex());
}
}
using (var tr = db.StartTransaction())
{
var key = new byte[99];
for (int i = 0; i < keyCount; i++)
{
key[0] = (byte)(i / 256);
key[1] = (byte)(i % 256);
var findKeyResult = tr.Find(ByteBuffer.NewSync(key));
if (i == 0)
{
Assert.Equal(FindResult.Next, findKeyResult);
Assert.Equal(i, tr.GetKeyIndex());
}
else
{
Assert.Equal(FindResult.Previous, findKeyResult);
Assert.Equal(i - 1, tr.GetKeyIndex());
}
}
}
}
}