-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathLightningDatabase.cs
More file actions
135 lines (116 loc) · 4.31 KB
/
LightningDatabase.cs
File metadata and controls
135 lines (116 loc) · 4.31 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
using System;
using static LightningDB.Native.Lmdb;
namespace LightningDB;
/// <summary>
/// Represents a database in the Lightning environment, providing mechanisms to perform
/// various operations such as opening, dropping, and accessing database statistics. Generally,
/// a database can be reused and rarely needs to be disposed.
/// </summary>
public sealed class LightningDatabase : IDisposable
{
internal uint _handle;
private bool _disposed;
private readonly DatabaseConfiguration _configuration;
private readonly bool _closeOnDispose;
private readonly LightningTransaction _transaction;
private readonly IDisposable _pinnedConfig;
/// <summary>
/// Creates a LightningDatabase instance.
/// </summary>
/// <param name="name">Database name.</param>
/// <param name="transaction">Active transaction.</param>
/// <param name="configuration">Options for the database, like encoding, option flags, and comparison logic.</param>
/// <param name="closeOnDispose">Close database handle on dispose</param>
internal LightningDatabase(string? name, LightningTransaction transaction, DatabaseConfiguration configuration,
bool closeOnDispose)
{
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));
Name = name;
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_closeOnDispose = closeOnDispose;
Environment = transaction.Environment;
_transaction = transaction;
mdb_dbi_open(transaction._handle, name, _configuration.Flags, out _handle).ThrowOnError();
_pinnedConfig = _configuration.ConfigureDatabase(transaction, this);
IsOpened = true;
}
/// <summary>
/// Whether the database handle has been release from Dispose, or from unsuccessful OpenDatabase call.
/// </summary>
public bool IsReleased => _handle == default;
/// <summary>
/// Is database opened.
/// </summary>
public bool IsOpened { get; private set; }
public Stats DatabaseStats => _transaction.GetStats(this);
/// <summary>
/// Database name.
/// </summary>
public string? Name { get; }
/// <summary>
/// Environment in which the database was opened.
/// </summary>
public LightningEnvironment Environment { get; }
/// <summary>
/// Gets the flags used when opening this database.
/// </summary>
/// <param name="transaction">The transaction to use for retrieving the flags</param>
/// <returns>The database flags</returns>
public DatabaseOpenFlags GetFlags(LightningTransaction transaction)
{
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));
mdb_dbi_flags(transaction._handle, _handle, out var flags).ThrowOnError();
return (DatabaseOpenFlags)flags;
}
/// <summary>
/// Drops the database.
/// </summary>
public MDBResultCode Drop(LightningTransaction transaction)
{
var result = mdb_drop(transaction._handle, _handle, true);
IsOpened = false;
_handle = default;
return result;
}
/// <summary>
/// Truncates all data from the database.
/// </summary>
public MDBResultCode Truncate(LightningTransaction transaction)
{
return mdb_drop(transaction._handle, _handle, false);
}
/// <summary>
/// Deallocates resources opened by the database.
/// </summary>
/// <param name="disposing">true if called from Dispose.</param>
private void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (!IsOpened)
return;
if (!Environment.IsOpened && _closeOnDispose && disposing)
throw new InvalidOperationException("A database must be disposed before closing the environment");
IsOpened = false;
_pinnedConfig?.Dispose();
if (_closeOnDispose && Environment.IsOpened)
mdb_dbi_close(Environment._handle, _handle);
if (disposing)
_handle = default;
}
/// <summary>
/// Deallocates resources opened by the database.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);
}
~LightningDatabase()
{
Dispose(false);
}
}