Skip to content

Commit

Permalink
Refactor a bit. Add new column to Root table. Does not do anything ye…
Browse files Browse the repository at this point in the history
…t, but will be used for automatic deletion of old partitions
  • Loading branch information
nuuskamummu committed Nov 4, 2024
1 parent 4110285 commit 2b3604e
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/shadow_tables/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ impl<'vtab> VirtualTable<'vtab> {
column_declarations: ColumnDeclarations,
partition_column: String,
interval: i64,
expiration: Option<i64>,
) -> sqlite3_ext::Result<Self> {
Ok(VirtualTable {
connection: db,
base_name: name.to_string(),
lookup_table: LookupTable::create(db, name)?,
root_table: RootTable::create(db, name, partition_column, interval)?,
root_table: RootTable::create(db, name, partition_column, interval, expiration)?,
template_table: TemplateTable::create(db, name, column_declarations)?,
})
}
Expand Down Expand Up @@ -264,6 +265,7 @@ mod tests {
columns,
partition_column_name.to_string(),
interval,
None,
);
assert!(table.is_ok());
let table = table.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/shadow_tables/lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ mod tests {
ColumnDeclarations::from_iter(&["col1 timestamp partition_column", "col2 text"]);

let virtual_table =
VirtualTable::create(db, "test", declarations, "col1".to_string(), 3600).unwrap();
VirtualTable::create(db, "test", declarations, "col1".to_string(), 3600, None).unwrap();
virtual_table
}
#[test]
Expand Down
37 changes: 34 additions & 3 deletions src/shadow_tables/root_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use sqlite3_ext::FromValue;
use sqlite3_ext::Result as ExtResult;
use sqlite3_ext::ValueType;

use crate::ColumnDeclaration;
use crate::ColumnDeclarations;
use crate::PartitionColumn;

use super::operations::Connect;
use super::operations::Create;
use super::operations::Drop;
Expand All @@ -26,6 +30,8 @@ pub struct RootTable {
partition_column: String,
/// The interval at which new partitions are created.
interval: i64,
/// The Lifetime of each partition expressed as seconds
expiration: Option<i64>,
/// The schema declaration for the root table, detailing its structure.
schema: SchemaDeclaration,
}
Expand All @@ -48,13 +54,24 @@ impl PartitionType for RootTable {
/// The column name storing partition values, the specified interval will be stored here as a
/// 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: PartitionValue = PartitionValue::Interval;
/// The data type of the partition name column, typically text for naming partitions.
const PARTITION_NAME_COLUMN_TYPE: ValueType = ValueType::Text;
const COLUMNS: &'static [crate::ColumnDeclaration] = &[
Self::PARTITION_IDENTIFIER,
Self::PARTITION_TYPE,
ColumnDeclaration::new(
std::borrow::Cow::Borrowed(Self::PARTITION_EXPIRATION_COLUMN),
Self::PARTITION_EXPIRATION_COLUMN_TYPE,
),
];
}

impl RootTable {
const PARTITION_EXPIRATION_COLUMN: &'static str = "expiration_value";
const PARTITION_EXPIRATION_COLUMN_TYPE: ValueType = ValueType::Integer;
/// Accesses the partition column name.
pub fn partition_column(&self) -> &str {
&self.partition_column
Expand All @@ -74,13 +91,15 @@ impl RootTable {
base_name: &str,
partition_column: String,
interval: i64,
expiration: Option<i64>,
) -> 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, columns)?;
let table = Self {
partition_column,
interval,
expiration,
schema,
};
table.insert(db)?;
Expand Down Expand Up @@ -109,6 +128,7 @@ impl RootTable {
let query = format!("SELECT {columns} FROM {table_name}");
let mut partition_column: String = String::default();
let mut interval: i64 = 0i64;
let mut expiration: Option<i64> = None;
db.query_row(&query, (), |row| {
let column_count = row.len();
for index in 0..column_count {
Expand All @@ -118,6 +138,8 @@ impl RootTable {
partition_column = column.get_str()?.to_owned();
} else if name.eq(<Self as PartitionType>::COLUMNS[1].get_name()) {
interval = column.get_i64();
} else if name.eq(<Self as PartitionType>::COLUMNS[2].get_name()) {
expiration = Some(column.get_i64());
}
}
Ok(())
Expand All @@ -126,6 +148,7 @@ impl RootTable {
schema,
partition_column,
interval,
expiration,
})
}

Expand All @@ -139,12 +162,18 @@ impl RootTable {
fn insert(&self, db: &Connection) -> ExtResult<bool> {
let partition_name_column = Self::COLUMNS[0].get_name().to_owned();
let partition_value_column = Self::COLUMNS[1].get_name().to_owned();
let partition_expiration_column = Self::COLUMNS[2].get_name().to_owned();

let sql = format!(
"INSERT INTO {} ({partition_name_column}, {partition_value_column}) VALUES (?, ?);",
"INSERT INTO {} ({partition_name_column}, {partition_value_column}, {partition_expiration_column}) VALUES (?, ?, ?);",
self.name()
);

db.insert(&sql, params![self.partition_column, self.get_interval()])?;
db.insert(
&sql,
params![self.partition_column, self.get_interval(), ""], //TODO: Fix proper expiration
//handling
)?;
Ok(true)
}

Expand Down Expand Up @@ -173,6 +202,7 @@ mod tests {
"test",
"col".to_string(),
3600,
None,
);

assert_eq!(root_table.as_ref().unwrap().schema().name(), "test_root");
Expand All @@ -192,7 +222,8 @@ mod tests {
Err(err) => panic!("{}", err.to_string()),
};
let connection = Connection::from_rusqlite(&rusq_conn);
let root_table = RootTable::create(connection, "test", "col".to_string(), 3600).unwrap();
let root_table =
RootTable::create(connection, "test", "col".to_string(), 3600, None).unwrap();
root_table.insert(connection).unwrap();

let connected_table = RootTable::connect(connection, "test");
Expand Down
File renamed without changes.
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions src/types/column_declaration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod column;
pub mod columns;
pub mod expiration;
pub mod partition;
pub use column::ColumnDeclaration;
pub use column::ColumnDeclarations;
pub use column::PartitionColumn;
Empty file.
5 changes: 2 additions & 3 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sqlite3_ext::Blob;
use sqlite3_ext::{vtab::ConstraintOp, Value};

pub use self::column_declaration::{ColumnDeclaration, ColumnDeclarations, PartitionColumn};

mod column_declaration;
pub mod column_declaration;
pub mod constraints;
pub use column_declaration::*;

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Copy)]
#[serde(remote = "ConstraintOp")]
Expand Down
2 changes: 1 addition & 1 deletion src/vtab_interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mod tests {
let result_query = result.index_mut(0).get_str()?;
assert_eq!(
result_query,
"CREATE TABLE test_root (partition_column TEXT, partition_value INTEGER)"
"CREATE TABLE test_root (partition_column TEXT, partition_value INTEGER, expiration_value INTEGER)"
);
Ok(())
},
Expand Down
1 change: 1 addition & 0 deletions src/vtab_interface/operations/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ pub fn create_virtual_table<'a>(
columns,
partition_column.get_name().to_string(),
interval,
None,
)?)
}

0 comments on commit 2b3604e

Please sign in to comment.