-
Notifications
You must be signed in to change notification settings - Fork 820
/
TransactionStorageActions.cs
221 lines (193 loc) · 8.55 KB
/
TransactionStorageActions.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//-----------------------------------------------------------------------
// <copyright file="TransactionStorageActions.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using Raven.Abstractions;
using Raven.Abstractions.Data;
using Raven.Abstractions.Exceptions;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.MEF;
using Raven.Database.Impl;
using Raven.Database.Plugins;
using Raven.Database.Storage;
using Raven.Http;
using Raven.Http.Exceptions;
using Raven.Json.Linq;
using Raven.Munin;
using Raven.Storage.Managed.Impl;
using System.Linq;
using Raven.Database.Json;
namespace Raven.Storage.Managed
{
public class TransactionStorageActions : ITransactionStorageActions
{
private readonly TableStorage storage;
private readonly IUuidGenerator generator;
private readonly OrderedPartCollection<AbstractDocumentCodec> documentCodecs;
public TransactionStorageActions(TableStorage storage, IUuidGenerator generator, OrderedPartCollection<AbstractDocumentCodec> documentCodecs)
{
this.storage = storage;
this.generator = generator;
this.documentCodecs = documentCodecs;
}
public Guid AddDocumentInTransaction(string key, Guid? etag, RavenJObject data, RavenJObject metadata, TransactionInformation transactionInformation)
{
var readResult = storage.Documents.Read(new RavenJObject {{"key", key}});
if (readResult != null) // update
{
StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation);
AssertValidEtag(key, readResult, storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } }), etag);
((RavenJObject)readResult.Key)["txId"] = transactionInformation.Id.ToByteArray();
if (storage.Documents.UpdateKey(readResult.Key) == false)
throw new ConcurrencyException("PUT attempted on document '" + key +
"' that is currently being modified by another transaction");
}
else
{
readResult = storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } });
StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation);
}
storage.Transactions.UpdateKey(new RavenJObject
{
{"txId", transactionInformation.Id.ToByteArray()},
{"timeout", SystemTime.UtcNow.Add(transactionInformation.Timeout)}
});
var ms = new MemoryStream();
metadata.WriteTo(ms);
var dataBytes = documentCodecs.Aggregate(data.ToBytes(), (bytes, codec) => codec.Encode(key, data, metadata, bytes));
ms.Write(dataBytes, 0, dataBytes.Length);
var newEtag = generator.CreateSequentialUuid();
storage.DocumentsModifiedByTransactions.Put(new RavenJObject
{
{"key", key},
{"etag", newEtag.ToByteArray()},
{"modified", SystemTime.UtcNow},
{"txId", transactionInformation.Id.ToByteArray()}
}, ms.ToArray());
return newEtag;
}
private static void AssertValidEtag(string key, Table.ReadResult doc, Table.ReadResult docInTx, Guid? etag)
{
if (doc == null)
return;
var existingEtag =
docInTx != null
? new Guid(docInTx.Key.Value<byte[]>("etag"))
: new Guid(doc.Key.Value<byte[]>("etag"));
if (etag != null && etag.Value != existingEtag)
{
throw new ConcurrencyException("PUT attempted on document '" + key +
"' using a non current etag")
{
ActualETag = existingEtag,
ExpectedETag = etag.Value
};
}
}
public void DeleteDocumentInTransaction(TransactionInformation transactionInformation, string key, Guid? etag)
{
var readResult = storage.Documents.Read(new RavenJObject { { "key", key } });
if (readResult == null)
{
return;
}
readResult = storage.DocumentsModifiedByTransactions.Read(new RavenJObject { { "key", key } });
StorageHelper.AssertNotModifiedByAnotherTransaction(storage, this, key, readResult, transactionInformation);
AssertValidEtag(key, readResult, storage.DocumentsModifiedByTransactions.Read(new RavenJObject {{"key", key}}),etag);
if (readResult != null)
{
((RavenJObject)readResult.Key)["txId"] = transactionInformation.Id.ToByteArray();
if (storage.Documents.UpdateKey(readResult.Key) == false)
throw new ConcurrencyException("DELETE attempted on document '" + key +
"' that is currently being modified by another transaction");
}
storage.Transactions.UpdateKey(new RavenJObject
{
{"txId", transactionInformation.Id.ToByteArray()},
{"timeout", SystemTime.UtcNow.Add(transactionInformation.Timeout)}
});
var newEtag = generator.CreateSequentialUuid();
storage.DocumentsModifiedByTransactions.UpdateKey(new RavenJObject
{
{"key", key},
{"etag", newEtag.ToByteArray()},
{"modified", SystemTime.UtcNow},
{"deleted", true},
{"txId", transactionInformation.Id.ToByteArray()}
});
}
public void RollbackTransaction(Guid txId)
{
CompleteTransaction(txId, data =>
{
var readResult = storage.Documents.Read(new RavenJObject { { "key", data.Key } });
if (readResult == null)
return;
((RavenJObject)readResult.Key).Remove("txId");
storage.Documents.UpdateKey(readResult.Key);
});
}
public void ModifyTransactionId(Guid fromTxId, Guid toTxId, TimeSpan timeout)
{
storage.Transactions.UpdateKey(new RavenJObject
{
{"txId", toTxId.ToByteArray()},
{"timeout", SystemTime.UtcNow.Add(timeout)}
});
var transactionInformation = new TransactionInformation { Id = toTxId, Timeout = timeout };
CompleteTransaction(fromTxId, data =>
{
var readResult = storage.Documents.Read(new RavenJObject { { "key", data.Key } });
if (readResult != null)
{
((RavenJObject)readResult.Key)["txId"] = toTxId.ToByteArray();
storage.Documents.UpdateKey(readResult.Key);
}
if (data.Delete)
DeleteDocumentInTransaction(transactionInformation, data.Key, null);
else
AddDocumentInTransaction(data.Key, null, data.Data, data.Metadata, transactionInformation);
});
}
public bool TransactionExists(Guid txId)
{
return storage.Transactions.Read(new RavenJObject {{"txId", txId.ToByteArray()}}) != null;
}
public void CompleteTransaction(Guid txId, Action<DocumentInTransactionData> perDocumentModified)
{
storage.Transactions.Remove(new RavenJObject {{"txId", txId.ToByteArray()}});
var documentsInTx = storage.DocumentsModifiedByTransactions["ByTxId"]
.SkipTo(new RavenJObject {{"txId", txId.ToByteArray()}})
.TakeWhile(x => new Guid(x.Value<byte[]>("txId")) == txId);
foreach (var docInTx in documentsInTx)
{
var readResult = storage.DocumentsModifiedByTransactions.Read(docInTx);
storage.DocumentsModifiedByTransactions.Remove(docInTx);
RavenJObject metadata = null;
RavenJObject data = null;
if (readResult.Position > 0) // position can never be 0, because of the skip record
{
var ms = new MemoryStream(readResult.Data());
metadata = ms.ToJObject();
data = ms.ToJObject();
}
perDocumentModified(new DocumentInTransactionData
{
Key = readResult.Key.Value<string>("key"),
Etag = new Guid(readResult.Key.Value<byte[]>("etag")),
Delete = readResult.Key.Value<bool>("deleted"),
Metadata = metadata,
Data = data,
});
}
}
public IEnumerable<Guid> GetTransactionIds()
{
return storage.Transactions.Keys.Select(x => new Guid(x.Value<byte[]>("txId")));
}
}
}