public async Task <Result <bool> > SetNext(IMdNode node) { if (Next != null) { if (Next.XORName.SequenceEqual(node.MdLocator.XORName)) { return(Result.OK(false)); // no change } return(new InvalidOperation <bool>($"Cannot change Next. Current: {Next.XORName}")); } else if (!IsFull) { return(new InvalidOperation <bool>($"Cannot set Next until node is full (Current count: {Count} of capacity {Constants.MdCapacity}.")); } var metadata = new MdMetadata { Level = this.Level, Previous = this.Previous, StartIndex = this.StartIndex, Next = node.MdLocator }; // this is not 100% AD var version = await _dataOps.GetEntryVersionAsync(Constants.METADATA_KEY).ConfigureAwait(false); await _dataOps.UpdateObjectAsync(Constants.METADATA_KEY, metadata, version).ConfigureAwait(false); _metadata = metadata; return(Result.OK(true)); }
public async Task <IMdNode> CreateNewMdNodeAsync(MdMetadata metadata) { var mdInfo = await _networkOps.CreateEmptyMd(DataProtocol.DEFAULT_AD_PROTOCOL).ConfigureAwait(false); var dataOps = new MdDataOps(this, _networkOps, mdInfo); var newMd = new MdNode(dataOps, _snapshotter); await newMd.Initialize(metadata).ConfigureAwait(false); return(newMd); }
async Task GetOrAddMetadata(MdMetadata metadata = null) { var keyCount = await _dataOps.GetKeyCountAsync().ConfigureAwait(false); if (keyCount > 0) { _count = keyCount - 1; _metadata = await _dataOps.GetValueAsync <MdMetadata>(Constants.METADATA_KEY).ConfigureAwait(false); return; } metadata ??= new MdMetadata(); await _dataOps.AddObjectAsync(Constants.METADATA_KEY, metadata).ConfigureAwait(false); _metadata = metadata; }
async Task <Result <Pointer> > ExpandLevelAsync(StoredValue value, ExpectedVersion expectedVersion, IMdNode previous) { if (Level == 0) { return(new ArgumentOutOfRange <Pointer>(nameof(Level))); } byte[] snapshot = default; if (_snapshotter != null && previous != null) { var snapshotResult = await _snapshotter.StoreSnapshot(previous); if (!snapshotResult.HasValue) { return(snapshotResult.CastError <byte[], Pointer>()); } snapshot = snapshotResult.Value; } var meta = new MdMetadata { Level = this.Level - 1, Snapshot = snapshot, Previous = previous?.MdLocator, StartIndex = previous?.EndIndex + 1 ?? 0 }; var md = await _dataOps.NodeFactory.CreateNewMdNodeAsync(meta).ConfigureAwait(false); var leafPointer = await md.TryAppendAsync(value, expectedVersion).ConfigureAwait(false); if (!leafPointer.HasValue) { return(leafPointer); } switch (md.Type) { case MdType.Pointers: // i.e. we have still not reached the end of the tree await AddAsync(new Pointer { MdLocator = md.MdLocator, ValueType = typeof(Pointer).Name }).ConfigureAwait(false); break; case MdType.Values: // i.e. we are now right above leaf level await AddAsync(new Pointer { MdLocator = leafPointer.Value.MdLocator, ValueType = typeof(Pointer).Name }).ConfigureAwait(false); break; default: return(new ArgumentOutOfRange <Pointer>(nameof(md.Type))); } return(leafPointer); }
public Task Initialize(MdMetadata metadata) => GetOrAddMetadata(metadata);