Skip to content

Commit 8b9cabd

Browse files
committed
Added PageSize, UsedSize, EntriesCount to env.
1 parent 0bc81ec commit 8b9cabd

File tree

8 files changed

+168
-3
lines changed

8 files changed

+168
-3
lines changed

src/LightningDB.Tests/EnvironmentTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,56 @@ public void CanOpenEnvironmentMoreThan50Mb()
117117
//act-assert
118118
_env.Open();
119119
}
120+
121+
[Test]
122+
public void CanCountEnvironmentEntries()
123+
{
124+
const int entriesCount = 10;
125+
126+
//arrange
127+
_env = new LightningEnvironment(_path, EnvironmentOpenFlags.None);
128+
_env.Open();
129+
130+
using (var txn = _env.BeginTransaction())
131+
using (var db = txn.OpenDatabase(null, DatabaseOpenFlags.None))
132+
{
133+
for (var i = 0; i < entriesCount; i++)
134+
txn.Put(db, i.ToString(), i.ToString());
135+
136+
txn.Commit();
137+
}
138+
139+
//act
140+
var count = _env.EntriesCount;
141+
142+
//assert;
143+
Assert.AreEqual(entriesCount, count);
144+
}
145+
146+
[Test]
147+
public void CanGetUsedSize()
148+
{
149+
const int entriesCount = 1;
150+
151+
//arrange
152+
_env = new LightningEnvironment(_path, EnvironmentOpenFlags.None);
153+
_env.Open();
154+
155+
using (var txn = _env.BeginTransaction())
156+
using (var db = txn.OpenDatabase(null, DatabaseOpenFlags.None))
157+
{
158+
for (int i = 0; i < entriesCount; i++)
159+
txn.Put(db, i, i);
160+
161+
txn.Commit();
162+
}
163+
164+
//act
165+
var size = _env.UsedSize;
166+
167+
//act-assert;
168+
Assert.AreEqual(_env.PageSize, size);
169+
}
170+
120171
}
121172
}

src/LightningDB.Tests/TransactionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void DefaultDatabaseShouldBeDropped()
171171
}
172172

173173
[Test]
174-
public void CanCountEntries()
174+
public void CanCountTransactionEntries()
175175
{
176176
//arrange
177177
_txn = _env.BeginTransaction();

src/LightningDB/LightningEnvironment.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public LightningEnvironment(string directory, EnvironmentOpenFlags openFlags = E
6666
defaultConverters.RegisterDefault(this);
6767
}
6868

69+
private MDBStat GetStat()
70+
{
71+
EnsureOpened();
72+
73+
var stat = new MDBStat();
74+
NativeMethods.Execute(lib => lib.mdb_env_stat(_handle, out stat));
75+
76+
return stat;
77+
}
78+
6979
/// <summary>
7080
/// Whether the environment is opened.
7181
/// </summary>
@@ -89,8 +99,8 @@ public long MapSize
8999
get { return _mapSize; }
90100
set
91101
{
92-
if (this.IsOpened)
93-
throw new InvalidOperationException("Can't change MapSize of opened environment");
102+
// if (this.IsOpened)
103+
// throw new InvalidOperationException("Can't change MapSize of opened environment");
94104

95105
if (value == _mapSize)
96106
return;
@@ -101,6 +111,22 @@ public long MapSize
101111
}
102112
}
103113

114+
public uint PageSize { get { return GetStat().ms_psize; } }
115+
116+
public long UsedSize
117+
{
118+
get
119+
{
120+
var stat = GetStat();
121+
var totalPages =
122+
stat.ms_branch_pages.ToInt64() +
123+
stat.ms_leaf_pages.ToInt64() +
124+
stat.ms_overflow_pages.ToInt64();
125+
126+
return stat.ms_psize * totalPages;
127+
}
128+
}
129+
104130
/// <summary>
105131
/// Get the maximum number of threads for the environment.
106132
/// </summary>
@@ -145,6 +171,8 @@ public int MaxDatabases
145171
}
146172
}
147173

174+
public long EntriesCount { get { return GetStat().ms_entries.ToInt64(); } }
175+
148176
/// <summary>
149177
/// Directory path to store database files.
150178
/// </summary>

src/LightningDB/Native/INativeLibraryFacade.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ interface INativeLibraryFacade
313313
/// <returns></returns>
314314
int mdb_env_info (IntPtr env, out MDBEnvInfo stat);
315315

316+
/// <summary>
317+
/// Return statistics about the LMDB environment.
318+
/// </summary>
319+
/// <param name="env">An environment handle returned by mdb_env_create()</param>
320+
/// <param name="stat">The address of an MDB_stat structure where the statistics will be copied</param>
321+
/// <returns></returns>
322+
int mdb_env_stat(IntPtr env, out MDBStat stat);
323+
316324
/// <summary>
317325
/// Flush the data buffers to disk.
318326
/// Data is always written to disk when mdb_txn_commit() is called, but the operating system may keep it buffered.

src/LightningDB/Native/MDBEnvInfo.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,34 @@ namespace LightningDB.Native
99
[StructLayout(LayoutKind.Sequential)]
1010
struct MDBEnvInfo
1111
{
12+
/// <summary>
13+
/// Address of map, if fixed
14+
/// </summary>
1215
public IntPtr me_mapaddr;
16+
17+
/// <summary>
18+
/// Size of the data memory map
19+
/// </summary>
1320
public IntPtr me_mapsize;
21+
22+
/// <summary>
23+
/// ID of the last used page
24+
/// </summary>
1425
public IntPtr me_last_pgno;
26+
27+
/// <summary>
28+
/// ID of the last committed transaction
29+
/// </summary>
1530
public IntPtr me_last_txnid;
31+
32+
/// <summary>
33+
/// max reader slots in the environment
34+
/// </summary>
1635
public uint me_maxreaders;
36+
37+
/// <summary>
38+
/// max reader slots used in the environment
39+
/// </summary>
1740
public uint me_numreaders;
1841
}
1942
}

src/LightningDB/Native/MDBStat.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,34 @@ namespace LightningDB.Native
99
[StructLayout(LayoutKind.Sequential)]
1010
struct MDBStat
1111
{
12+
/// <summary>
13+
/// Size of a database page. This is currently the same for all databases.
14+
/// </summary>
1215
public uint ms_psize;
16+
17+
/// <summary>
18+
/// Depth (height) of the B-tree
19+
/// </summary>
1320
public uint ms_depth;
21+
22+
/// <summary>
23+
/// Number of internal (non-leaf) pages
24+
/// </summary>
1425
public IntPtr ms_branch_pages;
26+
27+
/// <summary>
28+
/// Number of leaf pages
29+
/// </summary>
1530
public IntPtr ms_leaf_pages;
31+
32+
/// <summary>
33+
/// Number of overflow pages
34+
/// </summary>
1635
public IntPtr ms_overflow_pages;
36+
37+
/// <summary>
38+
/// Number of data items
39+
/// </summary>
1740
public IntPtr ms_entries;
1841
}
1942
}

src/LightningDB/Native/NativeLibraryFacades.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class Native32BitLibraryFacade : INativeLibraryFacade
7676
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
7777
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);
7878

79+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
80+
private static extern int mdb_env_stat(IntPtr env, out MDBStat stat);
81+
7982
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
8083
private static extern int mdb_env_sync(IntPtr env, bool force);
8184

@@ -228,6 +231,11 @@ int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
228231
return Native32BitLibraryFacade.mdb_env_info(env, out stat);
229232
}
230233

234+
int INativeLibraryFacade.mdb_env_stat (IntPtr env, out MDBStat stat)
235+
{
236+
return Native32BitLibraryFacade.mdb_env_stat(env, out stat);
237+
}
238+
231239
int INativeLibraryFacade.mdb_env_sync(IntPtr env, bool force)
232240
{
233241
return Native32BitLibraryFacade.mdb_env_sync(env, force);
@@ -353,6 +361,9 @@ class Native64BitLibraryFacade : INativeLibraryFacade
353361
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
354362
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);
355363

364+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
365+
private static extern int mdb_env_stat(IntPtr env, out MDBStat stat);
366+
356367
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
357368
private static extern int mdb_env_sync(IntPtr env, bool force);
358369

@@ -493,6 +504,11 @@ int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
493504
return Native64BitLibraryFacade.mdb_env_info(env, out stat);
494505
}
495506

507+
int INativeLibraryFacade.mdb_env_stat (IntPtr env, out MDBStat stat)
508+
{
509+
return Native64BitLibraryFacade.mdb_env_stat(env, out stat);
510+
}
511+
496512
int INativeLibraryFacade.mdb_env_sync(IntPtr env, bool force)
497513
{
498514
return Native64BitLibraryFacade.mdb_env_sync(env, force);
@@ -618,6 +634,9 @@ class FallbackLibraryFacade : INativeLibraryFacade
618634
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
619635
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);
620636

637+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
638+
private static extern int mdb_env_stat(IntPtr env, out MDBStat stat);
639+
621640
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
622641
private static extern int mdb_env_sync(IntPtr env, bool force);
623642

@@ -770,6 +789,11 @@ int INativeLibraryFacade.mdb_env_info (IntPtr env, out MDBEnvInfo stat)
770789
return FallbackLibraryFacade.mdb_env_info(env, out stat);
771790
}
772791

792+
int INativeLibraryFacade.mdb_env_stat (IntPtr env, out MDBStat stat)
793+
{
794+
return FallbackLibraryFacade.mdb_env_stat(env, out stat);
795+
}
796+
773797
int INativeLibraryFacade.mdb_env_sync(IntPtr env, bool force)
774798
{
775799
return FallbackLibraryFacade.mdb_env_sync(env, force);

src/LightningDB/Native/NativeLibraryFacades.tt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ namespace LightningDB.Native
9191
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
9292
private static extern int mdb_env_info (IntPtr env, out MDBEnvInfo stat);
9393

94+
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
95+
private static extern int mdb_env_stat(IntPtr env, out MDBStat stat);
96+
9497
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
9598
private static extern int mdb_env_sync(IntPtr env, bool force);
9699

@@ -247,6 +250,11 @@ namespace LightningDB.Native
247250
return <#= pair.Value #>LibraryFacade.mdb_env_info(env, out stat);
248251
}
249252

253+
int INativeLibraryFacade.mdb_env_stat (IntPtr env, out MDBStat stat)
254+
{
255+
return <#= pair.Value #>LibraryFacade.mdb_env_stat(env, out stat);
256+
}
257+
250258
int INativeLibraryFacade.mdb_env_sync(IntPtr env, bool force)
251259
{
252260
return <#= pair.Value #>LibraryFacade.mdb_env_sync(env, force);

0 commit comments

Comments
 (0)