diff --git a/src/dde-grand-search-daemon/searcher/semantic/dslparser/dslparser.cpp b/src/dde-grand-search-daemon/searcher/semantic/dslparser/dslparser.cpp index 7f653ef..2a0160b 100644 --- a/src/dde-grand-search-daemon/searcher/semantic/dslparser/dslparser.cpp +++ b/src/dde-grand-search-daemon/searcher/semantic/dslparser/dslparser.cpp @@ -13,11 +13,8 @@ using namespace antlr4; bool BaseCond::kIsNeedAdjustBase = false; -BaseCond::BaseCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : QObject(parent) { +BaseCond::BaseCond(const QString &text, QObject *parent) : QObject(parent) { setCond(text); - m_querys = querys; - m_worker = worker; } BaseCond::~BaseCond() { @@ -42,40 +39,6 @@ void BaseCond::addOrCond(BaseCond *cond) { cond->setParent(this); } -bool BaseCond::isMatch(const QString &text) { - bool ret = true; - qDebug() << QString("BaseCond type(%1) isMatch(%2) m_cond(%3)").arg(m_condType).arg(text).arg(m_cond); - if (m_andCondList.empty() && m_orCondList.empty()) { - ret = false; - goto _EXIT; - } - - if (!m_andCondList.empty()) { - for (BaseCond *cond : m_andCondList) { - if (!cond->isMatch(text)) { - ret = false; - goto _EXIT; - } - } - - ret = true; - goto _EXIT; - } - - for (BaseCond *cond : m_orCondList) { - if (cond->isMatch(text)) { - ret = true; - goto _EXIT; - } - } - - ret = false; - -_EXIT: - qInfo() << QString("BaseCond type(%1) isMatch(%2) ret(%3) m_cond(%4)").arg(m_condType).arg(text).arg(ret).arg(m_cond); - return ret; -} - void BaseCond::adjust() { // 消除冗余的Binary层级 this->mergeBinary(); @@ -88,7 +51,6 @@ void BaseCond::adjust() { this->merge4Engine(); this->adjust4OrCond(); - //this->loadCond(); } void BaseCond::mergeBinary() { @@ -104,7 +66,7 @@ void BaseCond::mergeBinary() { } if (isAllDate) { - DateInfoCond *allCond = new DateInfoCond("", this->m_querys, this->m_worker); + DateInfoCond *allCond = new DateInfoCond(""); allCond->m_compType = "=="; for (int i = m_andCondList.size() - 1; i >= 0; i--) { DateInfoCond *cond = dynamic_cast(m_andCondList[i]); @@ -174,7 +136,7 @@ void BaseCond::mergeBase() { } if (isAllDate) { - DateInfoCond *allCond = new DateInfoCond("", this->m_querys, this->m_worker); + DateInfoCond *allCond = new DateInfoCond(""); allCond->m_compType = "=="; for (int i = m_andCondList.size() - 1; i >= 0; i--) { DateInfoCond *cond = dynamic_cast(m_andCondList[i]); @@ -242,44 +204,12 @@ void BaseCond::merge4Engine() { return; } - bool hasFeature = false, hasFulltext = false; - for (int i = 0; i < m_andCondList.size(); i++) { - if (m_andCondList[i]->getCondType() == querylangParser::RuleMetaSearch - || m_andCondList[i]->getCondType() == querylangParser::RuleDurationSearch) { - hasFeature = true; - break; - } - - if (m_andCondList[i]->getCondType() == querylangParser::RuleContentSearch) { - hasFulltext = true; - break; - } - } - - if (hasFeature) { - FeatureCond *cond = new FeatureCond(&m_andCondList, m_querys, m_worker); - if (cond->isValid()) { - m_andCondList.append(cond); - cond->setParent(this); - } else { - delete cond; - } - } else if (hasFulltext) { - FulltextCond *cond = new FulltextCond(&m_andCondList, m_querys, m_worker); - if (cond->isValid()) { - m_andCondList.append(cond); - cond->setParent(this); - } else { - delete cond; - } + EngineCond *cond = new EngineCond(&m_andCondList); + if (cond->isValid()) { + m_andCondList.append(cond); + cond->setParent(this); } else { - AnythingCond *cond = new AnythingCond(&m_andCondList, m_querys, m_worker); - if (cond->isValid()) { - m_andCondList.append(cond); - cond->setParent(this); - } else { - delete cond; - } + delete cond; } } } @@ -287,34 +217,7 @@ void BaseCond::merge4Engine() { void BaseCond::adjust4OrCond() { if (m_andCondList.size() == 2 && m_andCondList[0]->getCondType() == querylangParser::RulePrimary && !m_andCondList[0]->m_orCondList.isEmpty() - && m_andCondList[1]->getCondType() == BinaryCond::RuleAnything) { - bool isAllSimpleCond = true; - for (int i = 0; i < m_andCondList[0]->m_orCondList.size(); i++) { - BaseCond *cond = m_andCondList[0]->m_orCondList[i]; - if (cond->getCondType() != querylangParser::RuleTypeSearch) { - isAllSimpleCond = false; - break; - } - } - - if (isAllSimpleCond) { - for (int i = 0; i < m_andCondList[0]->m_orCondList.size(); i++) { - BaseCond *cond = m_andCondList[0]->m_orCondList[i]; - AnythingCond *anyCond = dynamic_cast(m_andCondList[1]); - if (!anyCond->copyCondOut(cond)) { - return; - } - } - - delete m_andCondList[1]; - m_andCondList.removeAt(1); - return; - } - } - - if (m_andCondList.size() == 2 && m_andCondList[0]->getCondType() == querylangParser::RulePrimary - && !m_andCondList[0]->m_orCondList.isEmpty() - && m_andCondList[1]->getCondType() == BinaryCond::RuleFeature) { + && m_andCondList[1]->getCondType() == BinaryCond::RuleEngine) { bool isAllSimpleCond = true; for (int i = 0; i < m_andCondList[0]->m_orCondList.size(); i++) { BaseCond *cond = m_andCondList[0]->m_orCondList[i]; @@ -325,37 +228,11 @@ void BaseCond::adjust4OrCond() { } if (isAllSimpleCond) { + // 吸收 Type 类型条件 for (int i = 0; i < m_andCondList[0]->m_orCondList.size(); i++) { BaseCond *cond = m_andCondList[0]->m_orCondList[i]; - FeatureCond *anyCond = dynamic_cast(m_andCondList[1]); - if (!anyCond->copyCondOut(cond)) { - return; - } - } - - delete m_andCondList[1]; - m_andCondList.removeAt(1); - return; - } - } - - if (m_andCondList.size() == 2 && m_andCondList[0]->getCondType() == querylangParser::RulePrimary - && !m_andCondList[0]->m_orCondList.isEmpty() - && m_andCondList[1]->getCondType() == BinaryCond::RuleFulltext) { - bool isAllSimpleCond = true; - for (int i = 0; i < m_andCondList[0]->m_orCondList.size(); i++) { - BaseCond *cond = m_andCondList[0]->m_orCondList[i]; - if (cond->getCondType() != querylangParser::RuleTypeSearch) { - isAllSimpleCond = false; - break; - } - } - - if (isAllSimpleCond) { - for (int i = 0; i < m_andCondList[0]->m_orCondList.size(); i++) { - BaseCond *cond = m_andCondList[0]->m_orCondList[i]; - FulltextCond *anyCond = dynamic_cast(m_andCondList[1]); - if (!anyCond->copyCondOut(cond)) { + EngineCond *engineCond = dynamic_cast(m_andCondList[1]); + if (!engineCond->absorbCond(cond)) { return; } } @@ -371,18 +248,6 @@ void BaseCond::adjust4OrCond() { } } -void BaseCond::loadCond() { - if (!m_andCondList.isEmpty()) { - for (int i = 0; i < m_andCondList.size(); i++) { - m_andCondList[i]->loadCond(); - } - } else { - for (int i = 0; i < m_orCondList.size(); i++) { - m_orCondList[i]->loadCond(); - } - } -} - QList BaseCond::entityList() { QList list; if (m_condType == querylangParser::RulePrimary || m_condType == querylangParser::RuleBinaryExpression) { @@ -401,60 +266,6 @@ QList BaseCond::entityList() { return list; } -void BaseCond::addMatchedItems(const MatchedItems &items) { - m_matchedItems.append(items); -} - -const MatchedItems &BaseCond::getMatchedItems() { - if (m_condType != querylangParser::RulePrimary && m_condType != querylangParser::RuleBinaryExpression) { - return m_matchedItems; - } - - if (!m_andCondList.isEmpty()) { - int quantity = -1; - bool isGetItems = false; - for (int i = 0; i < m_andCondList.size(); i++) { - if (m_andCondList[i]->getCondType() == querylangParser::RuleQuantityCondition) { - quantity = dynamic_cast(m_andCondList[i])->getQuantity(); - continue; - } - - if (!isGetItems) { - m_matchedItems = m_andCondList[i]->getMatchedItems(); - isGetItems = true; - continue; - } - - // 存在删除操作,所以采用倒序 - for (int j = m_matchedItems.size() - 1; j >= 0; j--) { - if (m_andCondList[i]->getMatchedItems().contains(m_matchedItems[j])) { - continue; - } - - m_matchedItems.removeAt(j); - } - } - if (quantity > 0 && m_matchedItems.size() > quantity) { - while (m_matchedItems.size() > quantity) { - m_matchedItems.removeLast(); - } - } - return m_matchedItems; - } - - for (int i = 0; i < m_orCondList.size(); i++) { - const MatchedItems &items = m_orCondList[i]->getMatchedItems(); - for (int j = 0; j < items.size(); j++) { - if (m_matchedItems.contains(items[j])) { - continue; - } - - m_matchedItems.append(items[j]); - } - } - return m_matchedItems; -} - QString BaseCond::toString(int spaceCounts) { // 控制缩进 QString space; @@ -475,8 +286,7 @@ QString BaseCond::toString(int spaceCounts) { return str; } -DateInfoCond::DateInfoCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +DateInfoCond::DateInfoCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleDateSearchinfo; const QDateTime curDT = QDateTime::currentDateTime(); @@ -541,14 +351,7 @@ DateInfoCond::DateInfoCond(const QString &text, QListappend(func); -} - -PathCond::PathCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +PathCond::PathCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RulePathSearch; if (m_cond.contains("IS NOT")) { m_isTruePath = false; @@ -571,14 +374,7 @@ PathCond::PathCond(const QString &text, QListappend(func); -} - -NameCond::NameCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +NameCond::NameCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleNameSearch; int pos = m_cond.indexOf("\"") + 1; m_name = m_cond.mid(pos, m_cond.length() - pos - 1); @@ -587,14 +383,7 @@ NameCond::NameCond(const QString &text, QListappend(func); -} - -SizeCond::SizeCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +SizeCond::SizeCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleSizeSearch; int pos = m_cond.indexOf("\"") + 1; QString temp = m_cond.mid(pos, m_cond.length() - pos - 1); @@ -628,14 +417,7 @@ SizeCond::SizeCond(const QString &text, QListappend(func); -} - -TypeCond::TypeCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +TypeCond::TypeCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleTypeSearch; if (m_cond.contains("IS NOT")) { m_isTrueType = false; @@ -671,14 +453,7 @@ TypeCond::TypeCond(const QString &text, QListappend(func); -} - -DurationCond::DurationCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +DurationCond::DurationCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleDurationSearch; // DURATION>"3 minute" if (m_cond.contains(">")) { @@ -733,14 +508,7 @@ QString DurationCond::formatTime(qint64 msec) { return formattedDuration; } -void DurationCond::loadCond() { - m_feature.setEntity({m_entity}); - SemanticWorkerPrivate::QueryFunction func = {&m_feature, &FeatureQuery::run, this}; - m_querys->append(func); -} - -MetaCond::MetaCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +MetaCond::MetaCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleMetaSearch; // META_TYPEIS"ARTIST"ANDMETA_VALUEIS"xxx" // META_TYPEIS"ALBUM"ANDMETA_VALUEIS"xxx" @@ -787,18 +555,7 @@ MetaCond::MetaCond(const QString &text, QListappend(func); -} - -QuantityCond::QuantityCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +QuantityCond::QuantityCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleQuantityCondition; int pos = m_cond.indexOf("=") + 1; QString temp = m_cond.mid(pos); @@ -806,8 +563,7 @@ QuantityCond::QuantityCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond(text, querys, worker, parent) { +ContentCond::ContentCond(const QString &text, QObject *parent) : BaseCond(text, parent) { m_condType = querylangParser::RuleContentSearch; int pos = m_cond.indexOf("\"") + 1; m_content = m_cond.mid(pos, m_cond.length() - pos - 1); @@ -816,147 +572,17 @@ ContentCond::ContentCond(const QString &text, QListappend(func); -} - -AnythingCond::AnythingCond(QList *andList, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond("", querys, worker, parent) { - const qint64 _NOW = QDateTime::currentSecsSinceEpoch(); - m_condType = BaseCond::RuleAnything; - m_isValid = false; - - // 支持:DateInfo Path Name Size Type - // Date的条件,一般都是成对出现的 - qint64 timestamp1 = 0, timestamp2 = _NOW; - // 使用倒序的原因:含有删除操作 - for (int i = andList->size() - 1; i >= 0; i--) { - if ((*andList)[i]->getCondType() == querylangParser::RuleDateSearchinfo) { - m_isValid = true; - DateInfoCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - if (cond->m_compType == "==") { - timestamp1 = cond->m_timestamp; - timestamp2 = cond->m_timestamp2; - } else if (cond->m_compType.contains(">")) { - timestamp1 = cond->m_timestamp; - } else { - timestamp2 = cond->m_timestamp; - } - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RulePathSearch) { - m_isValid = true; - PathCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - m_entity.isContainPath = cond->m_isTruePath; - m_entity.partPath = cond->m_pathName; - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RuleNameSearch) { - m_isValid = true; - NameCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - m_entity.keys.append(cond->m_name); - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RuleSizeSearch) { - m_isValid = true; - SizeCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - m_entity.fileCompType = cond->m_compType; - m_entity.fileSize = cond->m_fileSize; - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RuleTypeSearch) { - m_isValid = true; - TypeCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - /* - * #define DOCUMENT_GROUP "text" - * #define PICTURE_GROUP "img" - * #define AUDIO_GROUP "msc" - * #define VIDEO_GROUP "vdo" - * #define FILE_GROUP "file" - * #define FOLDER_GROUP "fld" - * #define APPLICATION_GROUP "app" - */ - // 优先匹配为类别,其实是扩展名 - if (cond->m_typeName == "song" || cond->m_typeName == "music") { - m_entity.types.append(AUDIO_GROUP); - } else if (cond->m_typeName == "paper" || cond->m_typeName == "document") { - m_entity.types.append(DOCUMENT_GROUP); - } else if (cond->m_typeName == "picture") { - m_entity.types.append(PICTURE_GROUP); - } else if (cond->m_typeName == "video") { - m_entity.types.append(VIDEO_GROUP); - } else if (cond->m_typeName == "file") { - m_entity.types.clear(); - } else { - m_entity.suffix = cond->m_typeName; - } - m_entity.isContainType = cond->m_isTrueType; - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - (*andList)[i]->merge4Engine(); - } - if (timestamp1 != 0 || timestamp2 != _NOW) { - m_entity.times.append(QPair(timestamp1, timestamp2)); - } -} - -bool AnythingCond::copyCondOut(BaseCond *cond) { - if (cond->getCondType() == querylangParser::RuleTypeSearch) { - cond->m_condType = BaseCond::RuleAnything; - cond->m_cond = m_cond + cond->m_cond; - SemanticEntity entity = m_entity; - entity.types.append(cond->m_entity.types); - entity.suffix = cond->m_entity.suffix; - cond->m_entity = entity; - return true; - } - - return false; -} - -void AnythingCond::loadCond() { - if (!m_isValid) { - return; - } - - m_anything.setEntity({m_entity}); - SemanticWorkerPrivate::QueryFunction func = {&m_anything, &AnythingQuery::run, this}; - m_querys->append(func); -} - -FeatureCond::FeatureCond(QList *andList, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond("", querys, worker, parent) { +EngineCond::EngineCond(QList *andList, QObject *parent) : BaseCond("", parent) { const qint64 _NOW = QDateTime::currentSecsSinceEpoch(); - m_condType = BaseCond::RuleFeature; + m_condType = BaseCond::RuleEngine; m_isValid = false; - // 支持:Meta Duration DateInfo Path Name Size Type + // 支持:DateInfo Path Name Size Type 特殊1(Meta Duration) 特殊2(Content) // Date的条件,一般都是成对出现的 qint64 timestamp1 = 0, timestamp2 = _NOW; // 使用倒序的原因:含有删除操作 for (int i = andList->size() - 1; i >= 0; i--) { + // Meta if ((*andList)[i]->getCondType() == querylangParser::RuleMetaSearch) { m_isValid = true; MetaCond *cond = dynamic_cast((*andList)[i]); @@ -982,6 +608,7 @@ FeatureCond::FeatureCond(QList *andList, QListgetCondType() == querylangParser::RuleDurationSearch) { m_isValid = true; DurationCond *cond = dynamic_cast((*andList)[i]); @@ -993,130 +620,7 @@ FeatureCond::FeatureCond(QList *andList, QListgetCondType() == querylangParser::RuleDateSearchinfo) { - m_isValid = true; - DateInfoCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - if (cond->m_compType == "==") { - timestamp1 = cond->m_timestamp; - timestamp2 = cond->m_timestamp2; - } else if (cond->m_compType.contains(">")) { - timestamp1 = cond->m_timestamp; - } else { - timestamp2 = cond->m_timestamp; - } - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RulePathSearch) { - m_isValid = true; - PathCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - m_entity.isContainPath = cond->m_isTruePath; - m_entity.partPath = cond->m_pathName; - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RuleNameSearch) { - m_isValid = true; - NameCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - m_entity.keys.append(cond->m_name); - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RuleSizeSearch) { - m_isValid = true; - SizeCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - m_entity.fileCompType = cond->m_compType; - m_entity.fileSize = cond->m_fileSize; - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - if ((*andList)[i]->getCondType() == querylangParser::RuleTypeSearch) { - m_isValid = true; - TypeCond *cond = dynamic_cast((*andList)[i]); - m_cond += cond->getCond() + " "; - /* - * #define DOCUMENT_GROUP "text" - * #define PICTURE_GROUP "img" - * #define AUDIO_GROUP "msc" - * #define VIDEO_GROUP "vdo" - * #define FILE_GROUP "file" - * #define FOLDER_GROUP "fld" - * #define APPLICATION_GROUP "app" - */ - // 优先匹配为类别,其实是扩展名 - if (cond->m_typeName == "song" || cond->m_typeName == "music") { - m_entity.types.append(AUDIO_GROUP); - } else if (cond->m_typeName == "paper" || cond->m_typeName == "document") { - m_entity.types.append(DOCUMENT_GROUP); - } else if (cond->m_typeName == "picture") { - m_entity.types.append(PICTURE_GROUP); - } else if (cond->m_typeName == "video") { - m_entity.types.append(VIDEO_GROUP); - } else if (cond->m_typeName == "file") { - m_entity.types.clear(); - } else { - m_entity.suffix = cond->m_typeName; - } - m_entity.isContainType = cond->m_isTrueType; - delete (*andList)[i]; - andList->removeAt(i); - continue; - } - - (*andList)[i]->merge4Engine(); - } - if (timestamp1 != 0 || timestamp2 != _NOW) { - m_entity.times.append(QPair(timestamp1, timestamp2)); - } -} - -bool FeatureCond::copyCondOut(BaseCond *cond) { - if (cond->getCondType() == querylangParser::RuleTypeSearch) { - cond->m_condType = BaseCond::RuleFeature; - cond->m_cond = m_cond + cond->m_cond; - SemanticEntity entity = m_entity; - entity.types.append(cond->m_entity.types); - entity.suffix = cond->m_entity.suffix; - cond->m_entity = entity; - return true; - } - - return false; -} - -void FeatureCond::loadCond() { - if (!m_isValid) { - return; - } - - m_feature.setEntity({m_entity}); - SemanticWorkerPrivate::QueryFunction func = {&m_feature, &FeatureQuery::run, this}; - m_querys->append(func); -} - -FulltextCond::FulltextCond(QList *andList, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent) : BaseCond("", querys, worker, parent) { - const qint64 _NOW = QDateTime::currentSecsSinceEpoch(); - m_condType = BaseCond::RuleFulltext; - m_isValid = false; - - // 支持:Content DateInfo Path Name Size Type - // Date的条件,一般都是成对出现的 - qint64 timestamp1 = 0, timestamp2 = _NOW; - // 使用倒序的原因:含有删除操作 - for (int i = andList->size() - 1; i >= 0; i--) { + // Content if ((*andList)[i]->getCondType() == querylangParser::RuleContentSearch) { m_isValid = true; ContentCond *cond = dynamic_cast((*andList)[i]); @@ -1127,6 +631,7 @@ FulltextCond::FulltextCond(QList *andList, QListgetCondType() == querylangParser::RuleDateSearchinfo) { m_isValid = true; DateInfoCond *cond = dynamic_cast((*andList)[i]); @@ -1144,6 +649,7 @@ FulltextCond::FulltextCond(QList *andList, QListgetCondType() == querylangParser::RulePathSearch) { m_isValid = true; PathCond *cond = dynamic_cast((*andList)[i]); @@ -1155,6 +661,7 @@ FulltextCond::FulltextCond(QList *andList, QListgetCondType() == querylangParser::RuleNameSearch) { m_isValid = true; NameCond *cond = dynamic_cast((*andList)[i]); @@ -1165,6 +672,7 @@ FulltextCond::FulltextCond(QList *andList, QListgetCondType() == querylangParser::RuleSizeSearch) { m_isValid = true; SizeCond *cond = dynamic_cast((*andList)[i]); @@ -1176,6 +684,7 @@ FulltextCond::FulltextCond(QList *andList, QListgetCondType() == querylangParser::RuleTypeSearch) { m_isValid = true; TypeCond *cond = dynamic_cast((*andList)[i]); @@ -1216,9 +725,9 @@ FulltextCond::FulltextCond(QList *andList, QListgetCondType() == querylangParser::RuleTypeSearch) { - cond->m_condType = BaseCond::RuleFulltext; + cond->m_condType = BaseCond::RuleEngine; cond->m_cond = m_cond + cond->m_cond; SemanticEntity entity = m_entity; entity.types.append(cond->m_entity.types); @@ -1230,16 +739,6 @@ bool FulltextCond::copyCondOut(BaseCond *cond) { return false; } -void FulltextCond::loadCond() { - if (!m_isValid) { - return; - } - - m_fulltext.setEntity({m_entity}); - SemanticWorkerPrivate::QueryFunction func = {&m_fulltext, &FullTextQuery::run, this}; - m_querys->append(func); -} - void DslParserListener::enterEveryRule(antlr4::ParserRuleContext *ctx) { QString text = QString().fromStdString(ctx->getText()); switch (ctx->getRuleIndex()) { @@ -1271,12 +770,12 @@ void DslParserListener::enterEveryRule(antlr4::ParserRuleContext *ctx) { bool isAnd = true, isOr = true; switch (ctx->getRuleIndex()) { case querylangParser::RulePrimary: - ptr = new BaseCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new BaseCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleBinaryExpression: - ptr = new BinaryCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new BinaryCond(text); isAnd = (m_cond->getCond().contains(QString("AND") + text)); isOr = (m_cond->getCond().contains(QString("OR") + text)); if (isAnd) { @@ -1291,52 +790,52 @@ void DslParserListener::enterEveryRule(antlr4::ParserRuleContext *ctx) { m_tempPtr = ptr; break; case querylangParser::RuleDateSearchinfo: - ptr = new DateInfoCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new DateInfoCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RulePathSearch: - ptr = new PathCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new PathCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleNameSearch: - ptr = new NameCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new NameCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleSizeSearch: - ptr = new SizeCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new SizeCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleTypeSearch: - ptr = new TypeCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new TypeCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleDurationSearch: - ptr = new DurationCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new DurationCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleMetaSearch: - ptr = new MetaCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new MetaCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleQuantityCondition: - ptr = new QuantityCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new QuantityCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleContentSearch: - ptr = new ContentCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new ContentCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; case querylangParser::RuleFilename: - ptr = new FileNameCond(text, m_tempPtr->m_querys, m_tempPtr->m_worker); + ptr = new FileNameCond(text); m_tempPtr->addAndCond(ptr); m_tempPtr = ptr; break; @@ -1370,8 +869,7 @@ void DslParserListener::exitEveryRule(antlr4::ParserRuleContext *ctx) { } } -DslParser::DslParser(const QString &text, QList *querys, FileResultsHandler *fileHandler, - SemanticWorkerPrivate *worker, QObject *parent) : QObject(parent), m_cond("", querys, worker) { +DslParser::DslParser(const QString &text, QObject *parent) : QObject(parent), m_cond("") { qDebug() << QString("DslParser DslParser(%1)").arg(text); ANTLRInputStream input(text.toUtf8().toStdString()); querylangLexer lexer(&input); @@ -1385,10 +883,3 @@ DslParser::DslParser(const QString &text, QList #include #include - -#include "../fileresultshandler.h" #include "../semanticworker_p.h" -#include "../database/anythingquery.h" -#include "../database/fulltextquery.h" -#include "../database/featurequery.h" -#include "../database/vectorquery.h" - namespace GrandSearch { -class SemanticWorkerPrivate; // 对应RulePrimary class BaseCond : public QObject { Q_OBJECT public: - explicit BaseCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); + explicit BaseCond(const QString &text, QObject *parent = nullptr); virtual ~BaseCond(); void setCond(const QString &text) { m_cond = text; } QString getCond() { return m_cond; } @@ -36,26 +27,20 @@ class BaseCond : public QObject { BaseCond *getParent() { return m_parent; } void addAndCond(BaseCond *cond); void addOrCond(BaseCond *cond); - // 调试使用 - virtual bool isMatch(const QString &text); // 某些条件没有存在的必要 virtual bool isValid() { return m_isValid; } // 结构优化调整 void adjust(); // 合并Binary层级的条件 virtual void mergeBinary(); - // 合并Base层级的条件 + // 合并Base层级的条件(Binary层级之上) virtual void mergeBase(); - // 合并各引擎的条件 + // 为搜索引擎,合并各条件 virtual void merge4Engine(); // 为OR条件调整条件组合 void adjust4OrCond(); - // 装载检索条件进入检索引擎 - virtual void loadCond(); virtual QList entityList(); - virtual void addMatchedItems(const MatchedItems &items); - virtual const MatchedItems &getMatchedItems(); virtual QString toString(int spaceCounts = 0); public: @@ -63,10 +48,7 @@ class BaseCond : public QObject { static bool kIsNeedAdjustBase; QString m_cond; int m_condType = querylangParser::RulePrimary; - QList *m_querys = nullptr; - SemanticWorkerPrivate *m_worker = nullptr; BaseCond *m_parent = nullptr; - MatchedItems m_matchedItems; QList m_andCondList; QList m_orCondList; bool m_isValid = true; @@ -74,7 +56,8 @@ class BaseCond : public QObject { public: enum { - RuleAnything = 100, + RuleEngine = 100, + RuleAnything, RuleFeature, RuleFulltext, }; @@ -83,8 +66,7 @@ class BaseCond : public QObject { // 对应RuleBinaryExpression class BinaryCond : public BaseCond { public: - explicit BinaryCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr) : BaseCond(text, querys, worker, parent) { + explicit BinaryCond(const QString &text, QObject *parent = nullptr) : BaseCond(text, parent) { m_condType = querylangParser::RuleBinaryExpression; } }; @@ -92,115 +74,79 @@ class BinaryCond : public BaseCond { // 对应RuleDateSearchinfo DATE <= CURRENT - "16 hour" class DateInfoCond : public BaseCond { public: - explicit DateInfoCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit DateInfoCond(const QString &text, QObject *parent = nullptr); public: QString m_compType; qint64 m_timestamp; qint64 m_timestamp2; - -private: - AnythingQuery m_anything; }; // 对应RulePathSearch PATH IS "Downloads" class PathCond : public BaseCond { public: - explicit PathCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit PathCond(const QString &text, QObject *parent = nullptr); public: bool m_isTruePath = true; QString m_pathName; - -private: - AnythingQuery m_anything; }; // 对应RuleNameSearch NAME CONTAINS "名字" class NameCond : public BaseCond { public: - explicit NameCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit NameCond(const QString &text, QObject *parent = nullptr); public: QString m_name; - -private: - AnythingQuery m_anything; }; // 对应RuleSizeSearch SIZE <= "4GB" class SizeCond : public BaseCond { public: - explicit SizeCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit SizeCond(const QString &text, QObject *parent = nullptr); public: QString m_compType; qint64 m_fileSize; - -private: - AnythingQuery m_anything; }; // 对应RuleTypeSearch TYPE IS "txt" class TypeCond : public BaseCond { public: - explicit TypeCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit TypeCond(const QString &text, QObject *parent = nullptr); public: bool m_isTrueType = true; QString m_typeName; - -private: - AnythingQuery m_anything; }; // 对应RuleDurationSearch DURATION > \"3 minute\" class DurationCond : public BaseCond { public: - explicit DurationCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); + explicit DurationCond(const QString &text, QObject *parent = nullptr); static QString formatTime(qint64 msec); - void loadCond() override; public: QString m_compType; QString m_duration; - -private: - FeatureQuery m_feature; }; // 对应RuleMetaSearch META_TYPE IS \"ARTIST\" AND META_VALUE IS \"xxx\" class MetaCond : public BaseCond { public: - explicit MetaCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit MetaCond(const QString &text, QObject *parent = nullptr); public: bool m_isTrue = true; QString m_metaType; QString m_metaValue; - -private: - FeatureQuery m_feature; }; // 对应RuleQuantityCondition QUANTITY = 10 class QuantityCond : public BaseCond { public: - explicit QuantityCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); + explicit QuantityCond(const QString &text, QObject *parent = nullptr); int getQuantity() { return m_quantity; } private: @@ -210,58 +156,24 @@ class QuantityCond : public BaseCond { // 对应RuleContentSearch CONTENT CONTAINS "read" class ContentCond : public BaseCond { public: - explicit ContentCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - void loadCond() override; + explicit ContentCond(const QString &text, QObject *parent = nullptr); public: QString m_content; - -private: - FullTextQuery m_fulltext; }; // 对应RuleFilename class FileNameCond : public BaseCond { public: - explicit FileNameCond(const QString &text, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr) : BaseCond(text, querys, worker, parent) {} -}; - -// 对应合并后的Anything -class AnythingCond : public BaseCond { -public: - explicit AnythingCond(QList *andList, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - bool copyCondOut(BaseCond *cond); - void loadCond() override; - -private: - AnythingQuery m_anything; + explicit FileNameCond(const QString &text, QObject *parent = nullptr) : BaseCond(text, parent) {} }; -// 对应合并后的Feature -class FeatureCond : public BaseCond { +// 对应合并后的Engine +class EngineCond : public BaseCond { public: - explicit FeatureCond(QList *andList, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - bool copyCondOut(BaseCond *cond); - void loadCond() override; - -private: - FeatureQuery m_feature; -}; - -// 对应合并后的Fulltext -class FulltextCond : public BaseCond { -public: - explicit FulltextCond(QList *andList, QList *querys, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); - bool copyCondOut(BaseCond *cond); - void loadCond() override; - -private: - FullTextQuery m_fulltext; + explicit EngineCond(QList *andList, QObject *parent = nullptr); + // 吸收目标条件至自身 + bool absorbCond(BaseCond *cond); }; class DslParserListener : public antlr4::tree::ParseTreeListener { @@ -335,12 +247,9 @@ class DslParserListener : public antlr4::tree::ParseTreeListener { class DslParser : public QObject { Q_OBJECT public: - explicit DslParser(const QString &text, QList *querys, FileResultsHandler *fileHandler, - SemanticWorkerPrivate *worker, QObject *parent = nullptr); + explicit DslParser(const QString &text, QObject *parent = nullptr); ~DslParser() {} - bool isMatch(const QString &text); QList entityList() { return m_cond.entityList(); } - const MatchedItems &getMatchedItems() { return m_cond.getMatchedItems(); } private: BaseCond m_cond; diff --git a/src/dde-grand-search-daemon/searcher/semantic/semanticworker.cpp b/src/dde-grand-search-daemon/searcher/semantic/semanticworker.cpp index 7a3c7c5..def7dd4 100644 --- a/src/dde-grand-search-daemon/searcher/semantic/semanticworker.cpp +++ b/src/dde-grand-search-daemon/searcher/semantic/semanticworker.cpp @@ -174,13 +174,11 @@ bool SemanticWorker::working(void *context) dslStr.replace("'", "\""); dslStr.replace("WITH META_VALUE", "AND META_VALUE"); dslStr.replace("DIRECTORY_NAME IS", "PATH IS"); - qDebug() << QString("query(%1) => dsl(%2), spend(%3 ms)").arg(d->m_context).arg(dslStr).arg(d->m_time.elapsed()); + qInfo() << QString("query(%1) => dsl(%2), spend(%3 ms)").arg(d->m_context).arg(dslStr).arg(d->m_time.elapsed()); //dslStr = "((DATE >= CURRENT -\"5 minute\") AND (DATE <= \"CURRENT\")) AND (TYPE IS \"document\")"; // parse DSL - QList querys; - FileResultsHandler fileHandler; - DslParser parser(dslStr, &querys, &fileHandler, d); + DslParser parser(dslStr); entityList = parser.entityList(); for (int i = 0; i < entityList.size(); i++) { qDebug() << "entityList" << i << entityList[i].toString();