| 83 | } |
| 84 | |
| 85 | SQLInfo::Ptr ZVecCachedSQLParser::get_from_cache(const std::string &query, |
| 86 | std::string *query_cache_key) { |
| 87 | // find [ and ], must only one occurrence. |
| 88 | std::string::size_type left_pos, right_pos; |
| 89 | left_pos = query.find("["); |
| 90 | if (left_pos == query.npos) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | // find from left_pos+1 |
| 94 | right_pos = query.rfind("]"); |
| 95 | if (right_pos == query.npos) { |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | // ok, let's find it. |
| 100 | *query_cache_key = query.substr(0, left_pos); |
| 101 | query_cache_key->append(query.begin() + right_pos + 1, query.end()); |
| 102 | std::string vector_text = query.substr(left_pos, right_pos - left_pos + 1); |
| 103 | |
| 104 | SQLInfo::Ptr cached_sql_info = nullptr; |
| 105 | SQLInfo::Ptr copied_sql_info = nullptr; |
| 106 | { // lock only in this block. after sql_info is copied, just unlock. |
| 107 | std::shared_lock guard(shared_mutex_); |
| 108 | auto iter = sql_info_map_.find(*query_cache_key); |
| 109 | if (iter == sql_info_map_.end()) { |
| 110 | ++miss; |
| 111 | LOG_DEBUG("cache miss. key: [%s]", query_cache_key->c_str()); |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | cached_sql_info = iter->second; |
| 116 | // copy cached_sql_info |
| 117 | copied_sql_info = std::make_shared<SQLInfo>(*cached_sql_info); |
| 118 | } |
| 119 | |
| 120 | // parse vector part |
| 121 | Node::Ptr vector_node = parse_vector_text(&vector_text); |
| 122 | if (vector_node == nullptr) { |
| 123 | LOG_DEBUG("wrong vector format: [%s]", vector_text.c_str()); |
| 124 | return nullptr; |
| 125 | } |
| 126 | // replace vector in copied_sql_info |
| 127 | if (replace_vector_node(copied_sql_info, vector_node) != 0) { |
| 128 | LOG_WARN("replace_vector_node failed. [%s][%s]", query.c_str(), |
| 129 | vector_text.c_str()); |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | ++hit; |
| 134 | LOG_DEBUG("cache hit. key: [%s] sql_info: [%s]", query_cache_key->c_str(), |
| 135 | copied_sql_info->to_string().c_str()); |
| 136 | return copied_sql_info; |
| 137 | } |
| 138 | |
| 139 | int ZVecCachedSQLParser::replace_vector_node(SQLInfo::Ptr cached_sql_info, |
| 140 | Node::Ptr vector_node) { |