Skip to content

Commit

Permalink
Increase use of static for values that will never change
Browse files Browse the repository at this point in the history
  • Loading branch information
nuuskamummu committed Nov 4, 2024
1 parent 36f04d2 commit 4110285
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
16 changes: 8 additions & 8 deletions src/shadow_tables/lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub trait Lookup<T> {
impl PartitionType for LookupTable<i64> {
const PARTITION_NAME_COLUMN: &'static str = "partition_table";
const PARTITION_VALUE_COLUMN: &'static str = "partition_value";
const PARTITION_VALUE_COLUMN_TYPE: &'static PartitionValue = &PartitionValue::Interval;
const PARTITION_NAME_COLUMN_TYPE: &'static ValueType = &ValueType::Text;
const PARTITION_VALUE_COLUMN_TYPE: PartitionValue = PartitionValue::Interval;
const PARTITION_NAME_COLUMN_TYPE: ValueType = ValueType::Text;
}
impl Table for LookupTable<i64> {
const POSTFIX: &'static str = "lookup";
Expand All @@ -89,8 +89,8 @@ impl Create for LookupTable<i64> {
Ok(format!(
"CREATE TABLE {} ({} UNIQUE, {} UNIQUE);",
schema.name(),
<Self as PartitionType>::partition_name_column(),
<Self as PartitionType>::partition_value_column()
<Self as PartitionType>::COLUMNS[0],
<Self as PartitionType>::COLUMNS[1]
))
}
}
Expand All @@ -106,11 +106,11 @@ impl LookupTable<i64> {
parse_to_unix_epoch(value).map(|epoch| epoch - epoch % interval)
}

pub fn partition_table_column(&self) -> ColumnDeclaration {
<Self as PartitionType>::partition_name_column()
pub fn partition_table_column(&self) -> &'static ColumnDeclaration {
&<Self as PartitionType>::COLUMNS[0]
}
pub fn partition_value_column(&self) -> ColumnDeclaration {
<Self as PartitionType>::partition_value_column()
pub fn partition_value_column(&self) -> &'static ColumnDeclaration {
&<Self as PartitionType>::COLUMNS[1]
}

/// Creates a new instance of `LookupTable` with a specified base name. This involves initializing
Expand Down
39 changes: 21 additions & 18 deletions src/shadow_tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ pub enum PartitionValue {
Interval,
}

impl PartitionValue {
const fn to_valuetype(partitionvalue: Self) -> ValueType {
match partitionvalue {
Self::Interval => ValueType::Integer,
}
}
}
impl From<PartitionValue> for ValueType {
fn from(value: PartitionValue) -> ValueType {
match value {
Expand Down Expand Up @@ -44,27 +51,23 @@ impl<'a> TryFrom<&'a ValueType> for PartitionValue {
}
}
}

// type IntervalPartition = ValueType::Integer;
pub trait PartitionType {
const PARTITION_VALUE_COLUMN_TYPE: &'static PartitionValue;
const PARTITION_VALUE_COLUMN_TYPE: PartitionValue;
const PARTITION_VALUE_COLUMN: &'static str;
const PARTITION_NAME_COLUMN: &'static str;
const PARTITION_NAME_COLUMN_TYPE: &'static ValueType;
const PARTITION_NAME_COLUMN_TYPE: ValueType;
const PARTITION_IDENTIFIER: ColumnDeclaration = ColumnDeclaration::new(
std::borrow::Cow::Borrowed(Self::PARTITION_NAME_COLUMN),
Self::PARTITION_NAME_COLUMN_TYPE,
);
const PARTITION_TYPE: ColumnDeclaration = ColumnDeclaration::new(
std::borrow::Cow::Borrowed(Self::PARTITION_VALUE_COLUMN),
PartitionValue::to_valuetype(Self::PARTITION_VALUE_COLUMN_TYPE),
);
const COLUMNS: &'static [ColumnDeclaration] =
&[Self::PARTITION_IDENTIFIER, Self::PARTITION_TYPE];
fn columns() -> ColumnDeclarations {
ColumnDeclarations(vec![
Self::partition_name_column(),
Self::partition_value_column(),
])
}
fn partition_name_column() -> ColumnDeclaration {
ColumnDeclaration::new(
Self::PARTITION_NAME_COLUMN.to_string(),
*Self::PARTITION_NAME_COLUMN_TYPE,
)
}

fn partition_value_column() -> ColumnDeclaration {
let value_type: &ValueType = Self::PARTITION_VALUE_COLUMN_TYPE.into();
ColumnDeclaration::new(Self::PARTITION_VALUE_COLUMN.to_string(), *value_type)
ColumnDeclarations(Self::COLUMNS.to_vec())
}
}
14 changes: 7 additions & 7 deletions src/shadow_tables/root_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ impl PartitionType for RootTable {
/// integer value in seconds. E.G 3600 if the interval was set to 1 hour.
const PARTITION_VALUE_COLUMN: &'static str = "partition_value";
/// The data type of the partition value column, indicating the nature of partitioning (e.g., time intervals).
const PARTITION_VALUE_COLUMN_TYPE: &'static PartitionValue = &PartitionValue::Interval;
const PARTITION_VALUE_COLUMN_TYPE: PartitionValue = PartitionValue::Interval;
/// The data type of the partition name column, typically text for naming partitions.
const PARTITION_NAME_COLUMN_TYPE: &'static ValueType = &ValueType::Text;
const PARTITION_NAME_COLUMN_TYPE: ValueType = ValueType::Text;
}

impl RootTable {
Expand All @@ -77,7 +77,7 @@ impl RootTable {
) -> ExtResult<Self> {
let table_name = Self::format_name(base_name);
let columns = <Self as PartitionType>::columns();
let schema = <Self as Create>::schema(db, table_name.to_string(), columns)?;
let schema = <Self as Create>::schema(db, table_name, columns)?;
let table = Self {
partition_column,
interval,
Expand Down Expand Up @@ -114,9 +114,9 @@ impl RootTable {
for index in 0..column_count {
let column = row.index_mut(index);
let name = column.name()?;
if name.eq(<Self as PartitionType>::partition_name_column().get_name()) {
if name.eq(<Self as PartitionType>::COLUMNS[0].get_name()) {
partition_column = column.get_str()?.to_owned();
} else if name.eq(<Self as PartitionType>::partition_value_column().get_name()) {
} else if name.eq(<Self as PartitionType>::COLUMNS[1].get_name()) {
interval = column.get_i64();
}
}
Expand All @@ -137,8 +137,8 @@ impl RootTable {
///
/// Returns a boolean indicating success of the insertion.
fn insert(&self, db: &Connection) -> ExtResult<bool> {
let partition_name_column = Self::partition_name_column().get_name().to_owned();
let partition_value_column = Self::partition_value_column().get_name().to_owned();
let partition_name_column = Self::COLUMNS[0].get_name().to_owned();
let partition_value_column = Self::COLUMNS[1].get_name().to_owned();
let sql = format!(
"INSERT INTO {} ({partition_name_column}, {partition_value_column}) VALUES (?, ?);",
self.name()
Expand Down
7 changes: 4 additions & 3 deletions src/types/column_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::Cow,
fmt::{self, Display},
vec,
};
Expand Down Expand Up @@ -48,15 +49,15 @@ impl<'a> From<&'a ColumnDeclaration> for PartitionColumn {
/// and whether it serves as a partition column.
#[derive(Clone, Debug)]
pub struct ColumnDeclaration {
name: String,
name: Cow<'static, str>,
data_type: ValueType,
is_partition_column: bool,
is_hidden: bool,
}

impl ColumnDeclaration {
/// Constructs a new `ColumnDeclaration`.
pub const fn new(name: String, data_type: ValueType) -> Self
pub const fn new(name: Cow<'static, str>, data_type: ValueType) -> Self
where
Self: Sized,
{
Expand Down Expand Up @@ -116,7 +117,7 @@ impl<'a> TryFrom<&'a str> for ColumnDeclaration {
}

Ok(Self {
name: tokens[0].trim().to_string(),
name: Cow::Owned(tokens[0].trim().to_string()),
data_type: parse_value_type(&tokens[1].trim().to_uppercase())?,
is_partition_column,
is_hidden: false,
Expand Down

0 comments on commit 4110285

Please sign in to comment.