public BTreePage Split(IFixedLengthKey key, int offset, long pageOffset, float splitFactor) { ProcessDeferredUpdates(); if (splitFactor >= 1 || splitFactor < 0.1) { throw new Exception("BTreePage.Split: splitFactor = " + splitFactor.ToString() + " is not applicable"); } int mIndex = (int)(_maxCount * splitFactor); int off; IFixedLengthKey minKey = MemoryRead(mIndex, out off).FactoryMethod(); BTreePage splittedPage = new BTreePage( new BTreeNode(minKey, off, pageOffset, _maxCount - mIndex), _maxCount, _stream, _factoryKey); CopyBytes(_bytes, mIndex, splittedPage._bytes, 0, _maxCount - mIndex); _treeNode.SetNewCount(mIndex); _rightBound = mIndex; _modified = splittedPage._modified = true; if (key.CompareTo(minKey) < 0) { InsertKey(key, offset); } else { splittedPage.InsertKey(key, offset); } return(splittedPage); }
private void UpdateMinKey(IFixedLengthKey key, int offset) { int keyCompare = key.CompareTo(_treeNode.MinKey); if (keyCompare < 0 || (keyCompare == 0 && offset < _treeNode.MinOffset)) { _treeNode.ChangeMinKey(key.FactoryMethod(), offset); } }
private IFixedLengthKey SearchR(int l, int r, IFixedLengthKey key, int offset, out int index) { index = -1; if (l > r) { return(null); } index = r; IFixedLengthKey mKey = null; while (l < r) { int m = (l + r) >> 1; int off; mKey = MemoryRead(m, out off); int compareResult = key.CompareTo(mKey); if (compareResult == 0 && offset != -1) { compareResult = offset - off; } if (compareResult >= 0) { l = m + 1; index = l; } else { r = m; index = m; } } return(mKey); }
public void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, ArrayList keys_offsets) { if (_treeNode.Count == 0) { return; } int index, offset; KeyPair pair; IFixedLengthKey deferredKey; int deferredInsertionsIndex = ~GetDeferredInsertionIndex(beginKey, -1); IFixedLengthKey foundKey = SearchL(0, _rightBound, beginKey, -1, out index); if (foundKey != null && index != -1 && index < _rightBound) { int deferredDeletionsIndex = ~GetDeferredDeletionIndex(beginKey, -1); SetPosition(index); for ( ; index < _rightBound; ++index) { IFixedLengthKey aKey = MemoryReadNext(out offset); for ( ; deferredInsertionsIndex < _deferredInsertions.Count; ++deferredInsertionsIndex) { pair = (KeyPair)_deferredInsertions[deferredInsertionsIndex]; deferredKey = pair._key; if (deferredKey.CompareTo(aKey) > 0) { break; } if (beginKey.CompareTo(deferredKey) <= 0) { if (endKey.CompareTo(deferredKey) < 0) { break; } keys_offsets.Add(pair); } } if (endKey.CompareTo(aKey) < 0) { break; } bool deleted = false; while (deferredDeletionsIndex < _deferredDeletions.Count) { pair = (KeyPair)_deferredDeletions[deferredDeletionsIndex]; int compareResult = pair._key.CompareTo(aKey); if (compareResult == 0) { compareResult = pair._offset - offset; } if (compareResult > 0) { break; } ++deferredDeletionsIndex; if (compareResult == 0) { deleted = true; break; } } if (!deleted) { keys_offsets.Add(new KeyPair(aKey.FactoryMethod(), offset)); } } } for ( ; deferredInsertionsIndex < _deferredInsertions.Count; ++deferredInsertionsIndex) { pair = (KeyPair)_deferredInsertions[deferredInsertionsIndex]; deferredKey = pair._key; if (beginKey.CompareTo(deferredKey) <= 0) { if (endKey.CompareTo(deferredKey) < 0) { break; } keys_offsets.Add(pair); } } }