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.
Syntax
Parameters
- 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
Remarks
Example
BPlusTree/BPlusTree.Test/ThreadedBTreeTest.cs
| 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())
{
copy.Delete();
RecordsCreated = 0;
int minRecordCreated;
using (BPlusTree<KeyInfo, DataValue> dictionary = new BPlusTree<KeyInfo, DataValue>(options))
using (WorkQueue work = new WorkQueue(options.ConcurrentWriters))
{
Exception lastError = null;
work.OnError += delegate(object o, ErrorEventArgs e) { lastError = e.GetException(); };
Thread.Sleep(1);
for (int i = 0; i < options.ConcurrentWriters; i++)
work.Enqueue(new ThreadedTest(dictionary, 10000000).Run);
while (RecordsCreated < 1000)
Thread.Sleep(1);
minRecordCreated = Interlocked.CompareExchange(ref RecordsCreated, 0, 0);
File.Copy(options.FileName, copy.TempPath);//just grab a copy any old time.
work.Complete(false, 0); //hard-abort all threads
}
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;
options.FileName = copy.TempPath;
int recoveredRecords = BPlusTree<KeyInfo, DataValue>.RecoverFile(options);
Assert.IsTrue(recoveredRecords >= minRecordCreated);
using (BPlusTree<KeyInfo, DataValue> dictionary = new BPlusTree<KeyInfo, DataValue>(options))
{
dictionary.EnableCount();
Assert.AreEqual(recoveredRecords, dictionary.Count);
foreach (KeyValuePair<KeyInfo, DataValue> kv in dictionary)
{
Assert.AreEqual(kv.Key.UID, kv.Value.Key.UID);
dictionary.Remove(kv.Key);
}
Assert.AreEqual(0, dictionary.Count);
}
}
} |
| VB.NET | Copy 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()
copy.Delete()
RecordsCreated = 0
Dim minRecordCreated As Integer
Using dictionary As New BPlusTree(Of KeyInfo, DataValue)(options)
Using work As New WorkQueue(options.ConcurrentWriters)
Dim lastError As Exception = Nothing
work.OnError += Function(o As Object, e As ErrorEventArgs) Do
lastError = e.GetException()
End Function
Thread.Sleep(1)
Dim i As Integer = 0
While i < options.ConcurrentWriters
work.Enqueue(New ThreadedTest(dictionary, 10000000).Run)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
While RecordsCreated < 1000
Thread.Sleep(1)
End While
minRecordCreated = Interlocked.CompareExchange(RecordsCreated, 0, 0)
File.Copy(options.FileName, copy.TempPath)
'just grab a copy any old time.
'hard-abort all threads
work.Complete(False, 0)
End Using
End Using
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
options.FileName = copy.TempPath
Dim recoveredRecords As Integer = BPlusTree(Of KeyInfo, DataValue).RecoverFile(options)
Assert.IsTrue(recoveredRecords >= minRecordCreated)
Using dictionary As New BPlusTree(Of KeyInfo, DataValue)(options)
dictionary.EnableCount()
Assert.AreEqual(recoveredRecords, dictionary.Count)
For Each kv As KeyValuePair(Of KeyInfo, DataValue) In dictionary
Assert.AreEqual(kv.Key.UID, kv.Value.Key.UID)
dictionary.Remove(kv.Key)
Next
Assert.AreEqual(0, dictionary.Count)
End Using
End Using
End Using
End Using |
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also