Refactor SDSSv1 primitives

next
Sayan Nandan 8 months ago
parent f769175083
commit 843adbaf7e
No known key found for this signature in database
GPG Key ID: 0EBD769024B24F0A

@ -218,7 +218,7 @@ impl Drop for PrimaryIndexKey {
unsafe {
// UNSAFE(@ohsayan): Aliasing, sole owner and correct initialization
let vdata = self.virtual_block_mut();
mem::dealloc_array(vdata.as_mut_ptr(), vdata.len());
mem::unsafe_apis::dealloc_array(vdata.as_mut_ptr(), vdata.len());
}
}
}

@ -465,7 +465,7 @@ impl Drop for Datacell {
TagClass::Str | TagClass::Bin => unsafe {
// UNSAFE(@ohsayan): we have checked that the cell is initialized (uninit will not satisfy this class), and we have checked its class
let (l, p) = self.load_word();
engine::mem::dealloc_array::<u8>(p, l)
engine::mem::unsafe_apis::dealloc_array::<u8>(p, l)
},
TagClass::List => unsafe {
// UNSAFE(@ohsayan): we have checked that the cell is initialized (uninit will not satisfy this class), and we have checked its class

@ -31,6 +31,7 @@ mod rawslice;
pub mod scanner;
mod stackop;
mod uarray;
pub mod unsafe_apis;
mod vinline;
mod word;
// test
@ -47,20 +48,6 @@ pub use {
vinline::VInline,
word::{DwordNN, DwordQN, WordIO, ZERO_BLOCK},
};
// imports
use std::alloc::{self, Layout};
pub unsafe fn dealloc_array<T>(ptr: *mut T, l: usize) {
if l != 0 {
alloc::dealloc(ptr as *mut u8, Layout::array::<T>(l).unwrap_unchecked())
}
}
pub unsafe fn memcpy<const N: usize>(src: &[u8]) -> [u8; N] {
let mut dst = [0u8; N];
src.as_ptr().copy_to_nonoverlapping(dst.as_mut_ptr(), N);
dst
}
/// Native double pointer width (note, native != arch native, but host native)
pub struct NativeDword([usize; 2]);

@ -0,0 +1,53 @@
/*
* Created on Thu Jan 18 2024
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2024, Sayan Nandan <nandansayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
use std::alloc::{self, Layout};
pub unsafe fn alloc_layout<T>(layout: Layout) -> *mut T {
let ptr = alloc::alloc(layout);
assert!(!ptr.is_null(), "malloc failed");
ptr as _
}
pub unsafe fn alloc_array<T>(l: usize) -> *mut T {
self::alloc_layout(Layout::array::<T>(l).unwrap_unchecked())
}
pub unsafe fn dealloc_layout(ptr: *mut u8, layout: Layout) {
alloc::dealloc(ptr, layout)
}
pub unsafe fn dealloc_array<T>(ptr: *mut T, l: usize) {
if l != 0 {
self::dealloc_layout(ptr as *mut u8, Layout::array::<T>(l).unwrap_unchecked())
}
}
pub unsafe fn memcpy<const N: usize>(src: &[u8]) -> [u8; N] {
let mut dst = [0u8; N];
src.as_ptr().copy_to_nonoverlapping(dst.as_mut_ptr(), N);
dst
}

@ -0,0 +1,39 @@
/*
* Created on Wed Jan 10 2024
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2024, Sayan Nandan <nandansayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/*!
# SDSS spec
This module provides traits and types to deal with the SDSS spec, especially headers.
The static SDSS header block has a special segment that defines the header version which is static and will
never change across any versions. While the same isn't warranted for the rest of the header, it's exceedingly
unlikely that we'll ever change the static block.
The only header that we currently use is [`v1::HeaderV1`].
*/
pub mod v1;

@ -1,5 +1,5 @@
/*
* Created on Wed Jan 10 2024
* Created on Thu Jan 18 2024
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
@ -24,26 +24,20 @@
*
*/
/*!
# SDSS spec
This module provides traits and types to deal with the SDSS spec, especially headers.
The static SDSS header block has a special segment that defines the header version which is static and will
never change across any versions. While the same isn't warranted for the rest of the header, it's exceedingly
unlikely that we'll ever change the static block.
The only header that we currently use is [`HeaderV1`].
*/
//! # SDSS Spec v1
//!
//! This is the first spec of SDSS. It is highly unlikely that this will ever change. Different types
//! and functions are defined here to deal with SDSSv1 files.
//!
use {
super::super::{
super::super::super::{
interface::fs_traits::{FileInterfaceRead, FileInterfaceWrite},
static_meta::{HostArch, HostEndian, HostOS, HostPointerWidth, SDSS_MAGIC_8B},
versions::{self, DriverVersion, FileSpecifierVersion, HeaderVersion, ServerVersion},
},
crate::{
engine::{error::StorageError, mem::memcpy, RuntimeResult},
engine::{error::StorageError, mem::unsafe_apis::memcpy, RuntimeResult},
util::os,
},
std::{

@ -24,5 +24,5 @@
*
*/
mod spec;
pub use spec::{FileSpecV1, HeaderV1, HeaderV1Enumeration, HeaderV1Spec, SimpleFileSpecV1};
mod impls;
pub use impls::v1;

@ -61,7 +61,7 @@ const CRC: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);
pub fn open_or_create_journal<
TA: JournalAdapter,
Fs: FSInterface,
F: sdss::FileSpecV1<DecodeArgs = (), EncodeArgs = ()>,
F: sdss::v1::FileSpecV1<DecodeArgs = (), EncodeArgs = ()>,
>(
log_file_name: &str,
gs: &TA::GlobalState,
@ -76,13 +76,21 @@ pub fn open_or_create_journal<
)?))
}
pub fn create_journal<TA: JournalAdapter, Fs: FSInterface, F: sdss::FileSpecV1<EncodeArgs = ()>>(
pub fn create_journal<
TA: JournalAdapter,
Fs: FSInterface,
F: sdss::v1::FileSpecV1<EncodeArgs = ()>,
>(
log_file_name: &str,
) -> RuntimeResult<JournalWriter<Fs, TA>> {
JournalWriter::new(SDSSFileIO::create::<F>(log_file_name)?, 0, true)
}
pub fn load_journal<TA: JournalAdapter, Fs: FSInterface, F: sdss::FileSpecV1<DecodeArgs = ()>>(
pub fn load_journal<
TA: JournalAdapter,
Fs: FSInterface,
F: sdss::v1::FileSpecV1<DecodeArgs = ()>,
>(
log_file_name: &str,
gs: &TA::GlobalState,
) -> RuntimeResult<JournalWriter<Fs, TA>> {

@ -156,19 +156,19 @@ pub struct SDSSFileIO<Fs: FSInterface, F = <Fs as FSInterface>::File> {
}
impl<Fs: FSInterface> SDSSFileIO<Fs> {
pub fn open<F: sdss::FileSpecV1<DecodeArgs = ()>>(
pub fn open<F: sdss::v1::FileSpecV1<DecodeArgs = ()>>(
fpath: &str,
) -> RuntimeResult<(Self, F::Metadata)> {
let mut f = Self::_new(Fs::fs_fopen_rw(fpath)?);
let v = F::read_metadata(&mut f.f, ())?;
Ok((f, v))
}
pub fn create<F: sdss::FileSpecV1<EncodeArgs = ()>>(fpath: &str) -> RuntimeResult<Self> {
pub fn create<F: sdss::v1::FileSpecV1<EncodeArgs = ()>>(fpath: &str) -> RuntimeResult<Self> {
let mut f = Self::_new(Fs::fs_fcreate_rw(fpath)?);
F::write_metadata(&mut f.f, ())?;
Ok(f)
}
pub fn open_or_create_perm_rw<F: sdss::FileSpecV1<DecodeArgs = (), EncodeArgs = ()>>(
pub fn open_or_create_perm_rw<F: sdss::v1::FileSpecV1<DecodeArgs = (), EncodeArgs = ()>>(
fpath: &str,
) -> RuntimeResult<FileOpen<Self, (Self, F::Metadata)>> {
match Fs::fs_fopen_or_create_rw(fpath)? {

@ -29,17 +29,17 @@ use crate::engine::storage::common::{
versions::{self, DriverVersion, FileSpecifierVersion, ServerVersion},
};
pub(super) type Header = sdss::HeaderV1<HeaderImplV1>;
pub(super) type Header = sdss::v1::HeaderV1<HeaderImplV1>;
#[derive(Debug)]
pub(super) struct HeaderImplV1;
impl sdss::HeaderV1Spec for HeaderImplV1 {
impl sdss::v1::HeaderV1Spec for HeaderImplV1 {
type FileClass = FileScope;
type FileSpecifier = FileSpecifier;
const CURRENT_SERVER_VERSION: ServerVersion = versions::v1::V1_SERVER_VERSION;
const CURRENT_DRIVER_VERSION: DriverVersion = versions::v1::V1_DRIVER_VERSION;
}
impl sdss::HeaderV1Enumeration for FileScope {
impl sdss::v1::HeaderV1Enumeration for FileScope {
const MAX: u8 = FileScope::MAX;
unsafe fn new(x: u8) -> Self {
core::mem::transmute(x)
@ -48,7 +48,7 @@ impl sdss::HeaderV1Enumeration for FileScope {
FileScope::value_u8(self)
}
}
impl sdss::HeaderV1Enumeration for FileSpecifier {
impl sdss::v1::HeaderV1Enumeration for FileSpecifier {
const MAX: u8 = FileSpecifier::MAX;
unsafe fn new(x: u8) -> Self {
core::mem::transmute(x)
@ -84,7 +84,7 @@ pub enum FileSpecifier {
#[cfg(test)]
pub(super) struct TestFile;
#[cfg(test)]
impl sdss::SimpleFileSpecV1 for TestFile {
impl sdss::v1::SimpleFileSpecV1 for TestFile {
type HeaderSpec = HeaderImplV1;
const FILE_CLASS: FileScope = FileScope::FlatmapData;
const FILE_SPECIFIER: FileSpecifier = FileSpecifier::TestTransactionLog;
@ -93,7 +93,7 @@ impl sdss::SimpleFileSpecV1 for TestFile {
/// The file specification for the GNS transaction log (impl v1)
pub(super) struct GNSTransactionLogV1;
impl sdss::SimpleFileSpecV1 for GNSTransactionLogV1 {
impl sdss::v1::SimpleFileSpecV1 for GNSTransactionLogV1 {
type HeaderSpec = HeaderImplV1;
const FILE_CLASS: FileScope = FileScope::Journal;
const FILE_SPECIFIER: FileSpecifier = FileSpecifier::GNSTxnLog;
@ -102,7 +102,7 @@ impl sdss::SimpleFileSpecV1 for GNSTransactionLogV1 {
/// The file specification for a journal batch
pub(super) struct DataBatchJournalV1;
impl sdss::SimpleFileSpecV1 for DataBatchJournalV1 {
impl sdss::v1::SimpleFileSpecV1 for DataBatchJournalV1 {
type HeaderSpec = HeaderImplV1;
const FILE_CLASS: FileScope = FileScope::DataBatch;
const FILE_SPECIFIER: FileSpecifier = FileSpecifier::TableDataBatch;
@ -111,7 +111,7 @@ impl sdss::SimpleFileSpecV1 for DataBatchJournalV1 {
/// The file specification for the system db
pub(super) struct SysDBV1;
impl sdss::SimpleFileSpecV1 for SysDBV1 {
impl sdss::v1::SimpleFileSpecV1 for SysDBV1 {
type HeaderSpec = HeaderImplV1;
const FILE_CLASS: FileScope = FileScope::FlatmapData;
const FILE_SPECIFIER: FileSpecifier = FileSpecifier::SysDB;

@ -181,8 +181,7 @@ impl<Fs: FSInterface> SystemStore<Fs> {
Ok((slf, state))
}
fn _restore(mut f: SDSSFileIO<Fs>, run_mode: ConfigMode) -> RuntimeResult<SysConfig> {
let mut sysdb_data =
inf::dec::dec_dict_full::<inf::map::GenericDictSpec>(&f.read_full()?)?;
let mut sysdb_data = inf::dec::dec_dict_full::<inf::map::GenericDictSpec>(&f.read_full()?)?;
// get our auth and sys stores
let mut auth_store = rkey(
&mut sysdb_data,

@ -26,7 +26,7 @@
use {
crate::engine::storage::common::{
sdss::{self, HeaderV1Spec},
sdss,
versions::{self, DriverVersion, FileSpecifierVersion, ServerVersion},
},
std::mem::transmute,
@ -47,7 +47,7 @@ pub enum FileSpecifier {
ModelData = 1,
}
impl sdss::HeaderV1Enumeration for FileClass {
impl sdss::v1::HeaderV1Enumeration for FileClass {
const MAX: u8 = FileClass::MAX;
unsafe fn new(x: u8) -> Self {
transmute(x)
@ -57,7 +57,7 @@ impl sdss::HeaderV1Enumeration for FileClass {
}
}
impl sdss::HeaderV1Enumeration for FileSpecifier {
impl sdss::v1::HeaderV1Enumeration for FileSpecifier {
const MAX: u8 = FileSpecifier::MAX;
unsafe fn new(x: u8) -> Self {
transmute(x)
@ -68,7 +68,7 @@ impl sdss::HeaderV1Enumeration for FileSpecifier {
}
pub struct HeaderImplV2;
impl HeaderV1Spec for HeaderImplV2 {
impl sdss::v1::HeaderV1Spec for HeaderImplV2 {
type FileClass = FileClass;
type FileSpecifier = FileSpecifier;
const CURRENT_SERVER_VERSION: ServerVersion = versions::v2::V2_SERVER_VERSION;
@ -76,7 +76,7 @@ impl HeaderV1Spec for HeaderImplV2 {
}
pub struct SystemDatabaseV1;
impl sdss::SimpleFileSpecV1 for SystemDatabaseV1 {
impl sdss::v1::SimpleFileSpecV1 for SystemDatabaseV1 {
type HeaderSpec = HeaderImplV2;
const FILE_CLASS: FileClass = FileClass::EventLog;
const FILE_SPECIFIER: FileSpecifier = FileSpecifier::GlobalNS;
@ -84,7 +84,7 @@ impl sdss::SimpleFileSpecV1 for SystemDatabaseV1 {
}
pub struct ModelDataBatchAofV1;
impl sdss::SimpleFileSpecV1 for ModelDataBatchAofV1 {
impl sdss::v1::SimpleFileSpecV1 for ModelDataBatchAofV1 {
type HeaderSpec = HeaderImplV2;
const FILE_CLASS: FileClass = FileClass::Batch;
const FILE_SPECIFIER: FileSpecifier = FileSpecifier::ModelData;

Loading…
Cancel
Save