]> git.sesse.net Git - nageru/commitdiff
Remove frame files that do not exist from the database.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 29 Nov 2018 19:53:07 +0000 (20:53 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 29 Nov 2018 19:53:07 +0000 (20:53 +0100)
db.cpp
db.h
main.cpp

diff --git a/db.cpp b/db.cpp
index e928e961bf268d55323024ad7b4a1a0496b519cc..39fd55776c40c9f094e84bfbbb4aed906b65fed6 100644 (file)
--- a/db.cpp
+++ b/db.cpp
@@ -249,3 +249,76 @@ void DB::store_frame_file(const string &filename, size_t size, const vector<Fram
                exit(1);
        }
 }
                exit(1);
        }
 }
+
+void DB::clean_unused_frame_files(const vector<string> &used_filenames)
+{
+       int ret = sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "BEGIN: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+
+       ret = sqlite3_exec(db, R"(
+               CREATE TEMPORARY TABLE used_filenames ( filename VARCHAR NOT NULL PRIMARY KEY )
+       )", nullptr, nullptr, nullptr);
+
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "CREATE TEMPORARY TABLE: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+
+       // Insert the new rows.
+       sqlite3_stmt *stmt;
+       ret = sqlite3_prepare_v2(db, "INSERT INTO used_filenames (filename) VALUES (?)", -1, &stmt, 0);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+
+       for (const string &filename : used_filenames) {
+               sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
+
+               ret = sqlite3_step(stmt);
+               if (ret == SQLITE_ROW) {
+                       fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+                       exit(1);
+               }
+
+               ret = sqlite3_reset(stmt);
+               if (ret == SQLITE_ROW) {
+                       fprintf(stderr, "INSERT reset: %s\n", sqlite3_errmsg(db));
+                       exit(1);
+               }
+       }
+
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+
+       ret = sqlite3_exec(db, R"(
+               DELETE FROM filev2 WHERE filename NOT IN ( SELECT filename FROM used_filenames )
+       )", nullptr, nullptr, nullptr);
+
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "DELETE: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+
+       ret = sqlite3_exec(db, R"(
+               DROP TABLE used_filenames
+       )", nullptr, nullptr, nullptr);
+
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "DROP TABLE: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+
+       // Commit.
+       ret = sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "COMMIT: %s\n", sqlite3_errmsg(db));
+               exit(1);
+       }
+}
diff --git a/db.h b/db.h
index 88196c7526407e1df24e80d5f6b4c4bd4122d74a..f8032c0e43d3502360fc74346de0ea87684a26f5 100644 (file)
--- a/db.h
+++ b/db.h
@@ -23,6 +23,7 @@ public:
        };
        std::vector<FrameOnDiskAndStreamIdx> load_frame_file(const std::string &filename, size_t size, unsigned frame_idx);  // Empty = none found, or there were no frames.
        void store_frame_file(const std::string &filename, size_t size, const std::vector<FrameOnDiskAndStreamIdx> &frames);
        };
        std::vector<FrameOnDiskAndStreamIdx> load_frame_file(const std::string &filename, size_t size, unsigned frame_idx);  // Empty = none found, or there were no frames.
        void store_frame_file(const std::string &filename, size_t size, const std::vector<FrameOnDiskAndStreamIdx> &frames);
+       void clean_unused_frame_files(const std::vector<std::string> &used_filenames);
 
 private:
        StateProto state;
 
 private:
        StateProto state;
index 95b3ff8cc85dc137df6bcb34496dc38d0f8a22c2..e0518afb95421d3ad906cc9a910f2dee01aef9d0 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -435,6 +435,8 @@ void load_existing_frames()
                sort(frames[stream_idx].begin(), frames[stream_idx].end(),
                        [](const auto &a, const auto &b) { return a.pts < b.pts; });
        }
                sort(frames[stream_idx].begin(), frames[stream_idx].end(),
                        [](const auto &a, const auto &b) { return a.pts < b.pts; });
        }
+
+       db.clean_unused_frame_files(frame_basenames);
 }
 
 int record_thread_func()
 }
 
 int record_thread_func()