forked from microsoft/FASTER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableLengthIteratorTests.cs
More file actions
90 lines (75 loc) · 3.01 KB
/
VariableLengthIteratorTests.cs
File metadata and controls
90 lines (75 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using FASTER.core;
using NUnit.Framework;
namespace FASTER.test
{
[TestFixture]
public class IteratorTests
{
[Test]
public void ShouldSkipEmptySpaceAtEndOfPage()
{
var vlLength = new VLValue();
var log = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "\\hlog-vl-iter.log", deleteOnClose: true);
var fht = new FasterKV<Key, VLValue, Input, int[], Empty, VLFunctions>
(128, new VLFunctions(),
new LogSettings { LogDevice = log, MemorySizeBits = 17, PageSizeBits = 10 }, // 1KB page
null, null, null, new VariableLengthStructSettings<Key, VLValue> { valueLength = vlLength }
);
var session = fht.NewSession();
try
{
Set(1L, 200, 1);// page#0
Set(2L, 200, 2);// page#1 because there is not enough space in page#0
var len = 1024; // fill page#1 exactly
len = len - 2 * RecordInfo.GetLength() - 2 * 8 - vlLength.GetLength(ref GetValue(200, 2));
Set(3, len / 4, 3); // should be in page#1
Set(4, 64, 4);
session.CompletePending(true);
var data = new List<Tuple<long, int, int>>();
using (var iterator = fht.Log.Scan(fht.Log.BeginAddress, fht.Log.TailAddress))
{
while (iterator.GetNext(out var info))
{
ref var scanKey = ref iterator.GetKey();
ref var scanValue = ref iterator.GetValue();
data.Add(Tuple.Create(scanKey.key, scanValue.length, scanValue.field1));
}
}
Assert.AreEqual(4, data.Count);
Assert.AreEqual(200, data[1].Item2);
Assert.AreEqual(2, data[1].Item3);
Assert.AreEqual(3, data[2].Item1);
Assert.AreEqual(3, data[2].Item3);
Assert.AreEqual(4, data[3].Item1);
Assert.AreEqual(64, data[3].Item2);
Assert.AreEqual(4, data[3].Item3);
}
finally
{
session.Dispose();
fht.Dispose();
log.Close();
}
void Set(long keyValue, int length, int tag)
{
var key = new Key() { key = keyValue };
ref var value = ref GetValue(length, tag);
session.Upsert(ref key, ref value, Empty.Default, 0);
}
}
private static ref VLValue GetValue(int length, int tag)
{
var data = new byte[length * 4];
ref var value = ref Unsafe.As<byte, VLValue>(ref data[0]);
value.length = length;
value.field1 = tag;
return ref value;
}
}
}