diff options
-rw-r--r-- | signature_db.cpp | 22 | ||||
-rw-r--r-- | signature_db.hpp | 6 | ||||
-rw-r--r-- | tests/testdrive_sqlite.cpp | 8 |
3 files changed, 21 insertions, 15 deletions
diff --git a/signature_db.cpp b/signature_db.cpp index 22b675c..607d1ad 100644 --- a/signature_db.cpp +++ b/signature_db.cpp @@ -4,7 +4,7 @@ #include "signature_db.hpp" -const int SIGDB_VERSION = 1; +const int SIGDB_VERSION = 2; enum batch_status { @@ -39,15 +39,15 @@ void signature_db_priv::init_db() sqlite3_exec(db, R"sql( create table images( - id int primary key, + id integer primary key, path text, signature text ); )sql", nullptr, nullptr, nullptr); sqlite3_exec(db, R"sql( create table subslices( - image int, - slice int, + image integer, + slice integer, slicesig text, primary key (image, slice), foreign key (image) references images (id) @@ -58,8 +58,8 @@ void signature_db_priv::init_db() )sql", nullptr, nullptr, nullptr); sqlite3_exec(db, R"sql( create table dupes( - id1 int, - id2 int, + id1 integer, + id2 integer, dist real, primary key (id1, id2), foreign key (id1, id2) references images (id, id) @@ -123,13 +123,16 @@ signature_db::~signature_db() bool signature_db::valid() { return static_cast<bool>(p->db); } -void signature_db::put_signature(size_t id, const fs::path &path, const signature &sig) +size_t signature_db::put_signature(const fs::path &path, const signature &sig,size_t id) { - if (!p->db) [[ unlikely ]] return; + if (!p->db) [[ unlikely ]] return ~size_t(0); sqlite3_stmt *st; std::string sigs = sig.to_string(); sqlite3_prepare_v2(p->db, "insert into images (id, path, signature) values(?, ?, ?);", -1, &st, 0); - sqlite3_bind_int(st, 1, id); + if (!~id) + sqlite3_bind_null(st, 1); + else + sqlite3_bind_int(st, 1, id); #if PATH_VALSIZE == 2 sqlite3_bind_text16(st, 2, path.c_str(), -1, nullptr); #else @@ -138,6 +141,7 @@ void signature_db::put_signature(size_t id, const fs::path &path, const signatur sqlite3_bind_text(st, 3, sigs.c_str(), -1, nullptr); sqlite3_step(st); sqlite3_finalize(st); + return static_cast<size_t>(sqlite3_last_insert_rowid(p->db)); } std::pair<fs::path, signature> signature_db::get_signature(size_t id) diff --git a/signature_db.hpp b/signature_db.hpp index 5b8f890..f83971d 100644 --- a/signature_db.hpp +++ b/signature_db.hpp @@ -36,8 +36,10 @@ public: bool valid(); //insert image signature into database - //id must be unique - void put_signature(size_t id, const fs::path &path, const signature &sig); + //if id is omitted, it's assigned automatically and returned + //if specificted, id must be unique + //treat automatically assigned id as arbitrary opaque integers + size_t put_signature(const fs::path &path, const signature &sig, size_t id = ~size_t(0)); //get image signature from database std::pair<fs::path, signature> get_signature(size_t id); diff --git a/tests/testdrive_sqlite.cpp b/tests/testdrive_sqlite.cpp index 168d974..02f9259 100644 --- a/tests/testdrive_sqlite.cpp +++ b/tests/testdrive_sqlite.cpp @@ -213,6 +213,8 @@ void job_func(int thid, size_t id) sdb->lock(); std::set<size_t> v; + size_t dbid = sdb->put_signature(files[id], ss.full); + sdb->batch_find_subslice_begin(); for (size_t i = 0; i < nsliceh * nslicev; ++i) { @@ -225,7 +227,7 @@ void job_func(int thid, size_t id) std::tie(std::ignore, othersig) = sdb->get_signature(match.id); double dist = ss.full.distance(othersig); if (dist < threshold) - sdb->put_dupe_pair(id, match.id, dist); + sdb->put_dupe_pair(dbid, match.id, dist); } } } @@ -233,11 +235,9 @@ void job_func(int thid, size_t id) sdb->batch_put_subslice_begin(); for (size_t i = 0; i < nsliceh * nslicev; ++i) - sdb->put_subslice(id, i, ss.subslices[i]); + sdb->put_subslice(dbid, i, ss.subslices[i]); sdb->batch_end(); - sdb->put_signature(id, files[id], ss.full); - sdb->unlock(); } |