forked from CoreyKaylor/Lightning.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLmdb.cs
More file actions
1195 lines (1075 loc) · 59.7 KB
/
Lmdb.cs
File metadata and controls
1195 lines (1075 loc) · 59.7 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Runtime.InteropServices;
namespace LightningDB.Native;
#if NET7_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif
public static partial class Lmdb
{
#if NET7_0_OR_GREATER
/// <summary>
/// Creates an LMDB environment handle.
/// </summary>
/// <param name="env">The address where the new handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_create(out nint env);
/// <summary>
/// Closes the environment and releases the memory map.
/// </summary>
/// <param name="env">The environment handle to close</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void mdb_env_close(nint env);
/// <summary>
/// Opens an environment handle with the specified path, flags, and permissions.
/// </summary>
/// <param name="env">The environment handle to open</param>
/// <param name="path">The directory in which the database files reside</param>
/// <param name="flags">Special options for this environment</param>
/// <param name="mode">The UNIX permissions to set on created files</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_open(nint env, string path, EnvironmentOpenFlags flags, UnixAccessMode mode);
/// <summary>
/// Sets the size of the memory map to use for this environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="size">The size in bytes</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_set_mapsize(nint env, nint size);
/// <summary>
/// Gets the maximum number of threads/reader slots for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="readers">Address where the number of readers will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_get_maxreaders(nint env, out uint readers);
/// <summary>
/// Sets the maximum number of threads/reader slots for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="readers">The number of reader slots to allocate</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_set_maxreaders(nint env, uint readers);
/// <summary>
/// Sets the maximum number of named databases for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="dbs">The maximum number of databases</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_set_maxdbs(nint env, uint dbs);
/// <summary>
/// Sets environment flags.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="flags">The flags to set</param>
/// <param name="onoff">A boolean to turn the flags on or off</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_set_flags(nint env, uint flags, [MarshalAs(UnmanagedType.I1)] bool onoff);
/// <summary>
/// Gets environment flags.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="flags">Address where the flags will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_get_flags(nint env, out uint flags);
/// <summary>
/// Returns the path that was used in mdb_env_open.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">Address where the path will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_get_path(nint env, out nint path);
/// <summary>
/// Returns the file descriptor for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="fd">Address where the descriptor will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_get_fd(nint env, out nint fd);
/// <summary>
/// Returns the maximum size of keys and MDB_DUPSORT data we can write.
/// </summary>
/// <param name="env">The environment handle</param>
/// <returns>The maximum size of a key we can write</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int mdb_env_get_maxkeysize(nint env);
/// <summary>
/// Set application information associated with the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="ctx">An arbitrary pointer for your application's use</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_set_userctx(nint env, nint ctx);
/// <summary>
/// Get the application information associated with the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <returns>The pointer set by mdb_env_set_userctx</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial nint mdb_env_get_userctx(nint env);
/// <summary>
/// Set or reset the assert callback for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="func">A callback function to run when an assertion fails</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_set_assert(nint env, nint func);
/// <summary>
/// Opens a database in the environment.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="name">The name of the database to open (NULL for the main DB)</param>
/// <param name="flags">Special options for this database</param>
/// <param name="db">Address where the database handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_dbi_open(nint txn, string? name, DatabaseOpenFlags flags, out uint db);
/// <summary>
/// Closes a database handle in the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="dbi">A database handle returned by mdb_dbi_open</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void mdb_dbi_close(nint env, uint dbi);
/// <summary>
/// Retrieves the flags for a database handle.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="flags">Address where the flags will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_dbi_flags(nint txn, uint dbi, out uint flags);
/// <summary>
/// Empties or deletes a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="del">If true, delete the DB from the environment; otherwise just empty it</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_drop(nint txn, uint dbi, [MarshalAs(UnmanagedType.I1)] bool del);
/// <summary>
/// Creates a transaction for use with the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="parent">Parent transaction (for nested transactions; NULL for none)</param>
/// <param name="flags">Special transaction flags</param>
/// <param name="txn">Address where the new transaction handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_txn_begin(nint env, nint parent, TransactionBeginFlags flags, out nint txn);
/// <summary>
/// Returns the transaction's environment.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>The environment handle</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial nint mdb_txn_env(nint txn);
/// <summary>
/// Returns the transaction's ID.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>The transaction ID</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial nuint mdb_txn_id(nint txn);
/// <summary>
/// Commits all the operations of a transaction into the database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_txn_commit(nint txn);
/// <summary>
/// Abandons all the operations of the transaction instead of saving them.
/// </summary>
/// <param name="txn">A transaction handle</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void mdb_txn_abort(nint txn);
/// <summary>
/// Resets a read-only transaction.
/// </summary>
/// <param name="txn">A transaction handle</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void mdb_txn_reset(nint txn);
/// <summary>
/// Renews a read-only transaction that was previously reset.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_txn_renew(nint txn);
/// <summary>
/// Returns the LMDB library version and version information.
/// </summary>
/// <param name="major">The library major version number</param>
/// <param name="minor">The library minor version number</param>
/// <param name="patch">The library patch version number</param>
/// <returns>Pointer to a version string</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial nint mdb_version(out int major, out int minor, out int patch);
/// <summary>
/// Returns a string describing a given error code.
/// </summary>
/// <param name="err">The error code</param>
/// <returns>A pointer to the error string</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial nint mdb_strerror(int err);
/// <summary>
/// Returns statistics about a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="stat">Address where the database statistics will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_stat(nint txn, uint dbi, out MDBStat stat);
/// <summary>
/// Copies an LMDB environment to the specified path.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">The directory in which the backup will reside</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_copy(nint env, string path);
/// <summary>
/// Copies an LMDB environment to the specified file descriptor.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="fd">The file descriptor to write to</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_copyfd(nint env, nint fd);
/// <summary>
/// Copies an LMDB environment to the specified path, with options for compaction.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">The directory in which the backup will reside</param>
/// <param name="copyFlags">Special options for this copy operation</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME, StringMarshalling = StringMarshalling.Utf8)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopyFlags copyFlags);
/// <summary>
/// Copies an LMDB environment to the specified file descriptor, with options for compaction.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="fd">The file descriptor to write to</param>
/// <param name="copyFlags">Special options for this copy operation</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_copyfd2(nint env, nint fd, EnvironmentCopyFlags copyFlags);
/// <summary>
/// Returns information about the LMDB environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="stat">Address where the environment info will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_info(nint env, out MDBEnvInfo stat);
/// <summary>
/// Returns statistics about the LMDB environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="stat">Address where the environment statistics will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_stat(nint env, out MDBStat stat);
/// <summary>
/// Flushes all data to the environment's data file.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="force">If true, force a synchronous flush; otherwise if the environment has MDB_NOSYNC or MDB_MAPASYNC, it will not be synchronous</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_env_sync(nint env, [MarshalAs(UnmanagedType.I1)] bool force);
/// <summary>
/// Retrieves items from a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to search for</param>
/// <param name="data">Address where the retrieved data will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);
/// <summary>
/// Returns count of duplicates for the current key in a cursor.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <param name="countp">Address where the count will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_cursor_count(nint cursor, out nuint countp);
/// <summary>
/// Stores key/data pairs in a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to store</param>
/// <param name="data">The data to store</param>
/// <param name="flags">Special options for this operation</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);
/// <summary>
/// Deletes items from a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to delete</param>
/// <param name="data">The data to delete (only needed for MDB_DUPSORT)</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_del(nint txn, uint dbi, ref MDBValue key, ref MDBValue data);
/// <summary>
/// Deletes items from a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to delete</param>
/// <param name="data">NULL pointer to delete all of the data items for the key</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_del(nint txn, uint dbi, ref MDBValue key, nint data);
/// <summary>
/// Creates a cursor handle for the specified transaction and database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="cursor">Address where the new cursor handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_cursor_open(nint txn, uint dbi, out nint cursor);
/// <summary>
/// Closes a cursor handle.
/// </summary>
/// <param name="cursor">A cursor handle to close</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void mdb_cursor_close(nint cursor);
/// <summary>
/// Renews a cursor handle.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="cursor">A cursor handle to renew</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_cursor_renew(nint txn, nint cursor);
/// <summary>
/// Returns the cursor's transaction handle.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <returns>The transaction handle</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial nint mdb_cursor_txn(nint cursor);
/// <summary>
/// Returns the cursor's database handle.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <returns>The database handle</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial uint mdb_cursor_dbi(nint cursor);
/// <summary>
/// Retrieves key/data pairs from the database using a cursor.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <param name="key">The key for the current item</param>
/// <param name="data">The data for the current item</param>
/// <param name="op">The cursor operation to perform</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_cursor_get(nint cursor, ref MDBValue key, ref MDBValue data, CursorOperation op);
/// <summary>
/// Stores key/data pairs into the database using a cursor.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <param name="key">The key to store</param>
/// <param name="mdbValue">The data to store</param>
/// <param name="flags">Special options for this operation</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_cursor_put(nint cursor, ref MDBValue key, ref MDBValue mdbValue, CursorPutOptions flags);
/// <summary>
/// Deletes the current key/data pair to which the cursor refers.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <param name="flags">Special options for this operation</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_cursor_del(nint cursor, CursorDeleteOption flags);
/// <summary>
/// Sets a custom key comparison function for a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="cmp">The comparison function to set</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_set_compare(nint txn, uint dbi, CompareFunction cmp);
/// <summary>
/// Sets a custom data comparison function for a database with MDB_DUPSORT.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="cmp">The comparison function to set</param>
/// <returns>A result code indicating success or failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial MDBResultCode mdb_set_dupsort(nint txn, uint dbi, CompareFunction cmp);
/// <summary>
/// Compares two data items according to a database's key comparison function.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="a">The first item to compare</param>
/// <param name="b">The second item to compare</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int mdb_cmp(nint txn, uint dbi, ref MDBValue a, ref MDBValue b);
/// <summary>
/// Compares two data items according to a database's data comparison function.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="a">The first item to compare</param>
/// <param name="b">The second item to compare</param>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int mdb_dcmp(nint txn, uint dbi, ref MDBValue a, ref MDBValue b);
/// <summary>
/// Lists all the readers in the environment and the transaction they're holding.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="func">A function to call for each reader</param>
/// <param name="ctx">Arbitrary context data to pass to the function</param>
/// <returns>Number of readers that were found</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int mdb_reader_list(nint env, nint func, nint ctx);
/// <summary>
/// Checks for stale readers in the environment reader table.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="dead">Address where the number of stale readers cleaned up will be stored</param>
/// <returns>0 on success, non-zero on failure</returns>
[LibraryImport(MDB_DLL_NAME)]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int mdb_reader_check(nint env, out int dead);
#endif
}
public static partial class Lmdb
{
private const string MDB_DLL_NAME = "lmdb";
/// <summary>
/// Sets the size of the memory map to use for this environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="size">The size in bytes</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_env_set_mapsize(nint env, long size)
{
return mdb_env_set_mapsize(env, (nint)size);
}
/// <summary>
/// Stores key/data pairs in a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to store</param>
/// <param name="value">The data to store</param>
/// <param name="flags">Special options for this operation</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_put(nint txn, uint dbi, MDBValue key, MDBValue value, PutOptions flags)
{
return mdb_put(txn, dbi, ref key, ref value, flags);
}
/// <summary>
/// Deletes items from a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to delete</param>
/// <param name="value">The data to delete</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_del(nint txn, uint dbi, MDBValue key, MDBValue value)
{
return mdb_del(txn, dbi, ref key, ref value);
}
/// <summary>
/// Deletes items from a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="key">The key to delete</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_del(nint txn, uint dbi, MDBValue key)
{
return mdb_del(txn, dbi, ref key, 0);
}
/// <summary>
/// Stores key/data pairs into the database using a cursor.
/// </summary>
/// <param name="cursor">A cursor handle</param>
/// <param name="key">The key to store</param>
/// <param name="value">The data to store</param>
/// <param name="flags">Special options for this operation</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_cursor_put(nint cursor, MDBValue key, MDBValue value, CursorPutOptions flags)
{
return mdb_cursor_put(cursor, ref key, ref value, flags);
}
/// <summary>
/// Stores multiple contiguous data elements in a single request with a cursor.
/// May only be used with MDB_DUPFIXED.
/// </summary>
/// <param name="cursor">Pointer to cursor</param>
/// <param name="key">The key to store</param>
/// <param name="data">This span must be pinned or stackalloc memory</param>
/// <param name="flags">Special options for this operation</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_cursor_put(nint cursor, ref MDBValue key, ref Span<MDBValue> data,
CursorPutOptions flags)
{
ref var dataRef = ref MemoryMarshal.GetReference(data);
return mdb_cursor_put(cursor, ref key, ref dataRef, flags);
}
#if !NET7_0_OR_GREATER
/// <summary>
/// Creates an LMDB environment handle.
/// </summary>
/// <param name="env">The address where the new handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_create(out nint env);
/// <summary>
/// Closes the environment and releases the memory map.
/// </summary>
/// <param name="env">The environment handle to close</param>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void mdb_env_close(nint env);
/// <summary>
/// Opens an environment handle with the specified path, flags, and permissions.
/// </summary>
/// <param name="env">The environment handle to open</param>
/// <param name="path">The directory in which the database files reside</param>
/// <param name="flags">Special options for this environment</param>
/// <param name="mode">The UNIX permissions to set on created files</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_open(nint env, byte[] path, EnvironmentOpenFlags flags,
UnixAccessMode mode);
/// <summary>
/// Opens an environment handle with the specified path, flags, and permissions.
/// </summary>
/// <param name="env">The environment handle to open</param>
/// <param name="path">The directory in which the database files reside</param>
/// <param name="flags">Special options for this environment</param>
/// <param name="mode">The UNIX permissions to set on created files</param>
/// <returns>A result code indicating success or failure</returns>
internal static MDBResultCode mdb_env_open(nint env, string path, EnvironmentOpenFlags flags,
UnixAccessMode mode)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(path + "\0");
return mdb_env_open(env, bytes, flags, mode);
}
/// <summary>
/// Sets the size of the memory map to use for this environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="size">The size in bytes</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_mapsize(nint env, nint size);
/// <summary>
/// Gets the maximum number of threads/reader slots for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="readers">Address where the number of readers will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_get_maxreaders(nint env, out uint readers);
/// <summary>
/// Sets the maximum number of threads/reader slots for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="readers">The number of reader slots to allocate</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_maxreaders(nint env, uint readers);
/// <summary>
/// Sets the maximum number of named databases for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="dbs">The maximum number of databases</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_maxdbs(nint env, uint dbs);
/// <summary>
/// Sets environment flags.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="flags">The flags to set</param>
/// <param name="onoff">A boolean to turn the flags on or off</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_flags(nint env, uint flags, bool onoff);
/// <summary>
/// Gets environment flags.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="flags">Address where the flags will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_get_flags(nint env, out uint flags);
/// <summary>
/// Returns the path that was used in mdb_env_open.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">Address where the path will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_get_path(nint env, out nint path);
/// <summary>
/// Returns the file descriptor for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="fd">Address where the descriptor will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_get_fd(nint env, out nint fd);
/// <summary>
/// Returns the maximum size of keys and MDB_DUPSORT data we can write.
/// </summary>
/// <param name="env">The environment handle</param>
/// <returns>The maximum size of a key we can write</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int mdb_env_get_maxkeysize(nint env);
/// <summary>
/// Set application information associated with the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="ctx">An arbitrary pointer for your application's use</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_userctx(nint env, nint ctx);
/// <summary>
/// Get the application information associated with the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <returns>The pointer set by mdb_env_set_userctx</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern nint mdb_env_get_userctx(nint env);
/// <summary>
/// Set or reset the assert callback for the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="func">A callback function to run when an assertion fails</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_set_assert(nint env, nint func);
/// <summary>
/// Opens a database in the environment.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="name">The name of the database to open (NULL for the main DB)</param>
/// <param name="flags">Special options for this database</param>
/// <param name="db">Address where the database handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_dbi_open(nint txn, string? name, DatabaseOpenFlags flags, out uint db);
/// <summary>
/// Closes a database handle in the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="dbi">A database handle returned by mdb_dbi_open</param>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void mdb_dbi_close(nint env, uint dbi);
/// <summary>
/// Retrieves the flags for a database handle.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="flags">Address where the flags will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_dbi_flags(nint txn, uint dbi, out uint flags);
/// <summary>
/// Empties or deletes a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="del">If true, delete the DB from the environment; otherwise just empty it</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_drop(nint txn, uint dbi, bool del);
/// <summary>
/// Creates a transaction for use with the environment.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="parent">Parent transaction (for nested transactions; NULL for none)</param>
/// <param name="flags">Special transaction flags</param>
/// <param name="txn">Address where the new transaction handle will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_txn_begin(nint env, nint parent, TransactionBeginFlags flags, out nint txn);
/// <summary>
/// Returns the transaction's environment.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>The environment handle</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern nint mdb_txn_env(nint txn);
/// <summary>
/// Returns the transaction's ID.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>The transaction ID</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern nuint mdb_txn_id(nint txn);
/// <summary>
/// Commits all the operations of a transaction into the database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_txn_commit(nint txn);
/// <summary>
/// Abandons all the operations of the transaction instead of saving them.
/// </summary>
/// <param name="txn">A transaction handle</param>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void mdb_txn_abort(nint txn);
/// <summary>
/// Resets a read-only transaction.
/// </summary>
/// <param name="txn">A transaction handle</param>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void mdb_txn_reset(nint txn);
/// <summary>
/// Renews a read-only transaction that was previously reset.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_txn_renew(nint txn);
/// <summary>
/// Returns the LMDB library version and version information.
/// </summary>
/// <param name="major">The library major version number</param>
/// <param name="minor">The library minor version number</param>
/// <param name="patch">The library patch version number</param>
/// <returns>Pointer to a version string</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern nint mdb_version(out int major, out int minor, out int patch);
/// <summary>
/// Returns a string describing a given error code.
/// </summary>
/// <param name="err">The error code</param>
/// <returns>A pointer to the error string</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern nint mdb_strerror(int err);
/// <summary>
/// Returns statistics about a database.
/// </summary>
/// <param name="txn">A transaction handle</param>
/// <param name="dbi">A database handle</param>
/// <param name="stat">Address where the database statistics will be stored</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_stat(nint txn, uint dbi, out MDBStat stat);
/// <summary>
/// Copies an LMDB environment to the specified path.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">The directory in which the backup will reside</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_copy(nint env, byte[] path);
/// <summary>
/// Copies an LMDB environment to the specified path.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">The directory in which the backup will reside</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_env_copy(nint env, string path)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(path + "\0");
return mdb_env_copy(env, bytes);
}
/// <summary>
/// Copies an LMDB environment to the specified file descriptor.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="fd">The file descriptor to write to</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_copyfd(nint env, nint fd);
/// <summary>
/// Copies an LMDB environment to the specified file descriptor, with options for compaction.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="fd">The file descriptor to write to</param>
/// <param name="copyFlags">Special options for this copy operation</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_copyfd2(nint env, nint fd, EnvironmentCopyFlags copyFlags);
/// <summary>
/// Copies an LMDB environment to the specified path, with options for compaction.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">The directory in which the backup will reside</param>
/// <param name="copyFlags">Special options for this copy operation</param>
/// <returns>A result code indicating success or failure</returns>
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern MDBResultCode mdb_env_copy2(nint env, byte[] path, EnvironmentCopyFlags copyFlags);
/// <summary>
/// Copies an LMDB environment to the specified path, with options for compaction.
/// </summary>
/// <param name="env">The environment handle</param>
/// <param name="path">The directory in which the backup will reside</param>
/// <param name="copyFlags">Special options for this copy operation</param>
/// <returns>A result code indicating success or failure</returns>
public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopyFlags copyFlags)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(path + "\0");
return mdb_env_copy2(env, bytes, copyFlags);
}
/// <summary>
/// Returns information about the LMDB environment.
/// </summary>