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
// device.rs
//
// This file is a part of the eXtremeDB source code
// Copyright (c) 2020 McObject LLC
// All Rights Reserved

//! Logical database devices.
//!
//! *e*X*treme*DB uses *logical devices* to describe the database storage.
//!
//! In-memory databases only need one memory device. Both conventional and
//! shared memory devices are supported.
//!
//! Persistent databases require multiple devices for operation:
//! in-memory devices for the in-memory part of the database and the disk page
//! pool, as well as file devices for the database data and log.
//!
//! For more information on the supported devices and their configuration,
//! refer to the *e*X*treme*DB reference pages.
//!
//! # Examples
//!
//! Creating a new in-memory device (the memory region is allocated
//! internally, and released when the device is dropped):
//!
//! ```
//! # use extremedb::{device, Result};
//! #
//! # fn main() -> Result<()> {
//!     let dev = device::Device::new_mem_conv(device::Assignment::Database, 1024 * 1024)?;
//! #
//! #     drop(dev);
//! #
//! #     Ok(())
//! # }
//! ```
//!
//! Creating a new file device:
//!
//! ```
//! # use extremedb::{device, Result};
//! # use std::fs;
//! #
//! # fn main() -> Result<()> {
//!     let db_file = "db.dbs";
//!     let dev = device::Device::new_file(
//!         device::Assignment::Persistent,
//!         device::FileOpenFlags::new(),
//!         db_file,
//!     )?;
//! #
//! #     drop(dev);
//! #
//! #     let _ = fs::remove_file(db_file);
//! #
//! #     Ok(())
//! # }
//! ```

use std::alloc::{self, Layout};
use std::ffi::{c_void, CStr};
use std::mem;
use std::ptr;

use crate::runtime;
use crate::{exdb_sys, mco_ret, Error, Result};

#[doc(hidden)]
pub mod util;

type McoDeviceTypeUnion = exdb_sys::mco_device_t_dev;
type McoDeviceTypeConv = exdb_sys::mco_device_t_dev_conv;
type McoDeviceTypeNamed = exdb_sys::mco_device_t_dev_named;
type McoDeviceTypeFile = exdb_sys::mco_device_t_dev_file;
type McoDeviceTypeMultiFile = exdb_sys::mco_device_t_dev_multifile;
type McoDeviceTypeRaid = exdb_sys::mco_device_t_dev_raid;

mod mco_dev_type {
    pub const MCO_MEMORY_CONV: u32 = 1;
    pub const MCO_MEMORY_NAMED: u32 = 2;
    pub const MCO_MEMORY_FILE: u32 = 3;
    pub const MCO_MEMORY_MULTIFILE: u32 = 4;
    pub const MCO_MEMORY_RAID: u32 = 5;
}

/// Device assignment.
///
/// The applications use this enumeration to describe the intended purpose
/// of the device.
#[derive(Copy, Clone, Debug)]
pub enum Assignment {
    /// Metadata and database objects, indexes, and other database structures.
    Database,
    /// Disk manager cache (page pool).
    Cache,
    /// A persistent storage device that contains the data.
    Persistent,
    /// A persistent storage device that contains the database log.
    Log,
}

impl Assignment {
    fn to_mco(&self) -> u32 {
        match self {
            Assignment::Database => 0,   // MCO_MEMORY_ASSIGN_DATABASE
            Assignment::Cache => 1,      // MCO_MEMORY_ASSIGN_CACHE
            Assignment::Persistent => 2, // MCO_MEMORY_ASSIGN_PERSISTENT
            Assignment::Log => 3,        // MCO_MEMORY_ASSIGN_LOG
        }
    }
}

macro_rules! flags_builder_method {
    ($(#[$outer:meta])* $method:ident, $ns:path, $flag:expr) => {
        $(#[$outer])*
        pub fn $method(&mut self) -> &mut Self {
            use $ns::*;
            self.0 |= $flag;
            self
        }
    }
}

macro_rules! named_mem_flags_builder_method {
    ($(#[$outer:meta])* $method:ident, $flag:expr) => {
        flags_builder_method!(
            $(#[$outer])*
            $method,
            runtime::options::mco_rt_defines::values::shm_posix,
            $flag
        );
    };
}

/// Shared memory device flags.
///
/// Only used on Posix systems.
pub struct NamedMemFlags(u32);

impl NamedMemFlags {
    /// Creates an empty flags structure. All flags are reset.
    pub fn new() -> Self {
        NamedMemFlags(0)
    }

    named_mem_flags_builder_method!(
        /// Enables anonymous memory mapping.
        anonymous,
        MCO_RT_POSIX_SHM_ANONYMOUS
    );

    named_mem_flags_builder_method!(
        /// Enables shared memory mapping (`MAP_SHARED`).
        shared,
        MCO_RT_POSIX_SHM_SHARED
    );

    named_mem_flags_builder_method!(
        /// Sets the `MAP_HUGETLB` `mmap()` flag (where available).
        huge_tlb,
        MCO_RT_POSIX_SHM_HUGETLB
    );
}

macro_rules! file_open_flags_builder_method {
    ($(#[$outer:meta])* $method:ident, $flag:expr) => {
        flags_builder_method!(
            $(#[$outer])*
            $method,
            exdb_sys::mco_file_open_flags,
            $flag
        );
    };
}

/// Flags for persistent devices.
///
/// These flags allow for fine tuning of file system operations.
/// The default value produced by the [`new()`] method will be suitable
/// for most applications.
///
/// [`new()`]: #method.new
pub struct FileOpenFlags(u32);

impl FileOpenFlags {
    /// The standard, unified, and fail-safe way to open a file in a manner
    /// supported by the target platform file system.
    pub fn new() -> Self {
        FileOpenFlags(exdb_sys::mco_file_open_flags::MCO_FILE_OPEN_DEFAULT as u32)
    }

    file_open_flags_builder_method!(
        /// Opens file in read-only mode.
        read_only,
        MCO_FILE_OPEN_READ_ONLY as u32
    );
    file_open_flags_builder_method!(
        /// Truncates the file after opening.
        truncate,
        MCO_FILE_OPEN_TRUNCATE as u32
    );
    file_open_flags_builder_method!(
        /// Instructs the underlying filesystem to disable caching.
        no_buffering,
        MCO_FILE_OPEN_NO_BUFFERING as u32
    );
    file_open_flags_builder_method!(
        /// Requires the file to exist; a new file is not created.
        existing,
        MCO_FILE_OPEN_EXISTING as u32
    );
    file_open_flags_builder_method!(
        /// Instructs the underlying filesystem to open the file as temporary.
        temporary,
        MCO_FILE_OPEN_TEMPORARY as u32
    );
    file_open_flags_builder_method!(
        /// Enables the flush-operation fix for the *u98* filesystem
        /// wrappers.
        fsync_fix,
        MCO_FILE_OPEN_FSYNC_FIX as u32
    );
    file_open_flags_builder_method!(
        /// Requires the filesystem wrapper to take the RAID device's offset
        /// value into account for all segments of the RAID.
        subpartition,
        MCO_FILE_OPEN_SUBPARTITION as u32
    );
    file_open_flags_builder_method!(
        /// Execute a barrier for flush operations (AIO filesystem wrapper
        /// only).
        fsync_aio_barrier,
        MCO_FILE_OPEN_FSYNC_AIO_BARRIER as u32
    );
    file_open_flags_builder_method!(
        /// Enables file compression (UNIX systems using the
        /// *u98zip*/*u98ziplog* wrappers only).
        compressed,
        MCO_FILE_OPEN_COMPRESSED as u32
    );
    file_open_flags_builder_method!(
        /// Instructs the filesystem wrapper to use `flock()` to setup locking
        /// (*u98* wrapper only).
        lock,
        MCO_FILE_OPEN_LOCK as u32
    );
    file_open_flags_builder_method!(
        /// Use `posix_fadvise()` to control the usage of the pre-read hints
        /// (*u98* wrapper only; use only if it is advised by McObject
        /// support).
        no_read_buffering,
        MCO_FILE_OPEN_NO_READ_BUFFERING as u32
    );
    file_open_flags_builder_method!(
        /// Use `posix_fadvise()` to control the usage of the pre-write hints
        /// (*u98* wrapper only; use only if it is advised by McObject
        /// support).
        no_write_buffering,
        MCO_FILE_OPEN_NO_WRITE_BUFFERING as u32
    );
}

/// A logical device.
#[repr(transparent)]
pub struct Device(exdb_sys::mco_device_t);

impl Device {
    /// Creates a new in-memory device.
    ///
    /// This method allocates `s` bytes of memory using the `std::alloc`
    /// allocator. The memory is freed when the device is dropped.
    pub fn new_mem_conv(a: Assignment, s: usize) -> Result<Self> {
        let l = Layout::from_size_align(s, 1).unwrap();
        let p = unsafe { alloc::alloc(l) };

        if p.is_null() {
            Err(Error::new_core(mco_ret::MCO_E_NOMEM))
        } else {
            Ok(Device(exdb_sys::mco_device_t {
                type_: mco_dev_type::MCO_MEMORY_CONV,
                assignment: a.to_mco(),
                size: s as exdb_sys::mco_size_t,
                dev: McoDeviceTypeUnion {
                    conv: McoDeviceTypeConv {
                        ptr: p as *mut c_void,
                        flags: 0,
                    },
                },
            }))
        }
    }

    /// Creates a new named (shared) memory device.
    ///
    /// The `hint` argument is the address of the shared memory block
    /// allocated for the database. Setting it to zero causes *e*X*treme*DB
    /// to determine the actual shared memory segment address. This could fail
    /// when called from a second process attempting to open the shared
    /// database. In this case it is the application's responsibility
    /// to provide a valid hint address.
    pub fn new_mem_named(
        a: Assignment,
        s: usize,
        name: &str,
        flags: NamedMemFlags,
        hint: usize,
    ) -> Result<Self> {
        let mut named = unsafe { mem::zeroed::<McoDeviceTypeNamed>() };

        if name.len() >= named.name.len() {
            return Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM));
        }

        unsafe {
            ptr::copy_nonoverlapping(
                name.as_ptr(),
                named.name.as_mut_ptr() as *mut u8,
                name.len(),
            )
        }

        named.flags = flags.0;
        named.hint = hint as *mut c_void;

        Ok(Device(exdb_sys::mco_device_t {
            type_: mco_dev_type::MCO_MEMORY_NAMED,
            assignment: a.to_mco(),
            size: s as exdb_sys::mco_size_t,
            dev: McoDeviceTypeUnion { named },
        }))
    }

    /// Creates a new file device.
    pub fn new_file(a: Assignment, flags: FileOpenFlags, name: &str) -> Result<Self> {
        let mut file = unsafe { mem::zeroed::<McoDeviceTypeFile>() };

        if name.len() >= file.name.len() {
            return Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM));
        }

        unsafe {
            ptr::copy_nonoverlapping(name.as_ptr(), file.name.as_mut_ptr() as *mut u8, name.len())
        }

        file.flags = flags.0 as i32;

        Ok(Device(exdb_sys::mco_device_t {
            type_: mco_dev_type::MCO_MEMORY_FILE,
            assignment: a.to_mco(),
            size: 0,
            dev: McoDeviceTypeUnion { file },
        }))
    }

    /// Creates a new multi-file device.
    pub fn new_multifile(
        a: Assignment,
        flags: FileOpenFlags,
        name: &str,
        segment_size: isize,
    ) -> Result<Self> {
        let mut multifile = unsafe { mem::zeroed::<McoDeviceTypeMultiFile>() };

        if name.len() >= multifile.name.len() {
            return Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM));
        }

        unsafe {
            ptr::copy_nonoverlapping(
                name.as_ptr(),
                multifile.name.as_mut_ptr() as *mut u8,
                name.len(),
            )
        }

        multifile.flags = flags.0 as i32;
        multifile.segment_size = segment_size as exdb_sys::mco_offs_t;

        Ok(Device(exdb_sys::mco_device_t {
            type_: mco_dev_type::MCO_MEMORY_MULTIFILE,
            assignment: a.to_mco(),
            size: 0,
            dev: McoDeviceTypeUnion { multifile },
        }))
    }

    /// Creates a new RAID device.
    pub fn new_raid(
        a: Assignment,
        flags: FileOpenFlags,
        name: &str,
        level: i32,
        offset: isize,
    ) -> Result<Self> {
        let mut raid = unsafe { mem::zeroed::<McoDeviceTypeRaid>() };

        if name.len() >= raid.name.len() {
            return Err(Error::new_core(mco_ret::MCO_E_ILLEGAL_PARAM));
        }

        unsafe {
            ptr::copy_nonoverlapping(name.as_ptr(), raid.name.as_mut_ptr() as *mut u8, name.len())
        }

        raid.flags = flags.0 as i32;
        raid.level = level;
        raid.offset = offset as exdb_sys::mco_offs_t;

        Ok(Device(exdb_sys::mco_device_t {
            type_: mco_dev_type::MCO_MEMORY_RAID,
            assignment: a.to_mco(),
            size: 0,
            dev: McoDeviceTypeUnion { raid },
        }))
    }

    fn file_name(&self) -> Option<&str> {
        match self.0.type_ {
            mco_dev_type::MCO_MEMORY_FILE => {
                let cname = unsafe { CStr::from_ptr(self.0.dev.file.name.as_ptr()) };
                cname.to_str().ok()
            }
            _ => None,
        }
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        if self.0.type_ == mco_dev_type::MCO_MEMORY_CONV {
            let l = Layout::from_size_align(self.0.size as usize, 1).unwrap();
            unsafe { alloc::dealloc(self.0.dev.conv.ptr as *mut u8, l) };
        }
    }
}