Skip to content

Commit

Permalink
Constructs a new, empty Block with the specified capacity
Browse files Browse the repository at this point in the history
(cherry picked from commit f8d5989)
  • Loading branch information
suharev7 committed Feb 4, 2020
1 parent 1fa85ed commit f546252
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn execute(database_url: String) -> Result<(), Box<dyn Error>> {
account_name Nullable(FixedString(3))
) Engine=Memory";

let mut block = Block::new();
let mut block = Block::with_capacity(5);
block.push(row! { customer_id: 1_u32, amount: 2_u32, account_name: Some("foo") })?;
block.push(row! { customer_id: 3_u32, amount: 4_u32, account_name: None::<&str> })?;
block.push(row! { customer_id: 5_u32, amount: 6_u32, account_name: None::<&str> })?;
Expand Down
14 changes: 13 additions & 1 deletion src/types/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ mod row;

const INSERT_BLOCK_SIZE: usize = 1_048_576;

const DEFAULT_CAPACITY: usize = 100;

pub trait ColumnIdx {
fn get_index<K: ColumnType>(&self, columns: &[Column<K>]) -> Result<usize>;
}
Expand Down Expand Up @@ -73,6 +75,7 @@ sliceable! {
pub struct Block<K: ColumnType = Simple> {
info: BlockInfo,
columns: Vec<Column<K>>,
capacity: usize,
}

impl<L: ColumnType, R: ColumnType> PartialEq<Block<R>> for Block<L> {
Expand All @@ -96,6 +99,7 @@ impl<K: ColumnType> Clone for Block<K> {
Self {
info: self.info,
columns: self.columns.iter().map(|c| (*c).clone()).collect(),
capacity: self.capacity,
}
}
}
Expand Down Expand Up @@ -133,11 +137,17 @@ impl ColumnIdx for String {
}

impl Block {
/// Constructs a new, empty Block.
/// Constructs a new, empty `Block`.
pub fn new() -> Self {
Self::with_capacity(DEFAULT_CAPACITY)
}

/// Constructs a new, empty `Block` with the specified capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self {
info: Default::default(),
columns: vec![],
capacity
}
}

Expand Down Expand Up @@ -284,6 +294,7 @@ impl Block<Simple> {
Block {
info: first.info,
columns,
capacity: blocks.iter().map(|b| b.capacity).sum(),
}
}
}
Expand All @@ -309,6 +320,7 @@ impl<K: ColumnType> Block<K> {
Ok(Block {
info,
columns: new_columns,
capacity: self.capacity,
})
}

Expand Down
3 changes: 1 addition & 2 deletions src/types/column/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::{
SqlType,
};

const DEFAULT_CAPACITY: usize = 100;

macro_rules! match_str {
($arg:ident, {
$( $($var:literal)|* => $doit:expr,)*
Expand All @@ -33,6 +31,7 @@ macro_rules! match_str {
}

impl dyn ColumnData {
#[allow(clippy::cognitive_complexity)]
pub(crate) fn load_data<W: ColumnWrapper, T: ReadEx>(
reader: &mut T,
type_name: &str,
Expand Down

0 comments on commit f546252

Please sign in to comment.