Update `sqlite` dependency

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-11-05 20:51:24 +01:00
parent 8fe949b2a0
commit 609d05fff2
No known key found for this signature in database
7 changed files with 98 additions and 92 deletions

4
Cargo.lock generated
View File

@ -1962,9 +1962,9 @@ dependencies = [
[[package]] [[package]]
name = "sqlite" name = "sqlite"
version = "0.27.3" version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e66cb949f931ece6201d72bffad3f3601b94998a345793713dd13af70a77c185" checksum = "2d7dcfcc5c3247141692d47227842b3e9c3cf7fe8dd78d91786d76eb3f27b717"
dependencies = [ dependencies = [
"libc", "libc",
"sqlite3-sys", "sqlite3-sys",

View File

@ -16,7 +16,7 @@ ssh = ["base64", "radicle-ssh", "sha2", "ssh-key"]
ed25519-compact = { version = "1.0.12", features = ["pem"] } ed25519-compact = { version = "1.0.12", features = ["pem"] }
multibase = { version = "0.9.1" } multibase = { version = "0.9.1" }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
sqlite = { version = "0.27.0", optional = true } sqlite = { version = "0.28.1", optional = true }
thiserror = { version = "1" } thiserror = { version = "1" }
zeroize = { version = "1.5.7" } zeroize = { version = "1.5.7" }

View File

@ -342,10 +342,17 @@ impl sqlite::ValueInto for PublicKey {
} }
} }
#[cfg(feature = "sqlite")]
impl From<PublicKey> for sqlite::Value {
fn from(pk: PublicKey) -> Self {
sqlite::Value::String(pk.to_human())
}
}
#[cfg(feature = "sqlite")] #[cfg(feature = "sqlite")]
impl sqlite::Bindable for &PublicKey { impl sqlite::Bindable for &PublicKey {
fn bind(self, stmt: &mut sqlite::Statement<'_>, i: usize) -> sqlite::Result<()> { fn bind(self, stmt: &mut sqlite::Statement<'_>, i: usize) -> sqlite::Result<()> {
self.to_human().as_str().bind(stmt, i) sqlite::Value::from(*self).bind(stmt, i)
} }
} }

View File

@ -19,7 +19,7 @@ log = { version = "0.4.17", features = ["std"] }
nakamoto-net = { version = "0.3.0" } nakamoto-net = { version = "0.3.0" }
nakamoto-net-poll = { version = "0.3.0" } nakamoto-net-poll = { version = "0.3.0" }
nonempty = { version = "0.8.0", features = ["serialize"] } nonempty = { version = "0.8.0", features = ["serialize"] }
sqlite = { version = "0.27.0" } sqlite = { version = "0.28.1" }
sqlite3-src = { version = "0.4.0", features = ["bundled"] } # Ensures static linking sqlite3-src = { version = "0.4.0", features = ["bundled"] } # Ensures static linking
scrypt = { version = "0.10.0", default-features = false } scrypt = { version = "0.10.0", default-features = false }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }

View File

@ -57,29 +57,28 @@ impl Book {
impl Store for Book { impl Store for Book {
fn get(&self, node: &NodeId) -> Result<Option<types::Node>, Error> { fn get(&self, node: &NodeId) -> Result<Option<types::Node>, Error> {
let row = self let mut stmt = self
.db .db
.prepare("SELECT features, alias, timestamp FROM nodes WHERE id = ?")? .prepare("SELECT features, alias, timestamp FROM nodes WHERE id = ?")?;
.bind(1, node)?
.into_cursor()
.next();
if let Some(Ok(row)) = row { stmt.bind(1, node)?;
let features = row.get::<node::Features, _>(0);
let alias = row.get::<String, _>(1); if let Some(Ok(row)) = stmt.into_cursor().next() {
let timestamp = row.get::<i64, _>(2) as Timestamp; let features = row.get::<node::Features, _>("features");
let alias = row.get::<String, _>("alias");
let timestamp = row.get::<i64, _>("timestamp") as Timestamp;
let mut addrs = Vec::new(); let mut addrs = Vec::new();
let mut stmt = self let mut stmt = self
.db .db
.prepare("SELECT type, value, source FROM addresses WHERE node = ?")? .prepare("SELECT type, value, source FROM addresses WHERE node = ?")?;
.bind(1, node)? stmt.bind(1, node)?;
.into_cursor();
while let Some(Ok(row)) = stmt.next() { for row in stmt.into_cursor() {
let _typ = row.get::<AddressType, _>(0); let row = row?;
let addr = row.get::<Address, _>(1); let _typ = row.get::<AddressType, _>("type");
let source = row.get::<Source, _>(2); let addr = row.get::<Address, _>("value");
let source = row.get::<Source, _>("source");
addrs.push(KnownAddress { addrs.push(KnownAddress {
addr, addr,
@ -121,35 +120,34 @@ impl Store for Book {
timestamp: Timestamp, timestamp: Timestamp,
addrs: impl IntoIterator<Item = KnownAddress>, addrs: impl IntoIterator<Item = KnownAddress>,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
self.db let mut stmt = self.db.prepare(
.prepare( "INSERT INTO nodes (id, features, alias, timestamp)
"INSERT INTO nodes (id, features, alias, timestamp) VALUES (?1, ?2, ?3, ?4)
VALUES (?1, ?2, ?3, ?4) ON CONFLICT DO UPDATE
ON CONFLICT DO UPDATE SET features = ?2, alias = ?3, timestamp = ?4
SET features = ?2, alias = ?3, timestamp = ?4 WHERE timestamp < ?4",
WHERE timestamp < ?4", )?;
)?
.bind(1, node)? stmt.bind(1, node)?;
.bind(2, features)? stmt.bind(2, features)?;
.bind(3, alias)? stmt.bind(3, alias)?;
.bind(4, timestamp as i64)? stmt.bind(4, timestamp as i64)?;
.next()?; stmt.next()?;
for addr in addrs { for addr in addrs {
self.db let mut stmt = self.db.prepare(
.prepare( "INSERT INTO addresses (node, type, value, source, timestamp)
"INSERT INTO addresses (node, type, value, source, timestamp) VALUES (?1, ?2, ?3, ?4, ?5)
VALUES (?1, ?2, ?3, ?4, ?5) ON CONFLICT DO UPDATE
ON CONFLICT DO UPDATE SET timestamp = ?5
SET timestamp = ?5 WHERE timestamp < ?5",
WHERE timestamp < ?5", )?;
)? stmt.bind(1, node)?;
.bind(1, node)? stmt.bind(2, AddressType::from(&addr.addr))?;
.bind(2, AddressType::from(&addr.addr))? stmt.bind(3, addr.addr)?;
.bind(3, addr.addr)? stmt.bind(4, addr.source)?;
.bind(4, addr.source)? stmt.bind(5, timestamp as i64)?;
.bind(5, timestamp as i64)? stmt.next()?;
.next()?;
} }
Ok(self.db.change_count() > 0) Ok(self.db.change_count() > 0)
} }
@ -157,12 +155,15 @@ impl Store for Book {
fn remove(&mut self, node: &NodeId) -> Result<bool, Error> { fn remove(&mut self, node: &NodeId) -> Result<bool, Error> {
self.db self.db
.prepare("DELETE FROM nodes WHERE id = ?")? .prepare("DELETE FROM nodes WHERE id = ?")?
.bind(1, node)? .into_cursor()
.next()?; .bind(&[(*node).into()])?
.next();
self.db self.db
.prepare("DELETE FROM addresses WHERE node = ?")? .prepare("DELETE FROM addresses WHERE node = ?")?
.bind(1, node)? .into_cursor()
.next()?; .bind(&[(*node).into()])?
.next();
Ok(self.db.change_count() > 0) Ok(self.db.change_count() > 0)
} }
@ -175,10 +176,10 @@ impl Store for Book {
let mut entries = Vec::new(); let mut entries = Vec::new();
while let Some(Ok(row)) = stmt.next() { while let Some(Ok(row)) = stmt.next() {
let node = row.get(0); let node = row.get("node");
let _typ = row.get::<AddressType, _>(1); let _typ = row.get::<AddressType, _>("type");
let addr = row.get::<Address, _>(2); let addr = row.get::<Address, _>("value");
let source = row.get::<Source, _>(3); let source = row.get::<Source, _>("source");
entries.push(( entries.push((
node, node,

View File

@ -73,45 +73,42 @@ impl Store for Table {
fn get(&self, id: &Id) -> Result<HashSet<NodeId>, Error> { fn get(&self, id: &Id) -> Result<HashSet<NodeId>, Error> {
let mut stmt = self let mut stmt = self
.db .db
.prepare("SELECT (node) FROM routing WHERE resource = ?")? .prepare("SELECT (node) FROM routing WHERE resource = ?")?;
.bind(1, id)? stmt.bind(1, id)?;
.into_cursor();
let mut nodes = HashSet::new();
while let Some(Ok(row)) = stmt.next() { let mut nodes = HashSet::new();
nodes.insert(row.get::<NodeId, _>(0)); for row in stmt.into_cursor() {
nodes.insert(row?.get::<NodeId, _>("node"));
} }
Ok(nodes) Ok(nodes)
} }
fn entry(&self, id: &Id, node: &NodeId) -> Result<Option<Timestamp>, Error> { fn entry(&self, id: &Id, node: &NodeId) -> Result<Option<Timestamp>, Error> {
let row = self let mut stmt = self
.db .db
.prepare("SELECT (time) FROM routing WHERE resource = ? AND node = ?")? .prepare("SELECT (time) FROM routing WHERE resource = ? AND node = ?")?;
.bind(1, id)?
.bind(2, node)?
.into_cursor()
.next();
if let Some(Ok(row)) = row { stmt.bind(1, id)?;
return Ok(Some(row.get::<i64, _>(0) as Timestamp)); stmt.bind(2, node)?;
if let Some(Ok(row)) = stmt.into_cursor().next() {
return Ok(Some(row.get::<i64, _>("time") as Timestamp));
} }
Ok(None) Ok(None)
} }
fn insert(&mut self, id: Id, node: NodeId, time: Timestamp) -> Result<bool, Error> { fn insert(&mut self, id: Id, node: NodeId, time: Timestamp) -> Result<bool, Error> {
let time: i64 = time.try_into().map_err(|_| Error::TimeOverflow)?; let time: i64 = time.try_into().map_err(|_| Error::TimeOverflow)?;
let mut stmt = self.db.prepare(
"INSERT INTO routing (resource, node, time)
VALUES (?, ?, ?)
ON CONFLICT DO NOTHING",
)?;
self.db stmt.bind(1, &id)?;
.prepare( stmt.bind(2, &node)?;
"INSERT INTO routing (resource, node, time) stmt.bind(3, time)?;
VALUES (?, ?, ?) stmt.next()?;
ON CONFLICT DO NOTHING",
)?
.bind(1, &id)?
.bind(2, &node)?
.bind(3, time)?
.next()?;
Ok(self.db.change_count() > 0) Ok(self.db.change_count() > 0)
} }
@ -124,8 +121,8 @@ impl Store for Table {
let mut entries = Vec::new(); let mut entries = Vec::new();
while let Some(Ok(row)) = stmt.next() { while let Some(Ok(row)) = stmt.next() {
let id = row.get(0); let id = row.get("resource");
let node = row.get(1); let node = row.get("node");
entries.push((id, node)); entries.push((id, node));
} }
@ -133,22 +130,23 @@ impl Store for Table {
} }
fn remove(&mut self, id: &Id, node: &NodeId) -> Result<bool, Error> { fn remove(&mut self, id: &Id, node: &NodeId) -> Result<bool, Error> {
self.db let mut stmt = self
.prepare("DELETE FROM routing WHERE resource = ? AND node = ?")? .db
.bind(1, id)? .prepare("DELETE FROM routing WHERE resource = ? AND node = ?")?;
.bind(2, node)?
.next()?; stmt.bind(1, id)?;
stmt.bind(2, node)?;
stmt.next()?;
Ok(self.db.change_count() > 0) Ok(self.db.change_count() > 0)
} }
fn prune(&mut self, oldest: Timestamp) -> Result<usize, Error> { fn prune(&mut self, oldest: Timestamp) -> Result<usize, Error> {
let oldest: i64 = oldest.try_into().map_err(|_| Error::TimeOverflow)?; let oldest: i64 = oldest.try_into().map_err(|_| Error::TimeOverflow)?;
let mut stmt = self.db.prepare("DELETE FROM routing WHERE time < ?")?;
self.db stmt.bind(1, oldest)?;
.prepare("DELETE FROM routing WHERE time < ?")? stmt.next()?;
.bind(1, oldest)?
.next()?;
Ok(self.db.change_count()) Ok(self.db.change_count())
} }

View File

@ -26,7 +26,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] } serde_json = { version = "1", features = ["preserve_order"] }
siphasher = { version = "0.3.10" } siphasher = { version = "0.3.10" }
radicle-git-ext = { version = "0", features = ["serde"] } radicle-git-ext = { version = "0", features = ["serde"] }
sqlite = { version = "0.27.0", optional = true } sqlite = { version = "0.28.1", optional = true }
nonempty = { version = "0.8.0", features = ["serialize"] } nonempty = { version = "0.8.0", features = ["serialize"] }
tempfile = { version = "3.3.0" } tempfile = { version = "3.3.0" }
thiserror = { version = "1" } thiserror = { version = "1" }