db: switch to sqlite as the primary backend

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a69648f81d0d094e11a3e0f0a19d3b8eccd5d
This commit is contained in:
raf 2025-08-12 18:54:07 +03:00
commit 4f725425fc
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
12 changed files with 246 additions and 274 deletions

View file

@ -1,4 +1,4 @@
use crate::db::{Entry, SledClipboardDb, detect_mime, u64_to_ivec};
use crate::db::{Entry, SqliteClipboardDb, detect_mime};
use log::{error, info};
use std::io::{self, BufRead};
@ -6,31 +6,27 @@ pub trait ImportCommand {
fn import_tsv(&self, input: impl io::Read);
}
impl ImportCommand for SledClipboardDb {
impl ImportCommand for SqliteClipboardDb {
fn import_tsv(&self, input: impl io::Read) {
let reader = io::BufReader::new(input);
let mut imported = 0;
for line in reader.lines().map_while(Result::ok) {
let mut parts = line.splitn(2, '\t');
if let (Some(id_str), Some(val)) = (parts.next(), parts.next()) {
if let Ok(id) = id_str.parse::<u64>() {
if let Ok(_id) = id_str.parse::<u64>() {
let entry = Entry {
contents: val.as_bytes().to_vec(),
mime: detect_mime(val.as_bytes()),
};
let enc = match rmp_serde::encode::to_vec(&entry) {
Ok(enc) => enc,
Err(e) => {
error!("Failed to encode entry for id {id}: {e}");
continue;
}
};
match self.db.insert(u64_to_ivec(id), enc) {
match self.conn.execute(
"INSERT INTO clipboard (contents, mime) VALUES (?1, ?2)",
rusqlite::params![entry.contents, entry.mime],
) {
Ok(_) => {
imported += 1;
info!("Imported entry with id {id}");
info!("Imported entry from TSV");
}
Err(e) => error!("Failed to insert entry with id {id}: {e}"),
Err(e) => error!("Failed to insert entry: {e}"),
}
} else {
error!("Failed to parse id from line: {id_str}");
@ -39,6 +35,6 @@ impl ImportCommand for SledClipboardDb {
error!("Malformed TSV line: {line:?}");
}
}
info!("Imported {imported} records from TSV into sled database.");
info!("Imported {imported} records from TSV into SQLite database.");
}
}