CSharpTest.Net
RecoveryScan Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.BPlusTree Assembly > CSharpTest.Net.Collections Namespace > BPlusTree<TKey,TValue> Class : RecoveryScan Method

options
The options normally used to create the BPlusTree<TKey,TValue> instance
sharing
System.IO.FileShare options used to open the file

Glossary Item Box

Performs a low-level scan of the storage file to yield all Key/Value pairs it was able to read from the file.

Syntax

Visual Basic (Declaration) 
Public Shared Function RecoveryScan( _
   ByVal options As BPlusTree(Of TKey,TValue), _
   ByVal sharing As FileShare _
) As IEnumerable(Of KeyValuePair(Of TKey,TValue))

Parameters

options
The options normally used to create the BPlusTree<TKey,TValue> instance
sharing
System.IO.FileShare options used to open the file

Return Value

Yields the Key/Value pairs found in the file

Example

BPlusTree/BPlusTree.Test/TestFileSerialized.cs

C#Copy Code
BPlusTree<int, string>.Options options = (BPlusTree<int, string>.Options)Options;
options.BTreeOrder = 4;
options.FileBlockSize = 512;
options.FileGrowthRate = 25;
options.ConcurrentWriters = 4;
options.FileOpenOptions = FileOptions.None;

using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
{
    for(int i=0; i < 100; i++)
        Assert.IsTrue(tree.TryAdd(i, i.ToString()));
}

using (Stream io = TempFile.Open())
{
    //first we can corrupt the root node, which is always at an offset of BlockSize
    io.Seek(512, SeekOrigin.Begin);
    io.Write(new byte[512], 0, 512);

    //Now let's corrupt one byte from the end of the node at index 3
    io.Seek(1024 + 16, SeekOrigin.Begin);
    int len = PrimitiveSerializer.Int32.ReadFrom(io);
    io.Seek(1024 + 32 + len - 1, SeekOrigin.Begin);//secrets of fragmented file revealed... ugly i know.
    io.WriteByte(255); //overwrite last used byte in chunk.
}

options.CreateFile = CreatePolicy.Never;

//Now that we've corrupted part of the file content, let's take a peek
try
{
    using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
    {
        foreach (KeyValuePair<int, string> kv in tree)
            Assert.AreEqual(kv.Key.ToString(), kv.Value);
    }
    Assert.Fail("Expected InvalidDataException");
}
catch (InvalidDataException)
{ }

Dictionary<int, string> found = new Dictionary<int, string>();
List<int> duplicates = new List<int>();
foreach (KeyValuePair<int, string> kv in BPlusTree<int, string>.RecoveryScan(options, FileShare.None))
{
    if (!found.ContainsKey(kv.Key))
        found.Add(kv.Key, kv.Value);
    else
        duplicates.Add(kv.Key);
    
    Assert.AreEqual(kv.Key.ToString(), kv.Value);
}
Assert.AreNotEqual(0, found.Count);

//The following may change...
Assert.AreEqual(99, found.Count);
Assert.IsFalse(found.ContainsKey(3), "should be missing #3");
Assert.AreEqual(0, duplicates.Count);
VB.NETCopy Code
Dim options As BPlusTree(Of Integer, String).Options = DirectCast(Options, BPlusTree(Of Integer, String).Options)
options.BTreeOrder = 4
options.FileBlockSize = 512
options.FileGrowthRate = 25
options.ConcurrentWriters = 4
options.FileOpenOptions = FileOptions.None

Using tree As New BPlusTree(Of Integer, String)(options)
    Dim i As Integer = 0
    While i < 100
        Assert.IsTrue(tree.TryAdd(i, i.ToString()))
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While
End Using

Using io As Stream = TempFile.Open()
    'first we can corrupt the root node, which is always at an offset of BlockSize
    io.Seek(512, SeekOrigin.Begin)
    io.Write(New Byte(512) {}, 0, 512)

    'Now let's corrupt one byte from the end of the node at index 3
    io.Seek(1024 + 16, SeekOrigin.Begin)
    Dim len As Integer = PrimitiveSerializer.Int32.ReadFrom(io)
    io.Seek(1024 + 32 + len - 1, SeekOrigin.Begin)
    'secrets of fragmented file revealed... ugly i know.
        'overwrite last used byte in chunk.
    io.WriteByte(255)
End Using

options.CreateFile = CreatePolicy.Never

'Now that we've corrupted part of the file content, let's take a peek
Try
    Using tree As New BPlusTree(Of Integer, String)(options)
        For Each kv As KeyValuePair(Of Integer, String) In tree
            Assert.AreEqual(kv.Key.ToString(), kv.Value)
        Next
    End Using
    Assert.Fail("Expected InvalidDataException")
Catch generatedExceptionName As InvalidDataException
End Try

Dim found As New Dictionary(Of Integer, String)()
Dim duplicates As New List(Of Integer)()
For Each kv As KeyValuePair(Of Integer, String) In BPlusTree(Of Integer, String).RecoveryScan(options, FileShare.None)
    If Not found.ContainsKey(kv.Key) Then
        found.Add(kv.Key, kv.Value)
    Else
        duplicates.Add(kv.Key)
    End If

    Assert.AreEqual(kv.Key.ToString(), kv.Value)
Next
Assert.AreNotEqual(0, found.Count)

'The following may change...
Assert.AreEqual(99, found.Count)
Assert.IsFalse(found.ContainsKey(3), "should be missing #3")
Assert.AreEqual(0, duplicates.Count)

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Reference

BPlusTree<TKey,TValue> Class
BPlusTree<TKey,TValue> Members
BPlusTree.Options

Used By

Int32 BPlusTree.RecoverFile(BPlusTree.Options)

Source Code

BPlusTree/Collections/BPlusTree.cs

Generated with Document! X 2011 by Innovasys