Repository navigation
Expand file tree
/
Copy pathTransactionalWriteOperation.cs
More file actions
64 lines (58 loc) · 2.85 KB
/
Copy pathTransactionalWriteOperation.cs
File metadata and controls
64 lines (58 loc) · 2.85 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EventStore.ClientAPI.Exceptions;
using EventStore.ClientAPI.Messages;
using EventStore.ClientAPI.SystemData;
namespace EventStore.ClientAPI.ClientOperations
{
internal class TransactionalWriteOperation : OperationBase<object, ClientMessage.TransactionWriteCompleted>
{
private readonly bool _requireMaster;
private readonly long _transactionId;
private readonly IEnumerable<EventData> _events;
public TransactionalWriteOperation(ILogger log, TaskCompletionSource<object> source,
bool requireMaster, long transactionId, IEnumerable<EventData> events,
UserCredentials userCredentials)
: base(log, source, TcpCommand.TransactionWrite, TcpCommand.TransactionWriteCompleted, userCredentials)
{
_requireMaster = requireMaster;
_transactionId = transactionId;
_events = events;
}
protected override object CreateRequestDto()
{
var dtos = _events.Select(x => new ClientMessage.NewEvent(x.EventId.ToByteArray(), x.Type, x.IsJson ? 1 : 0, 0, x.Data, x.Metadata)).ToArray();
return new ClientMessage.TransactionWrite(_transactionId, dtos, _requireMaster);
}
protected override InspectionResult InspectResponse(ClientMessage.TransactionWriteCompleted response)
{
switch (response.Result)
{
case ClientMessage.OperationResult.Success:
Succeed();
return new InspectionResult(InspectionDecision.EndOperation, "Success");
case ClientMessage.OperationResult.PrepareTimeout:
return new InspectionResult(InspectionDecision.Retry, "PrepareTimeout");
case ClientMessage.OperationResult.CommitTimeout:
return new InspectionResult(InspectionDecision.Retry, "CommitTimeout");
case ClientMessage.OperationResult.ForwardTimeout:
return new InspectionResult(InspectionDecision.Retry, "ForwardTimeout");
case ClientMessage.OperationResult.AccessDenied:
Fail(new AccessDeniedException("Write access denied."));
return new InspectionResult(InspectionDecision.EndOperation, "AccessDenied");
default:
throw new Exception(string.Format("Unexpected OperationResult: {0}.", response.Result));
}
}
protected override object TransformResponse(ClientMessage.TransactionWriteCompleted response)
{
return null;
}
public override string ToString()
{
return string.Format("TransactionId: {0}", _transactionId);
}
}
}