Exemplo n.º 1
0
        /// <summary>Decrements the count of an (index, value) pair in the multimap, and optionally removes it if the count reaches zero.</summary>
        /// <param name="trans">Transaction used for the operation</param>
        /// <param name="key">Key of the entry</param>
        /// <param name="value">Value for the <paramref name="key"/> to decrement</param>
        /// <remarks>If the updated count reaches zero or less, and AllowNegativeValues is not set, the key will be cleared from the map.</remarks>
        public async Task SubtractAsync([NotNull] IFdbTransaction trans, TKey key, TValue value)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }

            Slice k = this.Subspace.Keys[key, value];

            if (this.AllowNegativeValues)
            {
                trans.AtomicDecrement64(k);
                // note: it's faster, but we will end up with counts less than or equal to 0
                // If 'k' does not already exist, its count will be set to -1
            }
            else
            {
                Slice v = await trans.GetAsync(k).ConfigureAwait(false);

                if (this.AllowNegativeValues || v.ToInt64() > 1)                 //note: Slice.Nil.ToInt64() will return 0
                {
                    trans.AtomicDecrement64(k);
                    //note: since we already read 'k', the AtomicAdd will be optimized into the equivalent of Set(k, v - 1) by the client, unless RYW has been disabled on the transaction
                    //TODO: if AtomicMax ever gets implemented, we could use it to truncate the values to 0
                }
                else
                {
                    trans.Clear(k);
                }
            }
        }
            /// <summary>Decrements the count of an (index, value) pair in the multimap, and optionally removes it if the count reaches zero.</summary>
            /// <param name="trans">Transaction used for the operation</param>
            /// <param name="key">Key of the entry</param>
            /// <param name="value">Value for the <paramref name="key"/> to decrement</param>
            /// <remarks>If the updated count reaches zero or less, and AllowNegativeValues is not set, the key will be cleared from the map.</remarks>
            public void Subtract(IFdbTransaction trans, TKey key, TValue value)
            {
                if (trans == null)
                {
                    throw new ArgumentNullException(nameof(trans));
                }

                // decrement, and optionally clear the key if it reaches zero
                trans.AtomicDecrement64(this.Subspace[key, value], clearIfZero: !this.AllowNegativeValues);
            }