public async Task <Result <Pointer> > UpdateAsync(string type, MdLocator location) { var(pointer, value) = (await _dataTree.GetAllPointerValuesAsync().ConfigureAwait(false)) .Single(c => c.Item1.MdKey == type); var mdResult = await MdAccess.LocateAsync(pointer.MdLocator).ConfigureAwait(false); if (!mdResult.HasValue) { return(Result.Fail <Pointer>(mdResult.ErrorCode.Value, mdResult.ErrorMsg)); } value.Payload = location.Json(); return(await mdResult.Value.SetAsync(type, value).ConfigureAwait(false)); }
/// <summary> /// Instead of traversing through the tree on every add, /// we keep a reference to current leaf, and add to it directly. /// </summary> /// <param name="key">Key under which the value will be stored.</param> /// <param name="value">The value to store.</param> /// <returns>A pointer to the value that was added.</returns> async Task <Result <Pointer> > DirectlyAddToLeaf(string key, StoredValue value) { if (_currentLeaf == null) { _currentLeaf = _head; } else if (_currentLeaf.IsFull) { var result = await _head.AddAsync(key, value).ConfigureAwait(false); var leafResult = await MdAccess.LocateAsync(result.Value.MdLocator); if (leafResult.HasValue) { _currentLeaf = leafResult.Value; } return(result); } return(await _currentLeaf.AddAsync(key, value)); }
/// <summary> /// Adds data to a tree structure /// that grows in an unbalanced way. /// </summary> /// <param name="key">Key under which the value will be stored.</param> /// <param name="value">The value to store.</param> /// <returns>A pointer to the value that was added, to be used for indexing.</returns> public async Task <Result <Pointer> > AddAsync(string key, StoredValue value) { if (_head.IsFull) { // create new head, add pointer to current head in to it. // the level > 0 indicates its role as pointer holder var newHead = await MdAccess.CreateAsync(_head.Level + 1).ConfigureAwait(false); var pointer = new Pointer { MdLocator = _head.MdLocator, ValueType = typeof(Pointer).Name }; await newHead.AddAsync(pointer).ConfigureAwait(false); _head = newHead; await _onHeadAddressChange(newHead.MdLocator).ConfigureAwait(false); } return(await DirectlyAddToLeaf(key, value).ConfigureAwait(false)); }