public ITransactionState RequestAbort(ITransactionState state)
        {
            var convertedState = ToTransactionState(state);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            if (convertedState.Status.IsCommitted())
            {
                throw new InvalidOperationException();
            }

            if (convertedState.Status == TransactionStatus.Aborted || convertedState.Status == TransactionStatus.AbortRequested)
            {
                return(convertedState);
            }

            if (convertedState.Status == TransactionStatus.Initial || !convertedState.Operations.Any() || convertedState.Operations.All(p => p.State == OperationState.Unapplied))
            {
                return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.Aborted, convertedState.Operations));
            }

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.AbortRequested, convertedState.Operations));
        }
        public ITransactionState Abort(ITransactionState state)
        {
            var convertedState = ToTransactionState(state);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            if (convertedState.Status == TransactionStatus.Aborted)
            {
                return(convertedState);
            }

            if (convertedState.Status == TransactionStatus.Pending ||
                convertedState.Status == TransactionStatus.Committed ||
                convertedState.Status == TransactionStatus.CleanedUp ||
                convertedState.Status == TransactionStatus.Initial ||
                convertedState.Status == TransactionStatus.Prepare)
            {
                throw new InvalidOperationException();
            }

            if (!convertedState.Operations.All(p => p.State == OperationState.Unapplied))
            {
                throw new InvalidOperationException();
            }

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.Aborted, convertedState.Operations));
        }
        public ITransactionState Commit(ITransactionState state)
        {
            var convertedState = ToTransactionState(state);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            // Already committed
            if (convertedState.Status.IsCommitted())
            {
                return(convertedState);
            }

            if (convertedState.Status == TransactionStatus.Initial || convertedState.Status == TransactionStatus.Prepare)
            {
                throw new InvalidOperationException("Cannot commit a this that was not started.");
            }

            if (convertedState.Status == TransactionStatus.Aborted || convertedState.Status == TransactionStatus.AbortRequested)
            {
                throw new InvalidOperationException("Cannot commit an aborted this");
            }

            if (!convertedState.Operations.All(p => p.State == OperationState.Applied))
            {
                throw new InvalidOperationException();
            }

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.Committed, convertedState.Operations));
        }
        public ITransactionState Unapply(ITransactionState state, IOperation operation)
        {
            var convertedState    = ToTransactionState(state);
            var convertedOperaion = ToOperation(operation);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            if (convertedState.Status != TransactionStatus.AbortRequested &&
                convertedState.Status != TransactionStatus.Aborted)
            {
                throw new InvalidOperationException();
            }

            var op = convertedState.Operations.FirstOrDefault(p => p.Id == operation.Id);

            if (op == null)
            {
                throw new InvalidOperationException();
            }

            if (((Operation)op).State == OperationState.Unapplied || convertedState.Status == TransactionStatus.Aborted)
            {
                return(convertedState);
            }

            var id = convertedState.Id;

            var operations = convertedState.Operations.Select(p => p.Id == operation.Id ? new Operation(id, p.Id, p.OperationType, p.EntryType, p.Entry, p.ExpectedVersion, OperationState.Unapplied) : p);

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, convertedState.Status, operations.ToImmutableArray()));
        }
        public ITransactionState BeginCommit(ITransactionState state)
        {
            var convertedState = ToTransactionState(state);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            // Already committed
            if (convertedState.Status.IsCommitted())
            {
                return(convertedState);
            }

            if (convertedState.Status == TransactionStatus.Pending)
            {
                return(convertedState);
            }

            if (convertedState.Status == TransactionStatus.Aborted ||
                convertedState.Status == TransactionStatus.AbortRequested ||
                convertedState.Status == TransactionStatus.Initial)
            {
                throw new InvalidOperationException();
            }

            Assert(convertedState.Status == TransactionStatus.Prepare);

            if (!convertedState.Operations.Any())
            {
                return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.CleanedUp, convertedState.Operations));
            }

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.Pending, convertedState.Operations));
        }
示例#6
0
            public StoredTransaction(ITransactionState transaction)
            {
                Assert(transaction != null);

                Id         = transaction.Id;
                Operations = new List <StoredOperation>(transaction.Operations.Select(p => AsStoredOperation(p)));
                Status     = transaction.Status;
                Version    = transaction.Version;
            }
示例#7
0
        public Task <bool> CompareExchangeAsync(ITransactionState transaction,
                                                ITransactionState comparand,
                                                CancellationToken cancellation = default)
        {
            var data = AsStoredTransaction(transaction);
            var cmpr = AsStoredTransaction(comparand);

            return(_database.CompareExchangeAsync(data, cmpr, p => p.Version, cancellation));
        }
        public Transaction()
        {
            _sumState                 = new SumTransactionState(this);
            _receiverState            = new ReceiverTransactionState(this);
            _startTransactionState    = new StartTransactionState(this);
            _completeTransactionState = new CompleteTransactionState(this);

            _currentState     = _startTransactionState;
            _transactionState = TransactionState.None;
        }
示例#9
0
        private static StoredTransaction AsStoredTransaction(ITransactionState transaction)
        {
            if (transaction == null)
            {
                return(null);
            }

            if (transaction is StoredTransaction result)
            {
                return(result);
            }

            return(new StoredTransaction(transaction));
        }
        public ITransactionState Prepare(ITransactionState state)
        {
            var convertedState = ToTransactionState(state);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            if (convertedState.Status != TransactionStatus.Initial)
            {
                throw new InvalidOperationException();
            }

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.Prepare, convertedState.Operations));
        }
        internal void SetState(TransactionState state)
        {
            _transactionState = state;
            switch (state)
            {
            case TransactionState.None: _currentState = null; break;

            case TransactionState.SumState: _currentState = _sumState; break;

            case TransactionState.ReceiverState: _currentState = _receiverState; break;

            case TransactionState.StartTransactionState: _currentState = _startTransactionState; break;

            case TransactionState.CompleteTransactionState: _currentState = _completeTransactionState; break;
            }
        }
        public WonkaBrePermissionsException(string psErrorMessage, ITransactionState poTransactionState) : base(psErrorMessage)
        {
            CurrentScoreForApproval = MinReqScoreForApproval = 0;

            OwnersConfirmed   = new HashSet <string>();
            OwnersUnconfirmed = new HashSet <string>();

            if (poTransactionState != null)
            {
                CurrentScoreForApproval = poTransactionState.GetCurrentScore();
                MinReqScoreForApproval  = poTransactionState.GetMinScoreRequirement();

                OwnersConfirmed   = poTransactionState.GetOwnersConfirmed();
                OwnersUnconfirmed = poTransactionState.GetOwnersUnconfirmed();
            }
        }
        private TransactionState ToTransactionState(ITransactionState transactionState)
        {
            if (transactionState == null)
            {
                return(null);
            }

            if (transactionState is TransactionState result)
            {
                return(result);
            }

            return(new TransactionState(transactionState.Id,
                                        transactionState.Version,
                                        transactionState.Status,
                                        transactionState.Operations.Select(p => ToOperation(p)).ToImmutableArray()));
        }
        public ITransactionState CleanUp(ITransactionState state)
        {
            var convertedState = ToTransactionState(state);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            // Already committed
            if (convertedState.Status == TransactionStatus.CleanedUp)
            {
                return(convertedState);
            }

            if (convertedState.Status != TransactionStatus.Committed)
            {
                throw new InvalidOperationException();
            }

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.CleanedUp, convertedState.Operations));
        }
        public ITransactionState RemoveOperation(ITransactionState state, IOperation operation)
        {
            var convertedState    = ToTransactionState(state);
            var convertedOperaion = ToOperation(operation);

#if DEBUG
            ValidateEntry(convertedState);
#endif

            if (convertedState.Status != TransactionStatus.Initial)
            {
                throw new InvalidOperationException("Cannot modify a this after it has started.");
            }

            if (!convertedState.Operations.Any(p => p.Id == operation.Id))
            {
                return(convertedState);
            }

            return(new TransactionState(convertedState.Id,
                                        convertedState.Version + 1,
                                        convertedState.Status,
                                        convertedState.Operations.RemoveAll(p => p.Id == operation.Id)));
        }
示例#16
0
 internal void updateState(ITransactionState state)
 {
     stateLabel.Text = state.Name;
 }
示例#17
0
        public void Update()
        {
            ITransactionState state = Robot.State;

            Form.updateState(state);
        }
        public static ITransactionState Delete <TEntry>(this ITransactionStateTransformer transactionStateTransformer, ITransactionState transactionState, TEntry entry, int?expectedVersion, out IOperation operation)
        {
            if (transactionStateTransformer == null)
            {
                throw new ArgumentNullException(nameof(transactionStateTransformer));
            }

            return(transactionStateTransformer.Delete(transactionState, typeof(TEntry), entry, expectedVersion, out operation));
        }
        public static ITransactionState Delete(this ITransactionStateTransformer transactionStateTransformer, ITransactionState transactionState, Type entryType, object entry, int?expectedVersion, out IOperation operation)
        {
            if (transactionStateTransformer == null)
            {
                throw new ArgumentNullException(nameof(transactionStateTransformer));
            }

            return(transactionStateTransformer.AddOperation(transactionState, OperationType.Delete, entryType, entry, expectedVersion, out operation));
        }
示例#20
0
        public Task RemoveAsync(ITransactionState transaction, CancellationToken cancellation = default)
        {
            var data = AsStoredTransaction(transaction);

            return(_database.RemoveAsync(data, cancellation));
        }
        public ITransactionState AddOperation(ITransactionState state, OperationType operationType, Type entryType, object entry, int?expectedVersion, out IOperation operation)
        {
            var convertedState = ToTransactionState(state);

            if (convertedState.Status != TransactionStatus.Initial)
            {
                throw new InvalidOperationException("Cannot modify a this after it has started.");
            }

            if (entryType == null)
            {
                throw new ArgumentNullException(nameof(entryType));
            }

            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }



#if DEBUG
            ValidateEntry(convertedState);
#endif

            if (!operationType.IsValid())
            {
                throw new ArgumentException($"The argument must be one of the values defined in { typeof(OperationType).FullName }.", nameof(operationType));
            }

            if (expectedVersion < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(expectedVersion));
            }

            if (entryType.IsGenericTypeDefinition)
            {
                throw new ArgumentException("The argument must not be an open generic type.", nameof(entryType));
            }

            if (!entryType.IsClass || (entryType.IsGenericType && entryType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                throw new ArgumentException("The argument must be a reference type.", nameof(entryType));
            }

            if (!entryType.IsAssignableFrom(entry.GetType()))
            {
                throw new ArgumentException($"The specified entry must be of a type assignale to '{nameof(entryType)}'.");
            }

            var predicate = DataPropertyHelper.CompilePredicate(entryType, entry);

            if (convertedState.Operations.Any(p => p.EntryType == entryType && predicate(p.Entry)))
            {
                throw new InvalidOperationException("The this cannot have multiple operations for a single entry.");
            }

            var operationId = GetNextOperationId(convertedState);

            var op = new Operation(convertedState.Id, operationId, operationType, entryType, entry, expectedVersion, OperationState.Unapplied);
            operation = op;

            return(new TransactionState(convertedState.Id, convertedState.Version + 1, TransactionStatus.Initial, convertedState.Operations.Add(op)));
        }