public void Update <TNode>(IStorageHandle handle, ISerializer <TNode> serializer, TNode tnode) { if (_serializer == null) { _serializer = (ISerializer <Node>)serializer; } StorageInfo info; Node node = (Node)(object)tnode; using (_lock.Write()) { if (_cache.TryGetValue(handle, out info)) { Interlocked.Increment(ref info.RefCount); info.Action = StoreAction.Write; info.Node = node; Interlocked.Increment(ref info.RefCount); _ordered.Enqueue(handle); return; } CacheAdd(handle, info = new StorageInfo(handle, node)); info.Action = StoreAction.Write; info.Node = node; Interlocked.Increment(ref info.RefCount); _ordered.Enqueue(handle); } }
Node ISerializer <Node> .ReadFrom(Stream stream) { IStorageHandle handle = _storageHandleSerializer.ReadFrom(stream); bool isLeaf = _boolSerializer.ReadFrom(stream); bool isRoot = _boolSerializer.ReadFrom(stream); int count = _intSerializer.ReadFrom(stream); Element[] items = new Element[count]; for (int i = 0; i < count; i++) { TKey key = default(TKey); if (i > 0 || isLeaf) { key = _keySerializer.ReadFrom(stream); } if (isLeaf) { items[i] = new Element(key, _valueSerializer.ReadFrom(stream)); } else { items[i] = new Element(key, _handleSerializer.ReadFrom(stream)); } } int nodeSize = isLeaf ? _options.MaximumValueNodes : _options.MaximumChildNodes; Assert(nodeSize >= count); Node resurrected = Node.FromElements(handle, isRoot, nodeSize, items); return(resurrected); }
/// <summary> /// Directly enumerates the contents of BPlusTree from disk in read-only mode. /// </summary> /// <param name="options"> The options normally used to create the <see cref="BPlusTree{TKey, TValue}"/> instance </param> /// <returns> Yields the Key/Value pairs found in the file </returns> public static IEnumerable <KeyValuePair <TKey, TValue> > EnumerateFile(BPlusTreeOptions <TKey, TValue> options) { options = options.Clone(); options.CreateFile = CreatePolicy.Never; options.ReadOnly = true; using (INodeStorage store = options.CreateStorage()) { bool isnew; Node root; IStorageHandle hroot = store.OpenRoot(out isnew); if (isnew) { yield break; } NodeSerializer nodeReader = new NodeSerializer(options, new NodeHandleSerializer(store)); if (isnew || !store.TryGetNode(hroot, out root, nodeReader)) { throw new InvalidDataException(); } Stack <KeyValuePair <Node, int> > todo = new Stack <KeyValuePair <Node, int> >(); todo.Push(new KeyValuePair <Node, int>(root, 0)); while (todo.Count > 0) { KeyValuePair <Node, int> cur = todo.Pop(); if (cur.Value == cur.Key.Count) { continue; } todo.Push(new KeyValuePair <Node, int>(cur.Key, cur.Value + 1)); Node child; if (!store.TryGetNode(cur.Key[cur.Value].ChildNode.StoreHandle, out child, nodeReader)) { throw new InvalidDataException(); } if (child.IsLeaf) { for (int ix = 0; ix < child.Count; ix++) { var set = child[ix].ToKeyValuePair(); var enumerator = set.Value.GetEnumerator(); while (enumerator.MoveNext()) { yield return(new KeyValuePair <TKey, TValue>(set.Key, enumerator.Current.Key)); } } } else { todo.Push(new KeyValuePair <Node, int>(child, 0)); } } } }
void ISerializer <IStorageHandle> .WriteTo(IStorageHandle handleIn, Stream stream) { FileId fid = ((FileId)handleIn); PrimitiveSerializer.UInt32.WriteTo(fid.Id, stream); PrimitiveSerializer.UInt32.WriteTo(fid.Unique, stream); }
public Node(IStorageHandle handle, int elementCount) { _handle = handle; _list = new Element[elementCount]; _ltype = LockType.Insert; _count = 0; }
public void Destroy(IStorageHandle handleIn) { Check.Assert <InvalidNodeHandleException>(handleIn is FileId); FileId handle = (FileId)handleIn; _file.Delete(handle.Id); }
public void Destroy(IStorageHandle handle) { _dirty[handle] = null; _dirty.Remove(handle); _cache.Remove(handle); _store.Destroy(handle); }
public void Destroy(IStorageHandle handleIn) { InvalidNodeHandleException.Assert(handleIn is MyStorageHandle); MyStorageHandle handle = (MyStorageHandle)handleIn; handle.Clear(); }
public void Update <T>(IStorageHandle handleIn, ISerializer <T> serializer, T node) { InvalidNodeHandleException.Assert(handleIn is MyStorageHandle); MyStorageHandle handle = (MyStorageHandle)handleIn; handle.Node = node; }
public StorageInfo(IStorageHandle handle, Node node) { _handle = handle; Node = node; Action = StoreAction.None; RefCount = 0; }
public void Destroy(IStorageHandle handle) { using (_lock.Write()) { _cache.Remove(handle); } _store.Destroy(handle); }
void INodeStorage.Update <TNode>(IStorageHandle handle, ISerializer <TNode> serializer, TNode node) { Check.Assert <InvalidOperationException>(ReadOnly == false); using (MemoryStream ms = new MemoryStream()) { serializer.WriteTo(node, ms); _data[((Handle)handle).Id] = ms.ToArray(); } }
public void Update <T>(IStorageHandle handleIn, ISerializer <T> serializer, T node) { Check.Assert <InvalidNodeHandleException>(handleIn is FileId); FileId handle = (FileId)handleIn; using (Stream s = _file.Open(handle.Id, FileAccess.Write)) { try { } finally { serializer.WriteTo(node, s); } } }
public void Update <T>(IStorageHandle handleIn, ISerializer <T> serializer, T node) { Check.Assert <InvalidNodeHandleException>(handleIn is FileId); FileId handle = (FileId)handleIn; using (MemoryStream s = new MemoryStream()) { serializer.WriteTo(node, s); _file.Write(handle.Id, s.GetBuffer(), 0, (int)s.Position); } }
public bool TryGetNode <TNode>(IStorageHandle handleIn, out TNode node, ISerializer <TNode> serializer) { Check.Assert <InvalidNodeHandleException>(handleIn is FileId); FileId handle = (FileId)handleIn; using (Stream s = _file.Open(handle.Id, FileAccess.Read)) { node = serializer.ReadFrom(s); return(true); } }
protected Node(Node copyFrom, LockType type) { _handle = copyFrom._handle; _list = (Element[])copyFrom._list.Clone(); _count = copyFrom._count; _ltype = type; if (_ltype == LockType.Update && !IsLeaf) { _ltype = LockType.Read; } _version = copyFrom._version + 1; }
public void Destroy(IStorageHandle handle) { var recordId = (handle as RecordId); if (recordId == null) { throw new ArgumentException("Handle type is not RecordId"); } var segments = _map.RecordIdMap[recordId.Id]; _map.SetSegmentsFree(segments); _map.RecordIdMap.Remove(recordId.Id); }
public NodePin Create(LockType ltype, bool isLeaf) { IStorageHandle storeHandle = _cache.Storage.Create(); NodeHandle handle = new NodeHandle(storeHandle); object refobj; ILockStrategy lck = _cache.CreateLock(handle, out refobj); int size = isLeaf ? _cache.Options.MaximumValueNodes : _cache.Options.MaximumChildNodes; NodePin pin = new NodePin(handle, lck, ltype, LockType.Insert, refobj, null, new Node(handle.StoreHandle, size)); NodePin.Append(ref _created, pin); return(pin); }
bool INodeStorage.TryGetNode <TNode>(IStorageHandle handle, out TNode node, ISerializer <TNode> serializer) { byte[] bytes; if (_data.TryGetValue(((Handle)handle).Id, out bytes)) { using (MemoryStream ms = new MemoryStream(bytes, false)) node = serializer.ReadFrom(ms); return(true); } node = default(TNode); return(false); }
public bool TryGetNode <TNode>(IStorageHandle handleIn, out TNode node, ISerializer <TNode> serializer) { InvalidNodeHandleException.Assert(handleIn is MyStorageHandle); MyStorageHandle handle = (MyStorageHandle)handleIn; if (handle.Node != null) { node = (TNode)handle.Node; return(true); } node = default(TNode); return(false); }
public bool Equals(IStorageHandle other) { var handle = other as RecordId; if (handle == null) { return(false); } if (handle.Id == Id) { return(true); } return(false); }
public bool TryGetNode <TNode>(IStorageHandle handle, out TNode node, ISerializer <TNode> serializer) { var recordId = (handle as RecordId); if (recordId == null) { throw new ArgumentException("Handle type is not RecordId"); } var segments = _map.RecordIdMap[recordId.Id]; byte[] buffer = ReadBuffer(segments); node = serializer.FromByteArray(buffer); return(true); }
public static Node FromElements(IStorageHandle handle, bool isRoot, int nodeSize, Element[] items) { if (isRoot) { RootNode root = new RootNode(handle); Assert(items.Length == 1); root._list[0] = items[0]; return(root); } Node node = new Node(handle, nodeSize); Array.Copy(items, 0, node._list, 0, items.Length); node._count = items.Length; node._ltype = LockType.Read; return(node); }
public bool CreateValue(IStorageHandle key, out object value) { if (DirtyCache.TryGetValue(key, out value) && value != null) { Success = true; Value = (TNode)value; return(true); } Success = Storage.TryGetNode(key, out Value, Serializer); if (Success) { value = Value; return(true); } value = null; return(false); }
private void Flush(int maxBacklog) { while (_cache.Count >= maxBacklog && _ordered.Count > 0) { IStorageHandle hremove = _ordered.Dequeue(); StorageInfo remove; if (!_cache.TryGetValue(hremove, out remove)) { continue; } int refCount = Interlocked.Decrement(ref remove.RefCount); if (refCount == 0) { _cache.Remove(hremove); if (remove.Action == StoreAction.Write) { _store.Update(remove.Handle, _serializer, remove.Node); } } } }
public void Update <TNode>(IStorageHandle handle, ISerializer <TNode> serializer, TNode node) { var recordId = (handle as RecordId); if (recordId == null) { throw new ArgumentException("Handle type is not RecordId"); } var segments = _map.RecordIdMap[recordId.Id]; var buffer = serializer.ToByteArray(node); if (_map.GetSegmentsNeeded(buffer.Length) != segments.Count) { _map.SetSegmentsFree(segments); segments = _map.GetFreeSegments(buffer.Length); _map.RecordIdMap[recordId.Id] = segments; } WriteBuffer(buffer, segments); }
public bool TryGetNode <TNode>(IStorageHandle handle, out TNode tnode, ISerializer <TNode> serializer) { if (_serializer == null) { _serializer = (ISerializer <Node>)serializer; } StorageInfo info; Node node; using (_lock.Read()) { if (_cache.TryGetValue(handle, out info)) { tnode = (TNode)(object)info.Node; return(true); } } using (_lock.Write()) { if (_cache.TryGetValue(handle, out info)) { tnode = (TNode)(object)info.Node; Interlocked.Increment(ref info.RefCount); _ordered.Enqueue(handle); return(true); } if (!_store.TryGetNode(handle, out tnode, serializer)) { return(false); } node = (Node)(object)tnode; CacheAdd(handle, info = new StorageInfo(handle, node)); tnode = (TNode)(object)info.Node; Interlocked.Increment(ref info.RefCount); _ordered.Enqueue(handle); } return(true); }
public void Update <TNode>(IStorageHandle handle, ISerializer <TNode> serializer, TNode tnode) { if (_serializer == null) { _serializer = (ISerializer <Node>)serializer; } _cache[handle] = tnode; _dirty[handle] = tnode; var completion = _asyncWriteBehind; if (_dirty.Count > _asyncThreshold && (completion == null || completion.IsCompleted)) { try { } finally { bool locked = Monitor.TryEnter(_flushSync); try { if (locked) { completion = _asyncWriteBehind; if (completion == null || completion.IsCompleted) { _asyncWriteBehind = _writeBehindFunc.BeginInvoke(null, null); } } } finally { if (locked) { Monitor.Exit(_flushSync); } } } } }
public void Update <TNode>(IStorageHandle handle, ISerializer <TNode> serializer, TNode tnode) { if (_serializer == null) { _serializer = (ISerializer <Node>)serializer; } _cache[handle] = tnode; _dirty[handle] = tnode; var completion = _asyncWriteBehind; if (_dirty.Count > _asyncThreshold && (completion == null || completion.IsCompleted)) { lock (_writeBehindFunc) { if (completion == null || completion.IsCompleted) { _asyncWriteBehind = _writeBehindFunc.BeginInvoke(null, null); } } } }
public bool TryGetNode <TNode>(IStorageHandle handle, out TNode tnode, ISerializer <TNode> serializer) { if (_serializer == null) { _serializer = (ISerializer <Node>)serializer; } var fetch = new FetchFromStore <TNode> { DirtyCache = _dirty, Storage = _store, Serializer = serializer, }; _cache.AddOrUpdate(handle, ref fetch); if (fetch.Success) { tnode = fetch.Value; return(true); } tnode = default(TNode); return(false); }