-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCompositeTransaction.cs
More file actions
152 lines (128 loc) · 3.09 KB
/
CompositeTransaction.cs
File metadata and controls
152 lines (128 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Threading.Tasks;
namespace Waher.Runtime.Transactions
{
/// <summary>
/// A transaction built up of a set of sub-transactions.
/// </summary>
public class CompositeTransaction : Transaction
{
private readonly ITransaction[] transactions;
private readonly bool parallel;
/// <summary>
/// A transaction built up of a set of sub-transactions.
/// </summary>
/// <param name="Id">ID of transaction.</param>
/// <param name="Parallel">If sub-transactions can be run in parallel (true), or in sequence (false).</param>
/// <param name="Transactions">Subtransactions</param>
public CompositeTransaction(Guid Id, bool Parallel, params ITransaction[] Transactions)
: base(Id)
{
if (Transactions is null)
throw new ArgumentNullException("Array of transactions cannot be null.");
this.transactions = Transactions;
this.parallel = Parallel;
}
/// <summary>
/// Performs actual preparation.
/// </summary>
protected override async Task<bool> DoPrepare()
{
if (this.parallel)
{
int i, c = this.transactions.Length;
Task<bool>[] Tasks = new Task<bool>[c];
for (i = 0; i < c; i++)
Tasks[i] = this.transactions[i].Prepare();
await Task.WhenAll(Tasks);
return And(Tasks);
}
else
{
foreach (ITransaction Transaction in this.transactions)
{
if (!await Transaction.Prepare())
return false;
}
return true;
}
}
private static bool And(Task<bool>[] Tasks)
{
foreach (Task<bool> Task in Tasks)
{
if (!Task.Result)
return false;
}
return true;
}
/// <summary>
/// Performs actual execution.
/// </summary>
protected override async Task<bool> DoExecute()
{
if (this.parallel)
{
int i, c = this.transactions.Length;
Task<bool>[] Tasks = new Task<bool>[c];
for (i = 0; i < c; i++)
Tasks[i] = this.transactions[i].Execute();
await Task.WhenAll(Tasks);
return And(Tasks);
}
else
{
foreach (ITransaction Transaction in this.transactions)
{
if (!await Transaction.Execute())
return false;
}
return true;
}
}
/// <summary>
/// Performs actual commit.
/// </summary>
protected override async Task<bool> DoCommit()
{
if (this.parallel)
{
int i, c = this.transactions.Length;
Task<bool>[] Tasks = new Task<bool>[c];
for (i = 0; i < c; i++)
Tasks[i] = this.transactions[i].Commit();
await Task.WhenAll(Tasks);
return And(Tasks);
}
else
{
foreach (ITransaction Transaction in this.transactions)
{
if (!await Transaction.Commit())
return false;
}
return true;
}
}
/// <summary>
/// Performs actual rollback.
/// </summary>
protected override async Task<bool> DoRollback()
{
if (this.parallel)
{
int i, c = this.transactions.Length;
Task[] Tasks = new Task[c];
for (i = 0; i < c; i++)
Tasks[i] = this.transactions[i].Abort();
await Task.WhenAll(Tasks);
}
else
{
foreach (ITransaction Transaction in this.transactions)
await Transaction.Abort();
}
return true;
}
}
}