private byte[] AssembleGetResult(Key lookupKey, Value result) {
switch (result.Type) {
case ValueFlag.Null:
case ValueFlag.Deleted:
return null;
case ValueFlag.SmallValue:
return result.ValueBytes;
case ValueFlag.LargeValueDescriptor: {
lock (multiPageLock) {
// read the descriptor again in case it changed
result = InternalGet(lookupKey);
// make sure type is still large value descriptor and continue
if (result.Type == ValueFlag.LargeValueDescriptor) {
int valueSize = BitConverter.ToInt32(result.ValueBytes, 0);
byte[] bytes = new byte[valueSize];
int offset = 0;
byte seqNum = 1;
while (offset < valueSize) {
var blockKey = lookupKey.WithSequence(seqNum);
var block = InternalGet(blockKey);
if (block.Type != ValueFlag.LargeValueChunk)
throw new InvalidDataException(string.Format("Corrupted data: block is missing. Block Type: {0} SeqNum: {1}, Block Key: {2}", block.Type, seqNum, blockKey));
offset += block.CopyValueBytesTo(bytes, offset);
seqNum++;
}
return bytes;
} else {
return AssembleGetResult(lookupKey, result);
}
}
}
default:
throw new InvalidOperationException("Unexpected value flag for result.");
}
}