Skip to content

Commit 98cc231

Browse files
committed
Use TransactionManager to release transactions
Instead of events
1 parent 4c5f644 commit 98cc231

File tree

6 files changed

+151
-89
lines changed

6 files changed

+151
-89
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LightningDB.Factories
7+
{
8+
class CursorManager
9+
{
10+
}
11+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using LightningDB.Native;
6+
7+
namespace LightningDB.Factories
8+
{
9+
class TransactionManager
10+
{
11+
private readonly LightningEnvironment _environment;
12+
private readonly LightningTransaction _parentTransaction;
13+
private readonly IntPtr _parentHandle;
14+
15+
private readonly HashSet<LightningTransaction> _transactions;
16+
17+
public TransactionManager(LightningEnvironment environment, LightningTransaction parentTransaction)
18+
{
19+
if (environment == null)
20+
throw new ArgumentNullException("environment");
21+
22+
_environment = environment;
23+
24+
_parentTransaction = parentTransaction;
25+
_parentHandle = parentTransaction != null
26+
? parentTransaction._handle
27+
: IntPtr.Zero;
28+
29+
_transactions = new HashSet<LightningTransaction>();
30+
}
31+
32+
private void EnsureEnvironmentOpened()
33+
{
34+
if (!_environment.IsOpened)
35+
throw new InvalidOperationException("Environment should be opened");
36+
}
37+
38+
public LightningTransaction Create(TransactionBeginFlags beginFlags)
39+
{
40+
EnsureEnvironmentOpened();
41+
42+
IntPtr handle = default(IntPtr);
43+
NativeMethods.Execute(lib => lib.mdb_txn_begin(_environment._handle, _parentHandle, beginFlags, out handle));
44+
45+
var tran = new LightningTransaction(_environment, handle, _parentTransaction, beginFlags);
46+
47+
_transactions.Add(tran);
48+
49+
return tran;
50+
}
51+
52+
public void WasDiscarded(LightningTransaction tn)
53+
{
54+
_transactions.Remove(tn);
55+
}
56+
57+
private static void AbortAll(TransactionManager manager)
58+
{
59+
60+
foreach (var tn in manager._transactions.ToList())
61+
{
62+
AbortAll(tn.SubTransactionsManager);
63+
tn.Abort();
64+
}
65+
}
66+
67+
public void AbortAll()
68+
{
69+
AbortAll(this);
70+
}
71+
}
72+
}

src/LightningDB/LightningCursor.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@ public class LightningCursor : IDisposable
1717
/// </summary>
1818
/// <param name="db">Database</param>
1919
/// <param name="txn">Transaction</param>
20-
public LightningCursor(LightningDatabase db, LightningTransaction txn)
20+
internal LightningCursor(LightningDatabase db, LightningTransaction txn)
2121
{
2222
if (db == null)
2323
throw new ArgumentNullException("db");
2424

2525
if (txn == null)
2626
throw new ArgumentNullException("txn");
2727

28-
if (db.Environment != txn.Environment)
29-
throw new ArgumentException("db and txn belong to different environments");
30-
3128
IntPtr handle = default(IntPtr);
3229
NativeMethods.Execute(lib => lib.mdb_cursor_open(txn._handle, db._handle, out handle));
3330

@@ -38,10 +35,11 @@ public LightningCursor(LightningDatabase db, LightningTransaction txn)
3835

3936
_shouldDispose = true;
4037

41-
if (txn.IsReadOnly)
38+
throw new NotImplementedException("Implement cursor manager");
39+
/*if (txn.IsReadOnly)
4240
this.Environment.Closing += EnvironmentOrTransactionClosing;
4341
else
44-
this.Transaction.Closing += EnvironmentOrTransactionClosing;
42+
this.Transaction.Closing += EnvironmentOrTransactionClosing;*/
4543
}
4644

4745
private void EnvironmentOrTransactionClosing(object sender, EventArgs e)
@@ -55,10 +53,10 @@ private void EnvironmentOrTransactionClosing(object sender, EventArgs e)
5553

5654
private void DetachClosingHandler()
5755
{
58-
if (this.Transaction.IsReadOnly)
56+
/*if (this.Transaction.IsReadOnly)
5957
this.Environment.Closing -= EnvironmentOrTransactionClosing;
6058
else
61-
this.Transaction.Closing -= EnvironmentOrTransactionClosing;
59+
this.Transaction.Closing -= EnvironmentOrTransactionClosing;*/
6260
}
6361

6462
/// <summary>

src/LightningDB/LightningDB.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@
7474
<Compile Include="Collections\CursorEnumerator.cs" />
7575
<Compile Include="CursorGetByOperation.cs" />
7676
<Compile Include="CursorPutOptions.cs" />
77+
<Compile Include="Factories\CursorManager.cs" />
7778
<Compile Include="Factories\DatabaseHandleCacheEntry.cs" />
7879
<Compile Include="Factories\DatabaseManager.cs" />
80+
<Compile Include="Factories\TransactionManager.cs" />
7981
<Compile Include="GetByOperation.cs" />
8082
<Compile Include="LightningCursorExtensions.cs" />
8183
<Compile Include="LightningCursorMoveExtensions.cs">

src/LightningDB/LightningEnvironment.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class LightningEnvironment : IClosingEventSource, IDisposable
2424
private int _maxDbs;
2525

2626
private readonly DatabaseManager _databaseManager;
27+
private readonly TransactionManager _transactionManager;
2728

2829
/// <summary>
2930
/// Creates a new instance of LightningEnvironment.
@@ -58,6 +59,7 @@ public LightningEnvironment(string directory, EnvironmentOpenFlags openFlags = E
5859
_maxDbs = LightningConfig.Environment.LibDefaultMaxDatabases;
5960

6061
_databaseManager = new DatabaseManager(_handle);
62+
_transactionManager = new TransactionManager(this, null);
6163

6264
ConverterStore = new ConverterStore();
6365
var defaultConverters = new DefaultConverters();
@@ -160,6 +162,8 @@ public int MaxDatabases
160162

161163
internal DatabaseManager DatabaseManager { get { return _databaseManager; } }
162164

165+
internal TransactionManager TransactionManager { get { return _transactionManager; } }
166+
163167
/// <summary>
164168
/// Open the environment.
165169
/// </summary>
@@ -187,6 +191,7 @@ public void Close()
187191

188192
this.OnClosing();
189193

194+
_transactionManager.AbortAll();
190195
_databaseManager.CloseAll();
191196

192197
NativeMethods.Library.mdb_env_close(_handle);
@@ -225,9 +230,10 @@ protected virtual void OnClosing()
225230
/// </returns>
226231
public LightningTransaction BeginTransaction(LightningTransaction parent, TransactionBeginFlags beginFlags)
227232
{
228-
this.EnsureOpened();
233+
if (parent != null)
234+
return parent.SubTransactionsManager.Create(beginFlags);
229235

230-
return new LightningTransaction(this, parent, beginFlags);
236+
return _transactionManager.Create(beginFlags);
231237
}
232238

233239
/// <summary>

0 commit comments

Comments
 (0)