From f922afaa268e9b299b400f79283aceda219d0806 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Mon, 17 May 2021 10:50:12 -0700 Subject: [PATCH] update rusqlite The big difference here is query_named and execute_named have gone away. Fair number of lines changes but straightforward. --- server/Cargo.lock | 45 ++++++++++++++---- server/Cargo.toml | 2 +- server/db/Cargo.toml | 2 +- server/db/auth.rs | 86 +++++++++++++++++------------------ server/db/db.rs | 22 ++++----- server/db/raw.rs | 24 +++++----- server/db/signal.rs | 2 +- server/db/upgrade/v0_to_v1.rs | 52 ++++++++++----------- server/db/upgrade/v1_to_v2.rs | 18 ++++---- server/db/upgrade/v4_to_v5.rs | 2 +- server/db/upgrade/v5_to_v6.rs | 6 +-- 11 files changed, 144 insertions(+), 117 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index 52e249c..5a5cd8a 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -32,6 +32,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "ahash" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f200cbb1e856866d9eade941cf3aa0c5d7dd36f74311c4273b494f4ef036957" +dependencies = [ + "getrandom 0.2.2", + "once_cell", + "version_check", +] + [[package]] name = "ansi_term" version = "0.9.0" @@ -805,13 +816,31 @@ dependencies = [ "ahash 0.4.7", ] +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash 0.7.2", +] + [[package]] name = "hashlink" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d99cf782f0dc4372d26846bec3de7804ceb5df083c2d4462c0b8d2330e894fa8" dependencies = [ - "hashbrown", + "hashbrown 0.9.1", +] + +[[package]] +name = "hashlink" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf" +dependencies = [ + "hashbrown 0.11.2", ] [[package]] @@ -947,7 +976,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.9.1", ] [[package]] @@ -1045,9 +1074,9 @@ dependencies = [ [[package]] name = "libsqlite3-sys" -version = "0.20.1" +version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64d31059f22935e6c31830db5249ba2b7ecd54fd73a9909286f0a67aa55c2fbd" +checksum = "290b64917f8b0cb885d9de0f9959fe1f775d7fa12f1da2db9001c1c8ab60f89d" dependencies = [ "cc", "pkg-config", @@ -1170,7 +1199,7 @@ dependencies = [ "failure", "fnv", "h264-reader", - "hashlink", + "hashlink 0.6.0", "itertools", "lazy_static", "libc", @@ -1931,14 +1960,14 @@ dependencies = [ [[package]] name = "rusqlite" -version = "0.24.2" +version = "0.25.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38ee71cbab2c827ec0ac24e76f82eca723cee92c509a65f67dee393c25112" +checksum = "57adcf67c8faaf96f3248c2a7b419a0dbc52ebe36ba83dd57fe83827c1ea4eb3" dependencies = [ "bitflags", "fallible-iterator", "fallible-streaming-iterator", - "hashlink", + "hashlink 0.7.0", "libsqlite3-sys", "memchr", "smallvec", diff --git a/server/Cargo.toml b/server/Cargo.toml index 1c922e9..d4bf34e 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -50,7 +50,7 @@ parking_lot = { version = "0.11.1", features = [] } protobuf = { git = "https://github.com/stepancheg/rust-protobuf" } reffers = "0.6.0" ring = "0.16.2" -rusqlite = "0.24.1" +rusqlite = "0.25.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" smallvec = "1.0" diff --git a/server/db/Cargo.toml b/server/db/Cargo.toml index 4cdeb3e..7c02656 100644 --- a/server/db/Cargo.toml +++ b/server/db/Cargo.toml @@ -34,7 +34,7 @@ pretty-hex = "0.2.1" prettydiff = "0.4.0" protobuf = { git = "https://github.com/stepancheg/rust-protobuf" } ring = "0.16.2" -rusqlite = "0.24.1" +rusqlite = "0.25.3" smallvec = "1.0" tempdir = "0.3" time = "0.1" diff --git a/server/db/auth.rs b/server/db/auth.rs index 8c43145..b22668c 100644 --- a/server/db/auth.rs +++ b/server/db/auth.rs @@ -14,7 +14,7 @@ use log::info; use parking_lot::Mutex; use protobuf::Message; use ring::rand::{SecureRandom, SystemRandom}; -use rusqlite::{params, Connection, Transaction}; +use rusqlite::{named_params, params, Connection, Transaction}; use std::collections::BTreeMap; use std::fmt; use std::net::IpAddr; @@ -374,7 +374,7 @@ impl State { let id = row.get(0)?; let name: String = row.get(1)?; let mut permissions = Permissions::new(); - permissions.merge_from_bytes(row.get_raw_checked(7)?.as_blob()?)?; + permissions.merge_from_bytes(row.get_ref(7)?.as_blob()?)?; state.users_by_id.insert( id, User { @@ -444,16 +444,16 @@ impl State { .permissions .write_to_bytes() .expect("proto3->vec is infallible"); - stmt.execute_named(&[ - (":username", &&change.username[..]), - (":password_hash", phash), - (":password_id", &pid), - (":password_failure_count", &pcount), - (":flags", &change.flags), - (":unix_uid", &change.unix_uid), - (":id", &id), - (":permissions", &permissions), - ])?; + stmt.execute(named_params! { + ":username": &change.username[..], + ":password_hash": phash, + ":password_id": &pid, + ":password_failure_count": &pcount, + ":flags": &change.flags, + ":unix_uid": &change.unix_uid, + ":id": &id, + ":permissions": &permissions, + })?; } let u = e.into_mut(); u.username = change.username; @@ -480,13 +480,13 @@ impl State { .permissions .write_to_bytes() .expect("proto3->vec is infallible"); - stmt.execute_named(&[ - (":username", &&change.username[..]), - (":password_hash", &password_hash), - (":flags", &change.flags), - (":unix_uid", &change.unix_uid), - (":permissions", &permissions), - ])?; + stmt.execute(named_params! { + ":username": &change.username[..], + ":password_hash": &password_hash, + ":flags": &change.flags, + ":unix_uid": &change.unix_uid, + ":permissions": &permissions, + })?; let id = conn.last_insert_rowid() as i32; self.users_by_name.insert(change.username.clone(), id); let e = self.users_by_id.entry(id); @@ -647,18 +647,18 @@ impl State { let permissions_blob = permissions .write_to_bytes() .expect("proto3->vec is infallible"); - stmt.execute_named(&[ - (":session_id_hash", &&hash.0[..]), - (":user_id", &user.id), - (":seed", &&seed[..]), - (":flags", &flags), - (":domain", &domain), - (":creation_password_id", &creation_password_id), - (":creation_time_sec", &creation.when_sec), - (":creation_user_agent", &creation.user_agent), - (":creation_peer_addr", &addr), - (":permissions", &permissions_blob), - ])?; + stmt.execute(named_params! { + ":session_id_hash": &hash.0[..], + ":user_id": &user.id, + ":seed": &seed[..], + ":flags": &flags, + ":domain": &domain, + ":creation_password_id": &creation_password_id, + ":creation_time_sec": &creation.when_sec, + ":creation_user_agent": &creation.user_agent, + ":creation_peer_addr": &addr, + ":permissions": &permissions_blob, + })?; let e = match sessions.entry(hash) { ::std::collections::hash_map::Entry::Occupied(_) => panic!("duplicate session hash!"), ::std::collections::hash_map::Entry::Vacant(e) => e, @@ -787,11 +787,11 @@ impl State { "flushing user with hash: {}", u.password_hash.as_ref().unwrap() ); - u_stmt.execute_named(&[ - (":password_failure_count", &u.password_failure_count), - (":password_hash", &u.password_hash), - (":id", &id), - ])?; + u_stmt.execute(named_params! { + ":password_failure_count": &u.password_failure_count, + ":password_hash": &u.password_hash, + ":id": &id, + })?; } for (_, s) in &self.sessions { if !s.dirty { @@ -799,12 +799,12 @@ impl State { } let addr = s.last_use.addr_buf(); let addr: Option<&[u8]> = addr.as_ref().map(|a| a.as_ref()); - s_stmt.execute_named(&[ - (":last_use_time_sec", &s.last_use.when_sec), - (":last_use_user_agent", &s.last_use.user_agent), - (":last_use_peer_addr", &addr), - (":use_count", &s.use_count), - ])?; + s_stmt.execute(named_params! { + ":last_use_time_sec": &s.last_use.when_sec, + ":last_use_user_agent": &s.last_use.user_agent, + ":last_use_peer_addr": &addr, + ":use_count": &s.use_count, + })?; } Ok(()) } @@ -866,7 +866,7 @@ fn lookup_session(conn: &Connection, hash: &SessionHash) -> Result 0 { new_ranges.entry(stream_id).or_insert(None); - stmt.execute_named(named_params! { + stmt.execute(named_params! { ":stream_id": stream_id, ":cum_recordings": s.cum_recordings + s.synced_recordings as i32, ":cum_media_duration_90k": s.cum_media_duration.0 + new_duration, @@ -1444,7 +1444,7 @@ impl LockedDatabase { RawEntryMut::Vacant(vacant) => { trace!("cache miss for recording {}", id); let mut stmt = self.conn.prepare_cached(GET_RECORDING_PLAYBACK_SQL)?; - let mut rows = stmt.query_named(named_params! {":composite_id": id.0})?; + let mut rows = stmt.query(named_params! {":composite_id": id.0})?; if let Some(row) = rows.next()? { let video_index: VideoIndex = row.get(0)?; let result = f(&RecordingPlayback { @@ -1715,7 +1715,7 @@ impl LockedDatabase { } let mut stmt = self.conn.prepare_cached(INSERT_VIDEO_SAMPLE_ENTRY_SQL)?; - stmt.execute_named(named_params! { + stmt.execute(named_params! { ":width": i32::from(entry.width), ":height": i32::from(entry.height), ":pasp_h_spacing": i32::from(entry.pasp_h_spacing), @@ -1851,7 +1851,7 @@ impl LockedDatabase { :password) "#, )?; - stmt.execute_named(named_params! { + stmt.execute(named_params! { ":uuid": uuid_bytes, ":short_name": &camera.short_name, ":description": &camera.description, @@ -1905,7 +1905,7 @@ impl LockedDatabase { id = :id "#, )?; - let rows = stmt.execute_named(named_params! { + let rows = stmt.execute(named_params! { ":id": camera_id, ":short_name": &camera.short_name, ":description": &camera.description, @@ -1945,14 +1945,14 @@ impl LockedDatabase { if stream.range.is_some() { bail!("Can't remove camera {}; has recordings.", id); } - let rows = stream_stmt.execute_named(named_params! {":id": stream_id})?; + let rows = stream_stmt.execute(named_params! {":id": stream_id})?; if rows != 1 { bail!("Stream {} missing from database", id); } streams_to_delete.push(*stream_id); } let mut cam_stmt = tx.prepare_cached(r"delete from camera where id = :id")?; - let rows = cam_stmt.execute_named(named_params! {":id": id})?; + let rows = cam_stmt.execute(named_params! {":id": id})?; if rows != 1 { bail!("Camera {} missing from database", id); } @@ -1987,7 +1987,7 @@ impl LockedDatabase { c.new_limit ); } - let rows = stmt.execute_named(named_params! { + let rows = stmt.execute(named_params! { ":record": c.new_record, ":retain": c.new_limit, ":id": c.stream_id, diff --git a/server/db/raw.rs b/server/db/raw.rs index f9814af..b7dc07e 100644 --- a/server/db/raw.rs +++ b/server/db/raw.rs @@ -106,7 +106,7 @@ pub(crate) fn list_recordings_by_time( f: &mut dyn FnMut(db::ListRecordingsRow) -> Result<(), Error>, ) -> Result<(), Error> { let mut stmt = conn.prepare_cached(LIST_RECORDINGS_BY_TIME_SQL)?; - let rows = stmt.query_named(named_params! { + let rows = stmt.query(named_params! { ":stream_id": stream_id, ":start_time_90k": desired_time.start.0, ":end_time_90k": desired_time.end.0, @@ -122,7 +122,7 @@ pub(crate) fn list_recordings_by_id( f: &mut dyn FnMut(db::ListRecordingsRow) -> Result<(), Error>, ) -> Result<(), Error> { let mut stmt = conn.prepare_cached(LIST_RECORDINGS_BY_ID_SQL)?; - let rows = stmt.query_named(named_params! { + let rows = stmt.query(named_params! { ":start": CompositeId::new(stream_id, desired_ids.start).0, ":end": CompositeId::new(stream_id, desired_ids.end).0, })?; @@ -190,7 +190,7 @@ pub(crate) fn insert_recording( "#, ) .with_context(|e| format!("can't prepare recording insert: {}", e))?; - stmt.execute_named(named_params! { + stmt.execute(named_params! { ":composite_id": id.0, ":stream_id": i64::from(id.stream()), ":open_id": o.id, @@ -228,7 +228,7 @@ pub(crate) fn insert_recording( 0 => None, _ => Some(r.local_time_delta.0), }; - stmt.execute_named(named_params! { + stmt.execute(named_params! { ":composite_id": id.0, ":local_time_delta_90k": delta, ":sample_file_blake3": blake3, @@ -243,7 +243,7 @@ pub(crate) fn insert_recording( "#, ) .with_context(|e| format!("can't prepare recording_playback insert: {}", e))?; - stmt.execute_named(named_params! { + stmt.execute(named_params! { ":composite_id": id.0, ":video_index": &r.video_index, }) @@ -298,7 +298,7 @@ pub(crate) fn delete_recordings( composite_id < :end "#, )?; - let n = insert.execute_named(named_params! { + let n = insert.execute(named_params! { ":sample_file_dir_id": sample_file_dir_id, ":start": ids.start.0, ":end": ids.end.0, @@ -307,7 +307,7 @@ pub(crate) fn delete_recordings( ":start": ids.start.0, ":end": ids.end.0, }; - let n_playback = del_playback.execute_named(p)?; + let n_playback = del_playback.execute(p)?; if n_playback != n { bail!( "inserted {} garbage rows but deleted {} recording_playback rows!", @@ -315,7 +315,7 @@ pub(crate) fn delete_recordings( n_playback ); } - let n_integrity = del_integrity.execute_named(p)?; + let n_integrity = del_integrity.execute(p)?; if n_integrity > n { // fewer is okay; recording_integrity is optional. bail!( @@ -324,7 +324,7 @@ pub(crate) fn delete_recordings( n_integrity ); } - let n_main = del_main.execute_named(p)?; + let n_main = del_main.execute(p)?; if n_main != n { bail!( "inserted {} garbage rows but deleted {} recording rows!", @@ -367,7 +367,7 @@ pub(crate) fn get_range( ) -> Result>, Error> { // The minimum is straightforward, taking advantage of the start_time_90k index. let mut stmt = conn.prepare_cached(STREAM_MIN_START_SQL)?; - let mut rows = stmt.query_named(named_params! {":stream_id": stream_id})?; + let mut rows = stmt.query(named_params! {":stream_id": stream_id})?; let min_start = match rows.next()? { Some(row) => recording::Time(row.get(0)?), None => return Ok(None), @@ -378,7 +378,7 @@ pub(crate) fn get_range( // last MAX_RECORDING_DURATION must be examined in order to take advantage of the // start_time_90k index. let mut stmt = conn.prepare_cached(STREAM_MAX_START_SQL)?; - let mut rows = stmt.query_named(named_params! {":stream_id": stream_id})?; + let mut rows = stmt.query(named_params! {":stream_id": stream_id})?; let mut maxes_opt = None; while let Some(row) = rows.next()? { let row_start = recording::Time(row.get(0)?); @@ -427,7 +427,7 @@ pub(crate) fn list_oldest_recordings( f: &mut dyn FnMut(db::ListOldestRecordingsRow) -> bool, ) -> Result<(), Error> { let mut stmt = conn.prepare_cached(LIST_OLDEST_RECORDINGS_SQL)?; - let mut rows = stmt.query_named(named_params! { + let mut rows = stmt.query(named_params! { ":start": start.0, ":end": CompositeId::new(start.stream() + 1, 0).0, })?; diff --git a/server/db/signal.rs b/server/db/signal.rs index 5cacbcc..cc7f3c1 100644 --- a/server/db/signal.rs +++ b/server/db/signal.rs @@ -722,7 +722,7 @@ impl State { while let Some(row) = rows.next()? { let time_90k = recording::Time(row.get(0)?); - let changes = row.get_raw_checked(1)?.as_blob()?; + let changes = row.get_ref(1)?.as_blob()?; let before = cur.clone(); let mut it = PointDataIterator::new(changes); while let Some((signal, state)) = it.next()? { diff --git a/server/db/upgrade/v0_to_v1.rs b/server/db/upgrade/v0_to_v1.rs index 725390f..ad06474 100644 --- a/server/db/upgrade/v0_to_v1.rs +++ b/server/db/upgrade/v0_to_v1.rs @@ -7,7 +7,7 @@ use crate::db; use crate::recording; use failure::Error; use log::warn; -use rusqlite::params; +use rusqlite::{named_params, params}; use std::collections::HashMap; pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> { @@ -179,32 +179,30 @@ fn fill_recording(tx: &rusqlite::Transaction) -> Result run_id, _ => composite_id, }; - insert1.execute_named(&[ - (":composite_id", &composite_id), - (":camera_id", &camera_id), - (":run_offset", &(composite_id - run_id)), - ( - ":flags", - &(if trailing_zero { + insert1.execute(named_params! { + ":composite_id": &composite_id, + ":camera_id": &camera_id, + ":run_offset": &(composite_id - run_id), + ":flags": &( + if trailing_zero { db::RecordingFlags::TrailingZero as i32 } else { 0 }), - ), - (":sample_file_bytes", &sample_file_bytes), - (":start_time_90k", &start_time_90k), - (":duration_90k", &duration_90k), - (":local_time_delta_90k", &local_time_delta_90k), - (":video_samples", &video_samples), - (":video_sync_samples", &video_sync_samples), - (":video_sample_entry_id", &video_sample_entry_id), - ])?; - insert2.execute_named(&[ - (":composite_id", &composite_id), - (":sample_file_uuid", &&sample_file_uuid.0.as_bytes()[..]), - (":sample_file_sha1", &sample_file_sha1), - (":video_index", &video_index), - ])?; + ":sample_file_bytes": &sample_file_bytes, + ":start_time_90k": &start_time_90k, + ":duration_90k": &duration_90k, + ":local_time_delta_90k": &local_time_delta_90k, + ":video_samples": &video_samples, + ":video_sync_samples": &video_sync_samples, + ":video_sample_entry_id": &video_sample_entry_id, + })?; + insert2.execute(named_params! { + ":composite_id": &composite_id, + ":sample_file_uuid": &sample_file_uuid.0.as_bytes()[..], + ":sample_file_sha1": &sample_file_sha1, + ":video_index": &video_index, + })?; camera_state.current_run = if trailing_zero { None } else { @@ -224,10 +222,10 @@ fn update_camera( "#, )?; for (ref id, ref state) in &camera_state { - stmt.execute_named(&[ - (":id", &id), - (":next_recording_id", &state.next_recording_id), - ])?; + stmt.execute(named_params! { + ":id": &id, + ":next_recording_id": &state.next_recording_id, + })?; } Ok(()) } diff --git a/server/db/upgrade/v1_to_v2.rs b/server/db/upgrade/v1_to_v2.rs index 41a79ec..71c0c20 100644 --- a/server/db/upgrade/v1_to_v2.rs +++ b/server/db/upgrade/v1_to_v2.rs @@ -8,7 +8,7 @@ use crate::schema::DirMeta; use failure::{bail, format_err, Error}; use nix::fcntl::{FlockArg, OFlag}; use nix::sys::stat::Mode; -use rusqlite::params; +use rusqlite::{named_params, params}; use std::os::unix::io::AsRawFd; use uuid::Uuid; @@ -397,14 +397,14 @@ fn fix_video_sample_entry(tx: &rusqlite::Transaction) -> Result<(), Error> { let mut rows = select.query(params![])?; while let Some(row) = rows.next()? { let data: Vec = row.get(4)?; - insert.execute_named(&[ - (":id", &row.get::<_, i32>(0)?), - (":sha1", &row.get::<_, Vec>(1)?), - (":width", &row.get::<_, i32>(2)?), - (":height", &row.get::<_, i32>(3)?), - (":rfc6381_codec", &rfc6381_codec_from_sample_entry(&data)?), - (":data", &data), - ])?; + insert.execute(named_params! { + ":id": &row.get::<_, i32>(0)?, + ":sha1": &row.get::<_, Vec>(1)?, + ":width": &row.get::<_, i32>(2)?, + ":height": &row.get::<_, i32>(3)?, + ":rfc6381_codec": &rfc6381_codec_from_sample_entry(&data)?, + ":data": &data, + })?; } Ok(()) } diff --git a/server/db/upgrade/v4_to_v5.rs b/server/db/upgrade/v4_to_v5.rs index 6b2f7fd..c2eec28 100644 --- a/server/db/upgrade/v4_to_v5.rs +++ b/server/db/upgrade/v4_to_v5.rs @@ -126,7 +126,7 @@ pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> )?; let mut rows = stmt.query(params![])?; while let Some(row) = rows.next()? { - let path = row.get_raw_checked(0)?.as_str()?; + let path = row.get_ref(0)?.as_str()?; info!("path: {}", path); let dir_uuid: FromSqlUuid = row.get(1)?; let open_id: Option = row.get(2)?; diff --git a/server/db/upgrade/v5_to_v6.rs b/server/db/upgrade/v5_to_v6.rs index 0efbb64..b94a310 100644 --- a/server/db/upgrade/v5_to_v6.rs +++ b/server/db/upgrade/v5_to_v6.rs @@ -102,7 +102,7 @@ pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> let id: i32 = row.get(0)?; let width: u16 = row.get::<_, i32>(1)?.try_into()?; let height: u16 = row.get::<_, i32>(2)?.try_into()?; - let rfc6381_codec: &str = row.get_raw_checked(3)?.as_str()?; + let rfc6381_codec: &str = row.get_ref(3)?.as_str()?; let mut data: Vec = row.get(4)?; let avcc = parse(&data)?; if avcc.num_of_sequence_parameter_sets() != 1 { @@ -132,7 +132,7 @@ pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> BigEndian::write_u32(&mut data[0..4], u32::try_from(len)?); } - insert.execute_named(named_params! { + insert.execute(named_params! { ":id": id, ":width": width, ":height": height, @@ -253,7 +253,7 @@ pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> cur_stream_id = Some(stream_id); } insert - .execute_named(named_params! { + .execute(named_params! { ":composite_id": composite_id, ":open_id": open_id, ":stream_id": stream_id,