Cleanup gns txn impls

next
Sayan Nandan 1 year ago
parent 13a39f1e1f
commit ea20611ebe
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -408,9 +408,7 @@ impl<LF: RawFileIOInterface, TA: JournalAdapter> JournalWriter<LF, TA> {
debug_assert!(TA::RECOVERY_PLUGIN);
match self.append_event(event) {
Ok(()) => Ok(()),
Err(_) => {
return self.appendrec_journal_reverse_entry();
}
Err(_) => compiler::cold_call(|| return self.appendrec_journal_reverse_entry()),
}
}
}

@ -42,10 +42,15 @@ use {
mod model;
mod space;
// test
#[cfg(test)]
mod tests;
// re-exports
pub use {
model::CreateModelTxn,
model::{
AlterModelAddTxn, AlterModelRemoveTxn, AlterModelUpdateTxn, CreateModelTxn, DropModelTxn,
},
space::{AlterSpaceTxn, CreateSpaceTxn, DropSpaceTxn},
};
@ -58,7 +63,7 @@ pub struct GNSTransactionDriver {
impl GNSTransactionDriver {
/// Attempts to commit the given event into the journal, handling any possible recovery triggers and returning
/// errors (if any)
pub fn try_commit<GE: GNSEvent>(&mut self, gns_event: GE::CommitType) -> TransactionResult<()> {
pub fn try_commit<GE: GNSEvent>(&mut self, gns_event: GE) -> TransactionResult<()> {
let mut buf = vec![];
buf.extend(GE::OPC.to_le_bytes());
GE::encode_super_event(gns_event, &mut buf);
@ -104,7 +109,7 @@ pub struct GNSSuperEvent(Box<[u8]>);
/// Definition for an event in the GNS (DDL queries)
pub trait GNSEvent
where
Self: PersistObject<InputType = Self::CommitType, OutputType = Self::RestoreType> + Sized,
Self: PersistObject<InputType = Self, OutputType = Self::RestoreType> + Sized,
{
/// OPC for the event (unique)
const OPC: u16;
@ -113,7 +118,7 @@ where
/// Expected type for a restore
type RestoreType;
/// Encodes the event into the given buffer
fn encode_super_event(commit: Self::CommitType, buf: &mut Vec<u8>) {
fn encode_super_event(commit: Self, buf: &mut Vec<u8>) {
inf::enc::enc_full_into_buffer::<Self>(buf, commit)
}
/// Attempts to decode the event using the given scanner

@ -144,22 +144,24 @@ fn with_model<T>(
create model
*/
/// Transaction for running a `create model ... (...) with {..}` query
pub struct CreateModelTxn<'a>(PhantomData<&'a ()>);
#[derive(Clone, Copy)]
/// The commit payload for a `create model ... (...) with {...}` txn
pub struct CreateModelTxn<'a> {
space_id: super::SpaceIDRef<'a>,
model_name: &'a str,
model: &'a Model,
model_read: &'a IRModel<'a>,
}
impl<'a> CreateModelTxn<'a> {
pub const fn new_commit(
space_name: &'a str,
space_uuid: Uuid,
pub const fn new(
space_id: super::SpaceIDRef<'a>,
model_name: &'a str,
model: &'a Model,
model_read: &'a IRModel<'a>,
) -> CreateModelTxnCommitPL<'a> {
CreateModelTxnCommitPL {
space_id: super::SpaceIDRef {
uuid: space_uuid,
name: space_name,
},
) -> Self {
Self {
space_id,
model_name,
model,
model_read,
@ -167,14 +169,6 @@ impl<'a> CreateModelTxn<'a> {
}
}
#[derive(Clone, Copy)]
pub struct CreateModelTxnCommitPL<'a> {
space_id: super::SpaceIDRef<'a>,
model_name: &'a str,
model: &'a Model,
model_read: &'a IRModel<'a>,
}
pub struct CreateModelTxnRestorePL {
space_id: super::SpaceIDRes,
model_name: Box<str>,
@ -191,7 +185,7 @@ impl<'a> PersistObject for CreateModelTxn<'a> {
const METADATA_SIZE: usize = <super::SpaceID as PersistObject>::METADATA_SIZE
+ sizeof!(u64)
+ <obj::ModelLayoutRef<'a> as PersistObject>::METADATA_SIZE;
type InputType = CreateModelTxnCommitPL<'a>;
type InputType = CreateModelTxn<'a>;
type OutputType = CreateModelTxnRestorePL;
type Metadata = CreateModelTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -244,7 +238,7 @@ impl<'a> PersistObject for CreateModelTxn<'a> {
impl<'a> GNSEvent for CreateModelTxn<'a> {
const OPC: u16 = 3;
type CommitType = CreateModelTxnCommitPL<'a>;
type CommitType = CreateModelTxn<'a>;
type RestoreType = CreateModelTxnRestorePL;
fn update_global_state(
CreateModelTxnRestorePL {
@ -281,12 +275,24 @@ impl<'a> GNSEvent for CreateModelTxn<'a> {
alter model add
*/
pub struct AlterModelAddTxn<'a>(PhantomData<&'a ()>);
#[derive(Debug, Clone, Copy)]
pub struct AlterModelAddTxnCommitPL<'a> {
/// Transaction commit payload for an `alter model add ...` query
pub struct AlterModelAddTxn<'a> {
model_id: ModelIDRef<'a>,
new_fields: &'a IndexSTSeqCns<Box<str>, Field>,
}
impl<'a> AlterModelAddTxn<'a> {
pub const fn new(
model_id: ModelIDRef<'a>,
new_fields: &'a IndexSTSeqCns<Box<str>, Field>,
) -> Self {
Self {
model_id,
new_fields,
}
}
}
pub struct AlterModelAddTxnMD {
model_id_meta: ModelIDMD,
new_field_c: u64,
@ -297,7 +303,7 @@ pub struct AlterModelAddTxnRestorePL {
}
impl<'a> PersistObject for AlterModelAddTxn<'a> {
const METADATA_SIZE: usize = <ModelID as PersistObject>::METADATA_SIZE + sizeof!(u64);
type InputType = AlterModelAddTxnCommitPL<'a>;
type InputType = AlterModelAddTxn<'a>;
type OutputType = AlterModelAddTxnRestorePL;
type Metadata = AlterModelAddTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -336,7 +342,7 @@ impl<'a> PersistObject for AlterModelAddTxn<'a> {
impl<'a> GNSEvent for AlterModelAddTxn<'a> {
const OPC: u16 = 4;
type CommitType = AlterModelAddTxnCommitPL<'a>;
type CommitType = AlterModelAddTxn<'a>;
type RestoreType = AlterModelAddTxnRestorePL;
fn update_global_state(
AlterModelAddTxnRestorePL {
@ -368,12 +374,20 @@ impl<'a> GNSEvent for AlterModelAddTxn<'a> {
alter model remove
*/
pub struct AlterModelRemoveTxn<'a>(PhantomData<&'a ()>);
#[derive(Debug, Clone, Copy)]
pub struct AlterModelRemoveTxnCommitPL<'a> {
/// Transaction commit payload for an `alter model remove` transaction
pub struct AlterModelRemoveTxn<'a> {
model_id: ModelIDRef<'a>,
removed_fields: &'a [Ident<'a>],
}
impl<'a> AlterModelRemoveTxn<'a> {
pub const fn new(model_id: ModelIDRef<'a>, removed_fields: &'a [Ident<'a>]) -> Self {
Self {
model_id,
removed_fields,
}
}
}
pub struct AlterModelRemoveTxnMD {
model_id_meta: ModelIDMD,
remove_field_c: u64,
@ -385,7 +399,7 @@ pub struct AlterModelRemoveTxnRestorePL {
impl<'a> PersistObject for AlterModelRemoveTxn<'a> {
const METADATA_SIZE: usize = <ModelID as PersistObject>::METADATA_SIZE + sizeof!(u64);
type InputType = AlterModelRemoveTxnCommitPL<'a>;
type InputType = AlterModelRemoveTxn<'a>;
type OutputType = AlterModelRemoveTxnRestorePL;
type Metadata = AlterModelRemoveTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -436,7 +450,7 @@ impl<'a> PersistObject for AlterModelRemoveTxn<'a> {
impl<'a> GNSEvent for AlterModelRemoveTxn<'a> {
const OPC: u16 = 5;
type CommitType = AlterModelRemoveTxnCommitPL<'a>;
type CommitType = AlterModelRemoveTxn<'a>;
type RestoreType = AlterModelRemoveTxnRestorePL;
fn update_global_state(
AlterModelRemoveTxnRestorePL {
@ -471,12 +485,24 @@ impl<'a> GNSEvent for AlterModelRemoveTxn<'a> {
alter model update
*/
pub struct AlterModelUpdateTxn<'a>(PhantomData<&'a ()>);
#[derive(Debug, Clone, Copy)]
pub struct AlterModelUpdateTxnCommitPL<'a> {
/// Transaction commit payload for an `alter model update ...` query
pub struct AlterModelUpdateTxn<'a> {
model_id: ModelIDRef<'a>,
updated_fields: &'a IndexST<Box<str>, Field>,
}
impl<'a> AlterModelUpdateTxn<'a> {
pub const fn new(
model_id: ModelIDRef<'a>,
updated_fields: &'a IndexST<Box<str>, Field>,
) -> Self {
Self {
model_id,
updated_fields,
}
}
}
pub struct AlterModelUpdateTxnMD {
model_id_md: ModelIDMD,
updated_field_c: u64,
@ -488,7 +514,7 @@ pub struct AlterModelUpdateTxnRestorePL {
impl<'a> PersistObject for AlterModelUpdateTxn<'a> {
const METADATA_SIZE: usize = <ModelID as PersistObject>::METADATA_SIZE + sizeof!(u64);
type InputType = AlterModelUpdateTxnCommitPL<'a>;
type InputType = AlterModelUpdateTxn<'a>;
type OutputType = AlterModelUpdateTxnRestorePL;
type Metadata = AlterModelUpdateTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -529,7 +555,7 @@ impl<'a> PersistObject for AlterModelUpdateTxn<'a> {
impl<'a> GNSEvent for AlterModelUpdateTxn<'a> {
const OPC: u16 = 6;
type CommitType = AlterModelUpdateTxnCommitPL<'a>;
type CommitType = AlterModelUpdateTxn<'a>;
type RestoreType = AlterModelUpdateTxnRestorePL;
fn update_global_state(
AlterModelUpdateTxnRestorePL {
@ -562,11 +588,17 @@ impl<'a> GNSEvent for AlterModelUpdateTxn<'a> {
drop model
*/
pub struct DropModelTxn<'a>(PhantomData<&'a ()>);
#[derive(Debug, Clone, Copy)]
pub struct DropModelTxnCommitPL<'a> {
/// Transaction commit payload for a `drop model ...` query
pub struct DropModelTxn<'a> {
model_id: ModelIDRef<'a>,
}
impl<'a> DropModelTxn<'a> {
pub const fn new(model_id: ModelIDRef<'a>) -> Self {
Self { model_id }
}
}
pub struct DropModelTxnMD {
model_id_md: ModelIDMD,
}
@ -575,7 +607,7 @@ pub struct DropModelTxnRestorePL {
}
impl<'a> PersistObject for DropModelTxn<'a> {
const METADATA_SIZE: usize = <ModelID as PersistObject>::METADATA_SIZE;
type InputType = DropModelTxnCommitPL<'a>;
type InputType = DropModelTxn<'a>;
type OutputType = DropModelTxnRestorePL;
type Metadata = DropModelTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -601,7 +633,7 @@ impl<'a> PersistObject for DropModelTxn<'a> {
impl<'a> GNSEvent for DropModelTxn<'a> {
const OPC: u16 = 7;
type CommitType = DropModelTxnCommitPL<'a>;
type CommitType = DropModelTxn<'a>;
type RestoreType = DropModelTxnRestorePL;
fn update_global_state(
DropModelTxnRestorePL { model_id }: Self::RestoreType,

@ -29,7 +29,7 @@ use {
crate::{
engine::{
core::{space::Space, GlobalNS},
data::{uuid::Uuid, DictGeneric},
data::DictGeneric,
idx::STIndex,
storage::v1::{
inf::{self, map, obj, PersistObject},
@ -39,23 +39,23 @@ use {
},
util::EndianQW,
},
std::marker::PhantomData,
};
/*
create space
*/
/// A transaction to run a `create space ...` operation
pub struct CreateSpaceTxn<'a>(PhantomData<&'a ()>);
#[derive(Clone, Copy)]
/// Transaction commit payload for a `create space ...` query
pub struct CreateSpaceTxn<'a> {
pub(crate) space_meta: &'a DictGeneric,
pub(crate) space_name: &'a str,
pub(crate) space: &'a Space,
}
impl<'a> CreateSpaceTxn<'a> {
pub const fn new_commit(
space_meta: &'a DictGeneric,
space_name: &'a str,
space: &'a Space,
) -> CreateSpaceTxnCommitPL<'a> {
CreateSpaceTxnCommitPL {
pub const fn new(space_meta: &'a DictGeneric, space_name: &'a str, space: &'a Space) -> Self {
Self {
space_meta,
space_name,
space,
@ -63,13 +63,6 @@ impl<'a> CreateSpaceTxn<'a> {
}
}
#[derive(Clone, Copy)]
pub struct CreateSpaceTxnCommitPL<'a> {
pub(crate) space_meta: &'a DictGeneric,
pub(crate) space_name: &'a str,
pub(crate) space: &'a Space,
}
pub struct CreateSpaceTxnRestorePL {
pub(crate) space_name: Box<str>,
pub(crate) space: Space,
@ -83,7 +76,7 @@ pub struct CreateSpaceTxnMD {
impl<'a> PersistObject for CreateSpaceTxn<'a> {
const METADATA_SIZE: usize =
<obj::SpaceLayoutRef<'static> as PersistObject>::METADATA_SIZE + sizeof!(u64);
type InputType = CreateSpaceTxnCommitPL<'a>;
type InputType = CreateSpaceTxn<'a>;
type OutputType = CreateSpaceTxnRestorePL;
type Metadata = CreateSpaceTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -118,7 +111,7 @@ impl<'a> PersistObject for CreateSpaceTxn<'a> {
impl<'a> GNSEvent for CreateSpaceTxn<'a> {
const OPC: u16 = 0;
type CommitType = CreateSpaceTxnCommitPL<'a>;
type CommitType = CreateSpaceTxn<'a>;
type RestoreType = CreateSpaceTxnRestorePL;
fn update_global_state(
CreateSpaceTxnRestorePL { space_name, space }: CreateSpaceTxnRestorePL,
@ -139,17 +132,17 @@ impl<'a> GNSEvent for CreateSpaceTxn<'a> {
for now dump the entire meta
*/
/// A transaction to run `alter space ...`
pub struct AlterSpaceTxn<'a>(PhantomData<&'a ()>);
#[derive(Clone, Copy)]
/// Transaction payload for an `alter space ...` query
pub struct AlterSpaceTxn<'a> {
space_id: super::SpaceIDRef<'a>,
space_meta: &'a DictGeneric,
}
impl<'a> AlterSpaceTxn<'a> {
pub const fn new_commit(
uuid: Uuid,
name: &'a str,
space_meta: &'a DictGeneric,
) -> AlterSpaceTxnCommitPL<'a> {
AlterSpaceTxnCommitPL {
space_id: super::SpaceIDRef { uuid, name },
pub const fn new(space_id: super::SpaceIDRef<'a>, space_meta: &'a DictGeneric) -> Self {
Self {
space_id,
space_meta,
}
}
@ -160,12 +153,6 @@ pub struct AlterSpaceTxnMD {
dict_len: u64,
}
#[derive(Clone, Copy)]
pub struct AlterSpaceTxnCommitPL<'a> {
space_id: super::SpaceIDRef<'a>,
space_meta: &'a DictGeneric,
}
pub struct AlterSpaceTxnRestorePL {
space_id: super::SpaceIDRes,
space_meta: DictGeneric,
@ -173,7 +160,7 @@ pub struct AlterSpaceTxnRestorePL {
impl<'a> PersistObject for AlterSpaceTxn<'a> {
const METADATA_SIZE: usize = sizeof!(u64, 2) + sizeof!(u128);
type InputType = AlterSpaceTxnCommitPL<'a>;
type InputType = AlterSpaceTxn<'a>;
type OutputType = AlterSpaceTxnRestorePL;
type Metadata = AlterSpaceTxnMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -208,9 +195,7 @@ impl<'a> PersistObject for AlterSpaceTxn<'a> {
impl<'a> GNSEvent for AlterSpaceTxn<'a> {
const OPC: u16 = 1;
type CommitType = AlterSpaceTxnCommitPL<'a>;
type CommitType = AlterSpaceTxn<'a>;
type RestoreType = AlterSpaceTxnRestorePL;
fn update_global_state(
@ -238,25 +223,21 @@ impl<'a> GNSEvent for AlterSpaceTxn<'a> {
drop space
*/
/// A transaction to run `drop space ...`
pub struct DropSpaceTxn<'a>(PhantomData<&'a ()>);
#[derive(Clone, Copy)]
/// Transaction commit payload for a `drop space ...` query
pub struct DropSpaceTxn<'a> {
space_id: super::SpaceIDRef<'a>,
}
impl<'a> DropSpaceTxn<'a> {
pub const fn new_commit(name: &'a str, uuid: Uuid) -> DropSpaceTxnCommitPL<'a> {
DropSpaceTxnCommitPL {
space_id: super::SpaceIDRef { uuid, name },
}
pub const fn new(space_id: super::SpaceIDRef<'a>) -> Self {
Self { space_id }
}
}
#[derive(Clone, Copy)]
pub struct DropSpaceTxnCommitPL<'a> {
space_id: super::SpaceIDRef<'a>,
}
impl<'a> PersistObject for DropSpaceTxn<'a> {
const METADATA_SIZE: usize = sizeof!(u128) + sizeof!(u64);
type InputType = DropSpaceTxnCommitPL<'a>;
type InputType = DropSpaceTxn<'a>;
type OutputType = super::SpaceIDRes;
type Metadata = super::SpaceIDMD;
fn pretest_can_dec_object(scanner: &BufferedScanner, md: &Self::Metadata) -> bool {
@ -278,7 +259,7 @@ impl<'a> PersistObject for DropSpaceTxn<'a> {
impl<'a> GNSEvent for DropSpaceTxn<'a> {
const OPC: u16 = 2;
type CommitType = DropSpaceTxnCommitPL<'a>;
type CommitType = DropSpaceTxn<'a>;
type RestoreType = super::SpaceIDRes;
fn update_global_state(
super::SpaceIDRes { uuid, name }: Self::RestoreType,

@ -0,0 +1,26 @@
/*
* Created on Thu Aug 24 2023
*
* 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) 2023, Sayan Nandan <ohsayan@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/>.
*
*/
Loading…
Cancel
Save