1
- using System ;
1
+
2
+
3
+
4
+
5
+ using System ;
2
6
using System . Collections . Generic ;
3
7
using System . Linq ;
4
8
using System . Runtime . InteropServices ;
7
11
namespace LightningDB
8
12
{
9
13
14
+
10
15
class Native32BitLibraryFacade : INativeLibraryFacade
11
16
{
12
17
public const string LibraryName = "lmdb32" ;
@@ -26,7 +31,7 @@ class Native32BitLibraryFacade : INativeLibraryFacade
26
31
private static extern int mdb_env_open ( IntPtr env , string path , EnvironmentOpenFlags flags , int mode ) ;
27
32
28
33
[ 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 ) ;
30
35
31
36
[ DllImport ( LibraryName , CallingConvention = CallingConvention . Cdecl ) ]
32
37
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
125
130
return Native32BitLibraryFacade . mdb_env_open ( env , path , flags , mode ) ;
126
131
}
127
132
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 ) ;
131
149
}
132
150
133
151
int INativeLibraryFacade . mdb_env_get_maxreaders ( IntPtr env , out uint readers )
@@ -256,6 +274,7 @@ int INativeLibraryFacade.mdb_cursor_del(IntPtr cursor, CursorDeleteOption flags)
256
274
}
257
275
}
258
276
277
+
259
278
class Native64BitLibraryFacade : INativeLibraryFacade
260
279
{
261
280
public const string LibraryName = "lmdb64" ;
@@ -275,7 +294,7 @@ class Native64BitLibraryFacade : INativeLibraryFacade
275
294
private static extern int mdb_env_open ( IntPtr env , string path , EnvironmentOpenFlags flags , int mode ) ;
276
295
277
296
[ 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 ) ;
279
298
280
299
[ DllImport ( LibraryName , CallingConvention = CallingConvention . Cdecl ) ]
281
300
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
374
393
return Native64BitLibraryFacade . mdb_env_open ( env , path , flags , mode ) ;
375
394
}
376
395
377
- int INativeLibraryFacade . mdb_env_set_mapsize ( IntPtr env , int size )
396
+ int INativeLibraryFacade . mdb_env_set_mapsize ( IntPtr env , long size )
378
397
{
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 ) ;
380
401
}
381
402
382
403
int INativeLibraryFacade . mdb_env_get_maxreaders ( IntPtr env , out uint readers )
@@ -505,6 +526,7 @@ int INativeLibraryFacade.mdb_cursor_del(IntPtr cursor, CursorDeleteOption flags)
505
526
}
506
527
}
507
528
529
+
508
530
class FallbackLibraryFacade : INativeLibraryFacade
509
531
{
510
532
public const string LibraryName = "lmdb" ;
@@ -524,7 +546,7 @@ class FallbackLibraryFacade : INativeLibraryFacade
524
546
private static extern int mdb_env_open ( IntPtr env , string path , EnvironmentOpenFlags flags , int mode ) ;
525
547
526
548
[ 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 ) ;
528
550
529
551
[ DllImport ( LibraryName , CallingConvention = CallingConvention . Cdecl ) ]
530
552
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
623
645
return FallbackLibraryFacade . mdb_env_open ( env , path , flags , mode ) ;
624
646
}
625
647
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 ) ;
629
664
}
630
665
631
666
int INativeLibraryFacade . mdb_env_get_maxreaders ( IntPtr env , out uint readers )
@@ -754,4 +789,5 @@ int INativeLibraryFacade.mdb_cursor_del(IntPtr cursor, CursorDeleteOption flags)
754
789
}
755
790
}
756
791
792
+
757
793
}
0 commit comments