From fbe1231af0e96f20b91aaaf0ee1c212c14c641a5 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Wed, 28 Feb 2018 20:52:43 -0800 Subject: [PATCH] move open_id from recording_playback to recording I want to be able to use it in etags without having to do a full scan of the recording_playback in advance, which would greatly increase time to first byte. I probably will even use it in urls to ensure the segments they point to are stable. I haven't actually done this yet - it will wait until I implement serving unflushed recordings - but I want to get the schema set up properly. --- db/raw.rs | 20 +++++++++++--------- db/schema.sql | 13 +++++++------ src/cmds/upgrade/v1_to_v2.rs | 27 +++++++++++++++------------ src/cmds/upgrade/v2_to_v3.rs | 10 ++++------ 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/db/raw.rs b/db/raw.rs index 5c7b68a..0d677e8 100644 --- a/db/raw.rs +++ b/db/raw.rs @@ -38,17 +38,19 @@ use rusqlite; use std::ops::Range; const INSERT_RECORDING_SQL: &'static str = r#" - insert into recording (composite_id, stream_id, run_offset, flags, sample_file_bytes, - start_time_90k, duration_90k, local_time_delta_90k, video_samples, - video_sync_samples, video_sample_entry_id) - values (:composite_id, :stream_id, :run_offset, :flags, :sample_file_bytes, - :start_time_90k, :duration_90k, :local_time_delta_90k, - :video_samples, :video_sync_samples, :video_sample_entry_id) + insert into recording (composite_id, stream_id, open_id, run_offset, flags, + sample_file_bytes, start_time_90k, duration_90k, + local_time_delta_90k, video_samples, video_sync_samples, + video_sample_entry_id) + values (:composite_id, :stream_id, :open_id, :run_offset, :flags, + :sample_file_bytes, :start_time_90k, :duration_90k, + :local_time_delta_90k, :video_samples, :video_sync_samples, + :video_sample_entry_id) "#; const INSERT_RECORDING_PLAYBACK_SQL: &'static str = r#" - insert into recording_playback (composite_id, open_id, sample_file_sha1, video_index) - values (:composite_id, :open_id, :sample_file_sha1, :video_index) + insert into recording_playback (composite_id, sample_file_sha1, video_index) + values (:composite_id, :sample_file_sha1, :video_index) "#; const STREAM_MIN_START_SQL: &'static str = r#" @@ -98,6 +100,7 @@ pub(crate) fn insert_recording(tx: &rusqlite::Transaction, o: &db::Open, id: Com stmt.execute_named(&[ (":composite_id", &id.0), (":stream_id", &(id.stream() as i64)), + (":open_id", &o.id), (":run_offset", &r.run_offset), (":flags", &r.flags), (":sample_file_bytes", &r.sample_file_bytes), @@ -113,7 +116,6 @@ pub(crate) fn insert_recording(tx: &rusqlite::Transaction, o: &db::Open, id: Com let sha1 = &r.sample_file_sha1[..]; stmt.execute_named(&[ (":composite_id", &id.0), - (":open_id", &o.id), (":sample_file_sha1", &sha1), (":video_index", &r.video_index), ])?; diff --git a/db/schema.sql b/db/schema.sql index cf5936c..ba2d6ba 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -134,6 +134,12 @@ create table recording ( -- recording_cover rows (which match this id format) are typically 1--5 KiB. composite_id integer primary key, + -- The open in which this was committed to the database. For a given + -- composite_id, only one recording will ever be committed to the database, + -- but in-memory state may reflect a recording which never gets committed. + -- This field allows disambiguation in etags and such. + open_id integer not null references open (id), + -- This field is redundant with id above, but used to enforce the reference -- constraint and to structure the recording_start_time index. stream_id integer not null references stream (id), @@ -184,6 +190,7 @@ create index recording_cover on recording ( -- These fields are not used for ordering; they cover most queries so -- that only database verification and actual viewing of recordings need -- to consult the underlying row. + open_id, duration_90k, video_samples, video_sync_samples, @@ -202,12 +209,6 @@ create table recording_playback ( -- See description on recording table. composite_id integer primary key references recording (composite_id), - -- The open in which this was committed to the database. For a given - -- composite_id, only one recording will ever be committed to the database, - -- but in-memory state may reflect a recording which never gets committed. - -- This field allows disambiguation in etags and such. - open_id integer not null references open (id), - -- The sha1 hash of the contents of the sample file. sample_file_sha1 blob not null check (length(sample_file_sha1) = 20), diff --git a/src/cmds/upgrade/v1_to_v2.rs b/src/cmds/upgrade/v1_to_v2.rs index f34b4b5..7c6be61 100644 --- a/src/cmds/upgrade/v1_to_v2.rs +++ b/src/cmds/upgrade/v1_to_v2.rs @@ -167,6 +167,7 @@ impl<'a> super::Upgrader for U<'a> { create table recording ( composite_id integer primary key, stream_id integer not null references stream (id), + open_id integer not null, run_offset integer not null, flags integer not null, sample_file_bytes integer not null check (sample_file_bytes > 0), @@ -183,6 +184,7 @@ impl<'a> super::Upgrader for U<'a> { create index recording_cover on recording ( stream_id, start_time_90k, + open_id, duration_90k, video_samples, video_sync_samples, @@ -252,19 +254,20 @@ impl<'a> super::Upgrader for U<'a> { insert into recording select - composite_id, - camera_id, - run_offset, - flags, - sample_file_bytes, - start_time_90k, - duration_90k, - local_time_delta_90k, - video_samples, - video_sync_samples, - video_sample_entry_id + r.composite_id, + r.camera_id, + o.open_id, + r.run_offset, + r.flags, + r.sample_file_bytes, + r.start_time_90k, + r.duration_90k, + r.local_time_delta_90k, + r.video_samples, + r.video_sync_samples, + r.video_sample_entry_id from - old_recording; + old_recording r cross join open o; "#)?; fix_video_sample_entry(tx)?; diff --git a/src/cmds/upgrade/v2_to_v3.rs b/src/cmds/upgrade/v2_to_v3.rs index 51227c5..a8bc7ba 100644 --- a/src/cmds/upgrade/v2_to_v3.rs +++ b/src/cmds/upgrade/v2_to_v3.rs @@ -148,18 +148,16 @@ impl super::Upgrader for U { alter table recording_playback rename to old_recording_playback; create table recording_playback ( composite_id integer primary key references recording (composite_id), - open_id integer not null references open (id), sample_file_sha1 blob not null check (length(sample_file_sha1) = 20), video_index blob not null check (length(video_index) > 0) ); insert into recording_playback select - p.composite_id, - o.id, - p.sample_file_sha1, - p.video_index + composite_id, + sample_file_sha1, + video_index from - old_recording_playback p cross join open o; + old_recording_playback; drop table old_recording_playback; "#)?; Ok(())