Skip to content

Commit

Permalink
fix: simplify skip_content table
Browse files Browse the repository at this point in the history
  • Loading branch information
Terkwood committed Jun 14, 2022
1 parent f9062e1 commit f2feb41
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zeditor"
version = "0.1.2-a"
version = "0.1.2-b"
edition = "2021"

[dependencies]
Expand Down
23 changes: 9 additions & 14 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::env::ZEDITOR_HOME;
use crate::replace::Replacement;
use crate::skip::SkipContent;
use rusqlite::{params, Connection, Result};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -27,8 +26,7 @@ impl Db {
hash BLOB NOT NULL,
start INTEGER NOT NULL,
end INTEGER NOT NULL,
search TEXT NOT NULL,
PRIMARY KEY (hash, start, end, search)
PRIMARY KEY (hash, start, end)
) STRICT",
[],
)?;
Expand All @@ -52,31 +50,28 @@ impl Db {

pub fn write_perm_skip(&self, skip: SkipContent) -> Result<()> {
self.conn.execute(
"INSERT INTO skip_content (hash, start, end, search)
VALUES (?1, ?2, ?3, ?4)",
params![skip.0.as_bytes(), skip.1.start, skip.1.end, skip.1.term],
"INSERT INTO skip_content (hash, start, end)
VALUES (?1, ?2, ?3)",
params![skip.hash.as_bytes(), skip.start, skip.end],
)?;
Ok(())
}

pub fn get_skip_contents(&self) -> Result<HashSet<SkipContent>> {
let mut stmt = self
.conn
.prepare("SELECT hash, start, end, search FROM skip_content")?;
.prepare("SELECT hash, start, end FROM skip_content")?;
let mut rows = stmt.query([])?;

let mut out = HashSet::new();
while let Some(row) = rows.next()? {
let hash_bytes: [u8; 32] = row.get(0)?;
let hash: blake3::Hash = hash_bytes.into();
out.insert(SkipContent(
out.insert(SkipContent {
hash,
Replacement {
start: row.get(1)?,
end: row.get(2)?,
term: row.get(3)?,
},
));
start: row.get(1)?,
end: row.get(2)?,
});
}

Ok(out)
Expand Down
12 changes: 9 additions & 3 deletions src/skip.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::db::Db;
use crate::replace::Replacement;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};

#[derive(Eq, Hash, PartialEq, Clone)]
pub struct SkipContent(pub blake3::Hash, pub Replacement);
pub struct SkipContent {
pub hash: blake3::Hash,
pub start: usize,
pub end: usize,
}

pub struct SkipRepo {
db: Arc<Mutex<Db>>,
Expand Down Expand Up @@ -36,6 +39,9 @@ impl SkipRepo {

impl From<crate::search::Hit> for SkipContent {
fn from(hit: crate::search::Hit) -> Self {
Self(hit.content_hash, hit.into())
let hash = hit.content_hash;
let start = hit.start;
let end = hit.end;
Self { hash, start, end }
}
}

0 comments on commit f2feb41

Please sign in to comment.