mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-22 08:25:29 +01:00
better, less cool names
This commit is contained in:
@@ -39,7 +39,7 @@ const BTREE_HEADER_OFFSET_RIGHTMOST: usize = 8; /* if internalnode, pointer righ
|
||||
pub const BTCURSOR_MAX_DEPTH: usize = 20;
|
||||
|
||||
/// Evaluate a Result<CursorResult<T>>, if IO return IO.
|
||||
macro_rules! io {
|
||||
macro_rules! return_on_io {
|
||||
($expr:expr) => {
|
||||
match $expr? {
|
||||
CursorResult::Ok(v) => v,
|
||||
@@ -49,7 +49,7 @@ macro_rules! io {
|
||||
}
|
||||
|
||||
/// Check if the page is unlocked, if not return IO.
|
||||
macro_rules! unlocked {
|
||||
macro_rules! return_if_locked {
|
||||
($expr:expr) => {{
|
||||
if $expr.is_locked() {
|
||||
return Ok(CursorResult::IO);
|
||||
@@ -145,7 +145,7 @@ impl BTreeCursor {
|
||||
fn is_empty_table(&mut self) -> Result<CursorResult<bool>> {
|
||||
let page = self.pager.read_page(self.root_page)?;
|
||||
let page = RefCell::borrow(&page);
|
||||
unlocked!(page);
|
||||
return_if_locked!(page);
|
||||
|
||||
let cell_count = page.contents.as_ref().unwrap().cell_count();
|
||||
Ok(CursorResult::Ok(cell_count == 0))
|
||||
@@ -241,7 +241,7 @@ impl BTreeCursor {
|
||||
let cell_idx = self.stack.current_index() as usize;
|
||||
|
||||
debug!("current id={} cell={}", mem_page_rc.borrow().id, cell_idx);
|
||||
unlocked!(mem_page_rc.borrow());
|
||||
return_if_locked!(mem_page_rc.borrow());
|
||||
if !mem_page_rc.borrow().is_loaded() {
|
||||
self.pager.load_page(mem_page_rc.clone())?;
|
||||
return Ok(CursorResult::IO);
|
||||
@@ -395,12 +395,12 @@ impl BTreeCursor {
|
||||
key: SeekKey<'_>,
|
||||
op: SeekOp,
|
||||
) -> Result<CursorResult<(Option<u64>, Option<OwnedRecord>)>> {
|
||||
io!(self.move_to(key.clone(), op.clone()));
|
||||
return_on_io!(self.move_to(key.clone(), op.clone()));
|
||||
|
||||
{
|
||||
let page_rc = self.stack.top();
|
||||
let page = page_rc.borrow();
|
||||
unlocked!(page);
|
||||
return_if_locked!(page);
|
||||
|
||||
let contents = page.contents.as_ref().unwrap();
|
||||
|
||||
@@ -495,7 +495,7 @@ impl BTreeCursor {
|
||||
let page_idx = mem_page.borrow().id;
|
||||
let page = self.pager.read_page(page_idx)?;
|
||||
let page = RefCell::borrow(&page);
|
||||
unlocked!(page);
|
||||
return_if_locked!(page);
|
||||
let contents = page.contents.as_ref().unwrap();
|
||||
if contents.is_leaf() {
|
||||
if contents.cell_count() > 0 {
|
||||
@@ -548,7 +548,7 @@ impl BTreeCursor {
|
||||
loop {
|
||||
let page_rc = self.stack.top();
|
||||
let page = RefCell::borrow(&page_rc);
|
||||
unlocked!(page);
|
||||
return_if_locked!(page);
|
||||
|
||||
let contents = page.contents.as_ref().unwrap();
|
||||
if contents.is_leaf() {
|
||||
@@ -659,7 +659,7 @@ impl BTreeCursor {
|
||||
// get page and find cell
|
||||
let (cell_idx, page_type) = {
|
||||
let mut page = page_ref.borrow_mut();
|
||||
unlocked!(page);
|
||||
return_if_locked!(page);
|
||||
|
||||
page.set_dirty();
|
||||
self.pager.add_dirty(page.id);
|
||||
@@ -699,7 +699,7 @@ impl BTreeCursor {
|
||||
WriteState::BalanceStart
|
||||
| WriteState::BalanceMoveUp
|
||||
| WriteState::BalanceGetParentPage => {
|
||||
io!(self.balance_leaf());
|
||||
return_on_io!(self.balance_leaf());
|
||||
}
|
||||
WriteState::Finish => {
|
||||
self.write_info.state = WriteState::Start;
|
||||
@@ -913,7 +913,7 @@ impl BTreeCursor {
|
||||
WriteState::BalanceGetParentPage => {
|
||||
let parent_rc = self.stack.parent();
|
||||
let loaded = parent_rc.borrow().is_loaded();
|
||||
unlocked!(parent_rc.borrow());
|
||||
return_if_locked!(parent_rc.borrow());
|
||||
|
||||
if !loaded {
|
||||
debug!("balance_leaf(loading page)");
|
||||
@@ -1689,10 +1689,10 @@ fn find_free_cell(page_ref: &PageContent, db_header: Ref<DatabaseHeader>, amount
|
||||
|
||||
impl Cursor for BTreeCursor {
|
||||
fn seek_to_last(&mut self) -> Result<CursorResult<()>> {
|
||||
io!(self.move_to_rightmost());
|
||||
let (rowid, record) = io!(self.get_next_record(None));
|
||||
return_on_io!(self.move_to_rightmost());
|
||||
let (rowid, record) = return_on_io!(self.get_next_record(None));
|
||||
if rowid.is_none() {
|
||||
let is_empty = io!(self.is_empty_table());
|
||||
let is_empty = return_on_io!(self.is_empty_table());
|
||||
assert!(is_empty);
|
||||
return Ok(CursorResult::Ok(()));
|
||||
}
|
||||
@@ -1708,7 +1708,7 @@ impl Cursor for BTreeCursor {
|
||||
fn rewind(&mut self) -> Result<CursorResult<()>> {
|
||||
self.move_to_root();
|
||||
|
||||
let (rowid, record) = io!(self.get_next_record(None));
|
||||
let (rowid, record) = return_on_io!(self.get_next_record(None));
|
||||
self.rowid.replace(rowid);
|
||||
self.record.replace(record);
|
||||
Ok(CursorResult::Ok(()))
|
||||
@@ -1722,7 +1722,7 @@ impl Cursor for BTreeCursor {
|
||||
}
|
||||
|
||||
fn next(&mut self) -> Result<CursorResult<()>> {
|
||||
let (rowid, record) = io!(self.get_next_record(None));
|
||||
let (rowid, record) = return_on_io!(self.get_next_record(None));
|
||||
self.rowid.replace(rowid);
|
||||
self.record.replace(record);
|
||||
Ok(CursorResult::Ok(()))
|
||||
@@ -1749,7 +1749,7 @@ impl Cursor for BTreeCursor {
|
||||
}
|
||||
|
||||
fn seek(&mut self, key: SeekKey<'_>, op: SeekOp) -> Result<CursorResult<bool>> {
|
||||
let (rowid, record) = io!(self.seek(key, op));
|
||||
let (rowid, record) = return_on_io!(self.seek(key, op));
|
||||
self.rowid.replace(rowid);
|
||||
self.record.replace(record);
|
||||
Ok(CursorResult::Ok(rowid.is_some()))
|
||||
@@ -1770,10 +1770,10 @@ impl Cursor for BTreeCursor {
|
||||
_ => unreachable!("btree tables are indexed by integers!"),
|
||||
};
|
||||
if !moved_before {
|
||||
io!(self.move_to(SeekKey::TableRowId(*int_key as u64), SeekOp::EQ));
|
||||
return_on_io!(self.move_to(SeekKey::TableRowId(*int_key as u64), SeekOp::EQ));
|
||||
}
|
||||
|
||||
io!(self.insert_into_page(key, _record));
|
||||
return_on_io!(self.insert_into_page(key, _record));
|
||||
Ok(CursorResult::Ok(()))
|
||||
}
|
||||
|
||||
@@ -1790,11 +1790,11 @@ impl Cursor for BTreeCursor {
|
||||
OwnedValue::Integer(i) => i,
|
||||
_ => unreachable!("btree tables are indexed by integers!"),
|
||||
};
|
||||
io!(self.move_to(SeekKey::TableRowId(*int_key as u64), SeekOp::EQ));
|
||||
return_on_io!(self.move_to(SeekKey::TableRowId(*int_key as u64), SeekOp::EQ));
|
||||
let page_ref = self.stack.top();
|
||||
let page = page_ref.borrow();
|
||||
// TODO(pere): request load
|
||||
unlocked!(page);
|
||||
return_if_locked!(page);
|
||||
|
||||
let contents = page.contents.as_ref().unwrap();
|
||||
|
||||
|
||||
@@ -535,7 +535,7 @@ pub enum StepResult<'a> {
|
||||
|
||||
/// If there is I/O, the instruction is restarted.
|
||||
/// Evaluate a Result<CursorResult<T>>, if IO return Ok(StepResult::IO).
|
||||
macro_rules! io {
|
||||
macro_rules! return_on_io {
|
||||
($expr:expr) => {
|
||||
match $expr? {
|
||||
CursorResult::Ok(v) => v,
|
||||
@@ -1113,7 +1113,7 @@ impl Program {
|
||||
}
|
||||
Insn::RewindAsync { cursor_id } => {
|
||||
let cursor = cursors.get_mut(cursor_id).unwrap();
|
||||
io!(cursor.rewind());
|
||||
return_on_io!(cursor.rewind());
|
||||
state.pc += 1;
|
||||
}
|
||||
Insn::LastAsync { cursor_id } => {
|
||||
@@ -1199,7 +1199,7 @@ impl Program {
|
||||
Insn::NextAsync { cursor_id } => {
|
||||
let cursor = cursors.get_mut(cursor_id).unwrap();
|
||||
cursor.set_null_flag(false);
|
||||
io!(cursor.next());
|
||||
return_on_io!(cursor.next());
|
||||
state.pc += 1;
|
||||
}
|
||||
Insn::PrevAsync { cursor_id } => {
|
||||
@@ -1385,7 +1385,7 @@ impl Program {
|
||||
));
|
||||
}
|
||||
};
|
||||
let found = io!(cursor.seek(SeekKey::TableRowId(rowid), SeekOp::EQ));
|
||||
let found = return_on_io!(cursor.seek(SeekKey::TableRowId(rowid), SeekOp::EQ));
|
||||
if !found {
|
||||
state.pc = *target_pc;
|
||||
} else {
|
||||
@@ -1410,8 +1410,9 @@ impl Program {
|
||||
let cursor = cursors.get_mut(cursor_id).unwrap();
|
||||
let record_from_regs: OwnedRecord =
|
||||
make_owned_record(&state.registers, start_reg, num_regs);
|
||||
let found =
|
||||
io!(cursor.seek(SeekKey::IndexKey(&record_from_regs), SeekOp::GE));
|
||||
let found = return_on_io!(
|
||||
cursor.seek(SeekKey::IndexKey(&record_from_regs), SeekOp::GE)
|
||||
);
|
||||
if !found {
|
||||
state.pc = *target_pc;
|
||||
} else {
|
||||
@@ -1422,7 +1423,7 @@ impl Program {
|
||||
let rowid = match &state.registers[*start_reg] {
|
||||
OwnedValue::Null => {
|
||||
// All integer values are greater than null so we just rewind the cursor
|
||||
io!(cursor.rewind());
|
||||
return_on_io!(cursor.rewind());
|
||||
state.pc += 1;
|
||||
continue;
|
||||
}
|
||||
@@ -1433,7 +1434,8 @@ impl Program {
|
||||
));
|
||||
}
|
||||
};
|
||||
let found = io!(cursor.seek(SeekKey::TableRowId(rowid), SeekOp::GE));
|
||||
let found =
|
||||
return_on_io!(cursor.seek(SeekKey::TableRowId(rowid), SeekOp::GE));
|
||||
if !found {
|
||||
state.pc = *target_pc;
|
||||
} else {
|
||||
@@ -1452,8 +1454,9 @@ impl Program {
|
||||
let cursor = cursors.get_mut(cursor_id).unwrap();
|
||||
let record_from_regs: OwnedRecord =
|
||||
make_owned_record(&state.registers, start_reg, num_regs);
|
||||
let found =
|
||||
io!(cursor.seek(SeekKey::IndexKey(&record_from_regs), SeekOp::GT));
|
||||
let found = return_on_io!(
|
||||
cursor.seek(SeekKey::IndexKey(&record_from_regs), SeekOp::GT)
|
||||
);
|
||||
if !found {
|
||||
state.pc = *target_pc;
|
||||
} else {
|
||||
@@ -1464,7 +1467,7 @@ impl Program {
|
||||
let rowid = match &state.registers[*start_reg] {
|
||||
OwnedValue::Null => {
|
||||
// All integer values are greater than null so we just rewind the cursor
|
||||
io!(cursor.rewind());
|
||||
return_on_io!(cursor.rewind());
|
||||
state.pc += 1;
|
||||
continue;
|
||||
}
|
||||
@@ -1475,7 +1478,8 @@ impl Program {
|
||||
));
|
||||
}
|
||||
};
|
||||
let found = io!(cursor.seek(SeekKey::TableRowId(rowid), SeekOp::GT));
|
||||
let found =
|
||||
return_on_io!(cursor.seek(SeekKey::TableRowId(rowid), SeekOp::GT));
|
||||
if !found {
|
||||
state.pc = *target_pc;
|
||||
} else {
|
||||
@@ -1846,7 +1850,7 @@ impl Program {
|
||||
} => {
|
||||
assert!(*pc_if_next >= 0);
|
||||
let cursor = cursors.get_mut(cursor_id).unwrap();
|
||||
io!(cursor.next());
|
||||
return_on_io!(cursor.next());
|
||||
if !cursor.is_empty() {
|
||||
state.pc = *pc_if_next;
|
||||
} else {
|
||||
@@ -2123,7 +2127,7 @@ impl Program {
|
||||
_ => unreachable!("Not a record! Cannot insert a non record value."),
|
||||
};
|
||||
let key = &state.registers[*key_reg];
|
||||
io!(cursor.insert(key, record, true));
|
||||
return_on_io!(cursor.insert(key, record, true));
|
||||
state.pc += 1;
|
||||
}
|
||||
Insn::InsertAwait { cursor_id } => {
|
||||
@@ -2136,7 +2140,7 @@ impl Program {
|
||||
} => {
|
||||
let cursor = cursors.get_mut(cursor).unwrap();
|
||||
// TODO: make io handle rng
|
||||
let rowid = io!(get_new_rowid(cursor, thread_rng()));
|
||||
let rowid = return_on_io!(get_new_rowid(cursor, thread_rng()));
|
||||
state.registers[*rowid_reg] = OwnedValue::Integer(rowid);
|
||||
state.pc += 1;
|
||||
}
|
||||
@@ -2161,7 +2165,7 @@ impl Program {
|
||||
target_pc,
|
||||
} => {
|
||||
let cursor = cursors.get_mut(cursor).unwrap();
|
||||
let exists = io!(cursor.exists(&state.registers[*rowid_reg]));
|
||||
let exists = return_on_io!(cursor.exists(&state.registers[*rowid_reg]));
|
||||
if exists {
|
||||
state.pc += 1;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user