public bool Exists(byte[] key) { BTreeNode <T> node = FindClosestNode(key); BTreeNodeValue <T> value = node.GetValue(key); return(value != null); }
public T Remove(byte[] key) { BTreeNode <T> node = FindClosestNode(key); BTreeNodeValue <T> value = node.RemoveValue(key); if (value == null) { return(default(T)); } return(value.Value); }
public void Insert(byte[] key, T value) { BTreeNode <T> node = FindClosestNode(key); BTreeNodeValue <T> v = node.GetValue(key); if (v != null) { throw new BTreeException("Value already exists."); } node.SetValue(key, value); }
public void Reset() { _currentNode = _rootNode; _currentValue = null; _currentValueType = ValueType.None; }
private BTreeNodeValue <T> RemoveValue(byte[] key) { BTreeNode <T> currentNode = this; while (true) { if (key.Length == currentNode._depth) { currentNode._nodeValueLock.EnterWriteLock(); try { BTreeNodeValue <T> oldValue = currentNode._nodeValue; currentNode._nodeValue = null; return(oldValue); } finally { currentNode._nodeValueLock.ExitWriteLock(); } } if (key.Length < currentNode._depth) { throw new BTreeException("Cannot remove value since the key length is less than node depth."); } ReaderWriterLockSlim currentNodeLock = currentNode._nodeLock; currentNodeLock.EnterWriteLock(); try { if (currentNode._childNodes == null) { //check and remove hold value if (currentNode._holdValue == null) { return(null); } byte[] holdKey = currentNode._holdValue.Key; if (holdKey.Length != key.Length) { return(null); } for (int i = 0; i < key.Length; i++) { if (holdKey[i] != key[i]) { return(null); } } BTreeNodeValue <T> oldValue = currentNode._holdValue; currentNode._holdValue = null; return(oldValue); } //set child node as current node { byte k = key[currentNode._depth]; BTreeNode <T> childNode = currentNode._childNodes[k]; if (childNode == null) { //no value set in child node return(null); } currentNode = childNode; } } finally { currentNodeLock.ExitWriteLock(); } } }
private BTreeNodeValue <T> SetValue(byte[] key, T value) { BTreeNode <T> currentNode = this; while (true) { if (key.Length == currentNode._depth) { currentNode._nodeValueLock.EnterWriteLock(); try { BTreeNodeValue <T> oldValue = currentNode._nodeValue; currentNode._nodeValue = new BTreeNodeValue <T>(key, value); return(oldValue); } finally { currentNode._nodeValueLock.ExitWriteLock(); } } if (key.Length < currentNode._depth) { throw new BTreeException("Cannot set value since the key length is less than node depth."); } ReaderWriterLockSlim currentNodeLock = currentNode._nodeLock; currentNodeLock.EnterWriteLock(); try { if (currentNode._childNodes == null) { if (currentNode._holdValue == null) { //set value into current node hold BTreeNodeValue <T> oldValue = currentNode._holdValue; currentNode._holdValue = new BTreeNodeValue <T>(key, value); return(oldValue); } //explode current node & move hold value to child node { currentNode._childNodes = new BTreeNode <T> [256]; byte k = currentNode._holdValue.Key[currentNode._depth]; BTreeNode <T> childNode = new BTreeNode <T>(currentNode, k); currentNode._childNodes[k] = childNode; if (currentNode._holdValue.Key.Length == childNode._depth) { childNode._nodeValue = currentNode._holdValue; } else { childNode._holdValue = currentNode._holdValue; } currentNode._holdValue = null; } } //set child node as current node { byte k = key[currentNode._depth]; BTreeNode <T> childNode = currentNode._childNodes[k]; if (childNode == null) { childNode = new BTreeNode <T>(currentNode, k); currentNode._childNodes[k] = childNode; } currentNode = childNode; } } finally { currentNodeLock.ExitWriteLock(); } } }