File indexing completed on 2025-03-09 03:52:59
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2017-07-07 0007 * Description : Similarity database interface. 0008 * 0009 * SPDX-FileCopyrightText: 2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> 0010 * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0011 * SPDX-FileCopyrightText: 2017 by Swati Lodha <swatilodha27 at gmail dot com> 0012 * SPDX-FileCopyrightText: 2018 by Mario Frank <mario dot frank at uni minus potsdam dot de> 0013 * 0014 * SPDX-License-Identifier: GPL-2.0-or-later 0015 * 0016 * ============================================================ */ 0017 0018 #ifndef DIGIKAM_SIMILARITY_DB_H 0019 #define DIGIKAM_SIMILARITY_DB_H 0020 0021 // Qt includes 0022 0023 #include <QStringList> 0024 #include <QString> 0025 #include <QList> 0026 #include <QPair> 0027 #include <QSet> 0028 0029 // Local includes 0030 0031 #include "dbenginesqlquery.h" 0032 #include "similaritydbbackend.h" 0033 #include "similaritydbaccess.h" 0034 #include "digikam_export.h" 0035 #include "iteminfo.h" 0036 0037 namespace Digikam 0038 { 0039 0040 enum class FuzzyAlgorithm 0041 { 0042 Unknown = 0, 0043 Haar = 1, 0044 TfIdf = 2 0045 }; 0046 0047 0048 class DIGIKAM_DATABASE_EXPORT SimilarityDb 0049 { 0050 public: 0051 0052 /** 0053 * Set the database setting entry given by keyword to the given value. 0054 * @param keyword The keyword, i.e. setting name. 0055 * @param value The value. 0056 * @return True, if the value was set and false, else.. 0057 */ 0058 bool setSetting(const QString& keyword, const QString& value); 0059 0060 /** 0061 * Returns the setting with the keyword name. 0062 * @param keyword The setting entry name. 0063 * @return The setting value. 0064 */ 0065 QString getSetting(const QString& keyword); 0066 0067 /** 0068 * Returns the legacy settings with the keyword name. 0069 * @param keyword The setting entry name. 0070 * @return The setting value. 0071 */ 0072 QString getLegacySetting(const QString& keyword); 0073 0074 /** 0075 * This method returns all image ids that are present in the similarity db tables. 0076 * \return a set of all present image ids. 0077 */ 0078 QSet<qlonglong> registeredImageIds() const; 0079 0080 // ----------- Methods for fingerprint (ImageHaarMatrix) table access ---------- 0081 0082 /** 0083 * This method checks if the given image has a fingerprint for the given algorithm. 0084 * 0085 * @param imageId The Id of the image to check. 0086 * @param algorithm The algorithm. 0087 * @return True, if there is a fingerprint. 0088 */ 0089 bool hasFingerprint(qlonglong imageId, FuzzyAlgorithm algorithm) const; 0090 0091 /** 0092 * This method checks if there are any fingerprints for any algorithm present. 0093 * @return True, if fingerprints exist. 0094 */ 0095 bool hasFingerprints(); 0096 0097 /** 0098 * This method checks if there are any fingerprints for the given algorithm. 0099 * @param algorithm The algorithm. 0100 * @return true, if there are fingerprints and false, otherwise. 0101 */ 0102 bool hasFingerprints(FuzzyAlgorithm algorithm) const; 0103 0104 /** 0105 * Checks if the given image has a dirty fingerprint or even none for the given algorithm. 0106 * 0107 * @param imageInfo The image info object representing the item. 0108 * @param algorithm The algorithm used for the fingerprint. 0109 * @return True, if the image either has no or a dirty fingerprint. 0110 */ 0111 bool hasDirtyOrMissingFingerprint(const ItemInfo& imageInfo, 0112 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar) const; 0113 0114 /** 0115 * Returns a list of all item ids (images, videos,...) where either no fingerprint for the given 0116 * algorithm exists or is outdated because the file is identified as changed since 0117 * the generation of the fingerprint. 0118 * @param imageInfos The image info objects representing the items. 0119 * @param algorithm The algorithm. 0120 * @return The ids of the items whose fingerprints are dirty or missing. 0121 */ 0122 QList<qlonglong> getDirtyOrMissingFingerprints(const QList<ItemInfo>& imageInfos, 0123 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0124 0125 /** 0126 * Returns a list of the URLs of all items (images, videos,...) where either no fingerprint for the given 0127 * algorithm exists or is outdated because the file is identified as changed since 0128 * the generation of the fingerprint. 0129 * @param imageInfos The image info objects representing the items. 0130 * @param algorithm The algorithm. 0131 * @return The URLs of the items whose fingerprints are dirty or missing. 0132 */ 0133 QStringList getDirtyOrMissingFingerprintURLs(const QList<ItemInfo>& imageInfos, 0134 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0135 0136 /** 0137 * This method removes the fingerprint entry for the given imageId and algorithm. 0138 * Also, this automatically removes the entries in the ImageSimilarities table for the 0139 * given algorithm and image id. 0140 * @param imageID The image id. 0141 * @param algorithm The algorithm. 0142 */ 0143 void removeImageFingerprint(qlonglong imageID, 0144 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0145 0146 /** 0147 * Copies all similarity-specific information, from image srcId to destId. 0148 */ 0149 void copySimilarityAttributes(qlonglong srcId, 0150 qlonglong destId); 0151 0152 // ----------- Methods for image similarity table access ---------- 0153 0154 /** 0155 * Returns the similarity value for two images. 0156 * A value of -1 means nonexistence. 0157 * A value of -2 means that there is a value that cannot be converted into a double 0158 */ 0159 double getImageSimilarity(qlonglong imageID1, 0160 qlonglong imageID2, 0161 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0162 0163 void setImageSimilarity(qlonglong imageID1, 0164 qlonglong imageID2, 0165 double value, 0166 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0167 0168 /** 0169 * This method removes the image similarity entries for the imageID and algorithm. 0170 * @param imageID The image id. 0171 * @param algorithm The algorithm. 0172 */ 0173 void removeImageSimilarity(qlonglong imageID, 0174 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0175 0176 /** 0177 * This method removes the image similarity entry for the imageIDs and algorithm. 0178 * @param imageID1 The first image id. 0179 * @param imageID2 The second image id. 0180 * @param algorithm The algorithm. 0181 */ 0182 void removeImageSimilarity(qlonglong imageID1, 0183 qlonglong imageID2, 0184 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0185 0186 /** 0187 * This method removes all image similarity entries for the algorithm. 0188 * @param algorithm The algorithm. 0189 */ 0190 void clearImageSimilarity(FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0191 0192 /** 0193 * Returns the algorithms for which a similarity value exists for the given image ids. 0194 * @param imageID1 The first image id. 0195 * @param imageID2 The second image id. 0196 * @return a list of all algorithms for which a similarity value exists. 0197 */ 0198 QList<FuzzyAlgorithm> getImageSimilarityAlgorithms(qlonglong imageID1, 0199 qlonglong imageID2); 0200 0201 // ----------- Database shrinking and integrity check methods ---------- 0202 0203 /** 0204 * This method checks the integrity of the similarity database. 0205 * @return true, if the integrity check was passed and false, else. 0206 */ 0207 bool integrityCheck(); 0208 0209 /** 0210 * This method shrinks the database. 0211 */ 0212 void vacuum(); 0213 0214 private: 0215 0216 /** 0217 * This private variant of getImageSimilarity assumes that imageID1 <= imageID2 0218 * @param imageID1 the id of the first image. 0219 * @param imageID2 the id of the second image. 0220 * @param algorithm the algorithm 0221 * @return An empty string, if no similarity value exists for the ids and the algorithm, and the value as string, else. 0222 */ 0223 QString getImageSimilarityOrdered(qlonglong imageID1, 0224 qlonglong imageID2, 0225 FuzzyAlgorithm algorithm = FuzzyAlgorithm::Haar); 0226 0227 /** 0228 * This method applies a partial ordering to id1 and id2, i.e. 0229 * the result is a pair where the first is id1 iff id1 <= id2 and id2 otherwise. 0230 * @param id1 The first id. 0231 * @param id2 The second id. 0232 * @return A pair in which the ids are in partial ascending order. 0233 */ 0234 QPair<qlonglong, qlonglong> orderIds(qlonglong id1, qlonglong id2); 0235 0236 /** 0237 * The constructor of the similarity db class 0238 * @param backend The database backend. 0239 */ 0240 explicit SimilarityDb(SimilarityDbBackend* const backend); 0241 0242 /** 0243 * The destructor of the similarity db class. 0244 */ 0245 ~SimilarityDb(); 0246 0247 private: 0248 0249 // Disable 0250 SimilarityDb(const SimilarityDb&) = delete; 0251 SimilarityDb& operator=(const SimilarityDb&) = delete; 0252 0253 class Private; 0254 Private* const d; 0255 0256 friend class SimilarityDbAccess; 0257 }; 0258 0259 } // namespace Digikam 0260 0261 #endif // DIGIKAM_SIMILARITY_DB_H