Skip to content

Commit 4855a07

Browse files
committed
Closes #20
Unified LightningEnvironment.MapSize for both 32 and 64 platforms.
1 parent c941e87 commit 4855a07

File tree

6 files changed

+116
-26
lines changed

6 files changed

+116
-26
lines changed

src/LightningDB/INativeLibraryFacade.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ interface INativeLibraryFacade
9898
/// A non-zero error value on failure and 0 on success. Some possible errors are:
9999
/// EINVAL - an invalid parameter was specified, or the environment is already open.
100100
/// </returns>
101-
int mdb_env_set_mapsize(IntPtr env, Int32 size); //OK
101+
int mdb_env_set_mapsize(IntPtr env, long size); //OK
102102

103103
/// <summary>
104104
/// Get the maximum number of threads for the environment.

src/LightningDB/LightningConfig.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LightningDB
7+
{
8+
public static class LightningConfig
9+
{
10+
public static class Environment
11+
{
12+
public const long LibDefaultMapSize = 10485760;
13+
public const int LibDefaultMaxReaders = 126;
14+
public const int LibDefaultMaxDatabases = 0;
15+
16+
static Environment()
17+
{
18+
AutoReduceMapSizeIn32BitProcess = false;
19+
20+
DefaultMapSize = LibDefaultMapSize;
21+
DefaultMaxReaders = LibDefaultMaxReaders;
22+
DefaultMaxDatabases = LibDefaultMaxDatabases;
23+
}
24+
25+
public static long DefaultMapSize { get; set; }
26+
27+
public static int DefaultMaxReaders { get; set; }
28+
29+
public static int DefaultMaxDatabases { get; set; }
30+
31+
public static bool AutoReduceMapSizeIn32BitProcess { get; set; }
32+
}
33+
}
34+
}

src/LightningDB/LightningDB.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
</Compile>
6767
<Compile Include="GetByOperation.cs" />
6868
<Compile Include="INativeLibraryFacade.cs" />
69+
<Compile Include="LightningConfig.cs" />
6970
<Compile Include="LightningDatabaseExtensions.cs" />
7071
<Compile Include="Converters\ConverterExtensions.cs" />
7172
<Compile Include="Converters\ConverterNotFoundException.cs" />

src/LightningDB/LightningEnvironment.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ namespace LightningDB
77
{
88
public class LightningEnvironment : IClosingEventSource, IDisposable
99
{
10-
public const int DefaultMapSize = 10485760;
11-
public const int DefaultMaxReaders = 126;
12-
public const int DefaultMaxDatabases = 0;
13-
1410
private readonly UnixAccessMode _accessMode;
1511
private readonly EnvironmentOpenFlags _openFlags;
1612
internal IntPtr _handle;
1713

1814
private bool _shouldDispose;
1915

20-
private int _mapSize;
16+
private long _mapSize;
2117
private int _maxDbs;
2218

2319
private readonly ConcurrentDictionary<string, LightningDatabase> _openedDatabases;
@@ -39,8 +35,15 @@ public LightningEnvironment(string directory, EnvironmentOpenFlags openFlags, Un
3935
this.Directory = directory;
4036
_openFlags = openFlags;
4137

42-
_mapSize = DefaultMapSize;
43-
_maxDbs = DefaultMaxDatabases;
38+
if (LightningConfig.Environment.DefaultMapSize != LightningConfig.Environment.LibDefaultMapSize)
39+
this.MapSize = LightningConfig.Environment.DefaultMapSize;
40+
else
41+
_mapSize = LightningConfig.Environment.LibDefaultMapSize;
42+
43+
if (LightningConfig.Environment.DefaultMaxDatabases != LightningConfig.Environment.LibDefaultMaxDatabases)
44+
this.MapDatabases = LightningConfig.Environment.DefaultMaxDatabases;
45+
else
46+
_maxDbs = LightningConfig.Environment.LibDefaultMaxDatabases;
4447

4548
_openedDatabases = new ConcurrentDictionary<string, LightningDatabase>();
4649
_databasesForReuse = new HashSet<uint>();
@@ -56,7 +59,7 @@ public LightningEnvironment(string directory, EnvironmentOpenFlags openFlags, Un
5659

5760
public LightningVersionInfo Version { get { return Native.LibraryVersion; } }
5861

59-
public int MapSize
62+
public long MapSize
6063
{
6164
get { return _mapSize; }
6265
set
@@ -65,7 +68,7 @@ public int MapSize
6568
throw new InvalidOperationException("Can't change MapSize of opened environment");
6669

6770
if (value == _mapSize)
68-
return;
71+
return;
6972

7073
Native.Execute(lib => lib.mdb_env_set_mapsize(_handle, value));
7174

src/LightningDB/NativeLibraryFacades.cs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+

2+
3+
4+
5+
using System;
26
using System.Collections.Generic;
37
using System.Linq;
48
using System.Runtime.InteropServices;
@@ -7,6 +11,7 @@
711
namespace LightningDB
812
{
913

14+
1015
class Native32BitLibraryFacade : INativeLibraryFacade
1116
{
1217
public const string LibraryName = "lmdb32";
@@ -26,7 +31,7 @@ class Native32BitLibraryFacade : INativeLibraryFacade
2631
private static extern int mdb_env_open(IntPtr env, string path, EnvironmentOpenFlags flags, int mode);
2732

2833
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
29-
private static extern int mdb_env_set_mapsize(IntPtr env, Int32 size);
34+
private static extern int mdb_env_set_mapsize(IntPtr env, IntPtr size);
3035

3136
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
3237
private static extern int mdb_env_get_maxreaders(IntPtr env, out UInt32 readers);
@@ -125,9 +130,22 @@ int INativeLibraryFacade.mdb_env_open(IntPtr env, string path, EnvironmentOpenFl
125130
return Native32BitLibraryFacade.mdb_env_open(env, path, flags, mode);
126131
}
127132

128-
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, int size)
129-
{
130-
return Native32BitLibraryFacade.mdb_env_set_mapsize(env, size);
133+
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, long size)
134+
{
135+
IntPtr sizeValue;
136+
if (size > Int32.MaxValue)
137+
{
138+
if (LightningConfig.Environment.AutoReduceMapSizeIn32BitProcess)
139+
sizeValue = new IntPtr(Int32.MaxValue);
140+
else
141+
throw new InvalidOperationException("Can't set MapSize larger than Int32.MaxValue in 32-bit process");
142+
}
143+
else
144+
{
145+
sizeValue = new IntPtr((int) size);
146+
}
147+
148+
return Native32BitLibraryFacade.mdb_env_set_mapsize(env, sizeValue);
131149
}
132150

133151
int INativeLibraryFacade.mdb_env_get_maxreaders(IntPtr env, out uint readers)
@@ -256,6 +274,7 @@ int INativeLibraryFacade.mdb_cursor_del(IntPtr cursor, CursorDeleteOption flags)
256274
}
257275
}
258276

277+
259278
class Native64BitLibraryFacade : INativeLibraryFacade
260279
{
261280
public const string LibraryName = "lmdb64";
@@ -275,7 +294,7 @@ class Native64BitLibraryFacade : INativeLibraryFacade
275294
private static extern int mdb_env_open(IntPtr env, string path, EnvironmentOpenFlags flags, int mode);
276295

277296
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
278-
private static extern int mdb_env_set_mapsize(IntPtr env, Int32 size);
297+
private static extern int mdb_env_set_mapsize(IntPtr env, IntPtr size);
279298

280299
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
281300
private static extern int mdb_env_get_maxreaders(IntPtr env, out UInt32 readers);
@@ -374,9 +393,11 @@ int INativeLibraryFacade.mdb_env_open(IntPtr env, string path, EnvironmentOpenFl
374393
return Native64BitLibraryFacade.mdb_env_open(env, path, flags, mode);
375394
}
376395

377-
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, int size)
396+
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, long size)
378397
{
379-
return Native64BitLibraryFacade.mdb_env_set_mapsize(env, size);
398+
var sizeValue = new IntPtr(size);
399+
400+
return Native64BitLibraryFacade.mdb_env_set_mapsize(env, sizeValue);
380401
}
381402

382403
int INativeLibraryFacade.mdb_env_get_maxreaders(IntPtr env, out uint readers)
@@ -505,6 +526,7 @@ int INativeLibraryFacade.mdb_cursor_del(IntPtr cursor, CursorDeleteOption flags)
505526
}
506527
}
507528

529+
508530
class FallbackLibraryFacade : INativeLibraryFacade
509531
{
510532
public const string LibraryName = "lmdb";
@@ -524,7 +546,7 @@ class FallbackLibraryFacade : INativeLibraryFacade
524546
private static extern int mdb_env_open(IntPtr env, string path, EnvironmentOpenFlags flags, int mode);
525547

526548
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
527-
private static extern int mdb_env_set_mapsize(IntPtr env, Int32 size);
549+
private static extern int mdb_env_set_mapsize(IntPtr env, IntPtr size);
528550

529551
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
530552
private static extern int mdb_env_get_maxreaders(IntPtr env, out UInt32 readers);
@@ -623,9 +645,22 @@ int INativeLibraryFacade.mdb_env_open(IntPtr env, string path, EnvironmentOpenFl
623645
return FallbackLibraryFacade.mdb_env_open(env, path, flags, mode);
624646
}
625647

626-
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, int size)
627-
{
628-
return FallbackLibraryFacade.mdb_env_set_mapsize(env, size);
648+
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, long size)
649+
{
650+
IntPtr sizeValue;
651+
if (!Environment.Is64BitProcess && size > Int32.MaxValue)
652+
{
653+
if (LightningConfig.Environment.AutoReduceMapSizeIn32BitProcess)
654+
sizeValue = new IntPtr(Int32.MaxValue);
655+
else
656+
throw new InvalidOperationException("Can't set MapSize larger than Int32.MaxValue in 32-bit process");
657+
}
658+
else
659+
{
660+
sizeValue = new IntPtr((int) size);
661+
}
662+
663+
return FallbackLibraryFacade.mdb_env_set_mapsize(env, sizeValue);
629664
}
630665

631666
int INativeLibraryFacade.mdb_env_get_maxreaders(IntPtr env, out uint readers)
@@ -754,4 +789,5 @@ int INativeLibraryFacade.mdb_cursor_del(IntPtr cursor, CursorDeleteOption flags)
754789
}
755790
}
756791

792+
757793
}

src/LightningDB/NativeLibraryFacades.tt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace LightningDB
4141
private static extern int mdb_env_open(IntPtr env, string path, EnvironmentOpenFlags flags, int mode);
4242

4343
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
44-
private static extern int mdb_env_set_mapsize(IntPtr env, Int32 size);
44+
private static extern int mdb_env_set_mapsize(IntPtr env, IntPtr size);
4545

4646
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)]
4747
private static extern int mdb_env_get_maxreaders(IntPtr env, out UInt32 readers);
@@ -140,9 +140,25 @@ namespace LightningDB
140140
return <#= pair.Value #>LibraryFacade.mdb_env_open(env, path, flags, mode);
141141
}
142142

143-
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, int size)
144-
{
145-
return <#= pair.Value #>LibraryFacade.mdb_env_set_mapsize(env, size);
143+
int INativeLibraryFacade.mdb_env_set_mapsize(IntPtr env, long size)
144+
{<# if (!pair.Value.EndsWith("64Bit")) { #>
145+
146+
IntPtr sizeValue;
147+
if (<#= pair.Value.Equals("Fallback") ? "!Environment.Is64BitProcess && " : "" #>size > Int32.MaxValue)
148+
{
149+
if (LightningConfig.Environment.AutoReduceMapSizeIn32BitProcess)
150+
sizeValue = new IntPtr(Int32.MaxValue);
151+
else
152+
throw new InvalidOperationException("Can't set MapSize larger than Int32.MaxValue in 32-bit process");
153+
}
154+
else
155+
{
156+
sizeValue = new IntPtr((int) size);
157+
}
158+
<# } else { #>
159+
var sizeValue = new IntPtr(size);
160+
<# } #>
161+
return <#= pair.Value #>LibraryFacade.mdb_env_set_mapsize(env, sizeValue);
146162
}
147163

148164
int INativeLibraryFacade.mdb_env_get_maxreaders(IntPtr env, out uint readers)

0 commit comments

Comments
 (0)