Skip to content

Commit

Permalink
feat: add WAL, WITHOUT ROWID, cached statment
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed May 31, 2021
1 parent 4e27712 commit 5093825
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/cargo/sources/registry/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const TABLE_SUMMARIES: &'static str = "\
CREATE TABLE IF NOT EXISTS summaries (
name TEXT PRIMARY KEY NOT NULL,
contents BLOB NOT NULL
)";
) WITHOUT ROWID";

const INSERT_SUMMERIES: &'static str = "\
INSERT OR REPLACE INTO summaries (name, contents) VALUES (?, ?)";
Expand All @@ -25,6 +25,7 @@ impl Db {
let conn = Connection::open(path.as_ref())?;
conn.pragma_update(None, "locking_mode", &"EXCLUSIVE")?;
conn.pragma_update(None, "cache_size", &2048)?;
conn.pragma_update(None, "journal_mode", &"WAL")?;
conn.execute(TABLE_SUMMARIES, [])?;
Ok(Mutex::new(Self(conn)))
})
Expand All @@ -35,19 +36,21 @@ impl Db {
K: AsRef<[u8]>,
{
let key = key.as_ref();
Ok(self.0.query_row(
"SELECT contents FROM summaries WHERE name = ? LIMIT 1",
[key],
|row| row.get(0),
)?)
Ok(self
.0
.prepare_cached("SELECT contents FROM summaries WHERE name = ? LIMIT 1")?
.query_row([key], |row| row.get(0))?)
}

pub fn insert<K>(&self, key: K, value: &[u8]) -> CargoResult<()>
where
K: AsRef<[u8]>,
{
let key = key.as_ref();
let modified = self.0.execute(INSERT_SUMMERIES, params![key, value])?;
let modified = self
.0
.prepare_cached(INSERT_SUMMERIES)?
.execute(params![key, value])?;
log::debug!(
"insert {} record for {}",
modified,
Expand Down

0 comments on commit 5093825

Please sign in to comment.