📄 bplustest.cs
字号:
{
Console.WriteLine("TESTING BPLUSTREE");
System.IO.Stream treefile=null, blockfile=null;
BplusDotNet.BplusTree bpt = getTree(null, ref treefile, ref blockfile);
Hashtable allmaps = new Hashtable();
for (int i=0; i<10; i++)
{
Console.WriteLine("Pass "+i+" of 10");
bpt.SetFootPrintLimit(16-i);
for (int j=0; j<30; j++)
{
Hashtable record = new Hashtable();
for (int k=0; k<30; k++)
{
string thiskey = keyMaker(i,j,k);
string thisvalue = ValueMaker(j,k,i);
record[thiskey] = thisvalue;
bpt[thiskey] = thisvalue;
}
if ((j%3)==1)
{
bpt.Recover(false);
}
if ( ((i+j)%2) == 1 )
{
bpt.Commit();
bpt.Abort(); // should have no effect
bpt.Commit(); // ditto
if ( (i+j)%5 < 2)
{
//Debug.WriteLine(bpt.toHtml());
foreach (DictionaryEntry d in record)
{
string thiskey = (string) d.Key;
bpt.RemoveKey(thiskey);
if (allmaps.ContainsKey(thiskey))
{
allmaps.Remove(thiskey);
}
}
//Debug.WriteLine(bpt.toHtml());
bpt.Commit();
//return;
}
else
{
foreach (DictionaryEntry d in record)
{
allmaps[d.Key] = d.Value;
}
}
}
else
{
bpt.Abort();
}
if ((j%4)==2)
{
bpt = getTree(bpt, ref treefile, ref blockfile);
}
// now check the structure
bool ReadOnly = ((i+j)%7)<2;
if (ReadOnly)
{
bpt = getTree(bpt, ref treefile, ref blockfile, true);
}
ArrayList allkeys = new ArrayList();
foreach (DictionaryEntry d in allmaps)
{
string thiskey = (string)d.Key;
string thisvalue = (string)d.Value;
string treemap = bpt[thiskey];
if (!treemap.Equals(thisvalue))
{
throw new ApplicationException("key "+thiskey+" maps to "+treemap+" but should map to "+thisvalue);
}
allkeys.Add(thiskey);
}
string currentkey = bpt.FirstKey();
allkeys.Sort();
foreach (object thing in allkeys)
{
string recordedkey = (string) thing;
if (currentkey==null)
{
throw new ApplicationException("end of keys found when expecting "+recordedkey);
}
if (!currentkey.Equals(recordedkey))
{
Debug.WriteLine(bpt.toHtml());
throw new ApplicationException("key "+currentkey+" found where expecting "+recordedkey);
}
currentkey = bpt.NextKey(currentkey);
}
if (currentkey!=null)
{
throw new ApplicationException("found "+currentkey+" when expecting end of keys");
}
// set up bpt for modification again...
if (ReadOnly)
{
bpt = getTree(bpt, ref treefile, ref blockfile, false);
}
}
}
}
public static BplusDotNet.BplusTree getTree(BplusDotNet.BplusTree bpt, ref System.IO.Stream treefile, ref System.IO.Stream blockfile)
{
return getTree(bpt, ref treefile, ref blockfile, false);
}
public static BplusDotNet.BplusTree getTree(BplusDotNet.BplusTree bpt, ref System.IO.Stream treefile, ref System.IO.Stream blockfile,
bool ReadOnly)
{
int CultureId = System.Globalization.CultureInfo.InvariantCulture.LCID;
if (tempdirectory!=null)
{
// allocate in filesystem
string treename = System.IO.Path.Combine(tempdirectory, "BPDNtree.dat");
string blockname = System.IO.Path.Combine(tempdirectory, "BPDNblock.dat");
treefile = null;
blockfile = null;
if (bpt == null)
{
// allocate new
if (System.IO.File.Exists(treename))
{
System.IO.File.Delete(treename);
}
if (System.IO.File.Exists(blockname))
{
System.IO.File.Delete(blockname);
}
bpt = BplusDotNet.BplusTree.Initialize(treename, blockname, keylength, CultureId, nodesize, buffersize);
}
else
{
// reopen old
bpt.Shutdown();
if (ReadOnly)
{
bpt = BplusDotNet.BplusTree.ReadOnly(treename, blockname);
}
else
{
bpt = BplusDotNet.BplusTree.ReOpen(treename, blockname);
}
}
}
else
{
// allocate in memory
if (bpt==null)
{
// allocate new
treefile = new System.IO.MemoryStream();
blockfile = new System.IO.MemoryStream();;
bpt = BplusDotNet.BplusTree.Initialize(treefile, blockfile, keylength, CultureId, nodesize, buffersize);
}
else
{
// reopen
bpt = BplusDotNet.BplusTree.ReOpen(treefile, blockfile);
}
}
return bpt;
}
public static BplusDotNet.SerializedTree getsTree(BplusDotNet.SerializedTree bpts, ref System.IO.Stream treefile, ref System.IO.Stream blockfile)
{
int CultureId = System.Globalization.CultureInfo.InvariantCulture.LCID;
BplusDotNet.IByteTree bpt = null;
if (tempdirectory!=null)
{
// allocate in filesystem
string treename = System.IO.Path.Combine(tempdirectory, "BPDNtreeS.dat");
string blockname = System.IO.Path.Combine(tempdirectory, "BPDNblockS.dat");
treefile = null;
blockfile = null;
if (bpts == null)
{
// allocate new
if (System.IO.File.Exists(treename))
{
System.IO.File.Delete(treename);
}
if (System.IO.File.Exists(blockname))
{
System.IO.File.Delete(blockname);
}
bpt = BplusDotNet.xBplusTreeBytes.Initialize(treename, blockname, prefixlength, CultureId, nodesize, buffersize);
}
else
{
// reopen old
bpts.Shutdown();
bpt = BplusDotNet.xBplusTreeBytes.ReOpen(treename, blockname);
}
}
else
{
// allocate in memory
if (bpts==null)
{
// allocate new
treefile = new System.IO.MemoryStream();
blockfile = new System.IO.MemoryStream();;
bpt = BplusDotNet.xBplusTreeBytes.Initialize(treefile, blockfile, prefixlength, CultureId, nodesize, buffersize);
}
else
{
// reopen
bpt = BplusDotNet.xBplusTreeBytes.ReOpen(treefile, blockfile);
}
}
return new BplusDotNet.SerializedTree(bpt);
}
public static BplusDotNet.hBplusTree getHTree(BplusDotNet.hBplusTree bpt, ref System.IO.Stream treefile, ref System.IO.Stream blockfile)
{
int CultureId = System.Globalization.CultureInfo.InvariantCulture.LCID;
if (tempdirectory!=null)
{
// allocate in filesystem
string treename = System.IO.Path.Combine(tempdirectory, "BPDNtreeH.dat");
string blockname = System.IO.Path.Combine(tempdirectory, "BPDNblockH.dat");
treefile = null;
blockfile = null;
if (bpt == null)
{
// allocate new
if (System.IO.File.Exists(treename))
{
System.IO.File.Delete(treename);
}
if (System.IO.File.Exists(blockname))
{
System.IO.File.Delete(blockname);
}
bpt = BplusDotNet.hBplusTree.Initialize(treename, blockname, prefixlength, CultureId, nodesize, buffersize);
}
else
{
// reopen old
bpt.Shutdown();
bpt = BplusDotNet.hBplusTree.ReOpen(treename, blockname);
}
}
else
{
// allocate in memory
if (bpt==null)
{
// allocate new
treefile = new System.IO.MemoryStream();
blockfile = new System.IO.MemoryStream();;
bpt = BplusDotNet.hBplusTree.Initialize(treefile, blockfile, prefixlength, CultureId, nodesize, buffersize);
}
else
{
// reopen
bpt = BplusDotNet.hBplusTree.ReOpen(treefile, blockfile);
}
}
return bpt;
}
public static BplusDotNet.xBplusTree getXTree(BplusDotNet.xBplusTree bpt, ref System.IO.Stream treefile, ref System.IO.Stream blockfile)
{
int CultureId = System.Globalization.CultureInfo.InvariantCulture.LCID;
if (tempdirectory!=null)
{
// allocate in filesystem
string treename = System.IO.Path.Combine(tempdirectory, "BPDNtreeX.dat");
string blockname = System.IO.Path.Combine(tempdirectory, "BPDNblockX.dat");
treefile = null;
blockfile = null;
if (bpt == null)
{
// allocate new
if (System.IO.File.Exists(treename))
{
System.IO.File.Delete(treename);
}
if (System.IO.File.Exists(blockname))
{
System.IO.File.Delete(blockname);
}
bpt = BplusDotNet.xBplusTree.Initialize(treename, blockname, prefixlength, CultureId, nodesize, buffersize);
}
else
{
// reopen old
bpt.Shutdown();
bpt = BplusDotNet.xBplusTree.ReOpen(treename, blockname);
}
}
else
{
// allocate in memory
if (bpt==null)
{
// allocate new
treefile = new System.IO.MemoryStream();
blockfile = new System.IO.MemoryStream();;
bpt = BplusDotNet.xBplusTree.Initialize(treefile, blockfile, keylength, CultureId, nodesize, buffersize);
}
else
{
// reopen
bpt = BplusDotNet.xBplusTree.ReOpen(treefile, blockfile);
}
}
bpt.LimitBucketSize(bucketsizelimit);
return bpt;
}
public static void bplustreetest()
{
allinserts = new Hashtable();
System.IO.Stream treestream = new System.IO.MemoryStream();
System.IO.Stream blockstream = new System.IO.MemoryStream();
int KeyLength = 15;
BplusDotNet.BplusTree bpt = BplusDotNet.BplusTree.Initialize(treestream, blockstream, KeyLength);
bpt["this"] = "that";
bpt["this2"] = "that";
bpt["a"] = "xxx";
bpt["c"] = "delete me";
bpt.Recover(true);
bpt.Commit();
bpt["b"] = "yyy";
bpt["a"] = "BAD VALUE";
bpt.Abort();
bpt.RemoveKey("c");
bpt.Recover(false);
string test = bpt["this"];
Debug.WriteLine("got "+test);
test = bpt.FirstKey();
while (test!=null)
{
Debug.WriteLine(test+" :: "+bpt[test]);
test = bpt.NextKey(test);
}
}
public static void abort(BplusDotNet.BplusTreeLong bpt)
{
Debug.WriteLine(" <h3>ABORT!</H3>");
bpt.Abort();
allinserts = (Hashtable) lastcommittedinserts.Clone();
checkit(bpt);
}
public static void commit(BplusDotNet.BplusTreeLong bpt)
{
Debug.WriteLine(" <h3>COMMIT!</H3>");
bpt.Commit();
lastcommittedinserts = (Hashtable)allinserts.Clone();
checkit(bpt);
}
public static BplusDotNet.BplusTreeLong restart(BplusDotNet.BplusTreeLong bpt)
{
Debug.WriteLine(" <h3>RESTART!</H3>");
commit(bpt);
return BplusDotNet.BplusTreeLong.SetupFromExistingStream(bpt.fromfile, bpt.seekStart);
}
public static void inserttest(BplusDotNet.BplusTreeLong bpt, string key, long map)
{
inserttest(bpt, key, map, false);
}
public static void deletetest(BplusDotNet.BplusTreeLong bpt, string key, long map)
{
inserttest(bpt, key, map, true);
}
public static void inserttest(BplusDotNet.BplusTreeLong bpt, string key, long map, bool del)
{
if (del)
{
Debug.WriteLine(" <h3>DELETE bpt["+key+"] = "+map+"</h3>");
bpt.RemoveKey(key);
allinserts.Remove(key);
}
else
{
Debug.WriteLine("<h3>bpt["+key+"] = "+map+"</h3>");
bpt[key] = map;
allinserts[key] = map;
}
checkit(bpt);
}
public static void checkit(BplusDotNet.BplusTreeLong bpt)
{
Debug.WriteLine(bpt.toHtml());
bpt.SanityCheck(true);
ArrayList allkeys = new ArrayList();
foreach (DictionaryEntry d in allinserts)
{
allkeys.Add(d.Key);
}
allkeys.Sort();
allkeys.Reverse();
foreach (object thing in allkeys)
{
string thekey = (string) thing;
long thevalue = (long) allinserts[thing];
if (thevalue!=bpt[thekey])
{
throw new ApplicationException("no match on retrieval "+thekey+" --> "+bpt[thekey]+" not "+thevalue);
}
}
allkeys.Reverse();
string currentkey = bpt.FirstKey();
foreach (object thing in allkeys)
{
string testkey = (string) thing;
if (currentkey==null)
{
throw new ApplicationException("end of keys found when expecting "+testkey);
}
if (!testkey.Equals(currentkey))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -