private NodeHeader* SearchPrefixed( MemorySlice key ) { key.PrepareForSearching(); int numberOfEntries = NumberOfEntries; if (numberOfEntries == 0) { LastSearchPosition = 0; LastMatch = 1; return null; } switch (key.Options) { case SliceOptions.Key: { var pageKey = CreateNewEmptyKey(); if (numberOfEntries == 1) { var node = GetNode(0); SetNodeKey(node, ref pageKey); LastMatch = key.Compare(pageKey); LastSearchPosition = LastMatch > 0 ? 1 : 0; return LastSearchPosition == 0 ? node : null; } int low = IsLeaf ? 0 : 1; int high = numberOfEntries - 1; int position = 0; while (low <= high) { position = (low + high) >> 1; var node = (NodeHeader*)(_base + KeysOffsets[position]); SetNodeKey(node, ref pageKey); 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); } case SliceOptions.BeforeAllKeys: { LastSearchPosition = 0; LastMatch = 1; return GetNode(0); } case SliceOptions.AfterAllKeys: { LastMatch = -1; LastSearchPosition = numberOfEntries - 1; return GetNode(LastSearchPosition); } default: throw new NotSupportedException("This SliceOptions is not supported. Make sure you have updated this code when adding a new one."); } }
public NodeHeader *Search(MemorySlice key) { if (KeysPrefixed) { key.PrepareForSearching(); } 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 = CreateNewEmptyKey(); if (NumberOfEntries == 1) { SetNodeKey(GetNode(0), ref pageKey); 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 = (NodeHeader *)(_base + KeysOffsets[position]); SetNodeKey(node, ref pageKey); 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)); }
private NodeHeader *SearchPrefixed(MemorySlice key) { key.PrepareForSearching(); int numberOfEntries = NumberOfEntries; if (numberOfEntries == 0) { LastSearchPosition = 0; LastMatch = 1; return(null); } switch (key.Options) { case SliceOptions.Key: { var pageKey = CreateNewEmptyKey(); if (numberOfEntries == 1) { var node = GetNode(0); SetNodeKey(node, ref pageKey); LastMatch = key.Compare(pageKey); LastSearchPosition = LastMatch > 0 ? 1 : 0; return(LastSearchPosition == 0 ? node : null); } int low = IsLeaf ? 0 : 1; int high = numberOfEntries - 1; int position = 0; while (low <= high) { position = (low + high) >> 1; var node = (NodeHeader *)(_base + KeysOffsets[position]); SetNodeKey(node, ref pageKey); 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)); } case SliceOptions.BeforeAllKeys: { LastSearchPosition = 0; LastMatch = 1; return(GetNode(0)); } case SliceOptions.AfterAllKeys: { LastMatch = -1; LastSearchPosition = numberOfEntries - 1; return(GetNode(LastSearchPosition)); } default: throw new NotSupportedException("This SliceOptions is not supported. Make sure you have updated this code when adding a new one."); } }
public NodeHeader* Search(MemorySlice key) { if(KeysPrefixed) key.PrepareForSearching(); 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 = CreateNewEmptyKey(); if (NumberOfEntries == 1) { SetNodeKey(GetNode(0), ref pageKey); 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 = (NodeHeader*)(_base + KeysOffsets[position]); SetNodeKey(node, ref pageKey); 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); }