DatabaseManager.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <cstdint>
  5. #include <optional>
  6. #include <sqlite3.h>
  7. // データベースに保存される画像情報を保持する構造体
  8. struct ImageData {
  9. int64_t id; // DBのプライマリキー
  10. std::string path; // 画像のファイルパス
  11. uint64_t dhash; // 計算済みのdHash値
  12. uint64_t phash; // 計算済みのpHash値
  13. int64_t timestamp; // ファイルの最終更新時刻(再計算判定用)
  14. int64_t file_size; // ファイルサイズ(再計算判定用)
  15. bool is_searched = false; // 検索完了ステータス(GUI等用)
  16. };
  17. // SQLiteを用いた画像メタデータとハッシュのDB管理クラス
  18. class DatabaseManager {
  19. public:
  20. DatabaseManager(const std::string& dbPath);
  21. ~DatabaseManager();
  22. // データベースとの接続を開く
  23. bool open();
  24. // データベースを閉じる
  25. void close();
  26. // 画像データをDBに追加または更新する
  27. bool addImage(const ImageData& data);
  28. // DB内の全画像情報を取得する
  29. std::vector<ImageData> getAllImages();
  30. // 指定されたディレクトリ群のいずれかに属する画像をSQLで高速に取得する
  31. std::vector<ImageData> getImagesInDirectories(const std::vector<std::string>& dirPaths);
  32. // 指定されたパスの画像をDBから削除する
  33. bool removeImage(const std::string& path);
  34. // 実体ファイルが削除済みの古いエントリをDBから消去する
  35. void cleanupStaleEntries();
  36. // トランザクション処理(大量追加時の高速化のため)
  37. void beginTransaction();
  38. void commitTransaction();
  39. void rollbackTransaction();
  40. // 単一パスの画像情報をDBから検索して返す
  41. std::optional<ImageData> getImageByPath(const std::string& path);
  42. // 指定ディレクトリ配下の全画像の検索完了状態を一括更新する
  43. bool setDirectorySearchedStatus(const std::string& dirPath, bool isSearched);
  44. private:
  45. std::string m_dbPath;
  46. sqlite3* m_db = nullptr;
  47. bool initSchema();
  48. };