use rusqlite::params;
use crate::error::Result;
use super::Store;
impl Store {
pub fn count_metadata_before(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM metadata
WHERE target_type != 'project' AND last_timestamp < ?1",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn count_list_values_before(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM list_values
WHERE timestamp < ?1
AND metadata_id IN (
SELECT rowid FROM metadata WHERE target_type != 'project'
)",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn count_tombstones_before(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM tombstones
WHERE tombstone_type = 'metadata'
AND target_type != 'project' AND timestamp < ?1",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn count_set_tombstones_before(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM tombstones
WHERE tombstone_type = 'set_member'
AND target_type != 'project' AND timestamp < ?1",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn count_log_entries_before(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM metadata_log
WHERE target_type != 'project' AND timestamp < ?1",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn count_metadata_remaining(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM metadata
WHERE target_type = 'project' OR last_timestamp >= ?1",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn count_list_values_remaining(&self, cutoff_ms: i64) -> Result<u64> {
Ok(self.conn.query_row(
"SELECT COUNT(*) FROM list_values
WHERE timestamp >= ?1
OR metadata_id IN (
SELECT rowid FROM metadata WHERE target_type = 'project'
)",
params![cutoff_ms],
|row| row.get::<_, i64>(0),
)? as u64)
}
pub fn prune_metadata_before(&self, cutoff_ms: i64) -> Result<u64> {
self.conn.execute(
"DELETE FROM list_values
WHERE metadata_id IN (
SELECT rowid FROM metadata
WHERE target_type != 'project' AND last_timestamp < ?1
)",
params![cutoff_ms],
)?;
self.conn.execute(
"DELETE FROM set_values
WHERE metadata_id IN (
SELECT rowid FROM metadata
WHERE target_type != 'project' AND last_timestamp < ?1
)",
params![cutoff_ms],
)?;
self.conn.execute(
"DELETE FROM list_values
WHERE timestamp < ?1
AND metadata_id IN (
SELECT rowid FROM metadata WHERE target_type != 'project'
)",
params![cutoff_ms],
)?;
let deleted = self.conn.execute(
"DELETE FROM metadata
WHERE target_type != 'project' AND last_timestamp < ?1",
params![cutoff_ms],
)?;
Ok(deleted as u64)
}
pub fn prune_tombstones_before(&self, cutoff_ms: i64) -> Result<u64> {
let deleted = self.conn.execute(
"DELETE FROM tombstones
WHERE tombstone_type = 'metadata'
AND target_type != 'project' AND timestamp < ?1",
params![cutoff_ms],
)?;
Ok(deleted as u64)
}
pub fn prune_set_tombstones_before(&self, cutoff_ms: i64) -> Result<u64> {
let deleted = self.conn.execute(
"DELETE FROM tombstones
WHERE tombstone_type = 'set_member'
AND target_type != 'project' AND timestamp < ?1",
params![cutoff_ms],
)?;
Ok(deleted as u64)
}
pub fn prune_log_before(&self, cutoff_ms: i64) -> Result<u64> {
let deleted = self.conn.execute(
"DELETE FROM metadata_log
WHERE target_type != 'project' AND timestamp < ?1",
params![cutoff_ms],
)?;
Ok(deleted as u64)
}
pub fn imported_trail_ids(&self) -> Result<std::collections::HashSet<String>> {
let mut ids = std::collections::HashSet::new();
let mut stmt = self.conn.prepare(
"SELECT value FROM metadata WHERE key = 'review:trail-id' AND target_type = 'branch'",
)?;
let rows = stmt.query_map([], |row| row.get::<_, String>(0))?;
for row in rows {
let val = row?;
if let Ok(s) = serde_json::from_str::<String>(&val) {
ids.insert(s);
}
}
Ok(ids)
}
}