Delete Tuple
Delete Tuple
Description:
Deletes a tuple from a heap file by marking its slot entry as deleted. The tuple is not physically removed from the page; instead, its slot directory entry is invalidated, allowing future maintenance operations (e.g., VACUUM) to reclaim space.
Function:
pub fn delete_tuple(
&mut self,
page_id: u32,
slot_id: u32,
) -> io::Result<u32>
Input:
page_id— Identifier of the page containing the tuple.slot_id— Slot identifier of the tuple within the page.
Output:
- Returns the number of bytes freed (
u32) on success. - Returns an
io::Errorif the page or slot is invalid, or if a page read/write operation fails.
Implementation:
- Validate that
page_idis within the heap file's page range. - Read the target page from disk.
- Retrieve the tuple's slot directory entry.
- Compute the number of bytes freed:
tuple_length + ITEM_ID_SIZE - Mark the slot as deleted by setting:
offset = 0
length = 0 - Read the page's
lowerandupperpointers. - If the deleted tuple occupies the current upper boundary, reclaim contiguous data space by adjusting
upper. - If the deleted slot is the last slot entry in the slot directory, reclaim slot array space by adjusting
lower. - Write the modified page back to disk.
- Decrement the heap file's tuple count.
- Persist the updated heap header.
- Return the number of freed bytes.
Internal API Calls:
-
read_page(&mut self.file_handle, &mut page, page_id)- Loads the target page from disk into memory.
-
get_slot_entry(&page, slot_id)- Retrieves the tuple offset and length from the slot directory.
-
get_tuple_count(&page)- Returns the number of slot entries in the page.
-
write_page(&mut self.file_handle, &mut page, page_id)- Persists the modified page back to disk.
-
update_header_page(&mut self.file_handle, &self.header)- Writes the updated heap header metadata to disk.
Files Created/Modified:
- Heap table file containing the target page.
- Heap header page.
Storage Updates:
- Marks the tuple's slot directory entry as deleted:
offset = 0
length = 0 - Updates page metadata (
lowerand/orupper) when contiguous space can be reclaimed. - Decrements:
self.header.total_tuples - Updates the heap header on disk.
Notes:
- The tuple is logically deleted but not physically removed from the page.
- Free Space Map (FSM) entries are not updated during deletion because contiguous free space does not necessarily increase.
- Space is fully reclaimed later through page compaction or VACUUM operations.