Ejemplo n.º 1
0
        public void SetAndGetValueUsingTwoTransactions()
        {
            Machine machine = new Machine();
            TransactionalReference reference = new TransactionalReference();
            reference.SetValue("foo");
            Assert.AreEqual("foo", reference.GetValue());

            Transaction transaction1 = new Transaction(machine);
            Transaction transaction2 = new Transaction(machine);

            reference.SetValue("bar", transaction1);
            Assert.AreEqual("foo", reference.GetValue(transaction2));
            Assert.AreEqual("bar", reference.GetValue(transaction1));

            transaction1.Complete();
            transaction1.Dispose();

            Assert.IsTrue(reference.HasSnapshots);

            Assert.AreEqual("bar", reference.GetValue());
            Assert.AreEqual("foo", reference.GetValue(transaction2));

            transaction2.Dispose();

            Assert.IsFalse(reference.HasSnapshots);
        }
Ejemplo n.º 2
0
 public void SetAndGetValueFromReferenceInNotCommitedTransaction()
 {
     Machine machine = new Machine();
     TransactionalReference reference = new TransactionalReference();
     reference.SetValue("foo");
     Assert.AreEqual("foo", reference.GetValue());
     Transaction transaction = new Transaction(machine);
     Machine.CurrentTransaction = transaction;
     reference.SetValue("bar");
     Assert.AreEqual("bar", reference.GetValue());
     transaction.Dispose();
     Machine.CurrentTransaction = null;
     Assert.AreEqual("foo", reference.GetValue());
     Assert.IsFalse(reference.HasSnapshots);
 }
Ejemplo n.º 3
0
        public void RaiseIfSetValueFromTwoTransactions()
        {
            Machine machine = new Machine();
            TransactionalReference reference = new TransactionalReference();
            reference.SetValue("foo");
            Assert.AreEqual("foo", reference.GetValue());

            Transaction transaction1 = new Transaction(machine);
            Transaction transaction2 = new Transaction(machine);

            reference.SetValue("bar", transaction1);
            reference.SetValue("newbar", transaction2);
            transaction1.Complete();
            transaction2.Complete();
        }
Ejemplo n.º 4
0
        public void Execute(IBindingEnvironment environment)
        {
            if (Machine.CurrentTransaction != null)
            {
                this.command.Execute(environment);
                return;
            }

            using (Transaction trans = new Transaction(Machine.Current))
            {
                Machine.CurrentTransaction = trans;
                this.command.Execute(environment);
                Machine.CurrentTransaction = null;
                trans.Complete();
            }
        }
Ejemplo n.º 5
0
        public void Complete(Transaction transaction, long timestamp)
        {
            lock (this)
            {
                if (this.pending != transaction)
                    return;

                if (this.snapshots.Count == 0)
                    transaction.Machine.RegisterSnapshot(this);

                this.snapshots[this.timestamp] = this.value;

                this.value = this.pendingvalue;
                this.timestamp = timestamp;

                this.pending = null;
                this.pendingvalue = null;
            }
        }
Ejemplo n.º 6
0
 public void Dispose(Transaction transaction)
 {
     lock (this)
     {
         if (this.pending == transaction)
         {
             this.pending = null;
             this.pendingvalue = null;
         }
     }
 }
Ejemplo n.º 7
0
        public void SetValue(object value, Transaction transaction)
        {
            lock (this)
            {
                if (this.pending != null && this.pending != transaction)
                    throw new InvalidOperationException("Reference changed in another running Transaction");

                this.pending = transaction;
                this.pendingvalue = value;
                transaction.RegisterTransactionalReference(this);
            }
        }
Ejemplo n.º 8
0
        public object GetValue(Transaction transaction)
        {
            lock (this)
            {
                if (transaction == this.pending)
                    return this.pendingvalue;

                // TODO review snapshots
                if (this.snapshots.Count == 0)
                    return this.value;

                long? selected = null;

                foreach (long ts in this.snapshots.Keys)
                    if (!Transaction.IsPrevious(transaction.Id, ts))
                        if (!selected.HasValue || Transaction.IsPrevious(selected.Value, ts))
                            selected = ts;

                if (selected.HasValue)
                    return this.snapshots[selected.Value];

                return this.value;
            }
        }
Ejemplo n.º 9
0
        internal void UnregisterTransaction(Transaction transaction)
        {
            Transaction oldest = null;

            lock (this.transactions)
            {
                this.transactions.Remove(transaction);

                foreach (Transaction t in this.transactions)
                    if (oldest == null || Transaction.IsPrevious(t.Id, oldest.Id))
                        oldest = t;

                lock (this.snapshots)
                {
                    IList<ITransactionalReference> toremove = new List<ITransactionalReference>();

                    foreach (ITransactionalReference snapshot in this.snapshots)
                    {
                        if (oldest == null)
                            snapshot.ClearSnapshots();
                        else
                            snapshot.ClearSnapshots(oldest.Id);

                        if (!snapshot.HasSnapshots)
                            toremove.Add(snapshot);
                    }

                    foreach (ITransactionalReference reference in toremove)
                        this.snapshots.Remove(reference);

                    if (this.snapshots.Count == 0)
                        this.snapshots.Clear();
                }
            }
        }
Ejemplo n.º 10
0
 internal void RegisterTransaction(Transaction transaction)
 {
     lock (this.transactions)
     {
         this.transactions.Add(transaction);
     }
 }