Example #1
0
        /// <summary>Remove an entity from the index</summary>
        /// <param name="trans">Transaction to use</param>
        /// <param name="id">Id of the entity that has been deleted</param>
        /// <param name="value">Previous value of the entity in the index</param>
        public async Task <bool> RemoveAsync([NotNull] IFdbTransaction trans, long id, TValue value)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }

            var key  = this.Location.Keys.Encode(value);
            var data = await trans.GetAsync(key).ConfigureAwait(false);

            if (data.HasValue)
            {
                var builder = new CompressedBitmapBuilder(data);
                builder.Clear((int)id);                 //BUGBUG: 64 bit id!
                trans.Set(key, builder.ToSlice());
                return(true);
            }
            return(false);
        }
Example #2
0
        /// <summary>Update the indexed values of an entity</summary>
        /// <param name="trans">Transaction to use</param>
        /// <param name="id">Id of the entity that has changed</param>
        /// <param name="newValue">Previous value of this entity in the index</param>
        /// <param name="previousValue">New value of this entity in the index</param>
        /// <returns>True if a change was performed in the index; otherwise false (if <paramref name="previousValue"/> and <paramref name="newValue"/>)</returns>
        /// <remarks>If <paramref name="newValue"/> and <paramref name="previousValue"/> are identical, then nothing will be done. Otherwise, the old index value will be deleted and the new value will be added</remarks>
        public async Task <bool> UpdateAsync([NotNull] IFdbTransaction trans, long id, TValue newValue, TValue previousValue)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }

            if (!this.ValueComparer.Equals(newValue, previousValue))
            {
                // remove previous value
                if (this.IndexNullValues || previousValue != null)
                {
                    var key  = this.Location.Keys.Encode(previousValue);
                    var data = await trans.GetAsync(key).ConfigureAwait(false);

                    if (data.HasValue)
                    {
                        var builder = new CompressedBitmapBuilder(data);
                        builder.Clear((int)id);                         //BUGBUG: 64 bit id!
                        trans.Set(key, builder.ToSlice());
                    }
                }

                // add new value
                if (this.IndexNullValues || newValue != null)
                {
                    var key  = this.Location.Keys.Encode(newValue);
                    var data = await trans.GetAsync(key).ConfigureAwait(false);

                    var builder = data.HasValue ? new CompressedBitmapBuilder(data) : CompressedBitmapBuilder.Empty;
                    builder.Set((int)id);                     //BUGBUG: 64 bit id!
                    trans.Set(key, builder.ToSlice());
                }

                // cannot be both null, so we did at least something)
                return(true);
            }
            return(false);
        }