public void Remove(TKey key, Func <KeyValuePair <TKey, TValue>, bool> filter) { if (BlockReference.BlockId != 0) { var blockId = BlockReference.BlockId; var root = Node <TKey, TValue> .GetNode(Config, BlockReference.BlockId); root.Remove(key, filter, added => { Config.Blocks.FreeBlock(blockId); if (added.Count == 0) { BlockReference.BlockId = 0; } else if (added.Count == 1) { BlockReference.BlockId = added[0].Value; } else { BlockReference.BlockId = InteriorNode <TKey, TValue> .CreateRoot( Config, added ); } }); } }
public void Insert(TKey key, TValue value) { if (BlockReference.BlockId == 0) { BlockReference.BlockId = LeafNode <TKey, TValue> .CreateRoot(Config, new KeyValuePair <TKey, TValue>(key, value)); } else { var blockId = BlockReference.BlockId; var root = Node <TKey, TValue> .GetNode(Config, BlockReference.BlockId); root.Insert(key, value, added => { Config.Blocks.FreeBlock(blockId); if (added.Count == 1) { BlockReference.BlockId = added[0].Value; } else { BlockReference.BlockId = InteriorNode <TKey, TValue> .CreateRoot( Config, added ); } }); } }
public static INode <TKey, TValue> GetNode(IGistConfig <TKey, TValue> config, int blockId) { var block = config.Blocks.GetBlock(blockId); if (block.UserData != null) { return((INode <TKey, TValue>)block.UserData); } INode <TKey, TValue> node = null; using (var stream = new MemoryStream(block.Data, false)) { using (var reader = new BinaryReader(stream)) { var header = ReadBlockHeader(reader); if ((header.Flags & NodeFlags.IsLeafNode) == NodeFlags.IsLeafNode) { node = new LeafNode <TKey, TValue>( config, block, config.Ext.CreateLeafRecords(reader) ); } else if ((header.Flags & NodeFlags.IsInteriorNode) == NodeFlags.IsInteriorNode) { node = new InteriorNode <TKey, TValue>( config, block, config.Ext.CreateIndexRecords(reader) ); } } } if (node == null) { throw new ApplicationException("Bad block header"); } block.UserData = node; return(node); }