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); }
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); }
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(); }
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(); } }
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; } }
public void Dispose(Transaction transaction) { lock (this) { if (this.pending == transaction) { this.pending = null; this.pendingvalue = null; } } }
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); } }
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; } }
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(); } } }
internal void RegisterTransaction(Transaction transaction) { lock (this.transactions) { this.transactions.Add(transaction); } }