public override unsafe void Execute(TransactionOperationContext context, Table items, long index, DatabaseRecord record, RachisState state, out object result)
        {
            var identitiesItems = context.Transaction.InnerTransaction.OpenTable(ClusterStateMachine.IdentitiesSchema, ClusterStateMachine.Identities);
            var listResult      = new List <long>();

            foreach (var identity in Identities)
            {
                CompareExchangeCommandBase.GetKeyAndPrefixIndexSlices(context.Allocator, DatabaseName, identity, index, out var keyTuple, out var indexTuple);

                using (keyTuple.Scope)
                    using (indexTuple.Scope)
                        using (Slice.External(context.Allocator, keyTuple.Buffer.Ptr, keyTuple.Buffer.Length, out var keySlice))
                            using (Slice.External(context.Allocator, indexTuple.Buffer.Ptr, indexTuple.Buffer.Length, out var prefixIndexSlice))
                            {
                                long value;
                                if (identitiesItems.ReadByKey(keySlice, out var reader))
                                {
                                    value  = GetValue(reader);
                                    value += 1;
                                }
                                else
                                {
                                    value = 1;
                                }

                                UpdateTableRow(index, identitiesItems, value, keySlice, prefixIndexSlice);

                                listResult.Add(value);
                            }
            }

            result = listResult;
        }
Beispiel #2
0
        public override unsafe void Execute(ClusterOperationContext context, Table items, long index, RawDatabaseRecord record, RachisState state, out object result)
        {
            var identitiesItems = context.Transaction.InnerTransaction.OpenTable(ClusterStateMachine.IdentitiesSchema, ClusterStateMachine.Identities);

            CompareExchangeCommandBase.GetKeyAndPrefixIndexSlices(context.Allocator, DatabaseName, Prefix, index, out var keyTuple, out var indexTuple);

            using (keyTuple.Scope)
                using (indexTuple.Scope)
                    using (Slice.External(context.Allocator, keyTuple.Buffer.Ptr, keyTuple.Buffer.Length, out var keySlice))
                        using (Slice.External(context.Allocator, indexTuple.Buffer.Ptr, indexTuple.Buffer.Length, out var prefixIndexSlice))
                        {
                            long value;
                            if (identitiesItems.SeekOnePrimaryKeyPrefix(keySlice, out var entry))
                            {
                                value = GetValue(entry);
                                value++;
                            }
                            else
                            {
                                value = 1;
                            }

                            UpdateTableRow(index, identitiesItems, value, keySlice, prefixIndexSlice);
                            result = value;
                        }
        }
        public override unsafe void Execute(ClusterOperationContext context, Table items, long index, RawDatabaseRecord record, RachisState state, out object result)
        {
            var resultDict = new Dictionary<string, long>();
            var identitiesItems = context.Transaction.InnerTransaction.OpenTable(ClusterStateMachine.IdentitiesSchema, ClusterStateMachine.Identities);

            foreach (var kvp in Identities)
            {
                CompareExchangeCommandBase.GetKeyAndPrefixIndexSlices(context.Allocator, DatabaseName, kvp.Key, index, out var keyTuple, out var indexTuple);

                using (keyTuple.Scope)
                using (indexTuple.Scope)
                using (Slice.External(context.Allocator, keyTuple.Buffer.Ptr, keyTuple.Buffer.Length, out var keySlice))
                using (Slice.External(context.Allocator, indexTuple.Buffer.Ptr, indexTuple.Buffer.Length, out var prefixIndexSlice))
                {
                    bool isSet;
                    if (Force == false)
                    {
                        isSet = false;
                        if (identitiesItems.SeekOnePrimaryKeyPrefix(keySlice, out var tvr))
                        {
                            var value = GetValue(tvr);
                            if (value < kvp.Value)
                                isSet = true;
                        }
                        else
                        {
                            using (identitiesItems.Allocate(out var tvb))
                            {
                                tvb.Add(keySlice);
                                tvb.Add(kvp.Value);
                                tvb.Add(index);
                                tvb.Add(prefixIndexSlice);

                                identitiesItems.Set(tvb);
                            }
                        }

                    }
                    else
                        isSet = true;

                    var keyString = keySlice.ToString().ToLowerInvariant();
                    resultDict.TryGetValue(keyString, out var oldVal);
                    long newVar;

                    if (isSet)
                    {
                        UpdateTableRow(index, identitiesItems, kvp.Value, keySlice, prefixIndexSlice);
                        newVar = kvp.Value;
                    }
                    else
                    {
                        identitiesItems.SeekOnePrimaryKeyPrefix(keySlice, out var tvr);
                        newVar = GetValue(tvr);
                    }

                    resultDict[keyString] = Math.Max(oldVal, newVar);
                }
            }

            result = resultDict;
        }