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

The options normally used to create the BPlusTree<TKey,TValue> instance

Glossary Item Box

Recovers as much file content as possible into a newly created BPlusTree<TKey,TValue>, if the operation returns a non-zero result it was successful and the file has been replaced with a new database containing the recovered data. The original file remains in-tact but was renamed with a '.deleted' extension.


Visual Basic (Declaration) 
Public Shared Function RecoverFile( _
   ByVal options As BPlusTree(Of TKey,TValue) _
) As Integer
public static int RecoverFile( 
   BPlusTree<TKey,TValue> options


The options normally used to create the BPlusTree<TKey,TValue> instance

Return Value

Returns 0 on failure, or the number of records successfully retrieved from the original file


InvalidConfigurationValueExceptionException class: InvalidConfigurationValueException The configuration value '{0}' is invalid.


If an exception occurs during the parsing of the file and one or more records were recovered, they will be stored in a file by the same name with an added extension of '.recovered'. This recovered file can be opened as a normal BPlusTree<TKey,TValue> to view it's contents. During the restore it is possible that a single Key was found multiple times, in this case the first occurrence found will be used.



C#Copy Code
BPlusTree<KeyInfo, DataValue>.Options options = new BPlusTree<KeyInfo, DataValue>.Options(
    new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());
options.CalcBTreeOrder(32, 300);
options.FileName = TempFile.TempPath;
options.CreateFile = CreatePolicy.Always;

using (TempFile copy = new TempFile())
    int minRecordCreated = StartAndAbortWriters(options, copy);

    using (TempFile.Attach(copy.TempPath + ".recovered")) //used to create the new copy
    using (TempFile.Attach(copy.TempPath + ".deleted"))  //renamed existing file
        options.CreateFile = CreatePolicy.Never;
        int recoveredRecords = BPlusTree<KeyInfo, DataValue>.RecoverFile(options);
        if (recoveredRecords < RecordsCreated)
            Assert.Fail("Unable to recover records, recieved ({0} of {1}).", recoveredRecords, RecordsCreated);

        options.FileName = copy.TempPath;
        recoveredRecords = BPlusTree<KeyInfo, DataValue>.RecoverFile(options);
        Assert.IsTrue(recoveredRecords >= minRecordCreated, "Expected at least " + minRecordCreated + " found " + recoveredRecords);

        using (BPlusTree<KeyInfo, DataValue> dictionary = new BPlusTree<KeyInfo, DataValue>(options))
            Assert.AreEqual(recoveredRecords, dictionary.Count);

            foreach (KeyValuePair<KeyInfo, DataValue> kv in dictionary)
                Assert.AreEqual(kv.Key.UID, kv.Value.Key.UID);

            Assert.AreEqual(0, dictionary.Count);
VB.NETCopy Code
Dim options As New BPlusTree(Of KeyInfo, DataValue).Options(New KeyInfoSerializer(), New DataValueSerializer(), New KeyInfoComparer())
options.CalcBTreeOrder(32, 300)
options.FileName = TempFile.TempPath
options.CreateFile = CreatePolicy.Always

Using copy As New TempFile()
    Dim minRecordCreated As Integer = StartAndAbortWriters(options, copy)

    Using TempFile.Attach(copy.TempPath + ".recovered")
        'used to create the new copy
        Using TempFile.Attach(copy.TempPath + ".deleted")
            'renamed existing file
            options.CreateFile = CreatePolicy.Never
            Dim recoveredRecords As Integer = BPlusTree(Of KeyInfo, DataValue).RecoverFile(options)
            If recoveredRecords < RecordsCreated Then
                Assert.Fail("Unable to recover records, recieved ({0} of {1}).", recoveredRecords, RecordsCreated)
            End If

            options.FileName = copy.TempPath
            recoveredRecords = BPlusTree(Of KeyInfo, DataValue).RecoverFile(options)
            Assert.IsTrue(recoveredRecords >= minRecordCreated, "Expected at least " + minRecordCreated + " found " + recoveredRecords)

            Using dictionary As New BPlusTree(Of KeyInfo, DataValue)(options)
                Assert.AreEqual(recoveredRecords, dictionary.Count)

                For Each kv As KeyValuePair(Of KeyInfo, DataValue) In dictionary
                    Assert.AreEqual(kv.Key.UID, kv.Value.Key.UID)

                Assert.AreEqual(0, dictionary.Count)
            End Using
        End Using
    End Using
End Using


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

See Also

Generated with Document! X 2011 by Innovasys