6 #include <unordered_set>
10 DB::DB(const string &filename)
12 int ret = sqlite3_open(filename.c_str(), &db);
13 if (ret != SQLITE_OK) {
14 fprintf(stderr, "%s: %s\n", filename.c_str(), sqlite3_errmsg(db));
18 // Set an effectively infinite timeout for waiting for write locks;
19 // if we get SQLITE_LOCKED, we just exit out, so this is much better.
20 ret = sqlite3_busy_timeout(db, 3600000);
21 if (ret != SQLITE_OK) {
22 fprintf(stderr, "sqlite3_busy_timeout: %s\n", sqlite3_errmsg(db));
27 CREATE TABLE IF NOT EXISTS state (state BLOB);
29 nullptr, nullptr, nullptr); // Ignore errors.
31 sqlite3_exec(db, "CREATE UNIQUE INDEX only_one_state ON state (1);", nullptr, nullptr, nullptr); // Ignore errors.
34 CREATE TABLE IF NOT EXISTS settings (settings BLOB);
36 nullptr, nullptr, nullptr); // Ignore errors.
38 sqlite3_exec(db, "CREATE UNIQUE INDEX only_one_settings ON settings (1);", nullptr, nullptr, nullptr); // Ignore errors.
43 nullptr, nullptr, nullptr); // Ignore errors.
48 nullptr, nullptr, nullptr); // Ignore errors.
51 CREATE TABLE IF NOT EXISTS filev2 (
52 file INTEGER NOT NULL PRIMARY KEY,
53 filename VARCHAR NOT NULL UNIQUE,
58 nullptr, nullptr, nullptr); // Ignore errors.
60 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
61 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
64 StateProto DB::get_state()
69 int ret = sqlite3_prepare_v2(db, "SELECT state FROM state", -1, &stmt, 0);
70 if (ret != SQLITE_OK) {
71 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
75 ret = sqlite3_step(stmt);
76 if (ret == SQLITE_ROW) {
77 bool ok = state.ParseFromArray(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
79 fprintf(stderr, "State in database is corrupted!\n");
82 } else if (ret != SQLITE_DONE) {
83 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
87 ret = sqlite3_finalize(stmt);
88 if (ret != SQLITE_OK) {
89 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
96 void DB::store_state(const StateProto &state)
99 state.SerializeToString(&serialized);
101 int ret = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
102 if (ret != SQLITE_OK) {
103 fprintf(stderr, "BEGIN: %s\n", sqlite3_errmsg(db));
108 ret = sqlite3_prepare_v2(db, "REPLACE INTO state VALUES (?)", -1, &stmt, 0);
109 if (ret != SQLITE_OK) {
110 fprintf(stderr, "REPLACE prepare: %s\n", sqlite3_errmsg(db));
114 sqlite3_bind_blob(stmt, 1, serialized.data(), serialized.size(), SQLITE_STATIC);
116 ret = sqlite3_step(stmt);
117 if (ret == SQLITE_ROW) {
118 fprintf(stderr, "REPLACE step: %s\n", sqlite3_errmsg(db));
122 ret = sqlite3_finalize(stmt);
123 if (ret != SQLITE_OK) {
124 fprintf(stderr, "REPLACE finalize: %s\n", sqlite3_errmsg(db));
128 ret = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
129 if (ret != SQLITE_OK) {
130 fprintf(stderr, "COMMIT: %s\n", sqlite3_errmsg(db));
135 SettingsProto DB::get_settings()
137 SettingsProto settings;
140 int ret = sqlite3_prepare_v2(db, "SELECT settings FROM settings", -1, &stmt, 0);
141 if (ret != SQLITE_OK) {
142 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
146 ret = sqlite3_step(stmt);
147 if (ret == SQLITE_ROW) {
148 bool ok = settings.ParseFromArray(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
150 fprintf(stderr, "State in database is corrupted!\n");
153 } else if (ret != SQLITE_DONE) {
154 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
158 ret = sqlite3_finalize(stmt);
159 if (ret != SQLITE_OK) {
160 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
167 void DB::store_settings(const SettingsProto &settings)
170 settings.SerializeToString(&serialized);
172 int ret = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
173 if (ret != SQLITE_OK) {
174 fprintf(stderr, "BEGIN: %s\n", sqlite3_errmsg(db));
179 ret = sqlite3_prepare_v2(db, "REPLACE INTO settings VALUES (?)", -1, &stmt, 0);
180 if (ret != SQLITE_OK) {
181 fprintf(stderr, "REPLACE prepare: %s\n", sqlite3_errmsg(db));
185 sqlite3_bind_blob(stmt, 1, serialized.data(), serialized.size(), SQLITE_STATIC);
187 ret = sqlite3_step(stmt);
188 if (ret == SQLITE_ROW) {
189 fprintf(stderr, "REPLACE step: %s\n", sqlite3_errmsg(db));
193 ret = sqlite3_finalize(stmt);
194 if (ret != SQLITE_OK) {
195 fprintf(stderr, "REPLACE finalize: %s\n", sqlite3_errmsg(db));
199 ret = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
200 if (ret != SQLITE_OK) {
201 fprintf(stderr, "COMMIT: %s\n", sqlite3_errmsg(db));
206 vector<DB::FrameOnDiskAndStreamIdx> DB::load_frame_file(const string &filename, size_t size, unsigned filename_idx)
208 FileContentsProto file_contents;
211 int ret = sqlite3_prepare_v2(db, "SELECT frames FROM filev2 WHERE filename=? AND size=?", -1, &stmt, 0);
212 if (ret != SQLITE_OK) {
213 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
217 sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
218 sqlite3_bind_int64(stmt, 2, size);
220 ret = sqlite3_step(stmt);
221 if (ret == SQLITE_ROW) {
222 bool ok = file_contents.ParseFromArray(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
224 fprintf(stderr, "Frame list in database is corrupted!\n");
227 } else if (ret != SQLITE_DONE) {
228 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
232 ret = sqlite3_finalize(stmt);
233 if (ret != SQLITE_OK) {
234 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
238 vector<FrameOnDiskAndStreamIdx> frames;
239 for (const StreamContentsProto &stream : file_contents.stream()) {
240 FrameOnDiskAndStreamIdx frame;
241 frame.stream_idx = stream.stream_idx();
242 for (int i = 0; i < stream.pts_size(); ++i) {
243 frame.frame.filename_idx = filename_idx;
244 frame.frame.pts = stream.pts(i);
245 frame.frame.offset = stream.offset(i);
246 frame.frame.size = stream.file_size(i);
247 if (i < stream.audio_size_size()) {
248 frame.frame.audio_size = stream.audio_size(i);
250 frame.frame.audio_size = 0;
252 frames.push_back(frame);
259 void DB::store_frame_file(const string &filename, size_t size, const vector<FrameOnDiskAndStreamIdx> &frames)
261 int ret = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
262 if (ret != SQLITE_OK) {
263 fprintf(stderr, "BEGIN: %s\n", sqlite3_errmsg(db));
267 // Delete any existing instances with this filename.
270 // Create the protobuf blob for the new row.
271 FileContentsProto file_contents;
272 unordered_set<unsigned> seen_stream_idx; // Usually only one.
273 for (const FrameOnDiskAndStreamIdx &frame : frames) {
274 seen_stream_idx.insert(frame.stream_idx);
276 for (unsigned stream_idx : seen_stream_idx) {
277 StreamContentsProto *stream = file_contents.add_stream();
278 stream->set_stream_idx(stream_idx);
279 stream->mutable_pts()->Reserve(frames.size());
280 stream->mutable_offset()->Reserve(frames.size());
281 stream->mutable_file_size()->Reserve(frames.size());
282 stream->mutable_audio_size()->Reserve(frames.size());
283 for (const FrameOnDiskAndStreamIdx &frame : frames) {
284 if (frame.stream_idx != stream_idx) {
287 stream->add_pts(frame.frame.pts);
288 stream->add_offset(frame.frame.offset);
289 stream->add_file_size(frame.frame.size);
290 stream->add_audio_size(frame.frame.audio_size);
294 file_contents.SerializeToString(&serialized);
296 // Insert the new row.
297 ret = sqlite3_prepare_v2(db, "REPLACE INTO filev2 (filename, size, frames) VALUES (?, ?, ?)", -1, &stmt, 0);
298 if (ret != SQLITE_OK) {
299 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
303 sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
304 sqlite3_bind_int64(stmt, 2, size);
305 sqlite3_bind_blob(stmt, 3, serialized.data(), serialized.size(), SQLITE_STATIC);
307 ret = sqlite3_step(stmt);
308 if (ret == SQLITE_ROW) {
309 fprintf(stderr, "REPLACE step: %s\n", sqlite3_errmsg(db));
313 ret = sqlite3_finalize(stmt);
314 if (ret != SQLITE_OK) {
315 fprintf(stderr, "REPLACE finalize: %s\n", sqlite3_errmsg(db));
320 ret = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
321 if (ret != SQLITE_OK) {
322 fprintf(stderr, "COMMIT: %s\n", sqlite3_errmsg(db));
327 void DB::clean_unused_frame_files(const vector<string> &used_filenames)
329 int ret = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
330 if (ret != SQLITE_OK) {
331 fprintf(stderr, "BEGIN: %s\n", sqlite3_errmsg(db));
335 ret = sqlite3_exec(db, R"(
336 CREATE TEMPORARY TABLE used_filenames ( filename VARCHAR NOT NULL PRIMARY KEY )
338 nullptr, nullptr, nullptr);
340 if (ret != SQLITE_OK) {
341 fprintf(stderr, "CREATE TEMPORARY TABLE: %s\n", sqlite3_errmsg(db));
345 // Insert the new rows.
347 ret = sqlite3_prepare_v2(db, "INSERT INTO used_filenames (filename) VALUES (?)", -1, &stmt, 0);
348 if (ret != SQLITE_OK) {
349 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
353 for (const string &filename : used_filenames) {
354 sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
356 ret = sqlite3_step(stmt);
357 if (ret == SQLITE_ROW) {
358 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
362 ret = sqlite3_reset(stmt);
363 if (ret == SQLITE_ROW) {
364 fprintf(stderr, "INSERT reset: %s\n", sqlite3_errmsg(db));
369 ret = sqlite3_finalize(stmt);
370 if (ret != SQLITE_OK) {
371 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
375 ret = sqlite3_exec(db, R"(
376 DELETE FROM filev2 WHERE filename NOT IN ( SELECT filename FROM used_filenames )
378 nullptr, nullptr, nullptr);
380 if (ret != SQLITE_OK) {
381 fprintf(stderr, "DELETE: %s\n", sqlite3_errmsg(db));
385 ret = sqlite3_exec(db, R"(
386 DROP TABLE used_filenames
388 nullptr, nullptr, nullptr);
390 if (ret != SQLITE_OK) {
391 fprintf(stderr, "DROP TABLE: %s\n", sqlite3_errmsg(db));
396 ret = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
397 if (ret != SQLITE_OK) {
398 fprintf(stderr, "COMMIT: %s\n", sqlite3_errmsg(db));