From 3739aa54fdd143fcfb5600d06898d447dc247c70 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Sun, 4 Jul 2021 21:05:28 +0530 Subject: [PATCH] Add basic `IArray` impl --- server/src/coredb/iarray.rs | 109 ++++++++++++++++++++++++++++++++++++ server/src/coredb/mod.rs | 1 + 2 files changed, 110 insertions(+) create mode 100644 server/src/coredb/iarray.rs diff --git a/server/src/coredb/iarray.rs b/server/src/coredb/iarray.rs new file mode 100644 index 00000000..acc63f75 --- /dev/null +++ b/server/src/coredb/iarray.rs @@ -0,0 +1,109 @@ +/* + * Created on Sun Jul 04 2021 + * + * 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) 2021, Sayan Nandan + * + * 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 . + * +*/ + +#![allow(dead_code)] // TODO(@ohsayan): Remove this once we're done + +use std::mem; +use std::mem::ManuallyDrop; +use std::mem::MaybeUninit; +use std::ptr; + +pub trait MemoryBlock { + type LayoutItem; + fn size() -> usize; +} + +impl MemoryBlock for [T; N] { + type LayoutItem = T; + fn size() -> usize { + N + } +} + +pub union InlineArray { + inline_store: ManuallyDrop>, + heap_ptr_len: (*mut A::LayoutItem, usize), +} + +impl InlineArray { + unsafe fn inline_ptr(&self) -> *const A::LayoutItem { + self.inline_store.as_ptr() as *const _ + } + unsafe fn inline_ptr_mut(&mut self) -> *mut A::LayoutItem { + self.inline_store.as_mut_ptr() as *mut _ + } + fn from_inline(inline_store: MaybeUninit) -> Self { + Self { + inline_store: ManuallyDrop::new(inline_store), + } + } + fn from_heap_ptr(start_ptr: *mut A::LayoutItem, len: usize) -> Self { + Self { + heap_ptr_len: (start_ptr, len), + } + } +} + +pub struct IArray { + cap: usize, + store: InlineArray, +} + +impl IArray { + pub fn new() -> IArray { + Self { + cap: 0, + store: InlineArray::from_inline(MaybeUninit::uninit()), + } + } + pub fn from_vec(mut vec: Vec) -> Self { + if vec.capacity() <= Self::inline_capacity() { + let mut store = InlineArray::::from_inline(MaybeUninit::uninit()); + let len = vec.len(); + unsafe { + ptr::copy_nonoverlapping(vec.as_ptr(), store.inline_ptr_mut(), len); + } + // done with the copy + Self { cap: len, store } + } else { + let (start_ptr, cap, len) = (vec.as_mut_ptr(), vec.capacity(), vec.len()); + // leak the vec + mem::forget(vec); + IArray { + cap, + store: InlineArray::from_heap_ptr(start_ptr, len), + } + } + } + fn inline_capacity() -> usize { + if mem::size_of::() > 0 { + // not a ZST, so cap of array + A::size() + } else { + usize::MAX + } + } +} diff --git a/server/src/coredb/mod.rs b/server/src/coredb/mod.rs index 0819f957..0ff8394d 100644 --- a/server/src/coredb/mod.rs +++ b/server/src/coredb/mod.rs @@ -39,6 +39,7 @@ pub use htable::Data; use libsky::TResult; use std::sync::Arc; pub mod htable; +pub mod iarray; pub mod lazy; pub mod lock; mod memstore;