Struct BTreeTable

Source
pub struct BTreeTable {
    pub key_field: usize,
    pub tuple_scheme: Schema,
    /* private fields */
}
Expand description

B+ Tree

Fields§

§key_field: usize§tuple_scheme: Schema

Implementations§

Source§

impl BTreeTable

Source

pub fn new(file_path: &str, key_field: usize, row_scheme: &Schema) -> Self

Source§

impl BTreeTable

Source

pub fn get_id(&self) -> u32

Source

pub fn get_tuple_scheme(&self) -> Schema

Source

pub fn tuples_count(&self) -> usize

Calculate the number of tuples in the table. Require S_LOCK on all pages.

Source

pub fn get_random_tuple(&self, _tx: &Transaction) -> Tuple

Source§

impl BTreeTable

Source

pub fn insert_tuple(&self, tx: &Transaction, tuple: &Tuple) -> SmallResult

Insert a tuple into this BTreeFile, keeping the tuples in sorted order. May cause pages to split if the page where tuple belongs is full.

Source

pub fn split_leaf_page( &self, tx: &Transaction, page_rc: Arc<RwLock<BTreeLeafPage>>, field: IntField, ) -> ResultPod<BTreeLeafPage>

Split a leaf page to make room for new tuples and recursively split the parent node as needed to accommodate a new entry. The new entry should have a key matching the key field of the first tuple in the right-hand page (the key is “copied up”), and child pointers pointing to the two leaf pages resulting from the split. Update sibling pointers and parent pointers as needed.

Return the leaf page into which a new tuple with key field “field” should be inserted.

§Arguments

field: the key field of the tuple to be inserted after the split is complete. Necessary to know which of the two pages to return.

Source

pub fn get_empty_page_index(&self, tx: &Transaction) -> u32

Source§

impl BTreeTable

Source

pub fn get_page(&self)

Method to encapsulate the process of locking/fetching a page. First the method checks the local cache (“dirtypages”), and if it can’t find the requested page there, it fetches it from the buffer pool. It also adds pages to the dirtypages cache if they are fetched with read-write permission, since presumably they will soon be dirtied by this transaction.

This method is needed to ensure that page updates are not lost if the same pages are accessed multiple times.

reference:

  • https://sourcegraph.com/github.com/XiaochenCui/small-db-hw@87607789b677d6afee00a223eacb4f441bd4ae87/-/blob/src/java/smalldb/BTreeFile.java?L551&subtree=true
Source§

impl BTreeTable

Source

pub fn set_root_pid(&self, tx: &Transaction, root_pid: &BTreePageID)

Source

pub fn get_file(&self) -> MutexGuard<'_, File>

Source

pub fn write_empty_page_to_disk(&self, page_id: &BTreePageID)

Source

pub fn write_page_to_disk(&self, page_id: &BTreePageID, data: &Vec<u8>)

Source

pub fn get_first_page( &self, tx: &Transaction, perm: Permission, ) -> Arc<RwLock<BTreeLeafPage>>

Source

pub fn get_last_page( &self, tx: &Transaction, perm: Permission, ) -> Arc<RwLock<BTreeLeafPage>>

Source

pub fn get_root_pid(&self, tx: &Transaction) -> BTreePageID

Get the root page pid.

Source

pub fn get_root_ptr_page( &self, tx: &Transaction, ) -> Arc<RwLock<BTreeRootPointerPage>>

Source

pub fn pages_count(&self) -> usize

The count of pages in this BTreeFile

(the ROOT_POINTER page is not included)

Source

pub fn get_first_tuple(&self, _pid: &BTreePageID) -> Option<Tuple>

Source

pub fn set_page_index(&self, i: u32)

Source

pub fn get_last_tuple( &self, tx: &Transaction, pid: &BTreePageID, ) -> Option<WrappedTuple>

Source§

impl BTreeTable

debug methods

Source

pub fn draw_tree(&self, max_level: i32)

Print the BTreeFile structure.

§Arguments
  • max_level - the max level of the print
    • 0: print the root pointer page
    • 1: print the root pointer page and the root page (internal or leaf)
    • -1: print all pages
Source

pub fn check_integrity(&self, check_occupancy: bool)

checks the integrity of the tree:

  • parent pointers.
  • sibling pointers.
  • range invariants.
  • record to page pointers.
  • occupancy invariants. (if enabled)

require s_lock on all pages.

panic on any error found.

Source§

impl BTreeTable

delete-related methods

Source

pub fn delete_tuple( &self, tx: &Transaction, tuple: &WrappedTuple, ) -> SmallResult

Delete a tuple from this BTreeFile.

May cause pages to merge or redistribute entries/tuples if the pages become less than half full.

Trait Implementations§

Source§

impl Display for BTreeTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.