Skip to content

Commit

Permalink
Fix clippy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
gtnao committed Jan 20, 2024
1 parent 8d32bf2 commit 1f8457d
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 243 deletions.
22 changes: 10 additions & 12 deletions src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,16 @@ impl Binder {
.iter()
.map(|function| self.bind_function_call_expression(function))
.collect::<Result<Vec<_>>>()?;
let needs_aggregation = group_by.len() > 0 || bound_aggregate_functions.len() > 0;
let needs_aggregation = !group_by.is_empty() || !bound_aggregate_functions.is_empty();
if needs_aggregation {
self.replace_scope_by_aggregation(&group_by, &aggregate_functions);
self.replace_scope_by_aggregation(&group_by, aggregate_functions);
}
let having = match &statement.having {
Some(having) => Some(self.bind_expression(having)?),
None => None,
};
let select_elements = self.bind_select_elements(&statement.select_elements)?;
if select_elements.len() > 0 {
if !select_elements.is_empty() {
let scopes_len = self.scopes.len();
self.scopes[scopes_len - 1].tables.clear();
self.scopes[scopes_len - 1].aggregation = None;
Expand Down Expand Up @@ -686,7 +686,7 @@ impl Binder {
.filter(|(_, table_name)| table_name == &&expression.path[0])
.map(|(i, _)| i)
.collect::<Vec<_>>();
if matched_table_indexes.len() == 0 {
if matched_table_indexes.is_empty() {
return Err(anyhow::anyhow!("table {} not found", expression.path[0]));
}
if matched_table_indexes.len() > 1 {
Expand Down Expand Up @@ -724,19 +724,18 @@ impl Binder {
) -> Result<Vec<FunctionCallExpressionAST>> {
let mut aggregate_functions = Vec::new();
if let Some(having) = having {
let mut functions = self.extract_aggregate_functions_from_expression(having)?;
let mut functions = Self::extract_aggregate_functions_from_expression(having)?;
aggregate_functions.append(&mut functions);
}
for select_element in select_elements {
let mut functions =
self.extract_aggregate_functions_from_expression(&select_element.expression)?;
Self::extract_aggregate_functions_from_expression(&select_element.expression)?;
aggregate_functions.append(&mut functions);
}
Ok(aggregate_functions)
}

fn extract_aggregate_functions_from_expression(
&mut self,
expression: &ExpressionAST,
) -> Result<Vec<FunctionCallExpressionAST>> {
match expression {
Expand All @@ -750,13 +749,12 @@ impl Binder {
ExpressionAST::Path(_) => Ok(Vec::new()),
ExpressionAST::Literal(_) => Ok(Vec::new()),
ExpressionAST::Unary(expression) => {
self.extract_aggregate_functions_from_expression(&expression.operand)
Self::extract_aggregate_functions_from_expression(&expression.operand)
}
ExpressionAST::Binary(expression) => {
let mut left =
self.extract_aggregate_functions_from_expression(&expression.left)?;
let mut left = Self::extract_aggregate_functions_from_expression(&expression.left)?;
let mut right =
self.extract_aggregate_functions_from_expression(&expression.right)?;
Self::extract_aggregate_functions_from_expression(&expression.right)?;
left.append(&mut right);
Ok(left)
}
Expand All @@ -766,7 +764,7 @@ impl Binder {
fn replace_scope_by_aggregation(
&mut self,
group_by: &Vec<BoundExpressionAST>,
aggregate_functions: &Vec<FunctionCallExpressionAST>,
aggregate_functions: Vec<FunctionCallExpressionAST>,
) {
let mut group_by_items = Vec::new();
for expression in group_by {
Expand Down
45 changes: 21 additions & 24 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,23 @@ impl BufferPoolManager {
size,
frames: Vec::with_capacity(size),
page_table: HashMap::new(),
replacer: Replacer::LRU(LRUReplacer::default()),
replacer: Replacer::Lru(LRUReplacer::default()),
}
}
pub fn fetch_page(&mut self, page_id: PageID) -> Result<Arc<RwLock<Page>>> {
if !self.page_table.contains_key(&page_id) {
if self.is_full() {
self.evict_page()?;
match self.page_table.contains_key(&page_id) {
true => (),
false => {
if self.is_full() {
self.evict_page()?;
}
let mut data = vec![0u8; PAGE_SIZE];
self.disk_manager.read_page(page_id, &mut data)?;
let page = Arc::new(RwLock::new(Page::from_data(&data)));
let frame_id = self.frames.len();
self.frames.push(Some(Frame::new(page.clone())));
self.page_table.insert(page_id, frame_id);
}
let mut data = vec![0u8; PAGE_SIZE];
self.disk_manager.read_page(page_id, &mut data)?;
let page = Arc::new(RwLock::new(Page::from_data(&data)));
let frame_id = self.frames.len();
self.frames.push(Some(Frame::new(page.clone())));
self.page_table.insert(page_id, frame_id);
}
if let Some(&frame_id) = self.page_table.get(&page_id) {
if let Some(frame) = &mut self.frames[frame_id] {
Expand Down Expand Up @@ -157,7 +160,7 @@ impl BufferPoolManager {
.lock()
.map_err(|_| anyhow!("lock error"))?
.flush()?;
self.disk_manager.write_page(page_id, &page.data())?;
self.disk_manager.write_page(page_id, page.data())?;
}
}
}
Expand Down Expand Up @@ -186,38 +189,32 @@ impl BufferPoolManager {
}

enum Replacer {
LRU(LRUReplacer),
Lru(LRUReplacer),
}
impl Replacer {
pub fn victim(&mut self) -> Option<usize> {
match self {
Self::LRU(replacer) => replacer.victim(),
Self::Lru(replacer) => replacer.victim(),
}
}
pub fn pin(&mut self, frame_id: usize) {
match self {
Self::LRU(replacer) => replacer.pin(frame_id),
Self::Lru(replacer) => replacer.pin(frame_id),
}
}
pub fn unpin(&mut self, frame_id: usize) {
match self {
Self::LRU(replacer) => replacer.unpin(frame_id),
Self::Lru(replacer) => replacer.unpin(frame_id),
}
}
}

#[derive(Default)]
struct LRUReplacer {
frame_map: HashMap<usize, u128>,
counter: u128,
}
impl Default for LRUReplacer {
fn default() -> Self {
Self {
frame_map: HashMap::new(),
counter: 0,
}
}
}

impl LRUReplacer {
fn victim(&mut self) -> Option<usize> {
if self.frame_map.is_empty() {
Expand Down Expand Up @@ -251,7 +248,7 @@ mod tests {

#[test]
fn test_lru_replacer() {
let mut replacer = Replacer::LRU(LRUReplacer::default());
let mut replacer = Replacer::Lru(LRUReplacer::default());

assert_eq!(replacer.victim(), None);
replacer.pin(1);
Expand Down
Loading

0 comments on commit 1f8457d

Please sign in to comment.