Skip to content

Commit ffcfdc7

Browse files
committed
Adding overloads for non-nullable and no argument for null params
1 parent 00a5f9a commit ffcfdc7

File tree

6 files changed

+88
-33
lines changed

6 files changed

+88
-33
lines changed

src/LightningDB.Tests/DatabaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void can_get_database_flags()
203203
// Test default database (should have no special flags)
204204
using (var txn = env.BeginTransaction())
205205
{
206-
using var db = txn.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create });
206+
using var db = txn.OpenDatabase(new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create });
207207

208208
var flags = db.GetFlags(txn);
209209
flags.ShouldBe(DatabaseOpenFlags.None);

src/LightningDB.Tests/EnvironmentTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void can_put_multiple_key_value_pairs_and_flush_successfully()
115115
env.Open();
116116

117117
using var tx = env.BeginTransaction();
118-
using var db = tx.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create });
118+
using var db = tx.OpenDatabase(new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create });
119119

120120
for (int i = 0; i < 5; i++)
121121
{
@@ -250,7 +250,7 @@ public void can_copy_to_file_stream()
250250

251251
// Add some data to the source environment
252252
using (var tx = sourceEnv.BeginTransaction())
253-
using (var db = tx.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
253+
using (var db = tx.OpenDatabase(new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
254254
{
255255
for (int i = 0; i < 5; i++)
256256
{
@@ -286,7 +286,7 @@ public void can_copy_with_compaction_to_file_stream()
286286

287287
// Add some data to the source environment and then delete half of it to create free space
288288
using (var tx = sourceEnv.BeginTransaction())
289-
using (var db = tx.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
289+
using (var db = tx.OpenDatabase(new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
290290
{
291291
for (int i = 0; i < 10; i++)
292292
{
@@ -333,7 +333,7 @@ public void can_get_environment_file_stream()
333333

334334
// Add some data to make sure the file has content
335335
using (var tx = env.BeginTransaction())
336-
using (var db = tx.OpenDatabase(null, new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
336+
using (var db = tx.OpenDatabase(new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
337337
{
338338
tx.Put(db, "testkey", "testvalue");
339339
tx.Commit();

src/LightningDB.Tests/TestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected string TempPath(string seed = "")
1414
return path;
1515
}
1616
protected LightningEnvironment CreateEnvironment(string? path = null, EnvironmentConfiguration? config = null) =>
17-
new(path ?? TempPath(), config);
17+
config is null ? new(path ?? TempPath()) : new(path ?? TempPath(), config);
1818

1919
public static void CleanupSession()
2020
{

src/LightningDB.Tests/TestHelperExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void RunCursorScenario(this LightningEnvironment env,
5252
DatabaseOpenFlags flags = DatabaseOpenFlags.Create, TransactionBeginFlags transactionFlags = TransactionBeginFlags.None)
5353
{
5454
using var tx = env.BeginTransaction(transactionFlags);
55-
using var db = tx.OpenDatabase(configuration: new DatabaseConfiguration { Flags = flags });
55+
using var db = tx.OpenDatabase(new DatabaseConfiguration { Flags = flags });
5656
using var cursor = tx.CreateCursor(db);
5757
scenario(tx, db, cursor);
5858
}
@@ -62,7 +62,7 @@ public static void RunTransactionScenario(this LightningEnvironment env,
6262
DatabaseOpenFlags flags = DatabaseOpenFlags.Create, TransactionBeginFlags transactionFlags = TransactionBeginFlags.None)
6363
{
6464
using var tx = env.BeginTransaction(transactionFlags);
65-
using var db = tx.OpenDatabase(configuration: new DatabaseConfiguration { Flags = flags });
65+
using var db = tx.OpenDatabase(new DatabaseConfiguration { Flags = flags });
6666
scenario(tx, db);
6767
}
6868
}

src/LightningDB/LightningEnvironment.cs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,37 @@ public sealed class LightningEnvironment : IDisposable
1717
internal nint _handle;
1818

1919
/// <summary>
20-
/// Creates a new instance of LightningEnvironment.
20+
/// Creates a new instance of LightningEnvironment with default configuration.
2121
/// </summary>
2222
/// <param name="path">Directory for storing database files.</param>
23-
/// <param name="configuration">Configuration for the environment. If null, default configuration is used.</param>
24-
public LightningEnvironment(string path, EnvironmentConfiguration? configuration = null)
23+
public LightningEnvironment(string path)
2524
{
2625
if (string.IsNullOrWhiteSpace(path))
2726
throw new ArgumentException("Invalid directory name");
2827

29-
var config = configuration ?? _config;
30-
3128
mdb_env_create(out _handle).ThrowOnError();
32-
config.Configure(this);
33-
_config = config;
29+
_config.Configure(this);
3430

3531
Path = path;
32+
}
3633

34+
/// <summary>
35+
/// Creates a new instance of LightningEnvironment.
36+
/// </summary>
37+
/// <param name="path">Directory for storing database files.</param>
38+
/// <param name="configuration">Configuration for the environment.</param>
39+
public LightningEnvironment(string path, EnvironmentConfiguration configuration)
40+
{
41+
if (string.IsNullOrWhiteSpace(path))
42+
throw new ArgumentException("Invalid directory name");
43+
if (configuration == null)
44+
throw new ArgumentNullException(nameof(configuration));
45+
46+
mdb_env_create(out _handle).ThrowOnError();
47+
configuration.Configure(this);
48+
_config = configuration;
49+
50+
Path = path;
3751
}
3852

3953
/// <summary>
@@ -218,47 +232,52 @@ public void Open(EnvironmentOpenFlags openFlags = EnvironmentOpenFlags.None, Uni
218232
}
219233

220234
/// <summary>
221-
/// Create a transaction for use with the environment.
235+
/// Create a top-level transaction for use with the environment.
222236
/// The transaction handle may be discarded using Abort() or Commit().
223237
/// Note:
224238
/// Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction.
225239
/// Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
226240
/// </summary>
227-
/// <param name="parent">
228-
/// If this parameter is non-null, the new transaction will be a nested transaction, with the transaction indicated by parent as its parent.
229-
/// Transactions may be nested to any level.
230-
/// A parent transaction may not issue any other operations besides BeginTransaction, Abort, or Commit while it has active child transactions.
231-
/// </param>
232241
/// <param name="beginFlags">
233242
/// Special options for this transaction.
234243
/// </param>
235244
/// <returns>
236245
/// New LightningTransaction
237246
/// </returns>
238-
public LightningTransaction BeginTransaction(LightningTransaction? parent = null, TransactionBeginFlags beginFlags = LightningTransaction.DefaultTransactionBeginFlags)
247+
public LightningTransaction BeginTransaction(TransactionBeginFlags beginFlags = LightningTransaction.DefaultTransactionBeginFlags)
239248
{
240-
if (!IsOpened)
241-
throw new InvalidOperationException("Environment must be opened before starting a transaction");
242-
243-
return new LightningTransaction(this, parent, beginFlags);
249+
return BeginTransactionImpl(null, beginFlags);
244250
}
245251

246252
/// <summary>
247-
/// Create a transaction for use with the environment.
253+
/// Create a nested transaction for use with the environment.
248254
/// The transaction handle may be discarded using Abort() or Commit().
249255
/// Note:
250256
/// Transactions may not span threads; a transaction must only be used by a single thread. Also, a thread may only have a single transaction.
251257
/// Cursors may not span transactions; each cursor must be opened and closed within a single transaction.
252258
/// </summary>
259+
/// <param name="parent">
260+
/// The parent transaction. The new transaction will be a nested transaction.
261+
/// Transactions may be nested to any level.
262+
/// A parent transaction may not issue any other operations besides BeginTransaction, Abort, or Commit while it has active child transactions.
263+
/// </param>
253264
/// <param name="beginFlags">
254265
/// Special options for this transaction.
255266
/// </param>
256267
/// <returns>
257268
/// New LightningTransaction
258269
/// </returns>
259-
public LightningTransaction BeginTransaction(TransactionBeginFlags beginFlags)
270+
public LightningTransaction BeginTransaction(LightningTransaction parent, TransactionBeginFlags beginFlags = LightningTransaction.DefaultTransactionBeginFlags)
271+
{
272+
return BeginTransactionImpl(parent, beginFlags);
273+
}
274+
275+
private LightningTransaction BeginTransactionImpl(LightningTransaction? parent, TransactionBeginFlags beginFlags)
260276
{
261-
return BeginTransaction(null, beginFlags);
277+
if (!IsOpened)
278+
throw new InvalidOperationException("Environment must be opened before starting a transaction");
279+
280+
return new LightningTransaction(this, parent, beginFlags);
262281
}
263282

264283
/// <summary>

src/LightningDB/LightningTransaction.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,51 @@ public LightningTransaction BeginTransaction(TransactionBeginFlags beginFlags =
6969
}
7070

7171
/// <summary>
72-
/// Opens a database in context of this transaction.
72+
/// Opens the default (unnamed) database in context of this transaction with default configuration.
73+
/// </summary>
74+
/// <param name="closeOnDispose">Close database handle on dispose</param>
75+
/// <returns>Created database wrapper.</returns>
76+
public LightningDatabase OpenDatabase(bool closeOnDispose = false)
77+
{
78+
return OpenDatabaseImpl(null, new DatabaseConfiguration(), closeOnDispose);
79+
}
80+
81+
/// <summary>
82+
/// Opens the default (unnamed) database in context of this transaction.
7383
/// </summary>
74-
/// <param name="name">Database name (optional). If null then the default/unnamed database is used.</param>
7584
/// <param name="configuration">Database open options.</param>
7685
/// <param name="closeOnDispose">Close database handle on dispose</param>
7786
/// <returns>Created database wrapper.</returns>
78-
public LightningDatabase OpenDatabase(string? name = null, DatabaseConfiguration? configuration = null, bool closeOnDispose = false)
87+
public LightningDatabase OpenDatabase(DatabaseConfiguration configuration, bool closeOnDispose = false)
88+
{
89+
return OpenDatabaseImpl(null, configuration, closeOnDispose);
90+
}
91+
92+
/// <summary>
93+
/// Opens a named database in context of this transaction with default configuration.
94+
/// </summary>
95+
/// <param name="name">Database name.</param>
96+
/// <param name="closeOnDispose">Close database handle on dispose</param>
97+
/// <returns>Created database wrapper.</returns>
98+
public LightningDatabase OpenDatabase(string name, bool closeOnDispose = false)
99+
{
100+
return OpenDatabaseImpl(name, new DatabaseConfiguration(), closeOnDispose);
101+
}
102+
103+
/// <summary>
104+
/// Opens a named database in context of this transaction.
105+
/// </summary>
106+
/// <param name="name">Database name.</param>
107+
/// <param name="configuration">Database open options.</param>
108+
/// <param name="closeOnDispose">Close database handle on dispose</param>
109+
/// <returns>Created database wrapper.</returns>
110+
public LightningDatabase OpenDatabase(string name, DatabaseConfiguration configuration, bool closeOnDispose = false)
111+
{
112+
return OpenDatabaseImpl(name, configuration, closeOnDispose);
113+
}
114+
115+
private LightningDatabase OpenDatabaseImpl(string? name, DatabaseConfiguration configuration, bool closeOnDispose)
79116
{
80-
configuration ??= new DatabaseConfiguration();
81117
var db = new LightningDatabase(name, this, configuration, closeOnDispose);
82118
return db;
83119
}

0 commit comments

Comments
 (0)