Ejemplo n.º 1
0
        /// <inheritdoc/>
        public async Task <uint> AllocateAsync(CancellationToken ct)
        {
            while (true)
            {
                // Get current value
                var cur = await _indices.FindAsync <Bitmap>(_id, ct);

                if (cur == null)
                {
                    // Add new index
                    try {
                        var idx   = new Bitmap();
                        var value = idx.Allocate();
                        await _indices.AddAsync(idx, ct, _id,
                                                kWithStrongConsistency);

                        return(value);
                    }
                    catch (ConflictingResourceException) {
                        // Doc was added from another process/thread
                    }
                }
                else
                {
                    // Get next free index
                    try {
                        var idx   = new Bitmap(cur.Value);
                        var value = idx.Allocate();
                        await _indices.ReplaceAsync(cur, idx, ct,
                                                    kWithStrongConsistency);

                        return(value); // Success - return index
                    }
                    catch (ResourceOutOfDateException) {
                        // Etag is no match
                    }
                }
            }
        }