extremedb 0.1.2

McObject eXtremeDB bindings
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
// database.rs
//
// This file is a part of the eXtremeDB source code
// Copyright (c) 2020 McObject LLC
// All Rights Reserved

//! Core database types.
//!
//! For detailed information on database configuration, refer to
//! the *e*X*treme*DB reference pages.
//!
//! # Examples
//!
//! Creating an in-memory database using conventional memory:
//!
//! ```
//! use extremedb::{database, device, runtime, Result};
//!
//! fn main() -> Result<()> {
//!     let runtime = runtime::Runtime::start(vec![]);
//!
//! #     if runtime.info().multiprocess_access_supported() {
//! #         return Ok(());
//! #     }
//!     // This example creates a conventional memory device, and will not work with the
//!     // shared memory runtime.
//!     assert!(!runtime.info().multiprocess_access_supported());
//!
//!     // Default parameters.
//!     let db_params = database::Params::new();
//!
//!     // Create an in-memory device and allocate memory.
//!     let mut devs = vec![device::Device::new_mem_conv(
//!         device::Assignment::Database,
//!         1024 * 1024,
//!     )?];
//!
//!     // Open the database.
//!     let db = database::Database::open(&runtime, "test_db", None, &mut devs, db_params)?;
//!
//!     // ...
//!
//! #     drop(db);
//!     Ok(())
//! }
//! ```
//!
//! Creating a persistent database (note that `extremedb_sys` must be built
//! with persistent database support — refer to its documentation pages for
//! the details):
//!
//! ```
//! use extremedb::{database, device, runtime, Result};
//! # use std::fs;
//!
//! fn main() -> Result<()> {
//!     let runtime = runtime::Runtime::start(vec![]);
//! #     // Do not run this example if built without disk support, or with shared memory.
//! #     if !runtime.info().disk_supported() || runtime.info().multiprocess_access_supported() {
//! #         return Ok(());
//! #     }
//!
//!     // Make sure that persistent database support is enabled.
//!     assert!(runtime.info().disk_supported());
//!
//!     // This example creates conventional memory devices, and will not work with the
//!     // shared memory runtime.
//!     assert!(!runtime.info().multiprocess_access_supported());
//!
//!     // Default parameters.
//!     let db_params = database::Params::new();
//!
//!     // Database and log file names.
//!     let db_file = "db.dbs";
//!     let log_file = "db.log";
//!
//!     // Create an array of devices for the persistent database.
//!     let mut devs = vec![
//!         // In-memory portion of the database.
//!         device::Device::new_mem_conv(device::Assignment::Database, 1024 * 1024)?,
//!         // Disk page pool.
//!         device::Device::new_mem_conv(device::Assignment::Cache, 1024 * 1024)?,
//!         // Database file.
//!         device::Device::new_file(
//!             device::Assignment::Persistent,
//!             device::FileOpenFlags::new(),
//!             db_file,
//!         )?,
//!         // Database log file.
//!         device::Device::new_file(
//!             device::Assignment::Log,
//!             device::FileOpenFlags::new(),
//!             log_file,
//!         )?,
//!     ];
//!
//!     // Open the database.
//!     let db = database::Database::open(&runtime, "test_db", None, &mut devs, db_params)?;
//!
//!     // ...
//! #    drop(db);
//! #    drop(runtime);
//! #     let _ = fs::remove_file(db_file);
//! #     let _ = fs::remove_file(log_file);
//!
//!     Ok(())
//! }
//! ```

use std::ffi::{c_void, CStr, CString};
use std::marker::PhantomData;
use std::mem::{self, MaybeUninit};
use std::ptr;

use crate::device::Device;
use crate::dict;
use crate::runtime::Runtime;
use crate::util::BitMask32;
use crate::{exdb_sys, mco_ret, result_from_code, Error, Result};

use exdb_sys::MCO_COMMIT_POLICY_E as mco_commit_policy;
use exdb_sys::MCO_COMPRESSION_MASK_ as mco_compression_mask;
use exdb_sys::MCO_DB_MODE_MASK_ as mco_db_mode_mask;
use exdb_sys::MCO_LOG_TYPE_ as mco_log_type;
use exdb_sys::MCO_TRANS_SCHED_POLICY_E_ as mco_trans_sched_policy;

macro_rules! db_param_scalar {
    ($(#[$p_meta:meta])* $param:ident, $(#[$g_meta:meta])* $getter:ident, $ty:ty, $mco_ty:ty) => {
        $(#[$p_meta])*
        pub fn $param(&mut self, $param: $ty) -> &mut Self {
            self.p.$param = $param as $mco_ty;
            self
        }

        $(#[$g_meta])*
        pub fn $getter(&self) -> $ty {
            self.p.$param as $ty
        }
    };

    ($(#[$p_meta:meta])* $param:ident, $(#[$g_meta:meta])* $getter:ident, $ty:ty) => {
        db_param_scalar!($(#[$p_meta])* $param, $(#[$g_meta])* $getter, $ty, $ty);
    };
}

/// Database log type.
pub enum LogType {
    /// No log.
    None = mco_log_type::NO_LOG as isize,
    /// Redo log.
    Redo = mco_log_type::REDO_LOG as isize,
    /// Undo log.
    Undo = mco_log_type::UNDO_LOG as isize,
}

impl LogType {
    fn from_mco(t: mco_log_type::Type) -> Option<Self> {
        match t {
            mco_log_type::NO_LOG => Some(LogType::None),
            mco_log_type::REDO_LOG => Some(LogType::Redo),
            mco_log_type::UNDO_LOG => Some(LogType::Undo),
            _ => None,
        }
    }
}

/// Transaction commit policy.
///
/// The default policy is `SyncFlush`.
pub enum CommitPolicy {
    /// Flush the cache and synchronize the filesystem buffers for both
    /// database and log files on every commit.
    SyncFlush = mco_commit_policy::MCO_COMMIT_SYNC_FLUSH as isize,

    /// Don't flush the database cache to disk upon transaction commit.
    Buffered = mco_commit_policy::MCO_COMMIT_BUFFERED as isize,

    /// Keep modified pages in the page pool (like `Buffered`); commit delayed
    /// transactions to the persistent storage at once when the total size of
    /// all kept pages or the number of delayed commits reach a threshold
    /// value.
    Delayed = mco_commit_policy::MCO_COMMIT_DELAYED as isize,

    /// Don't synchronize the file system buffers explicitly and let the
    /// filesystem determine when the data is actually written to the media.
    NoSync = mco_commit_policy::MCO_COMMIT_NO_SYNC as isize,
}

impl CommitPolicy {
    fn from_mco(p: mco_commit_policy::Type) -> Option<Self> {
        match p {
            mco_commit_policy::MCO_COMMIT_SYNC_FLUSH => Some(CommitPolicy::SyncFlush),
            mco_commit_policy::MCO_COMMIT_BUFFERED => Some(CommitPolicy::Buffered),
            mco_commit_policy::MCO_COMMIT_DELAYED => Some(CommitPolicy::Delayed),
            mco_commit_policy::MCO_COMMIT_NO_SYNC => Some(CommitPolicy::NoSync),
            _ => None,
        }
    }
}

/// Transaction scheduling policy.
///
/// The default policy is `FIFO`.
pub enum TransSchedPolicy {
    /// First in, first out scheduling.
    FIFO = mco_trans_sched_policy::MCO_SCHED_FIFO as isize,

    /// Prioritize read-only transactions.
    ReaderFavor = mco_trans_sched_policy::MCO_SCHED_READER_FAVOR as isize,

    /// Prioritize read-write transactions.
    WriterFavor = mco_trans_sched_policy::MCO_SCHED_WRITER_FAVOR as isize,
}

impl TransSchedPolicy {
    fn from_mco(p: mco_trans_sched_policy::Type) -> Option<Self> {
        match p {
            mco_trans_sched_policy::MCO_SCHED_FIFO => Some(TransSchedPolicy::FIFO),
            mco_trans_sched_policy::MCO_SCHED_READER_FAVOR => Some(TransSchedPolicy::ReaderFavor),
            mco_trans_sched_policy::MCO_SCHED_WRITER_FAVOR => Some(TransSchedPolicy::WriterFavor),
            _ => None,
        }
    }
}

/// Database log parameters.
///
/// # Examples
///
/// Configuring the `Delayed` commit policy:
///
/// ```
/// # use extremedb::database::{LogParams, CommitPolicy};
/// let mut params = LogParams::new();
/// params.default_commit_policy(CommitPolicy::Delayed).max_delayed_transactions(10);
/// ```
pub struct LogParams {
    p: exdb_sys::mco_log_params_t,
}

impl LogParams {
    /// Creates a new log parameters structure initialized with default values.
    pub fn new() -> Self {
        LogParams {
            p: exdb_sys::mco_log_params_t {
                default_commit_policy: mco_commit_policy::MCO_COMMIT_SYNC_FLUSH,
                redo_log_limit: 16 * 1024 * 1024,
                delayed_commit_threshold: 0,
                max_delayed_transactions: 0,
                max_commit_delay: 0,
            },
        }
    }

    fn with_params(p: &exdb_sys::mco_log_params_t) -> Self {
        LogParams { p: *p }
    }

    /// Sets the default commit policy.
    pub fn default_commit_policy(&mut self, default_commit_policy: CommitPolicy) -> &mut Self {
        self.p.default_commit_policy = default_commit_policy as mco_commit_policy::Type;
        self
    }

    /// Returns the default commit policy.
    ///
    /// Returns `None` if the policy value is invalid.
    pub fn get_default_commit_policy(&self) -> Option<CommitPolicy> {
        CommitPolicy::from_mco(self.p.default_commit_policy)
    }

    db_param_scalar!(
        /// Sets the log file size limit.
        redo_log_limit,
        /// Returns the log file size limit.
        get_redo_log_limit,
        usize,
        exdb_sys::mco_offs_t
    );

    db_param_scalar!(
        /// Sets the delayed commit threshold.
        ///
        /// When the total size of all pinned pages reaches this threshold,
        /// all delayed transactions are committed to the persistent storage
        /// at once.
        ///
        /// This option is only used with [`CommitPolicy::Delayed`].
        ///
        /// [`CommitPolicy::Delayed`]: ./enum.CommitPolicy.html#variant.Delayed
        delayed_commit_threshold,
        /// Returns the delayed commit threshold.
        get_delayed_commit_threshold,
        usize,
        exdb_sys::mco_offs_t
    );

    db_param_scalar!(
        /// Sets the maximum number of delayed transactions.
        ///
        /// Delayed transactions are committed to the persistent storage
        /// when their number reaches this threshold.
        ///
        /// This option is only used with [`CommitPolicy::Delayed`].
        ///
        /// [`CommitPolicy::Delayed`]: ./enum.CommitPolicy.html#variant.Delayed
        max_delayed_transactions,
        /// Returns the maximum number of delayed transactions.
        get_max_delayed_transactions,
        u32,
        exdb_sys::mco_counter32_t
    );

    db_param_scalar!(
        /// Sets the maximum commit delay.
        ///
        /// This is the number of application-defined clock cycles to wait
        /// before all delayed transactions are committed to the persistent
        /// storage at once.
        ///
        /// This option is only used with [`CommitPolicy::Delayed`].
        ///
        /// [`CommitPolicy::Delayed`]: ./enum.CommitPolicy.html#variant.Delayed
        max_commit_delay,
        /// Returns the maximum commit delay.
        get_max_commit_delay,
        u32
    );
}

macro_rules! bitmask_flag {
    ($(#[$f_meta:meta])* $flag:ident, $(#[$g_meta:meta])* $getter:ident, $f:expr) => {
        $(#[$f_meta])*
        pub fn $flag(&mut self, v: bool) -> &mut Self {
            self.0.set_bit_value($f, v);
            self
        }

        $(#[$g_meta])*
        pub fn $getter(&self) -> bool {
            self.0.get_bit_value($f)
        }
    };
}

/// A mask of page classes for compression.
///
/// This structure is only used when the in-memory database compression
/// is enabled.
///
/// # Examples
///
/// Enable compression of object and blob header pages:
///
/// ```
/// # use extremedb::database::CompressionMask;
/// let mut mask = CompressionMask::new();
/// mask.obj_head(true).blob_head(true);
/// ```
pub struct CompressionMask(BitMask32);

impl CompressionMask {
    /// Creates a new compression mask filled with zeroes.
    pub fn new() -> Self {
        CompressionMask(BitMask32::new())
    }

    fn from_mco(m: u32) -> Self {
        CompressionMask(BitMask32::from(m))
    }

    bitmask_flag!(
        /// Enables compression of object header pages.
        obj_head,
        /// Returns the current flag value.
        get_obj_head,
        mco_compression_mask::MCO_COMPRESSION_OBJ_HEAD as u32
    );
    bitmask_flag!(
        /// Enables compression of object node pages.
        obj_node,
        /// Returns the current flag value.
        get_obj_node,
        mco_compression_mask::MCO_COMPRESSION_OBJ_NODE as u32
    );
    bitmask_flag!(
        /// Enables compression of blob header pages.
        blob_head,
        /// Returns the current flag value.
        get_blob_head,
        mco_compression_mask::MCO_COMPRESSION_BLOB_HEAD as u32
    );
    bitmask_flag!(
        /// Enables compression of blob tail pages.
        blob_tail,
        /// Returns the current flag value.
        get_blob_tail,
        mco_compression_mask::MCO_COMPRESSION_BLOB_TAIL as u32
    );
    bitmask_flag!(
        /// Enables compression of fixed record pages.
        fixed_rec_set,
        /// Returns the current flag value.
        get_fixed_rec_set,
        mco_compression_mask::MCO_COMPRESSION_FIXEDRECSET as u32
    );
}

/// Database open mode mask.
///
/// # Examples
///
/// Requesting the runtime to open an existing database instance:
///
/// ```
/// # use extremedb::database::ModeMask;
/// let mut mask = ModeMask::new();
/// mask.open_existing(true);
/// ```
pub struct ModeMask(BitMask32);

impl ModeMask {
    /// Creates a new mode mask filled with zeroes.
    pub fn new() -> Self {
        ModeMask(BitMask32::new())
    }

    fn from_mco(m: u32) -> Self {
        ModeMask(BitMask32::from(m))
    }

    bitmask_flag!(
        /// Enables automatic cleanup of stale versions (MVCC) when the
        /// database is opened.
        mode_mvcc_auto_vacuum,
        /// Returns the current flag value.
        get_mode_mvcc_auto_vacuum,
        mco_db_mode_mask::MCO_DB_MODE_MVCC_AUTO_VACUUM as u32
    );
    bitmask_flag!(
        /// Causes the runtime to pre-sort transaction data based on the first
        /// index declared for the class prior to including them in indexes.
        mode_smart_index_insert,
        /// Returns the current flag value.
        get_mode_smart_index_insert,
        mco_db_mode_mask::MCO_DB_MODE_SMART_INDEX_INSERT as u32
    );
    bitmask_flag!(
        /// Instructs the runtime not to initialize an all-in-memory database,
        /// but rather treat the database memory device as an already
        /// initialized memory space.
        open_existing,
        /// Returns the current flag value.
        get_open_existing,
        mco_db_mode_mask::MCO_DB_OPEN_EXISTING as u32
    );
    bitmask_flag!(
        /// Instructs the runtime to calculate and store the CRC for each
        /// database page.
        use_crc_check,
        /// Returns the current flag value.
        get_use_crc_check,
        mco_db_mode_mask::MCO_DB_USE_CRC_CHECK as u32
    );
    bitmask_flag!(
        /// Forces all database classes to be "in-memory" (`transient`) even if
        /// the classes are declared `persistent` in the database schema.
        transient,
        /// Returns the current flag value.
        get_transient,
        mco_db_mode_mask::MCO_DB_TRANSIENT as u32
    );
    bitmask_flag!(
        /// Enables on-demand initialization of memory pages.
        lazy_mem_initialization,
        /// Returns the current flag value.
        get_lazy_mem_initialization,
        mco_db_mode_mask::MCO_DB_LAZY_MEM_INITIALIZATION as u32
    );
    bitmask_flag!(
        /// When the MURSIW transaction manager is used, this flag causes the
        /// runtime to save modified pages to the transaction log file and
        /// flush the log outside of a critical section, allowing read-only
        /// transactions to process.
        mursiw_disk_commit_optimization,
        /// Returns the current flag value.
        get_mursiw_disk_commit_optimization,
        mco_db_mode_mask::MCO_DB_MURSIW_DISK_COMMIT_OPTIMIZATION as u32
    );
    bitmask_flag!(
        /// Causes the runtime to sort and write all non-pinned dirty pages,
        /// instead of writing just a single dirty page thrown away from
        /// the disk page pool when loading a new page from disk.
        bulk_write_modified_pages,
        /// Returns the current flag value.
        get_bulk_write_modified_pages,
        mco_db_mode_mask::MCO_DB_BULK_WRITE_MODIFIED_PAGES as u32
    );
    bitmask_flag!(
        /// Causes the runtime to try to preload the next B-Tree page using
        /// the `fadvise()` system call.
        index_preload,
        /// Returns the current flag value.
        get_index_preload,
        mco_db_mode_mask::MCO_DB_INDEX_PRELOAD as u32
    );
    bitmask_flag!(
        /// Disables nested transactions.
        disable_nested_transactions,
        /// Returns the current flag value.
        get_disable_nested_transactions,
        mco_db_mode_mask::MCO_DB_DISABLE_NESTED_TRANSACTIONS as u32
    );
    bitmask_flag!(
        /// Disables implicit rollback of active transactions on disconnect.
        disable_implicit_rollback,
        /// Returns the current flag value.
        get_disable_implicit_rollback,
        mco_db_mode_mask::MCO_DB_DISABLE_IMPLICIT_ROLLBACK as u32
    );
    bitmask_flag!(
        /// Enables page level encryption for in-memory databases.
        inmemory_protection,
        /// Returns the current flag value.
        get_inmemory_protection,
        mco_db_mode_mask::MCO_DB_INMEMORY_PROTECTION as u32
    );
    bitmask_flag!(
        /// Allows the runtime to exploit CPU cache memory for data structures
        /// and algorithms, which in many cases can improve performance
        /// dramatically over normal RAM access.
        inclusive_btree,
        /// Returns the current flag value.
        get_inclusive_btree,
        mco_db_mode_mask::MCO_DB_INCLUSIVE_BTREE as u32
    );
    bitmask_flag!(
        /// Enables page compression for in-memory databases.
        inmemory_compression,
        /// Returns the current flag value.
        get_inmemory_compression,
        mco_db_mode_mask::MCO_DB_INMEMORY_COMPRESSION as u32
    );
    bitmask_flag!(
        /// Allows the runtime to use separate memory allocation bitmap pages
        /// for aligned and unaligned memory objects.
        separate_bitmap,
        /// Returns the current flag value.
        get_separate_bitmap,
        mco_db_mode_mask::MCO_DB_SEPARATE_BITMAP as u32
    );
    bitmask_flag!(
        /// Disables B-Tree rebalancing when deleting objects.
        disable_btree_rebalance_on_delete,
        /// Returns the current flag value.
        get_disable_btree_rebalance_on_delete,
        mco_db_mode_mask::MCO_DB_DISABLE_BTREE_REBALANCE_ON_DELETE as u32
    );
    bitmask_flag!(
        /// If the first stage of a two-phase commit fails, then this flag
        /// causes the runtime to implicitly rollback the transaction,
        /// not waiting for the application to rollback the transaction
        /// explicitly.
        auto_rollback_first_phase,
        /// Returns the current flag value.
        get_auto_rollback_first_phase,
        mco_db_mode_mask::MCO_DB_AUTO_ROLLBACK_FIRST_PHASE as u32
    );
    bitmask_flag!(
        /// Opens a persistent database that was created while using the MVCC
        /// transaction manager with an application using the MURSIW
        /// transaction manager.
        mvcc_compatibility_mode,
        /// Returns the current flag value.
        get_mvcc_compatibility_mode,
        mco_db_mode_mask::MCO_DB_MVCC_COMPATIBILITY_MODE as u32
    );
    bitmask_flag!(
        /// Allows transactions larger than the page pool size (Redo log only).
        redo_log_optimization,
        /// Returns the current flag value.
        get_redo_log_optimization,
        mco_db_mode_mask::MCO_DB_REDO_LOG_OPTIMIZATION as u32
    );
    bitmask_flag!(
        /// Allows skipping the update of indexes if index keys are modified
        /// by an update operation.
        disable_hot_updates,
        /// Returns the current flag value.
        get_disable_hot_updates,
        mco_db_mode_mask::MCO_DB_DISABLE_HOT_UPDATES as u32
    );
    bitmask_flag!(
        /// Causes all newly created database objects to be inserted into the
        /// indexes so that they can be accessed immediately without an
        /// explicit *checkpoint* call.
        sql_autocheckpoint,
        /// Returns the current flag value.
        get_sql_autocheckpoint,
        mco_db_mode_mask::MCO_DB_SQL_AUTOCHECKPOINT as u32
    );
    bitmask_flag!(
        /// Opens the database in read-only mode.
        mode_read_only,
        /// Returns the current flag value.
        get_mode_read_only,
        mco_db_mode_mask::MCO_DB_MODE_READ_ONLY as u32
    );
    bitmask_flag!(
        /// Use Asynchronous IO for persistent database writes.
        use_aio,
        /// Returns the current flag value.
        get_use_aio,
        mco_db_mode_mask::MCO_DB_USE_AIO as u32
    );
    bitmask_flag!(
        /// Enables the marking of pages for incremental backup.
        incremental_backup,
        /// Returns the current flag value.
        get_incremental_backup,
        mco_db_mode_mask::MCO_DB_INCREMENTAL_BACKUP as u32
    );
    bitmask_flag!(
        /// Directs the runtime to block the entire table space instead of
        /// selected instances during database operations (MVCC only).
        mvcc_table_level_locking,
        /// Returns the current flag value.
        get_mvcc_table_level_locking,
        mco_db_mode_mask::MCO_DB_MVCC_TABLE_LEVEL_LOCKING as u32
    );
    bitmask_flag!(
        /// Instructs the runtime to allocate space for the average object
        /// size, calculated from statistics.
        disable_smart_alloc,
        /// Returns the current flag value.
        get_disable_smart_alloc,
        mco_db_mode_mask::MCO_DB_DISABLE_SMART_ALLOC as u32
    );
}

/// Database parameters.
///
/// For detailed information on the individual parameters and their
/// allowed values, refer to the *e*X*treme*DB reference pages.
///
/// # Examples
///
/// Setting the database log type (persistent databases only):
///
/// ```
/// # use extremedb::database::{Params, LogType};
/// # use extremedb::runtime::Runtime;
/// # let runtime = Runtime::start(vec![]);
/// let mut params = Params::new();
/// params.db_log_type(LogType::Redo);
/// ```
pub struct Params {
    p: exdb_sys::mco_db_params_t,
}

impl Params {
    /// Returns a new parameters structure initialized with default values.
    pub fn new() -> Self {
        let mut p = MaybeUninit::uninit();
        unsafe {
            exdb_sys::mco_db_params_init(p.as_mut_ptr());
        }

        Params {
            p: unsafe { p.assume_init() },
        }
    }

    fn replace_c_string(p: *mut *mut i8, s: Option<&str>) -> Result<()> {
        let new_p = match s {
            Some(s) => CString::new(s)
                .or(Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM)))?
                .into_raw(),
            None => ptr::null_mut(),
        };

        unsafe {
            Params::drop_c_string_if_not_null(*p);
            *p = new_p;
        }

        Ok(())
    }

    fn drop_c_string_if_not_null(p: *mut i8) {
        unsafe {
            if !p.is_null() {
                let cs = CString::from_raw(p);
                drop(cs);
            }
        }
    }

    fn get_c_string(&self, p: *mut i8) -> Result<Option<&str>> {
        if p.is_null() {
            Ok(None)
        } else {
            unsafe {
                let s = CStr::from_ptr(p)
                    .to_str()
                    .or(Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM)))?;
                Ok(Some(s))
            }
        }
    }

    db_param_scalar!(
        /// Sets the size of the memory page size for conventional and shared
        /// memory devices.
        mem_page_size,
        /// Returns the current parameter value.
        get_mem_page_size,
        u16
    );

    db_param_scalar!(
        /// Sets the size of the persistent storage page.
        disk_page_size,
        /// Returns the current parameter value.
        get_disk_page_size,
        u32
    );

    db_param_scalar!(
        /// Sets the maximum number of connections for this database.
        db_max_connections,
        /// Returns the current parameter value.
        get_db_max_connections,
        u32
    );

    db_param_scalar!(
        /// Sets the maximum size of the persistent database.
        disk_max_database_size,
        /// Returns the current parameter value.
        get_disk_max_database_size,
        usize,
        exdb_sys::mco_offs_t
    );

    db_param_scalar!(
        /// Sets the allocation block size for the database runtime to use when
        /// increasing the size of the database file.
        file_extension_quantum,
        /// Returns the current parameter value.
        get_file_extension_quantum,
        usize,
        exdb_sys::mco_offs_t
    );

    /// Sets the database log type.
    pub fn db_log_type(&mut self, db_log_type: LogType) -> &mut Self {
        self.p.db_log_type = db_log_type as mco_log_type::Type;
        self
    }

    /// Returns the current parameter value.
    pub fn get_db_log_type(&self) -> Option<LogType> {
        LogType::from_mco(self.p.db_log_type)
    }

    /// Indicates by how much the runtime increases the size of the connection
    /// structure to hold any application-specific data (referred to as the
    /// application's context). The context is required for the recovery
    /// process to verify that the task which has created the connection
    /// is alive.
    pub fn connection_context_size_for_recovery(&mut self, enable: bool) -> &mut Self {
        if enable {
            self.p.connection_context_size = mem::size_of::<*const c_void>() as u16;
        } else {
            self.p.connection_context_size = 0;
        }

        self
    }

    /// Returns the current parameter value.
    pub fn get_connection_context_size(&self) -> u16 {
        self.p.connection_context_size
    }

    db_param_scalar!(
        /// Sets the factor used to determine when a hash index table
        /// is extended.
        hash_load_factor,
        /// Returns the current parameter value.
        get_hash_load_factor,
        u16
    );

    db_param_scalar!(
        /// Sets the maximum number of active write transactions when
        /// optimistic locking of B-Tree indexes is being performed.
        index_optimistic_lock_threshold,
        /// Returns the current parameter value.
        get_index_optimistic_lock_threshold,
        u16
    );

    /// Sets the log parameters.
    pub fn log_params(&mut self, log_params: LogParams) -> &mut Self {
        self.p.log_params = log_params.p;
        self
    }

    /// Returns the current parameter value.
    pub fn get_log_params(&self) -> LogParams {
        LogParams::with_params(&self.p.log_params)
    }

    /// Sets the mode mask.
    pub fn mode_mask(&mut self, mode_mask: ModeMask) -> &mut Self {
        self.p.mode_mask = mode_mask.0.bit_mask() as i32;
        self
    }

    /// Returns the current parameter value.
    pub fn get_mode_mask(&self) -> ModeMask {
        ModeMask::from_mco(self.p.mode_mask as u32)
    }

    db_param_scalar!(
        /// Sets the minimum number of pages held by the per-connection
        /// allocator in MVCC mode.
        min_conn_local_pages,
        /// Returns the current parameter value.
        get_min_conn_local_pages,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the maximum number of pages held by the per-connection
        /// allocator in MVCC mode.
        max_conn_local_pages,
        /// Returns the current parameter value.
        get_max_conn_local_pages,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the cache priority for allocation bitmap pages.
        allocation_bitmap_caching_priority,
        /// Returns the current parameter value.
        get_allocation_bitmap_caching_priority,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the cache priority for index pages.
        index_caching_priority,
        /// Returns the current parameter value.
        get_index_caching_priority,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the cache priority for database objects (excluding blobs).
        object_caching_priority,
        /// Returns the current parameter value.
        get_object_caching_priority,
        u32,
        i32
    );

    // ddl_dict - unused, do not expose

    db_param_scalar!(
        /// Sets the amount of space reserved for the dictionary in the
        /// database header to allow dynamic schema modification.
        ddl_dict_size,
        /// Returns the current parameter value.
        get_ddl_dict_size,
        usize,
        exdb_sys::mco_size_t
    );

    // ddl_dict_flags - can be adjusted when creating the database; do not expose

    /// Enables database encryption.
    pub fn cipher_key(&mut self, cipher_key: Option<&str>) -> Result<()> {
        Params::replace_c_string(&mut self.p.cipher_key, cipher_key)
    }

    /// Returns the current parameter value.
    pub fn get_cipher_key(&self) -> Result<Option<&str>> {
        self.get_c_string(self.p.cipher_key)
    }

    /// Enables the dynamic hash table extension.
    pub fn dynamic_hash(&mut self, dynamic_hash: bool) -> &mut Self {
        self.p.dynamic_hash = dynamic_hash as i32;
        self
    }

    /// Returns the current parameter value.
    pub fn get_dynamic_hash(&self) -> bool {
        match self.p.dynamic_hash {
            0 => false,
            _ => true,
        }
    }

    /// Sets the *e*X*treme*DB license key.
    pub fn license_key(&mut self, license_key: Option<&str>) -> Result<()> {
        Params::replace_c_string(&mut self.p.license_key, license_key)
    }

    /// Returns the current parameter value.
    pub fn get_license_key(&self) -> Result<Option<&str>> {
        self.get_c_string(self.p.license_key)
    }

    db_param_scalar!(
        /// Sets the amount of space reserved for classes in the database
        /// header to allow dynamic schema modification.
        max_classes,
        /// Returns the current parameter value.
        get_max_classes,
        i32
    );

    db_param_scalar!(
        /// Sets the amount of space reserved for indexes in the database
        /// header to allow dynamic schema modification.
        max_indexes,
        /// Returns the current parameter value.
        get_max_indexes,
        i32
    );

    db_param_scalar!(
        /// Sets the object size threshold (in bytes) that triggers automatic
        /// de-fragmentation during a transaction commit.
        autocompact_threshold,
        /// Returns the current parameter value.
        get_autocompact_threshold,
        usize,
        exdb_sys::mco_size_t
    );

    /// Sets the transaction scheduling policy for transactions with the same
    /// priority.
    pub fn trans_sched_policy(&mut self, trans_sched_policy: TransSchedPolicy) -> &mut Self {
        self.p.trans_sched_policy = trans_sched_policy as mco_trans_sched_policy::Type;
        self
    }

    /// Returns the current parameter value.
    pub fn get_trans_sched_policy(&self) -> Option<TransSchedPolicy> {
        TransSchedPolicy::from_mco(self.p.trans_sched_policy)
    }

    db_param_scalar!(
        /// Sets the maximum transaction time for debugging; has no effect
        /// unless used with a custom-built *e*X*treme*DB runtime.
        max_trans_time,
        /// Returns the current parameter value.
        get_max_trans_time,
        u64
    );

    db_param_scalar!(
        /// Sets the maximum number of pages in the page hash used internally
        /// by the *e*X*treme*DB runtime when encryption or data compression
        /// is enabled.
        max_active_pages,
        /// Returns the current parameter value.
        get_max_active_pages,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the number of bundles in the page hash used internally by the
        /// *e*X*treme*DB runtime when encryption or data compression
        /// is enabled.
        page_hash_bundles,
        /// Returns the current parameter value.
        get_page_hash_bundles,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the compression level.
        compression_level,
        /// Returns the current parameter value.
        get_compression_level,
        i32
    );

    /// Defines the bitmap of page types to be compressed.
    pub fn compression_mask(&mut self, compression_mask: CompressionMask) -> &mut Self {
        self.p.compression_mask = compression_mask.0.bit_mask() as i32;
        self
    }

    /// Returns the current parameter value.
    pub fn get_compression_mask(&self) -> CompressionMask {
        CompressionMask::from_mco(self.p.compression_mask as u32)
    }

    db_param_scalar!(
        /// Controls the page map allocation.
        expected_compression_ratio,
        /// Returns the current parameter value.
        get_expected_compression_ratio,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the number of keys taken from a leaf of a B-Tree page on each
        /// access.
        btree_cursor_read_ahead_size,
        /// Returns the current parameter value.
        get_btree_cursor_read_ahead_size,
        u8
    );

    db_param_scalar!(
        /// Sets the size of a bitmap used internally to accelerate
        /// the performance of the MVCC transaction manager in some cases.
        mvcc_bitmap_size,
        /// Returns the current parameter value.
        get_mvcc_bitmap_size,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the amount of heap memory used by various internal
        /// DB functions.
        additional_heap_size,
        /// Returns the current parameter value.
        get_additional_heap_size,
        u32,
        i32
    );

    db_param_scalar!(
        /// Sets the size of the copy-on-write MVCC page map.
        cow_pagemap_size,
        /// Returns the current parameter value.
        get_cow_pagemap_size,
        usize,
        exdb_sys::mco_size_t
    );

    db_param_scalar!(
        /// Sets the size of the backup counters array.
        backup_map_size,
        /// Returns the current parameter value.
        get_backup_map_size,
        usize,
        exdb_sys::mco_size_t
    );

    db_param_scalar!(
        /// Sets the number of pages for the last exclusive pass of
        /// the backup procedure.
        backup_min_pages,
        /// Returns the current parameter value.
        get_backup_min_pages,
        u32
    );

    db_param_scalar!(
        /// Sets the max number of passes before the exclusive pass of
        /// the backup procedure.
        backup_max_passes,
        /// Returns the current parameter value.
        get_backup_max_passes,
        u32
    );

    /// Sets the name of the temporary backup data storage file.
    pub fn backup_map_filename(&mut self, backup_map_filename: &str) -> Result<()> {
        if backup_map_filename.len() >= self.p.backup_map_filename.len() {
            return Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM));
        }

        unsafe {
            ptr::copy_nonoverlapping(
                backup_map_filename.as_ptr(),
                self.p.backup_map_filename.as_mut_ptr() as *mut u8,
                backup_map_filename.len(),
            )
        }

        Ok(())
    }

    /// Returns the current parameter value.
    pub fn get_backup_map_filename(&self) -> Result<&str> {
        if self.p.backup_map_filename[0] == 0 {
            Ok("")
        } else {
            let cstr = unsafe { CStr::from_ptr(self.p.backup_map_filename.as_ptr()) };
            cstr.to_str()
                .or(Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM)))
        }
    }

    db_param_scalar!(
        /// Sets the IoT Agent identifier.
        iot_agent_id,
        /// Returns the current parameter value.
        get_iot_agent_id,
        u64
    );

    db_param_scalar!(
        /// Overrides the IoT router level defined in the schema.
        iot_level,
        /// Returns the current parameter value.
        get_iot_level,
        u16
    );

    db_param_scalar!(
        /// Sets the delay in milliseconds between writing backup blocks.
        file_backup_delay,
        /// Returns the current parameter value.
        get_file_backup_delay,
        u32
    );
}

impl Drop for Params {
    fn drop(&mut self) {
        Params::drop_c_string_if_not_null(self.p.cipher_key);
        Params::drop_c_string_if_not_null(self.p.license_key);
    }
}

/// A database instance.
///
/// A database instance cannot be used directly to manipulate the database
/// data; a [`Connection`] must be established and used for operations on the
/// database instead.
///
/// [`Connection`]: ../connection/index.html
pub struct Database<'a> {
    runtime: PhantomData<&'a Runtime>,
    devices: PhantomData<&'a mut Vec<Device>>, // Devices are mutated within eXtremeDB code
    name: CString,
}

impl<'a> Database<'a> {
    /// Opens a new database instance.
    ///
    /// `name` must be an ASCII string; strings containing other characters
    /// will be rejected.
    ///
    /// `dict` is not currently used and must be set to `None`.
    pub fn open(
        _runtime: &'a Runtime,
        name: &str,
        dict: Option<&'a dict::Dictionary>,
        devs: &'a mut Vec<Device>,
        params: Params,
    ) -> Result<Self> {
        if !name.is_ascii() {
            return Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM));
        }

        let cname = CString::new(name).unwrap();
        let mut params = params;
        let dict_p = match dict {
            Some(d) => &d.nested as *const exdb_sys::mco_dictionary_t,
            None => ptr::null_mut(),
        };

        result_from_code(unsafe {
            exdb_sys::mco_db_open_dev(
                cname.as_ptr(),
                dict_p as *mut exdb_sys::mco_dictionary_t,
                devs.as_mut_ptr() as *mut exdb_sys::mco_device_t,
                devs.len() as exdb_sys::mco_size_t,
                &mut params.p,
            )
        })?;

        Ok(Database {
            runtime: PhantomData,
            devices: PhantomData,
            name: cname,
        })
    }

    /// Removes a shared memory segment associated with a database.
    ///
    /// Also removes `name` from the registry.
    ///
    /// This function is a no-op if the specified database does not exist.
    ///
    /// # Safety
    ///
    /// If there are active database sessions using this database segment,
    /// the behavior is undefined.
    pub unsafe fn kill(name: &str) -> Result<()> {
        if !name.is_ascii() {
            Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM))
        } else {
            let cname = CString::new(name).unwrap();
            result_from_code(exdb_sys::mco_db_kill(cname.as_ptr()))
        }
    }

    /// Returns the database name.
    pub fn name(&self) -> &CStr {
        &self.name
    }
}

impl<'a> Drop for Database<'a> {
    fn drop(&mut self) {
        let rc = unsafe { exdb_sys::mco_db_close(self.name.as_ptr()) };
        debug_assert_eq!(mco_ret::MCO_S_OK, rc);
    }
}