Clean up
This commit is contained in:
parent
68ae92e4bf
commit
a6b584b6d3
4 changed files with 57 additions and 139 deletions
48
src/db.rs
48
src/db.rs
|
|
@ -11,7 +11,6 @@ use crate::error::AppError;
|
|||
pub struct Record {
|
||||
pub key: String,
|
||||
pub volumes: Vec<String>,
|
||||
pub path: String,
|
||||
pub size: Option<i64>,
|
||||
}
|
||||
|
||||
|
|
@ -63,12 +62,9 @@ fn create_tables(conn: &Connection) {
|
|||
CREATE TABLE IF NOT EXISTS kv (
|
||||
key TEXT PRIMARY KEY,
|
||||
volumes TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
size INTEGER,
|
||||
created_at INTEGER DEFAULT (unixepoch()),
|
||||
deleted INTEGER DEFAULT 0
|
||||
created_at INTEGER DEFAULT (unixepoch())
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_kv_deleted ON kv(deleted);
|
||||
",
|
||||
)
|
||||
.expect("failed to create tables");
|
||||
|
|
@ -120,22 +116,19 @@ impl ReadPool {
|
|||
// --- Read query functions ---
|
||||
|
||||
pub fn get(conn: &Connection, key: &str) -> Result<Record, AppError> {
|
||||
let mut stmt =
|
||||
conn.prepare_cached("SELECT key, volumes, path, size FROM kv WHERE key = ?1 AND deleted = 0")?;
|
||||
let mut stmt = conn.prepare_cached("SELECT key, volumes, size FROM kv WHERE key = ?1")?;
|
||||
Ok(stmt.query_row(params![key], |row| {
|
||||
let volumes_json: String = row.get(1)?;
|
||||
Ok(Record {
|
||||
key: row.get(0)?,
|
||||
volumes: parse_volumes(&volumes_json),
|
||||
path: row.get(2)?,
|
||||
size: row.get(3)?,
|
||||
size: row.get(2)?,
|
||||
})
|
||||
})?)
|
||||
}
|
||||
|
||||
pub fn list_keys(conn: &Connection, prefix: &str) -> Result<Vec<String>, AppError> {
|
||||
let mut stmt =
|
||||
conn.prepare_cached("SELECT key FROM kv WHERE key LIKE ?1 AND deleted = 0 ORDER BY key")?;
|
||||
let mut stmt = conn.prepare_cached("SELECT key FROM kv WHERE key LIKE ?1 ORDER BY key")?;
|
||||
let pattern = format!("{prefix}%");
|
||||
let keys = stmt
|
||||
.query_map(params![pattern], |row| row.get(0))?
|
||||
|
|
@ -144,16 +137,14 @@ pub fn list_keys(conn: &Connection, prefix: &str) -> Result<Vec<String>, AppErro
|
|||
}
|
||||
|
||||
pub fn all_records(conn: &Connection) -> Result<Vec<Record>, AppError> {
|
||||
let mut stmt =
|
||||
conn.prepare_cached("SELECT key, volumes, path, size FROM kv WHERE deleted = 0")?;
|
||||
let mut stmt = conn.prepare_cached("SELECT key, volumes, size FROM kv")?;
|
||||
let records = stmt
|
||||
.query_map([], |row| {
|
||||
let volumes_json: String = row.get(1)?;
|
||||
Ok(Record {
|
||||
key: row.get(0)?,
|
||||
volumes: parse_volumes(&volumes_json),
|
||||
path: row.get(2)?,
|
||||
size: row.get(3)?,
|
||||
size: row.get(2)?,
|
||||
})
|
||||
})?
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
|
@ -166,7 +157,6 @@ pub enum WriteCmd {
|
|||
Put {
|
||||
key: String,
|
||||
volumes: Vec<String>,
|
||||
path: String,
|
||||
size: Option<i64>,
|
||||
reply: oneshot::Sender<Result<(), AppError>>,
|
||||
},
|
||||
|
|
@ -175,27 +165,29 @@ pub enum WriteCmd {
|
|||
reply: oneshot::Sender<Result<(), AppError>>,
|
||||
},
|
||||
BulkPut {
|
||||
records: Vec<(String, Vec<String>, String, Option<i64>)>,
|
||||
records: Vec<(String, Vec<String>, Option<i64>)>,
|
||||
reply: oneshot::Sender<Result<(), AppError>>,
|
||||
},
|
||||
}
|
||||
|
||||
fn execute_cmd(conn: &Connection, cmd: WriteCmd) -> (Result<(), AppError>, oneshot::Sender<Result<(), AppError>>) {
|
||||
fn execute_cmd(
|
||||
conn: &Connection,
|
||||
cmd: WriteCmd,
|
||||
) -> (Result<(), AppError>, oneshot::Sender<Result<(), AppError>>) {
|
||||
match cmd {
|
||||
WriteCmd::Put {
|
||||
key,
|
||||
volumes,
|
||||
path,
|
||||
size,
|
||||
reply,
|
||||
} => {
|
||||
let volumes_json = encode_volumes(&volumes);
|
||||
let result = conn
|
||||
.prepare_cached(
|
||||
"INSERT INTO kv (key, volumes, path, size) VALUES (?1, ?2, ?3, ?4)
|
||||
ON CONFLICT(key) DO UPDATE SET volumes = ?2, path = ?3, size = ?4, deleted = 0",
|
||||
"INSERT INTO kv (key, volumes, size) VALUES (?1, ?2, ?3)
|
||||
ON CONFLICT(key) DO UPDATE SET volumes = ?2, size = ?3",
|
||||
)
|
||||
.and_then(|mut s| s.execute(params![key, volumes_json, path, size]))
|
||||
.and_then(|mut s| s.execute(params![key, volumes_json, size]))
|
||||
.map(|_| ())
|
||||
.map_err(AppError::from);
|
||||
(result, reply)
|
||||
|
|
@ -211,12 +203,12 @@ fn execute_cmd(conn: &Connection, cmd: WriteCmd) -> (Result<(), AppError>, onesh
|
|||
WriteCmd::BulkPut { records, reply } => {
|
||||
let result = (|| -> Result<(), AppError> {
|
||||
let mut stmt = conn.prepare_cached(
|
||||
"INSERT INTO kv (key, volumes, path, size) VALUES (?1, ?2, ?3, ?4)
|
||||
ON CONFLICT(key) DO UPDATE SET volumes = ?2, path = ?3, size = ?4, deleted = 0",
|
||||
"INSERT INTO kv (key, volumes, size) VALUES (?1, ?2, ?3)
|
||||
ON CONFLICT(key) DO UPDATE SET volumes = ?2, size = ?3",
|
||||
)?;
|
||||
for (key, volumes, path, size) in &records {
|
||||
for (key, volumes, size) in &records {
|
||||
let volumes_json = encode_volumes(volumes);
|
||||
stmt.execute(params![key, volumes_json, path, size])?;
|
||||
stmt.execute(params![key, volumes_json, size])?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
|
|
@ -237,7 +229,6 @@ impl WriterHandle {
|
|||
&self,
|
||||
key: String,
|
||||
volumes: Vec<String>,
|
||||
path: String,
|
||||
size: Option<i64>,
|
||||
) -> Result<(), AppError> {
|
||||
let (reply_tx, reply_rx) = oneshot::channel();
|
||||
|
|
@ -245,7 +236,6 @@ impl WriterHandle {
|
|||
.send(WriteCmd::Put {
|
||||
key,
|
||||
volumes,
|
||||
path,
|
||||
size,
|
||||
reply: reply_tx,
|
||||
})
|
||||
|
|
@ -268,7 +258,7 @@ impl WriterHandle {
|
|||
|
||||
pub async fn bulk_put(
|
||||
&self,
|
||||
records: Vec<(String, Vec<String>, String, Option<i64>)>,
|
||||
records: Vec<(String, Vec<String>, Option<i64>)>,
|
||||
) -> Result<(), AppError> {
|
||||
let (reply_tx, reply_rx) = oneshot::channel();
|
||||
self.tx
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue