Skip to content

Commit

Permalink
move function to method
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 12, 2023
1 parent 7c208a8 commit b2227c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
21 changes: 1 addition & 20 deletions crates/core/src/db/datastore/locking_tx_datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,25 +1174,6 @@ impl Inner {
}
}

/// Check if the value is one of the `numeric` types and is `0`.
fn can_replace_with_sequence(value: &AlgebraicValue) -> bool {
match value {
AlgebraicValue::I8(x) => *x == 0,
AlgebraicValue::U8(x) => *x == 0,
AlgebraicValue::I16(x) => *x == 0,
AlgebraicValue::U16(x) => *x == 0,
AlgebraicValue::I32(x) => *x == 0,
AlgebraicValue::U32(x) => *x == 0,
AlgebraicValue::I64(x) => *x == 0,
AlgebraicValue::U64(x) => *x == 0,
AlgebraicValue::I128(x) => *x == 0,
AlgebraicValue::U128(x) => *x == 0,
AlgebraicValue::F32(x) => *x == 0.0,
AlgebraicValue::F64(x) => *x == 0.0,
_ => false,
}
}

#[tracing::instrument(skip_all)]
fn insert(&mut self, table_id: TableId, mut row: ProductValue) -> super::Result<ProductValue> {
// TODO: Excuting schema_for_table for every row insert is expensive.
Expand All @@ -1202,7 +1183,7 @@ impl Inner {
let mut col_to_update = None;
for col in &*schema.columns {
if col.is_autoinc {
if !Self::can_replace_with_sequence(&row.elements[col.col_id as usize]) {
if !row.elements[col.col_id as usize].is_numeric_zero() {
continue;
}
let st_sequences_table_id_col = ColId(2);
Expand Down
21 changes: 21 additions & 0 deletions crates/sats/src/algebraic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,27 @@ impl AlgebraicValue {
Self::String(_) => AlgebraicType::String,
}
}

/// Returns whether this value represents a numeric zero.
///
/// Can only be true where the type is numeric.
pub fn is_numeric_zero(&self) -> bool {
match *self {
Self::I8(x) => x == 0,
Self::U8(x) => x == 0,
Self::I16(x) => x == 0,
Self::U16(x) => x == 0,
Self::I32(x) => x == 0,
Self::U32(x) => x == 0,
Self::I64(x) => x == 0,
Self::U64(x) => x == 0,
Self::I128(x) => x == 0,
Self::U128(x) => x == 0,
Self::F32(x) => x == 0.0,
Self::F64(x) => x == 0.0,
_ => false,
}
}
}

impl<T: Into<AlgebraicValue>> From<Option<T>> for AlgebraicValue {
Expand Down

0 comments on commit b2227c5

Please sign in to comment.