Skip to content

Commit

Permalink
Merge 'Store cursor types in program state and remove trait Cursor' f…
Browse files Browse the repository at this point in the history
…rom Jussi Saurio

I was planning on starting work on index insertion, but realized we need
to know whether our cursor refers to a table or an index etc., so it
resulted in this refactoring work.
- `cursor_ref` now contains what _type_ of cursor it is (table, index,
pseudo, sorter)
- `program.cursors` is now `program.btree_table_cursors`,
`program.btree_index_cursors` etc and they are unboxed because dynamic
dispatch is no longer necessary
- Cursor trait removed -- 95% of the shit was btree specific anyway, so
I just moved them to `BTreeCursor`. In certain instructions in the VDBE
we expect a btree cursor and in others we expect a pseudo/sorter etc,
lets make that explicit.
- I also removed `BTreeCursor::get_new_rowid()` specific tests that
required macros to generate a mock implementation of the `Cursor` trait
-- main reason is I couldn't figure out how to reimplement this without
the trait, and the second reason is that I don't think we really need
those tests, AND the proc macro is constantly failing in my editor as
well and screwing up `rust-analyzer`

Closes #655
  • Loading branch information
penberg committed Jan 13, 2025
2 parents 8769bf8 + 92888c5 commit c7ea239
Show file tree
Hide file tree
Showing 14 changed files with 484 additions and 707 deletions.
105 changes: 7 additions & 98 deletions core/pseudo.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,19 @@
use crate::{
types::{SeekKey, SeekOp},
Result,
};
use std::cell::{Ref, RefCell};

use crate::types::{Cursor, CursorResult, OwnedRecord, OwnedValue};
use crate::types::OwnedRecord;

pub struct PseudoCursor {
current: RefCell<Option<OwnedRecord>>,
current: Option<OwnedRecord>,
}

impl PseudoCursor {
pub fn new() -> Self {
Self {
current: RefCell::new(None),
}
}
}

impl Cursor for PseudoCursor {
fn is_empty(&self) -> bool {
self.current.borrow().is_none()
}

fn root_page(&self) -> usize {
unreachable!()
}

fn rewind(&mut self) -> Result<CursorResult<()>> {
*self.current.borrow_mut() = None;
Ok(CursorResult::Ok(()))
}

fn next(&mut self) -> Result<CursorResult<()>> {
*self.current.borrow_mut() = None;
Ok(CursorResult::Ok(()))
}

fn wait_for_completion(&mut self) -> Result<()> {
Ok(())
}

fn rowid(&self) -> Result<Option<u64>> {
let x = self
.current
.borrow()
.as_ref()
.map(|record| match record.values[0] {
OwnedValue::Integer(rowid) => rowid as u64,
ref ov => {
panic!("Expected integer value, got {:?}", ov);
}
});
Ok(x)
}

fn seek(&mut self, _: SeekKey<'_>, _: SeekOp) -> Result<CursorResult<bool>> {
unimplemented!();
}

fn seek_to_last(&mut self) -> Result<CursorResult<()>> {
unimplemented!();
}

fn record(&self) -> Result<Ref<Option<OwnedRecord>>> {
Ok(self.current.borrow())
}

fn insert(
&mut self,
key: &OwnedValue,
record: &OwnedRecord,
moved_before: bool,
) -> Result<CursorResult<()>> {
let _ = key;
let _ = moved_before;
*self.current.borrow_mut() = Some(record.clone());
Ok(CursorResult::Ok(()))
}

fn delete(&mut self) -> Result<CursorResult<()>> {
unimplemented!()
}

fn get_null_flag(&self) -> bool {
false
}

fn set_null_flag(&mut self, _null_flag: bool) {
// Do nothing
}

fn exists(&mut self, key: &OwnedValue) -> Result<CursorResult<bool>> {
let _ = key;
todo!()
}

fn btree_create(&mut self, _flags: usize) -> u32 {
unreachable!("Please don't.")
Self { current: None }
}

fn last(&mut self) -> Result<CursorResult<()>> {
todo!()
pub fn record(&self) -> Option<&OwnedRecord> {
self.current.as_ref()
}

fn prev(&mut self) -> Result<CursorResult<()>> {
todo!()
pub fn insert(&mut self, record: OwnedRecord) {
self.current = Some(record);
}
}
57 changes: 3 additions & 54 deletions core/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,93 +46,42 @@ impl Schema {
#[derive(Clone, Debug)]
pub enum Table {
BTree(Rc<BTreeTable>),
Index(Rc<Index>),
Pseudo(Rc<PseudoTable>),
}

impl Table {
pub fn is_pseudo(&self) -> bool {
matches!(self, Table::Pseudo(_))
}

pub fn get_rowid_alias_column(&self) -> Option<(usize, &Column)> {
match self {
Self::BTree(table) => table.get_rowid_alias_column(),
Self::Index(_) => None,
Self::Pseudo(_) => None,
}
}

pub fn column_is_rowid_alias(&self, col: &Column) -> bool {
match self {
Table::BTree(table) => table.column_is_rowid_alias(col),
Self::Index(_) => false,
Self::Pseudo(_) => false,
}
}

pub fn get_root_page(&self) -> usize {
match self {
Table::BTree(table) => table.root_page,
Table::Index(_) => unimplemented!(),
Table::Pseudo(_) => unimplemented!(),
}
}

pub fn get_name(&self) -> &str {
match self {
Self::BTree(table) => &table.name,
Self::Index(index) => &index.name,
Self::Pseudo(_) => "",
}
}

pub fn column_index_to_name(&self, index: usize) -> Option<&str> {
match self {
Self::BTree(table) => match table.columns.get(index) {
Some(column) => Some(&column.name),
None => None,
},
Self::Index(i) => match i.columns.get(index) {
Some(column) => Some(&column.name),
None => None,
},
Self::Pseudo(table) => match table.columns.get(index) {
Some(_) => None,
None => None,
},
}
}

pub fn get_column(&self, name: &str) -> Option<(usize, &Column)> {
match self {
Self::BTree(table) => table.get_column(name),
Self::Index(_) => unimplemented!(),
Self::Pseudo(table) => table.get_column(name),
}
}

pub fn get_column_at(&self, index: usize) -> &Column {
match self {
Self::BTree(table) => table.columns.get(index).unwrap(),
Self::Index(_) => unimplemented!(),
Self::Pseudo(table) => table.columns.get(index).unwrap(),
}
}

pub fn columns(&self) -> &Vec<Column> {
match self {
Self::BTree(table) => &table.columns,
Self::Index(_) => unimplemented!(),
Self::Pseudo(table) => &table.columns,
}
}

pub fn has_rowid(&self) -> bool {
pub fn btree(&self) -> Option<Rc<BTreeTable>> {
match self {
Self::BTree(table) => table.has_rowid,
Self::Index(_) => unimplemented!(),
Self::Pseudo(_) => unimplemented!(),
Self::BTree(table) => Some(table.clone()),
Self::Pseudo(_) => None,
}
}
}
Expand Down
Loading

0 comments on commit c7ea239

Please sign in to comment.