version 2
This commit is contained in:
+57
-7
@@ -9,6 +9,11 @@ DatabaseManager::DatabaseManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_initialized(false)
|
||||
{
|
||||
// Initialize search cache
|
||||
m_searchCache.clear();
|
||||
|
||||
// Create index on ocr_text if it doesn't exist
|
||||
// This will be executed once the database is initialized
|
||||
}
|
||||
|
||||
DatabaseManager::~DatabaseManager()
|
||||
@@ -78,6 +83,10 @@ bool DatabaseManager::initialize(const QString &dbPath)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create an index on the ocr_text column if it doesn't exist
|
||||
// This will speed up text searches dramatically
|
||||
query.exec("CREATE INDEX IF NOT EXISTS idx_ocr_text ON ocr_results(ocr_text)");
|
||||
|
||||
m_initialized = true;
|
||||
qDebug() << "Database initialized successfully.";
|
||||
return true;
|
||||
@@ -98,6 +107,9 @@ QList<DatabaseManager::ImageItem> DatabaseManager::getAllImages()
|
||||
m_initialized = false;
|
||||
return images;
|
||||
}
|
||||
|
||||
// Start transaction to speed up query
|
||||
m_db.transaction();
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("SELECT full_path, ocr_text FROM ocr_results");
|
||||
@@ -108,6 +120,9 @@ QList<DatabaseManager::ImageItem> DatabaseManager::getAllImages()
|
||||
}
|
||||
|
||||
// Check if files exist as we add them
|
||||
// Reserve space for results to avoid reallocations
|
||||
images.reserve(query.size() > 0 ? query.size() : 100);
|
||||
|
||||
while (query.next()) {
|
||||
ImageItem item;
|
||||
item.filePath = query.value(0).toString();
|
||||
@@ -119,6 +134,7 @@ QList<DatabaseManager::ImageItem> DatabaseManager::getAllImages()
|
||||
}
|
||||
}
|
||||
|
||||
m_db.commit();
|
||||
return images;
|
||||
}
|
||||
|
||||
@@ -140,24 +156,43 @@ QList<DatabaseManager::ImageItem> DatabaseManager::searchImages(const QString &s
|
||||
|
||||
// If search text is empty, return all images
|
||||
if (searchText.isEmpty()) {
|
||||
// Clear the search cache when empty search is performed
|
||||
m_searchCache.clear();
|
||||
return getAllImages();
|
||||
}
|
||||
|
||||
// Check if we have a cached result for this search query
|
||||
if (m_searchCache.contains(searchText)) {
|
||||
return m_searchCache[searchText];
|
||||
}
|
||||
|
||||
// Start transaction to speed up queries
|
||||
m_db.transaction();
|
||||
|
||||
QSqlQuery query;
|
||||
// Use LIKE query with wildcards for flexible searching
|
||||
query.prepare("SELECT full_path, ocr_text FROM ocr_results WHERE ocr_text LIKE :search");
|
||||
|
||||
// Ensure search text is properly sanitized
|
||||
QString sanitizedSearch = searchText;
|
||||
sanitizedSearch.replace('\'', "''"); // Escape single quotes
|
||||
|
||||
query.bindValue(":search", "%" + sanitizedSearch + "%");
|
||||
// Optimize the query based on length of search text
|
||||
if (searchText.length() <= 3) {
|
||||
// For short search terms, use a more targeted approach
|
||||
query.prepare("SELECT full_path, ocr_text FROM ocr_results WHERE ocr_text LIKE :search");
|
||||
query.bindValue(":search", "%" + searchText + "%");
|
||||
} else {
|
||||
// For longer search terms, use LIKE with a more specific pattern at start
|
||||
// which can utilize indexes better if they exist
|
||||
query.prepare("SELECT full_path, ocr_text FROM ocr_results WHERE ocr_text LIKE :search OR ocr_text LIKE :wordstart");
|
||||
query.bindValue(":search", "%" + searchText + "%");
|
||||
query.bindValue(":wordstart", "% " + searchText + "%");
|
||||
}
|
||||
|
||||
if (!query.exec()) {
|
||||
qDebug() << "Failed to search images:" << query.lastError().text();
|
||||
m_db.rollback();
|
||||
return images;
|
||||
}
|
||||
|
||||
// Reserve space for results to avoid reallocations
|
||||
images.reserve(query.size() > 0 ? query.size() : 100);
|
||||
|
||||
while (query.next()) {
|
||||
ImageItem item;
|
||||
item.filePath = query.value(0).toString();
|
||||
@@ -169,5 +204,20 @@ QList<DatabaseManager::ImageItem> DatabaseManager::searchImages(const QString &s
|
||||
}
|
||||
}
|
||||
|
||||
m_db.commit();
|
||||
|
||||
// Cache the result for future queries
|
||||
if (images.size() > 0) {
|
||||
m_searchCache.insert(searchText, images);
|
||||
|
||||
// Limit cache size to avoid memory issues
|
||||
if (m_searchCache.size() > MAX_CACHE_SIZE) {
|
||||
// Remove the first key (oldest entry)
|
||||
if (!m_searchCache.isEmpty()) {
|
||||
m_searchCache.remove(m_searchCache.firstKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return images;
|
||||
}
|
||||
Reference in New Issue
Block a user