Skip to content

Commit

Permalink
sql: use try_collect() where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jul 21, 2024
1 parent ea1889a commit a1d54aa
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/raft/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ mod tests {
use super::*;
use crate::encoding::format::{self, Formatter as _};
use crossbeam::channel::Receiver;
use itertools::Itertools as _;
use regex::Regex;
use std::fmt::Write as _;
use std::{error::Error, result::Result};
Expand Down Expand Up @@ -413,7 +414,7 @@ mod tests {
"get" => {
let mut args = command.consume_args();
let indexes: Vec<Index> =
args.rest_pos().iter().map(|a| a.parse()).collect::<Result<_, _>>()?;
args.rest_pos().iter().map(|a| a.parse()).try_collect()?;
args.reject_rest()?;
for index in indexes {
let entry = self
Expand Down Expand Up @@ -443,7 +444,7 @@ mod tests {
.rest_pos()
.iter()
.map(|a| Self::parse_index_term(&a.value))
.collect::<Result<_, _>>()?;
.try_collect()?;
args.reject_rest()?;
for (index, term) in indexes {
let has = self.log.has(index, term)?;
Expand Down
2 changes: 1 addition & 1 deletion src/raft/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ impl RawNode<Leader> {
.log
.scan(progress.next_index..)
.take(self.opts.max_append_entries)
.collect::<Result<_>>()?,
.try_collect()?,
true => Vec::new(),
};

Expand Down
4 changes: 3 additions & 1 deletion src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
//! and returns it to `Session::execute`. It in turns returns it to
//! `Server::sql_session`, which encodes it and sends it across the wire
//! to `toySQL`, which displays them to the user.
//!
//! TODO: expand this into a "Life of a SQL statement" document.
pub mod engine;
pub mod execution;
Expand Down Expand Up @@ -165,7 +167,7 @@ mod tests {
tables
.into_iter()
.map(|t| self.session.with_txn(true, |txn| txn.must_get_table(&t)))
.collect::<Result<_, _>>()?
.try_collect()?
};
return Ok(schemas.into_iter().map(|s| s.to_string()).join("\n"));
}
Expand Down
4 changes: 2 additions & 2 deletions src/sql/planner/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl Node {
expressions = expressions
.into_iter()
.map(|expr| expr.transform(before, after))
.collect::<Result<_>>()?;
.try_collect()?;
Self::Projection { source, expressions, aliases }
}
Self::Scan { table, alias, filter: Some(filter) } => {
Expand All @@ -325,7 +325,7 @@ impl Node {
rows = rows
.into_iter()
.map(|row| row.into_iter().map(|expr| expr.transform(before, after)).collect())
.collect::<Result<_>>()?;
.try_collect()?;
Self::Values { rows }
}

Expand Down
10 changes: 4 additions & 6 deletions src/sql/planner/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'a, C: Catalog> Planner<'a, C> {
.map(|exprs| {
exprs.into_iter().map(|expr| Self::build_expression(expr, &scope)).collect()
})
.collect::<Result<_>>()?;
.try_collect()?;
Ok(Plan::Insert { table, column_map, source: Node::Values { rows } })
}

Expand Down Expand Up @@ -369,14 +369,12 @@ impl<'a, C: Catalog> Planner<'a, C> {
aggregates.retain(|expr| child_scope.add_aggregate(expr, scope).is_some());

// Build the node from the remaining unique expressions.
let group_by = group_by
.into_iter()
.map(|expr| Self::build_expression(expr, scope))
.collect::<Result<_>>()?;
let group_by =
group_by.into_iter().map(|expr| Self::build_expression(expr, scope)).try_collect()?;
let aggregates = aggregates
.into_iter()
.map(|expr| Self::build_aggregate_function(expr, scope))
.collect::<Result<_>>()?;
.try_collect()?;

*scope = child_scope;
Ok(Node::Aggregate { source: Box::new(source), group_by, aggregates })
Expand Down
5 changes: 3 additions & 2 deletions src/storage/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub mod test {
use super::*;
use crate::encoding::format::{self, Formatter as _};
use crossbeam::channel::Sender;
use itertools::Itertools as _;
use regex::Regex;
use std::error::Error as StdError;
use std::fmt::Write as _;
Expand Down Expand Up @@ -127,9 +128,9 @@ pub mod test {
parse_key_range(args.next_pos().map(|a| a.value.as_str()).unwrap_or(".."))?;
args.reject_rest()?;
let items: Vec<_> = if reverse {
self.engine.scan(range).rev().collect::<Result<_>>()?
self.engine.scan(range).rev().try_collect()?
} else {
self.engine.scan(range).collect::<Result<_>>()?
self.engine.scan(range).try_collect()?
};
for (key, value) in items {
writeln!(output, "{}", format::Raw::key_value(&key, &value))?;
Expand Down
8 changes: 4 additions & 4 deletions src/storage/mvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ use crate::encoding::{self, bincode, keycode, Key as _, Value as _};
use crate::error::{Error, Result};
use crate::{errdata, errinput};

use itertools::Itertools as _;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::{BTreeSet, VecDeque};
Expand Down Expand Up @@ -471,10 +472,10 @@ impl<E: Engine> Transaction<E> {
return Ok(());
}
let mut engine = self.engine.lock()?;
let remove = engine
let remove: Vec<_> = engine
.scan_prefix(&KeyPrefix::TxnWrite(self.st.version).encode())
.map(|r| r.map(|(k, _)| k))
.collect::<Result<Vec<_>>>()?;
.map_ok(|(k, _)| k)
.try_collect()?;
for key in remove {
engine.delete(&key)?
}
Expand Down Expand Up @@ -749,7 +750,6 @@ pub mod tests {
use crate::storage::{BitCask, Memory};

use crossbeam::channel::Receiver;
use itertools::Itertools as _;
use std::collections::HashMap;
use std::fmt::Write as _;
use std::{error::Error, result::Result};
Expand Down

0 comments on commit a1d54aa

Please sign in to comment.