Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Commit

Permalink
Fixed the error handling to properly handle the index not existing at…
Browse files Browse the repository at this point in the history
… the beginning.
  • Loading branch information
chotchki committed Oct 10, 2021
1 parent ed77446 commit 75fcab9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
40 changes: 32 additions & 8 deletions src/engine/io/block_layer/file_manager2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,34 @@ impl FileManager2 {
let file_number = offset.get_file_number();
let file_handles = self.file_handles.clone();

let chunk = self
let chunk = match self
.page_cache
.get_or_try_insert_with((page_id, offset), async move {
let file_handle = file_handles
.get_or_try_insert_with((page_id, file_number), async move {
let handle =
FileOperations::open_path(&data_dir, &page_id, file_number).await?;
Ok::<Arc<Mutex<File>>, FileManager2Error>(Arc::new(Mutex::const_new(
Ok::<Arc<Mutex<File>>, FileOperationsError>(Arc::new(Mutex::const_new(
handle,
)))
})
.await?;
let mut file = file_handle.lock().await;

let chunk = FileOperations::read_chunk(file.deref_mut(), &offset).await?;
Ok::<Bytes, FileManager2Error>(chunk)
Ok::<Bytes, FileOperationsError>(chunk)
})
.await?;
.await
{
Ok(s) => s,
Err(e) => {
if let FileOperationsError::FileTooSmall(_, _) = e.as_ref() {
return Err(FileManager2Error::PageDoesNotExist(offset));
} else {
return Err(FileManager2Error::ArcFileOperationsError(e));
}
}
};

Ok((chunk, read_lock))
}
Expand All @@ -150,24 +160,34 @@ impl FileManager2 {
let file_number = offset.get_file_number();
let file_handles = self.file_handles.clone();

let chunk = self
let chunk = match self
.page_cache
.get_or_try_insert_with((page_id, offset), async move {
let file_handle = file_handles
.get_or_try_insert_with((page_id, file_number), async move {
let handle =
FileOperations::open_path(&data_dir, &page_id, file_number).await?;
Ok::<Arc<Mutex<File>>, FileManager2Error>(Arc::new(Mutex::const_new(
Ok::<Arc<Mutex<File>>, FileOperationsError>(Arc::new(Mutex::const_new(
handle,
)))
})
.await?;
let mut file = file_handle.lock().await;

let chunk = FileOperations::read_chunk(file.deref_mut(), &offset).await?;
Ok::<Bytes, FileManager2Error>(chunk)
Ok::<Bytes, FileOperationsError>(chunk)
})
.await?;
.await
{
Ok(s) => s,
Err(e) => {
if let FileOperationsError::FileTooSmall(_, _) = e.as_ref() {
return Err(FileManager2Error::PageDoesNotExist(offset));
} else {
return Err(FileManager2Error::ArcFileOperationsError(e));
}
}
};

Ok((chunk, write_lock))
}
Expand Down Expand Up @@ -309,10 +329,14 @@ pub enum FileManager2Error {
FileManager2Error(#[from] Arc<FileManager2Error>),
#[error(transparent)]
FileOperationsError(#[from] FileOperationsError),
#[error(transparent)]
ArcFileOperationsError(#[from] Arc<FileOperationsError>),
#[error("Incorrect page size of {0} on file {1} found. System cannot function")]
IncorrectPageSize(u64, PathBuf),
#[error(transparent)]
IOError(#[from] std::io::Error),
#[error("Page {0} does not exist")]
PageDoesNotExist(PageOffset),
#[error("Need a directory to store the data. Got ({0}) may be stripped of non Unicode chars.")]
NeedDirectory(String),
#[error(transparent)]
Expand Down
3 changes: 3 additions & 0 deletions src/engine/io/block_layer/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bytes::{Bytes, BytesMut};
use std::convert::TryFrom;
use std::num::TryFromIntError;
use std::path::PathBuf;
use std::sync::Arc;
use std::{io::SeekFrom, path::Path};
use thiserror::Error;
use tokio::fs;
Expand Down Expand Up @@ -119,6 +120,8 @@ impl FileOperations {

#[derive(Debug, Error)]
pub enum FileOperationsError {
#[error(transparent)]
FileOperationsError(#[from] Arc<FileOperationsError>),
#[error("Read {0} bytes instead of a page, the buffer has {1}")]
IncompleteRead(usize, usize),
#[error(transparent)]
Expand Down
10 changes: 9 additions & 1 deletion src/engine/io/index_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@ impl IndexManager {
};

let (mut first_page, _first_guard) =
self.file_manager.get_page(&page_id, &PageOffset(0)).await?;
match self.file_manager.get_page(&page_id, &PageOffset(0)).await {
Ok(s) => s,
Err(FileManager2Error::PageDoesNotExist(_)) => {
return Ok(None);
}
Err(e) => {
return Err(IndexManagerError::FileManager2Error(e));
}
};
let first_node = BTreeFirstPage::parse(&mut first_page)?;

let mut current_offset = first_node.root_offset;
Expand Down

0 comments on commit 75fcab9

Please sign in to comment.