本文整理汇总了C#中Slice.Set方法的典型用法代码示例。如果您正苦于以下问题:C# Slice.Set方法的具体用法?C# Slice.Set怎么用?C# Slice.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slice
的用法示例。
在下文中一共展示了Slice.Set方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DumpHumanReadable
public static void DumpHumanReadable(Transaction tx, string path, Page start)
{
using (var writer = File.CreateText(path))
{
var stack = new Stack<Page>();
stack.Push(start);
writer.WriteLine("Root page #{0}",start.PageNumber);
while (stack.Count > 0)
{
var currentPage = stack.Pop();
if (currentPage.IsLeaf)
{
writer.WriteLine();
writer.WriteLine("Page #{0}, NumberOfEntries = {1}, Flags = {2} (Leaf), Used: {3} : {4}", currentPage.PageNumber,currentPage.NumberOfEntries,currentPage.Flags, currentPage.SizeUsed, currentPage.CalcSizeUsed());
if(currentPage.NumberOfEntries <= 0)
writer.WriteLine("Empty page (tree corrupted?)");
var key = new Slice(SliceOptions.Key);
for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries;nodeIndex++)
{
var node = currentPage.GetNode(nodeIndex);
key.Set(node);
writer.WriteLine("Node #{0}, Flags = {1}, {4} = {2}, Key = {3}, Entry Size: {5}", nodeIndex, node->Flags, node->DataSize, MaxString(key.ToString(), 25), node->Flags == NodeFlags.Data ? "Size" : "Page",
SizeOf.NodeEntry(node));
}
writer.WriteLine();
}
else if(currentPage.IsBranch)
{
writer.WriteLine();
writer.WriteLine("Page #{0}, NumberOfEntries = {1}, Flags = {2} (Branch), Used: {3} : {4}", currentPage.PageNumber, currentPage.NumberOfEntries, currentPage.Flags, currentPage.SizeUsed, currentPage.SizeUsed);
var key = new Slice(SliceOptions.Key);
for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++)
{
var node = currentPage.GetNode(nodeIndex);
writer.WriteLine("Node #{2}, {0} / to page #{1}, Entry Size: {3}", GetBranchNodeString(nodeIndex, key, currentPage, node), node->PageNumber, nodeIndex,
SizeOf.NodeEntry(node));
}
for (int nodeIndex = 0; nodeIndex < currentPage.NumberOfEntries; nodeIndex++)
{
var node = currentPage.GetNode(nodeIndex);
if (node->PageNumber < 0 || node->PageNumber > tx.State.NextPageNumber)
{
writer.Write("Found invalid reference to page #{0}", currentPage.PageNumber);
stack.Clear();
break;
}
var child = tx.GetReadOnlyPage(node->PageNumber);
stack.Push(child);
}
writer.WriteLine();
}
}
}
}
示例2: CanIterateInOrder
public void CanIterateInOrder()
{
var random = new Random();
var buffer = new byte[512];
random.NextBytes(buffer);
using (var tx = Env.NewTransaction(TransactionFlags.ReadWrite))
{
for (int i = 0; i < 25; i++)
{
tx.State.Root.Add(tx, i.ToString("0000"), new MemoryStream(buffer));
}
tx.Commit();
}
using (var tx = Env.NewTransaction(TransactionFlags.Read))
{
var iterator = tx.State.Root.Iterate(tx);
Assert.True(iterator.Seek(Slice.BeforeAllKeys));
var slice = new Slice(SliceOptions.Key);
for (int i = 0; i < 24; i++)
{
slice.Set(iterator.Current);
Assert.Equal(i.ToString("0000"), slice);
Assert.True(iterator.MoveNext());
}
slice.Set(iterator.Current);
Assert.Equal(24.ToString("0000"), slice);
Assert.False(iterator.MoveNext());
}
}
示例3: ExpandMultiTreeNestedPageSize
private void ExpandMultiTreeNestedPageSize(Transaction tx, Slice key, Slice value, byte* nestedPagePtr, ushort newSize, int currentSize)
{
Debug.Assert(newSize > currentSize);
TemporaryPage tmp;
using (tx.Environment.GetTemporaryPage(tx, out tmp))
{
var tempPagePointer = tmp.TempPagePointer;
NativeMethods.memcpy(tempPagePointer, nestedPagePtr, currentSize);
Delete(key); // release our current page
Page nestedPage = new Page(tempPagePointer, "multi tree", (ushort)currentSize);
var ptr = DirectAdd(key, newSize);
var newNestedPage = new Page(ptr, "multi tree", newSize)
{
Lower = (ushort)Constants.PageHeaderSize,
Upper = newSize,
Flags = PageFlags.Leaf,
PageNumber = -1L // mark as invalid page number
};
Slice nodeKey = new Slice(SliceOptions.Key);
for (int i = 0; i < nestedPage.NumberOfEntries; i++)
{
var nodeHeader = nestedPage.GetNode(i);
nodeKey.Set(nodeHeader);
newNestedPage.AddDataNode(i, nodeKey, 0,
(ushort)(nodeHeader->Version - 1)); // we dec by one because AdddataNode will inc by one, and we don't want to change those values
}
newNestedPage.Search(value);
newNestedPage.AddDataNode(newNestedPage.LastSearchPosition, value, 0, 0);
}
}
示例4: GetActualKey
private Slice GetActualKey(Page page, int pos)
{
var node = page.GetNode(pos);
var key = new Slice(node);
while (key.Size == 0)
{
Debug.Assert(page.IsBranch);
page = _tx.GetReadOnlyPage(node->PageNumber);
node = page.GetNode(0);
key.Set(node);
}
return key;
}
示例5: Search
public NodeHeader* Search(Slice key)
{
if (NumberOfEntries == 0)
{
LastSearchPosition = 0;
LastMatch = 1;
return null;
}
if (key.Options == SliceOptions.BeforeAllKeys)
{
LastSearchPosition = 0;
LastMatch = 1;
return GetNode(0);
}
if (key.Options == SliceOptions.AfterAllKeys)
{
LastMatch = -1;
LastSearchPosition = NumberOfEntries - 1;
return GetNode(LastSearchPosition);
}
var pageKey = new Slice(SliceOptions.Key);
if (NumberOfEntries == 1)
{
pageKey.Set(GetNode(0));
LastMatch = key.Compare(pageKey);
LastSearchPosition = LastMatch > 0 ? 1 : 0;
return LastSearchPosition == 0 ? GetNode(0) : null;
}
int low = IsLeaf ? 0 : 1;
int high = NumberOfEntries - 1;
int position = 0;
while (low <= high)
{
position = (low + high) >> 1;
var node = GetNode(position);
pageKey.Set(node);
LastMatch = key.Compare(pageKey);
if (LastMatch == 0)
break;
if (LastMatch > 0)
low = position + 1;
else
high = position - 1;
}
if (LastMatch > 0) // found entry less than key
{
position++; // move to the smallest entry larger than the key
}
Debug.Assert(position < ushort.MaxValue);
LastSearchPosition = position;
if (position >= NumberOfEntries)
return null;
return GetNode(position);
}
示例6: Dump
public string Dump()
{
var sb = new StringBuilder();
var slice = new Slice(SliceOptions.Key);
for (var i = 0; i < NumberOfEntries; i++)
{
var n = GetNode(i);
slice.Set(n);
sb.Append(slice).AppendLine();
}
return sb.ToString();
}
示例7: Dump
public static void Dump(Transaction tx, string path, Page start, int showNodesEvery = 25)
{
using (var writer = File.CreateText(path))
{
writer.WriteLine(@"
digraph structs {
node [shape=Mrecord]
rankdir=LR;
bgcolor=transparent;
");
var stack = new Stack<Page>();
stack.Push(start);
var references = new StringBuilder();
while (stack.Count > 0)
{
var p = stack.Pop();
writer.WriteLine(@"
subgraph cluster_p_{0} {{
label=""Page #{0}"";
color={3};
p_{0} [label=""Page: {0}|{1}|Entries: {2:#,#} | {4:p} : {5:p} utilization""];
", p.PageNumber, p.Flags, p.NumberOfEntries, p.IsLeaf ? "black" : "blue",
Math.Round(((AbstractPager.PageSize - p.SizeLeft) / (double)AbstractPager.PageSize), 2),
Math.Round(((AbstractPager.PageSize - p.CalcSizeLeft()) / (double)AbstractPager.PageSize), 2));
var key = new Slice(SliceOptions.Key);
if (p.IsLeaf && showNodesEvery > 0)
{
writer.WriteLine(" p_{0}_nodes [label=\" Entries:", p.PageNumber);
for (int i = 0; i < p.NumberOfEntries; i += showNodesEvery)
{
if (i != 0 && showNodesEvery >= 5)
{
writer.WriteLine(" ... {0:#,#} keys redacted ...", showNodesEvery - 1);
}
var node = p.GetNode(i);
key.Set(node);
writer.WriteLine("{0} - {2} {1:#,#}", MaxString(key.ToString(), 25),
node->DataSize, node->Flags == NodeFlags.Data ? "Size" : "Page");
}
if (p.NumberOfEntries < showNodesEvery)
{
writer.WriteLine(" ... {0:#,#} keys redacted ...", p.NumberOfEntries - 1);
}
writer.WriteLine("\"];");
}
else if (p.IsBranch)
{
writer.Write(" p_{0}_refs [label=\"", p.PageNumber);
for (int i = 0; i < p.NumberOfEntries; i++)
{
var node = p.GetNode(i);
writer.Write("{3}<{2}> {0} / to page {1}", GetBranchNodeString(i, key, p, node), node->PageNumber,
i, i == 0 ? "" : "|");
}
writer.WriteLine("\"];");
var prev = -1L;
for (int i = 0; i < p.NumberOfEntries; i++)
{
var node = p.GetNode(i);
if (node->PageNumber < 0 || node->PageNumber > tx.State.NextPageNumber)
{
writer.Write(" p_{0}_refs [label=\"CORRUPTED\"; Color=RED];", p.PageNumber);
stack.Clear();
break;
}
var child = tx.GetReadOnlyPage(node->PageNumber);
stack.Push(child);
references.AppendFormat(" p_{0}_refs:{3} -> p_{1} [label=\"{2}\"];", p.PageNumber, child.PageNumber, GetBranchNodeString(i, key, p, node), i).AppendLine();
if (prev > -1)
references.AppendFormat(" p_{0} -> p_{1} [style=\"invis\"];", child.PageNumber, prev);
prev = child.PageNumber;
}
}
writer.WriteLine(" }");
}
writer.WriteLine(references.ToString());
writer.WriteLine("}");
}
}
示例8: GetBranchNodeString
private static unsafe string GetBranchNodeString(int i, Slice key, Page p, NodeHeader* node)
{
string keyStr;
if (i == 0 && key.Size == 0)
{
key.Set(p.GetNode(1));
keyStr = "(lt " + key + ")";
}
else
{
key.Set(node);
keyStr = key.ToString();
}
return MaxString(keyStr, 25);
}