Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 1 | /** NDN-Atmos: Cataloging Service for distributed data originally developed |
| 2 | * for atmospheric science data |
| 3 | * Copyright (C) 2015 Colorado State University |
| 4 | * |
| 5 | * NDN-Atmos is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * NDN-Atmos is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with NDN-Atmos. If not, see <http://www.gnu.org/licenses/>. |
| 17 | **/ |
| 18 | |
| 19 | #ifndef ATMOS_QUERY_QUERY_ADAPTER_HPP |
| 20 | #define ATMOS_QUERY_QUERY_ADAPTER_HPP |
| 21 | |
| 22 | #include "util/catalog-adapter.hpp" |
| 23 | #include "util/mysql-util.hpp" |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 24 | #include "util/config-file.hpp" |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 25 | |
| 26 | #include <thread> |
| 27 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 28 | #include <json/reader.h> |
| 29 | #include <json/value.h> |
| 30 | #include <json/writer.h> |
| 31 | |
| 32 | #include <ndn-cxx/data.hpp> |
| 33 | #include <ndn-cxx/face.hpp> |
| 34 | #include <ndn-cxx/interest.hpp> |
| 35 | #include <ndn-cxx/interest-filter.hpp> |
| 36 | #include <ndn-cxx/name.hpp> |
| 37 | #include <ndn-cxx/security/key-chain.hpp> |
| 38 | #include <ndn-cxx/util/time.hpp> |
| 39 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 40 | #include <ndn-cxx/util/in-memory-storage-lru.hpp> |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 41 | #include <ndn-cxx/util/string-helper.hpp> |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 42 | #include <ChronoSync/socket.hpp> |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 43 | |
| 44 | #include "mysql/mysql.h" |
| 45 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 46 | #include <map> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 47 | #include <unordered_map> |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 48 | #include <memory> |
| 49 | #include <mutex> |
| 50 | #include <sstream> |
| 51 | #include <string> |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 52 | #include <array> |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 53 | #include <utility> |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 54 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 55 | #include "util/logger.hpp" |
| 56 | |
| 57 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 58 | namespace atmos { |
| 59 | namespace query { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 60 | #ifdef HAVE_LOG4CXX |
| 61 | INIT_LOGGER("QueryAdapter"); |
| 62 | #endif |
| 63 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 64 | // todo: calculate payload limit by get the size of a signed empty Data packet |
| 65 | static const size_t PAYLOAD_LIMIT = 7000; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * QueryAdapter handles the Query usecases for the catalog |
| 69 | */ |
| 70 | template <typename DatabaseHandler> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 71 | class QueryAdapter : public atmos::util::CatalogAdapter { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 72 | public: |
| 73 | /** |
| 74 | * Constructor |
| 75 | * |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 76 | * @param face: Face that will be used for NDN communications |
| 77 | * @param keyChain: KeyChain that will be used for data signing |
| 78 | * @param syncSocket: ChronoSync socket |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 79 | */ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 80 | QueryAdapter(const std::shared_ptr<ndn::Face>& face, |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 81 | const std::shared_ptr<ndn::KeyChain>& keyChain, |
| 82 | const std::shared_ptr<chronosync::Socket>& syncSocket); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 83 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 84 | virtual |
| 85 | ~QueryAdapter(); |
| 86 | |
| 87 | /** |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 88 | * Helper function to specify section handler |
| 89 | */ |
| 90 | void |
| 91 | setConfigFile(util::ConfigFile& config, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 92 | const ndn::Name& prefix, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 93 | const std::vector<std::string>& nameFields, |
| 94 | const std::string& databaseTable); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | /** |
| 98 | * Helper function for configuration parsing |
| 99 | */ |
| 100 | void |
| 101 | onConfig(const util::ConfigSection& section, |
| 102 | bool isDryDun, |
| 103 | const std::string& fileName, |
| 104 | const ndn::Name& prefix); |
| 105 | |
| 106 | /** |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 107 | * Handles incoming query requests by stripping the filter off the Interest to get the |
| 108 | * actual request out. This removes the need for a 2-step Interest-Data retrieval. |
| 109 | * |
| 110 | * @param filter: InterestFilter that caused this Interest to be routed |
| 111 | * @param interest: Interest that needs to be handled |
| 112 | */ |
| 113 | virtual void |
| 114 | onQueryInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest); |
| 115 | |
| 116 | /** |
| 117 | * Handles requests for responses to an existing query |
| 118 | * |
| 119 | * @param filter: InterestFilter that caused this Interest to be routed |
| 120 | * @param interest: Interest that needs to be handled |
| 121 | */ |
| 122 | virtual void |
| 123 | onQueryResultsInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest); |
| 124 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 125 | /** |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 126 | * Handles requests for responses to an filter initialization request |
| 127 | * |
| 128 | * @param filter: InterestFilter that caused this Interest to be routed |
| 129 | * @param interest: Interest that needs to be handled |
| 130 | */ |
| 131 | virtual void |
| 132 | onFiltersInitializationInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest); |
| 133 | |
| 134 | /** |
| 135 | * Helper function that generates query results from a Json query carried in the Interest |
| 136 | * |
| 137 | * @param interest: Interest that needs to be handled |
| 138 | */ |
| 139 | void |
| 140 | populateFiltersMenu(std::shared_ptr<const ndn::Interest> interest); |
| 141 | |
| 142 | void |
| 143 | getFiltersMenu(Json::Value& value); |
| 144 | |
| 145 | /** |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 146 | * Helper function that makes query-results data |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 147 | * |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 148 | * @param segmentPrefix: Name that identifies the Prefix for the Data |
| 149 | * @param value: Json::Value to be sent in the Data |
| 150 | * @param segmentNo: uint64_t the segment for this Data |
| 151 | * @param isFinalBlock: bool to indicate whether this needs to be flagged in the Data as the |
| 152 | * last entry |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 153 | * @param isAutocomplete: bool to indicate whether this is an autocomplete message |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 154 | * @param resultCount: the number of records in the query results |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 155 | * @param viewStart: the start index of the record in the query results payload |
| 156 | * @param viewEnd: the end index of the record in the query results payload |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 157 | * @param lastComponent: flag to indicate the content contains the last component for |
| 158 | autocompletion query |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 159 | */ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 160 | std::shared_ptr<ndn::Data> |
| 161 | makeReplyData(const ndn::Name& segmentPrefix, |
| 162 | const Json::Value& value, |
| 163 | uint64_t segmentNo, |
| 164 | bool isFinalBlock, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 165 | bool isAutocomplete, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 166 | uint64_t resultCount, |
| 167 | uint64_t viewStart, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 168 | uint64_t viewEnd, |
| 169 | bool lastComponent); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 170 | |
| 171 | /** |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 172 | * Helper function that generates query results from a Json query carried in the Interest |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 173 | * |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 174 | * @param interest: Interest that needs to be handled |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 175 | */ |
| 176 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 177 | runJsonQuery(std::shared_ptr<const ndn::Interest> interest); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 178 | |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 179 | /** |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 180 | * Helper function that makes ACK data |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 181 | * |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 182 | * @param interest: Intersts that needs to be handled |
| 183 | * @param version: Version that needs to be in the data name |
| 184 | */ |
| 185 | std::shared_ptr<ndn::Data> |
| 186 | makeAckData(std::shared_ptr<const ndn::Interest> interest, |
| 187 | const ndn::Name::Component& version); |
| 188 | |
| 189 | /** |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 190 | * Helper function that sends NACK |
| 191 | * |
| 192 | * @param dataPrefix: prefix for the data packet |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 193 | */ |
| 194 | void |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 195 | sendNack(const ndn::Name& dataPrefix); |
| 196 | |
| 197 | /** |
| 198 | * Helper function that generates the sqlQuery string for component-based query |
| 199 | * @param sqlQuery: stringstream to save the sqlQuery string |
| 200 | * @param jsonValue: Json value that contains the query information |
| 201 | */ |
| 202 | bool |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 203 | json2Sql(std::stringstream& sqlQuery, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 204 | Json::Value& jsonValue); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Helper function that signs the data |
| 208 | */ |
| 209 | void |
| 210 | signData(ndn::Data& data); |
| 211 | |
| 212 | /** |
| 213 | * Helper function that publishes query-results data segments |
| 214 | */ |
| 215 | virtual void |
| 216 | prepareSegments(const ndn::Name& segmentPrefix, |
| 217 | const std::string& sqlString, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 218 | bool autocomplete, |
| 219 | bool lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Helper function to set the DatabaseHandler |
| 223 | */ |
| 224 | void |
| 225 | setDatabaseHandler(const util::ConnectionDetails& databaseId); |
| 226 | |
| 227 | /** |
| 228 | * Helper function that set filters to make the adapter work |
| 229 | */ |
| 230 | void |
| 231 | setFilters(); |
| 232 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 233 | void |
| 234 | setCatalogId(); |
| 235 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 236 | /** |
| 237 | * Helper function that generates the sqlQuery string for autocomplete query |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 238 | * @param sqlQuery: stringstream to save the sqlQuery string |
| 239 | * @param jsonValue: Json value that contains the query information |
| 240 | * @param lastComponent: Flag to mark the last component query |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 241 | */ |
| 242 | bool |
| 243 | json2AutocompletionSql(std::stringstream& sqlQuery, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 244 | Json::Value& jsonValue, |
| 245 | bool& lastComponent); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 246 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 247 | bool |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 248 | json2PrefixBasedSearchSql(std::stringstream& sqlQuery, |
| 249 | Json::Value& jsonValue); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 250 | |
| 251 | ndn::Name |
| 252 | getQueryResultsName(std::shared_ptr<const ndn::Interest> interest, |
| 253 | const ndn::Name::Component& version); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 254 | |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 255 | std::string |
| 256 | getChronoSyncDigest(); |
| 257 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 258 | protected: |
| 259 | typedef std::unordered_map<ndn::Name, const ndn::RegisteredPrefixId*> RegisteredPrefixList; |
| 260 | // Handle to the Catalog's database |
| 261 | std::shared_ptr<DatabaseHandler> m_databaseHandler; |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 262 | const std::shared_ptr<chronosync::Socket>& m_socket; |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 263 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 264 | // mutex to control critical sections |
| 265 | std::mutex m_mutex; |
| 266 | // @{ needs m_mutex protection |
| 267 | // The Queries we are currently writing to |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 268 | //std::map<std::string, std::shared_ptr<ndn::Data>> m_activeQueryToFirstResponse; |
| 269 | ndn::util::InMemoryStorageLru m_activeQueryToFirstResponse; |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 270 | ndn::util::InMemoryStorageLru m_cache; |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 271 | std::string m_chronosyncDigest; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 272 | // @} |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 273 | RegisteredPrefixList m_registeredPrefixList; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 274 | ndn::Name m_catalogId; // should be replaced with the PK digest |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 275 | std::vector<std::string> m_filterCategoryNames; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 276 | }; |
| 277 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 278 | template <typename DatabaseHandler> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 279 | QueryAdapter<DatabaseHandler>::QueryAdapter(const std::shared_ptr<ndn::Face>& face, |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 280 | const std::shared_ptr<ndn::KeyChain>& keyChain, |
| 281 | const std::shared_ptr<chronosync::Socket>& syncSocket) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 282 | : util::CatalogAdapter(face, keyChain) |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 283 | , m_socket(syncSocket) |
| 284 | , m_activeQueryToFirstResponse(100000) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 285 | , m_cache(250000) |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 286 | , m_chronosyncDigest("0") |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 287 | , m_catalogId("catalogIdPlaceHolder") // initialize for unitests |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 288 | { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | template <typename DatabaseHandler> |
| 292 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 293 | QueryAdapter<DatabaseHandler>::setFilters() |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 294 | { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 295 | ndn::Name queryPrefix = ndn::Name(m_prefix).append("query"); |
| 296 | m_registeredPrefixList[queryPrefix] = m_face->setInterestFilter(ndn::InterestFilter(queryPrefix), |
| 297 | bind(&query::QueryAdapter<DatabaseHandler>::onQueryInterest, |
| 298 | this, _1, _2), |
| 299 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterSuccess, |
| 300 | this, _1), |
| 301 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterFailure, |
| 302 | this, _1, _2)); |
| 303 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 304 | ndn::Name queryResultsPrefix = ndn::Name(m_prefix).append("query-results"); |
| 305 | m_registeredPrefixList[queryResultsPrefix] = |
| 306 | m_face->setInterestFilter(ndn::InterestFilter(ndn::Name(m_prefix) |
| 307 | .append("query-results").append(m_catalogId)), |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 308 | bind(&query::QueryAdapter<DatabaseHandler>::onQueryResultsInterest, |
| 309 | this, _1, _2), |
| 310 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterSuccess, |
| 311 | this, _1), |
| 312 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterFailure, |
| 313 | this, _1, _2)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 314 | |
| 315 | ndn::Name filtersInitializationPrefix = ndn::Name(m_prefix).append("filters-initialization"); |
| 316 | m_registeredPrefixList[filtersInitializationPrefix] = |
| 317 | m_face->setInterestFilter(ndn::InterestFilter(ndn::Name(m_prefix).append("filters-initialization")), |
| 318 | bind(&query::QueryAdapter<DatabaseHandler>::onFiltersInitializationInterest, |
| 319 | this, _1, _2), |
| 320 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterSuccess, |
| 321 | this, _1), |
| 322 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterFailure, |
| 323 | this, _1, _2)); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | template <typename DatabaseHandler> |
| 327 | void |
| 328 | QueryAdapter<DatabaseHandler>::setConfigFile(util::ConfigFile& config, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 329 | const ndn::Name& prefix, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 330 | const std::vector<std::string>& nameFields, |
| 331 | const std::string& databaseTable) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 332 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 333 | m_nameFields = nameFields; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 334 | m_databaseTable = databaseTable; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 335 | config.addSectionHandler("queryAdapter", bind(&QueryAdapter<DatabaseHandler>::onConfig, this, |
| 336 | _1, _2, _3, prefix)); |
| 337 | } |
| 338 | |
| 339 | template <typename DatabaseHandler> |
| 340 | void |
| 341 | QueryAdapter<DatabaseHandler>::onConfig(const util::ConfigSection& section, |
| 342 | bool isDryRun, |
| 343 | const std::string& filename, |
| 344 | const ndn::Name& prefix) |
| 345 | { |
| 346 | using namespace util; |
| 347 | if (isDryRun) { |
| 348 | return; |
| 349 | } |
| 350 | std::string signingId, dbServer, dbName, dbUser, dbPasswd; |
| 351 | for (auto item = section.begin(); |
| 352 | item != section.end(); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 353 | ++item) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 354 | { |
| 355 | if (item->first == "signingId") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 356 | signingId = item->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 357 | if (signingId.empty()) { |
| 358 | throw Error("Empty value for \"signingId\"" |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 359 | " in \"query\" section"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 360 | } |
| 361 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 362 | if (item->first == "filterCategoryNames") { |
| 363 | std::istringstream ss(item->second.get_value<std::string>()); |
| 364 | std::string token; |
| 365 | while(std::getline(ss, token, ',')) { |
| 366 | m_filterCategoryNames.push_back(token); |
| 367 | } |
| 368 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 369 | if (item->first == "database") { |
| 370 | const util::ConfigSection& dataSection = item->second; |
| 371 | for (auto subItem = dataSection.begin(); |
| 372 | subItem != dataSection.end(); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 373 | ++subItem) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 374 | { |
| 375 | if (subItem->first == "dbServer") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 376 | dbServer = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 377 | } |
| 378 | if (subItem->first == "dbName") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 379 | dbName = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 380 | } |
| 381 | if (subItem->first == "dbUser") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 382 | dbUser = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 383 | } |
| 384 | if (subItem->first == "dbPasswd") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 385 | dbPasswd = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 386 | } |
| 387 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 388 | |
| 389 | if (dbServer.empty()){ |
| 390 | throw Error("Invalid value for \"dbServer\"" |
| 391 | " in \"query\" section"); |
| 392 | } |
| 393 | if (dbName.empty()){ |
| 394 | throw Error("Invalid value for \"dbName\"" |
| 395 | " in \"query\" section"); |
| 396 | } |
| 397 | if (dbUser.empty()){ |
| 398 | throw Error("Invalid value for \"dbUser\"" |
| 399 | " in \"query\" section"); |
| 400 | } |
| 401 | if (dbPasswd.empty()){ |
| 402 | throw Error("Invalid value for \"dbPasswd\"" |
| 403 | " in \"query\" section"); |
| 404 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 408 | if (m_filterCategoryNames.empty()) { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 409 | throw Error("Empty value for \"filterCategoryNames\" in \"query\" section"); |
| 410 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 411 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 412 | m_prefix = prefix; |
| 413 | |
| 414 | m_signingId = ndn::Name(signingId); |
| 415 | setCatalogId(); |
| 416 | |
| 417 | util::ConnectionDetails mysqlId(dbServer, dbUser, dbPasswd, dbName); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 418 | setDatabaseHandler(mysqlId); |
| 419 | setFilters(); |
| 420 | } |
| 421 | |
| 422 | template <typename DatabaseHandler> |
| 423 | void |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 424 | QueryAdapter<DatabaseHandler>::setCatalogId() |
| 425 | { |
| 426 | //empty |
| 427 | } |
| 428 | |
| 429 | template <> |
| 430 | void |
| 431 | QueryAdapter<MYSQL>::setCatalogId() |
| 432 | { |
| 433 | // use public key digest as the catalog ID |
| 434 | ndn::Name keyId; |
| 435 | if (m_signingId.empty()) { |
| 436 | keyId = m_keyChain->getDefaultKeyNameForIdentity(m_keyChain->getDefaultIdentity()); |
| 437 | } else { |
| 438 | keyId = m_keyChain->getDefaultKeyNameForIdentity(m_signingId); |
| 439 | } |
| 440 | |
| 441 | std::shared_ptr<ndn::PublicKey> pKey = m_keyChain->getPib().getPublicKey(keyId); |
| 442 | ndn::Block keyDigest = pKey->computeDigest(); |
| 443 | m_catalogId.clear(); |
| 444 | m_catalogId.append(ndn::toHex(*keyDigest.getBuffer())); |
| 445 | } |
| 446 | |
| 447 | template <typename DatabaseHandler> |
| 448 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 449 | QueryAdapter<DatabaseHandler>::setDatabaseHandler(const util::ConnectionDetails& databaseId) |
| 450 | { |
| 451 | //empty |
| 452 | } |
| 453 | |
| 454 | template <> |
| 455 | void |
| 456 | QueryAdapter<MYSQL>::setDatabaseHandler(const util::ConnectionDetails& databaseId) |
| 457 | { |
| 458 | std::shared_ptr<MYSQL> conn = atmos::util::MySQLConnectionSetup(databaseId); |
| 459 | |
| 460 | m_databaseHandler = conn; |
| 461 | } |
| 462 | |
| 463 | template <typename DatabaseHandler> |
| 464 | QueryAdapter<DatabaseHandler>::~QueryAdapter() |
| 465 | { |
| 466 | for (const auto& itr : m_registeredPrefixList) { |
| 467 | if (static_cast<bool>(itr.second)) |
| 468 | m_face->unsetInterestFilter(itr.second); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | template <typename DatabaseHandler> |
| 473 | void |
| 474 | QueryAdapter<DatabaseHandler>::onQueryInterest(const ndn::InterestFilter& filter, |
| 475 | const ndn::Interest& interest) |
| 476 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 477 | _LOG_DEBUG(">> QueryAdapter::onQueryInterest"); |
| 478 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 479 | if (interest.getName().size() != filter.getPrefix().size() + 1) { |
| 480 | // @todo: return a nack |
| 481 | return; |
| 482 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 483 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 484 | std::shared_ptr<const ndn::Interest> interestPtr = interest.shared_from_this(); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 485 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 486 | _LOG_DEBUG("Interest : " << interestPtr->getName()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 487 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 488 | // @todo: use thread pool |
| 489 | std::thread queryThread(&QueryAdapter<DatabaseHandler>::runJsonQuery, |
| 490 | this, |
| 491 | interestPtr); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 492 | queryThread.join(); |
| 493 | } |
| 494 | |
| 495 | template <typename DatabaseHandler> |
| 496 | void |
| 497 | QueryAdapter<DatabaseHandler>::onQueryResultsInterest(const ndn::InterestFilter& filter, |
| 498 | const ndn::Interest& interest) |
| 499 | { |
| 500 | // FIXME Results are currently getting served out of the forwarder's |
| 501 | // CS so we just ignore any retrieval Interests that hit us for |
| 502 | // now. In the future, this should check some form of |
| 503 | // InMemoryStorage. |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 504 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 505 | _LOG_DEBUG(">> QueryAdapter::onQueryResultsInterest"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 506 | |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 507 | auto data = m_cache.find(interest.getName()); |
| 508 | if (data) { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 509 | m_face->put(*data); |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 510 | } |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 511 | |
| 512 | _LOG_DEBUG("<< QueryAdapter::onQueryResultsInterest"); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | template <typename DatabaseHandler> |
| 516 | void |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 517 | QueryAdapter<DatabaseHandler>::onFiltersInitializationInterest(const ndn::InterestFilter& filter, |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 518 | const ndn::Interest& interest) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 519 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 520 | _LOG_DEBUG(">> QueryAdapter::onFiltersInitializationInterest"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 521 | std::shared_ptr<const ndn::Interest> interestPtr = interest.shared_from_this(); |
| 522 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 523 | _LOG_DEBUG("Interest : " << interestPtr->getName()); |
| 524 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 525 | // TODO: save the content in memory, first check the memory, if not exists, start thread to generate it |
| 526 | // Note that if ChronoSync state changes, we need to clear the saved value, and regenerate it |
| 527 | |
| 528 | auto data = m_cache.find(interest.getName()); |
| 529 | if (data) { |
| 530 | m_face->put(*data); |
| 531 | } |
| 532 | else { |
| 533 | std::thread queryThread(&QueryAdapter<DatabaseHandler>::populateFiltersMenu, |
| 534 | this, |
| 535 | interestPtr); |
| 536 | queryThread.join(); |
| 537 | } |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 538 | |
| 539 | _LOG_DEBUG("<< QueryAdapter::onFiltersInitializationInterest"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | template <typename DatabaseHandler> |
| 543 | void |
| 544 | QueryAdapter<DatabaseHandler>::populateFiltersMenu(std::shared_ptr<const ndn::Interest> interest) |
| 545 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 546 | _LOG_DEBUG(">> QueryAdapter::populateFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 547 | Json::Value filters; |
| 548 | Json::FastWriter fastWriter; |
| 549 | getFiltersMenu(filters); |
| 550 | |
| 551 | const std::string filterValue = fastWriter.write(filters); |
| 552 | |
| 553 | if (!filters.empty()) { |
| 554 | ndn::Name filterDataName(interest->getName()); |
| 555 | filterDataName.append("stateVersion");// TODO: should replace with a state version |
| 556 | |
| 557 | const char* payload = filterValue.c_str(); |
| 558 | size_t payloadLength = filterValue.size(); |
| 559 | size_t startIndex = 0, seqNo = 0; |
| 560 | |
| 561 | if (filterValue.length() > PAYLOAD_LIMIT) { |
| 562 | payloadLength = PAYLOAD_LIMIT; |
| 563 | ndn::Name segmentName = ndn::Name(filterDataName).appendSegment(seqNo); |
| 564 | std::shared_ptr<ndn::Data> filterData = std::make_shared<ndn::Data>(segmentName); |
| 565 | filterData->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 566 | filterData->setContent(reinterpret_cast<const uint8_t*>(payload + startIndex), payloadLength); |
| 567 | |
| 568 | signData(*filterData); |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 569 | |
| 570 | _LOG_DEBUG("Populate Filter Data :" << segmentName); |
| 571 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 572 | m_mutex.lock(); |
| 573 | m_cache.insert(*filterData); |
| 574 | try { |
| 575 | m_face->put(*filterData); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 576 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 577 | catch (std::exception& e) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 578 | _LOG_ERROR(e.what()); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 579 | } |
| 580 | m_mutex.unlock(); |
| 581 | |
| 582 | seqNo++; |
| 583 | startIndex = payloadLength * seqNo + 1; |
| 584 | } |
| 585 | payloadLength = filterValue.size() - PAYLOAD_LIMIT * seqNo; |
| 586 | |
| 587 | ndn::Name lastSegment = ndn::Name(filterDataName).appendSegment(seqNo); |
| 588 | std::shared_ptr<ndn::Data> filterData = std::make_shared<ndn::Data>(lastSegment); |
| 589 | filterData->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 590 | filterData->setContent(reinterpret_cast<const uint8_t*>(payload + startIndex), payloadLength); |
| 591 | filterData->setFinalBlockId(ndn::Name::Component::fromSegment(seqNo)); |
| 592 | |
| 593 | signData(*filterData); |
| 594 | m_mutex.lock(); |
| 595 | m_cache.insert(*filterData); |
| 596 | m_face->put(*filterData); |
| 597 | m_mutex.unlock(); |
| 598 | } |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 599 | _LOG_DEBUG("<< QueryAdapter::populateFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | template <typename DatabaseHandler> |
| 603 | void |
| 604 | QueryAdapter<DatabaseHandler>::getFiltersMenu(Json::Value& value) |
| 605 | { |
| 606 | // empty |
| 607 | } |
| 608 | |
| 609 | // get distinct value of each column |
| 610 | template <> |
| 611 | void |
| 612 | QueryAdapter<MYSQL>::getFiltersMenu(Json::Value& value) |
| 613 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 614 | _LOG_DEBUG(">> QueryAdapter::getFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 615 | Json::Value tmp; |
| 616 | |
| 617 | for (size_t i = 0; i < m_filterCategoryNames.size(); i++) { |
| 618 | std::string columnName = m_filterCategoryNames[i]; |
| 619 | std::string getFilterSql("SELECT DISTINCT " + columnName + |
| 620 | " FROM " + m_databaseTable + ";"); |
| 621 | std::string errMsg; |
| 622 | bool success; |
| 623 | |
| 624 | std::shared_ptr<MYSQL_RES> results |
| 625 | = atmos::util::MySQLPerformQuery(m_databaseHandler, getFilterSql, |
| 626 | util::QUERY, success, errMsg); |
| 627 | if (!success) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 628 | _LOG_ERROR(errMsg); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 629 | value.clear(); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | while (MYSQL_ROW row = mysql_fetch_row(results.get())) |
| 634 | { |
| 635 | tmp[columnName].append(row[0]); |
| 636 | } |
| 637 | value.append(tmp); |
| 638 | tmp.clear(); |
| 639 | } |
| 640 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 641 | _LOG_DEBUG("<< QueryAdapter::getFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | template <typename DatabaseHandler> |
| 645 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 646 | QueryAdapter<DatabaseHandler>::signData(ndn::Data& data) |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 647 | { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 648 | if (m_signingId.empty()) |
| 649 | m_keyChain->sign(data); |
| 650 | else { |
| 651 | ndn::Name keyName = m_keyChain->getDefaultKeyNameForIdentity(m_signingId); |
| 652 | ndn::Name certName = m_keyChain->getDefaultCertificateNameForKey(keyName); |
| 653 | m_keyChain->sign(data, certName); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 654 | } |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | template <typename DatabaseHandler> |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 658 | ndn::Name |
| 659 | QueryAdapter<DatabaseHandler>::getQueryResultsName(std::shared_ptr<const ndn::Interest> interest, |
| 660 | const ndn::Name::Component& version) |
| 661 | { |
| 662 | // the server side should conform: http://redmine.named-data.net/projects/ndn-atmos/wiki/Query |
| 663 | // for now, should be /<prefix>/query-results/<catalog-id>/<query-parameters>/<version> |
| 664 | |
| 665 | ndn::Name queryResultName(m_prefix); |
| 666 | queryResultName.append("query-results") |
| 667 | .append(m_catalogId) |
| 668 | .append(interest->getName().get(-1)) |
| 669 | .append(version); |
| 670 | return queryResultName; |
| 671 | } |
| 672 | |
| 673 | template <typename DatabaseHandler> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 674 | std::shared_ptr<ndn::Data> |
| 675 | QueryAdapter<DatabaseHandler>::makeAckData(std::shared_ptr<const ndn::Interest> interest, |
| 676 | const ndn::Name::Component& version) |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 677 | { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 678 | std::string queryResultNameStr(getQueryResultsName(interest, version).toUri()); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 679 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 680 | std::shared_ptr<ndn::Data> ack = std::make_shared<ndn::Data>(interest->getName()); |
| 681 | ack->setContent(reinterpret_cast<const uint8_t*>(queryResultNameStr.c_str()), |
| 682 | queryResultNameStr.length()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 683 | ack->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 684 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 685 | signData(*ack); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 686 | |
| 687 | _LOG_DEBUG("Make ACK : " << queryResultNameStr); |
| 688 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 689 | return ack; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | template <typename DatabaseHandler> |
| 693 | void |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 694 | QueryAdapter<DatabaseHandler>::sendNack(const ndn::Name& dataPrefix) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 695 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 696 | uint64_t segmentNo = 0; |
| 697 | |
| 698 | std::shared_ptr<ndn::Data> nack = |
| 699 | std::make_shared<ndn::Data>(ndn::Name(dataPrefix).appendSegment(segmentNo)); |
| 700 | nack->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 701 | nack->setFinalBlockId(ndn::Name::Component::fromSegment(segmentNo)); |
| 702 | |
| 703 | signData(*nack); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 704 | |
| 705 | _LOG_DEBUG("Send Nack: " << ndn::Name(dataPrefix).appendSegment(segmentNo)); |
| 706 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 707 | m_mutex.lock(); |
| 708 | m_cache.insert(*nack); |
| 709 | m_mutex.unlock(); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | template <typename DatabaseHandler> |
| 714 | bool |
| 715 | QueryAdapter<DatabaseHandler>::json2Sql(std::stringstream& sqlQuery, |
| 716 | Json::Value& jsonValue) |
| 717 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 718 | _LOG_DEBUG(">> QueryAdapter::json2Sql"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 719 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 720 | _LOG_DEBUG(jsonValue.toStyledString()); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 721 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 722 | if (jsonValue.type() != Json::objectValue) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 723 | return false; |
| 724 | } |
| 725 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 726 | sqlQuery << "SELECT name FROM " << m_databaseTable; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 727 | bool input = false; |
| 728 | for (Json::Value::iterator iter = jsonValue.begin(); iter != jsonValue.end(); ++iter) |
| 729 | { |
| 730 | Json::Value key = iter.key(); |
| 731 | Json::Value value = (*iter); |
| 732 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 733 | if (key == Json::nullValue || value == Json::nullValue) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 734 | _LOG_ERROR("Null key or value in JsonValue"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | |
| 738 | // cannot convert to string |
| 739 | if (!key.isConvertibleTo(Json::stringValue) || !value.isConvertibleTo(Json::stringValue)) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 740 | _LOG_ERROR("Malformed JsonQuery string"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
| 744 | if (key.asString().compare("?") == 0) { |
| 745 | continue; |
| 746 | } |
| 747 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 748 | if (input) { |
| 749 | sqlQuery << " AND"; |
| 750 | } else { |
| 751 | sqlQuery << " WHERE"; |
| 752 | } |
| 753 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 754 | sqlQuery << " " << key.asString() << "='" << value.asString() << "'"; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 755 | input = true; |
| 756 | } |
| 757 | |
| 758 | if (!input) { // Force it to be the empty set |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 759 | return false; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 760 | } |
| 761 | sqlQuery << ";"; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 762 | return true; |
| 763 | } |
| 764 | |
| 765 | template <typename DatabaseHandler> |
| 766 | bool |
| 767 | QueryAdapter<DatabaseHandler>::json2AutocompletionSql(std::stringstream& sqlQuery, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 768 | Json::Value& jsonValue, |
| 769 | bool& lastComponent) |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 770 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 771 | _LOG_DEBUG(">> QueryAdapter::json2AutocompletionSql"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 772 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 773 | _LOG_DEBUG(jsonValue.toStyledString()); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 774 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 775 | if (jsonValue.type() != Json::objectValue) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | |
| 779 | std::string typedString; |
| 780 | // get the string in the jsonValue |
| 781 | for (Json::Value::iterator iter = jsonValue.begin(); iter != jsonValue.end(); ++iter) |
| 782 | { |
| 783 | Json::Value key = iter.key(); |
| 784 | Json::Value value = (*iter); |
| 785 | |
| 786 | if (key == Json::nullValue || value == Json::nullValue) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 787 | _LOG_ERROR("Null key or value in JsonValue"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 788 | return false; |
| 789 | } |
| 790 | |
| 791 | // cannot convert to string |
| 792 | if (!key.isConvertibleTo(Json::stringValue) || !value.isConvertibleTo(Json::stringValue)) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 793 | _LOG_ERROR("Malformed JsonQuery string"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | |
| 797 | if (key.asString().compare("?") == 0) { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 798 | typedString = value.asString(); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 799 | // since the front end triggers the autocompletion when users typed '/', |
| 800 | // there must be a '/' at the end, and the first char must be '/' |
| 801 | if (typedString.at(typedString.length() - 1) != '/' || typedString.find("/") != 0) |
| 802 | return false; |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // 1. get the expected column number by parsing the typedString, so we can get the filed name |
| 808 | size_t pos = 0; |
| 809 | size_t start = 1; // start from the 1st char which is not '/' |
| 810 | size_t count = 0; // also the name to query for |
| 811 | std::string token; |
| 812 | std::string delimiter = "/"; |
| 813 | std::map<std::string, std::string> typedComponents; |
| 814 | while ((pos = typedString.find(delimiter, start)) != std::string::npos) { |
| 815 | token = typedString.substr(start, pos - start); |
| 816 | if (count >= m_nameFields.size() - 1) { |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | // add column name and value (token) into map |
| 821 | typedComponents.insert(std::pair<std::string, std::string>(m_nameFields[count], token)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 822 | count++; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 823 | start = pos + 1; |
| 824 | } |
| 825 | |
| 826 | // 2. generate the sql string (append what appears in the typed string, like activity='xxx'), |
| 827 | // return true |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 828 | if (count == m_nameFields.size() - 1) |
| 829 | lastComponent = true; // indicate this query is to query the last component |
| 830 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 831 | bool more = false; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 832 | sqlQuery << "SELECT DISTINCT " << m_nameFields[count] << " FROM " << m_databaseTable; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 833 | for (std::map<std::string, std::string>::iterator it = typedComponents.begin(); |
| 834 | it != typedComponents.end(); ++it) { |
| 835 | if (more) |
| 836 | sqlQuery << " AND"; |
| 837 | else |
| 838 | sqlQuery << " WHERE"; |
| 839 | |
| 840 | sqlQuery << " " << it->first << "='" << it->second << "'"; |
| 841 | |
| 842 | more = true; |
| 843 | } |
| 844 | sqlQuery << ";"; |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | template <typename DatabaseHandler> |
| 849 | bool |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 850 | QueryAdapter<DatabaseHandler>::json2PrefixBasedSearchSql(std::stringstream& sqlQuery, |
| 851 | Json::Value& jsonValue) |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 852 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 853 | _LOG_DEBUG(">> QueryAdapter::json2CompleteSearchSql"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 854 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 855 | _LOG_DEBUG(jsonValue.toStyledString()); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 856 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 857 | if (jsonValue.type() != Json::objectValue) { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 858 | return false; |
| 859 | } |
| 860 | |
| 861 | std::string typedString; |
| 862 | // get the string in the jsonValue |
| 863 | for (Json::Value::iterator iter = jsonValue.begin(); iter != jsonValue.end(); ++iter) |
| 864 | { |
| 865 | Json::Value key = iter.key(); |
| 866 | Json::Value value = (*iter); |
| 867 | |
| 868 | if (key == Json::nullValue || value == Json::nullValue) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 869 | _LOG_ERROR("Null key or value in JsonValue"); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 870 | return false; |
| 871 | } |
| 872 | |
| 873 | // cannot convert to string |
| 874 | if (!key.isConvertibleTo(Json::stringValue) || !value.isConvertibleTo(Json::stringValue)) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 875 | _LOG_ERROR("Malformed JsonQuery string"); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 876 | return false; |
| 877 | } |
| 878 | |
| 879 | if (key.asString().compare("??") == 0) { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 880 | typedString = value.asString(); |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 881 | if (typedString.empty() || typedString.find("/") != 0) |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 882 | return false; |
| 883 | break; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // 1. get the expected column number by parsing the typedString, so we can get the filed name |
| 888 | size_t pos = 0; |
| 889 | size_t start = 1; // start from the 1st char which is not '/' |
| 890 | size_t count = 0; // also the name to query for |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 891 | size_t typedStringLen = typedString.length(); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 892 | std::string token; |
| 893 | std::string delimiter = "/"; |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 894 | std::vector<std::pair<std::string, std::string>> typedComponents; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 895 | while ((pos = typedString.find(delimiter, start)) != std::string::npos) { |
| 896 | token = typedString.substr(start, pos - start); |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 897 | if (count >= m_nameFields.size()) { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
| 901 | // add column name and value (token) into map |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 902 | typedComponents.push_back(std::make_pair(m_nameFields[count], token)); |
| 903 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 904 | count++; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 905 | start = pos + 1; |
| 906 | } |
| 907 | |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 908 | // we may have a component after the last "/" |
| 909 | if (start < typedStringLen) { |
| 910 | typedComponents.push_back(std::make_pair(m_nameFields[count], |
| 911 | typedString.substr(start, typedStringLen - start))); |
| 912 | } |
| 913 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 914 | // 2. generate the sql string (append what appears in the typed string, like activity='xxx'), |
| 915 | // return true |
| 916 | bool more = false; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 917 | sqlQuery << "SELECT name FROM " << m_databaseTable; |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 918 | for (std::vector<std::pair<std::string, std::string>>::iterator it = typedComponents.begin(); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 919 | it != typedComponents.end(); ++it) { |
| 920 | if (more) |
| 921 | sqlQuery << " AND"; |
| 922 | else |
| 923 | sqlQuery << " WHERE"; |
| 924 | |
| 925 | sqlQuery << " " << it->first << "='" << it->second << "'"; |
| 926 | |
| 927 | more = true; |
| 928 | } |
| 929 | sqlQuery << ";"; |
| 930 | return true; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | template <typename DatabaseHandler> |
| 934 | void |
| 935 | QueryAdapter<DatabaseHandler>::runJsonQuery(std::shared_ptr<const ndn::Interest> interest) |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 936 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 937 | _LOG_DEBUG(">> QueryAdapter::runJsonQuery"); |
| 938 | |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 939 | // 1) Strip the prefix off the ndn::Interest's ndn::Name |
| 940 | // +1 to grab JSON component after "query" component |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 941 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 942 | ndn::Name::Component jsonStr = interest->getName()[m_prefix.size()+1]; |
| 943 | // This one cannot parse the JsonQuery correctly, and should be moved to runJsonQuery |
| 944 | const std::string jsonQuery(reinterpret_cast<const char*>(jsonStr.value()), jsonStr.value_size()); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 945 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 946 | if (jsonQuery.length() <= 0) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 947 | // no JSON query, send Nack? |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 948 | return; |
| 949 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 950 | |
| 951 | // the version should be replaced with ChronoSync state digest |
| 952 | ndn::name::Component version; |
| 953 | |
| 954 | if(m_socket != nullptr) { |
| 955 | const ndn::ConstBufferPtr digestPtr = m_socket->getRootDigest(); |
| 956 | std::string digestStr = ndn::toHex(digestPtr->buf(), digestPtr->size()); |
| 957 | _LOG_DEBUG("Original digest" << m_chronosyncDigest); |
| 958 | _LOG_DEBUG("New digest : " << digestStr); |
| 959 | // if the m_chronosyncDigest and the rootdigest are not equal |
| 960 | if (digestStr != m_chronosyncDigest) { |
| 961 | // (1) update chronosyncDigest |
| 962 | // (2) clear all staled ACK data |
| 963 | m_mutex.lock(); |
| 964 | m_chronosyncDigest = digestStr; |
| 965 | m_activeQueryToFirstResponse.erase(ndn::Name("/")); |
| 966 | m_mutex.unlock(); |
| 967 | _LOG_DEBUG("Change digest to " << m_chronosyncDigest); |
| 968 | } |
| 969 | version = ndn::name::Component::fromEscapedString(digestStr); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 970 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 971 | else { |
| 972 | version = ndn::name::Component::fromEscapedString(m_chronosyncDigest); |
| 973 | } |
| 974 | |
| 975 | // try to respond with the inMemoryStorage |
| 976 | m_mutex.lock(); |
| 977 | { // !!! BEGIN CRITICAL SECTION !!! |
| 978 | auto data = m_activeQueryToFirstResponse.find(interest->getName()); |
| 979 | if (data) { |
| 980 | _LOG_DEBUG("Answer with Data in IMS : " << data->getName()); |
| 981 | m_face->put(*data); |
| 982 | m_mutex.unlock(); |
| 983 | return; |
| 984 | } |
| 985 | } // !!! END CRITICAL SECTION !!! |
| 986 | m_mutex.unlock(); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 987 | |
| 988 | // 2) From the remainder of the ndn::Interest's ndn::Name, get the JSON out |
| 989 | Json::Value parsedFromString; |
| 990 | Json::Reader reader; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 991 | if (!reader.parse(jsonQuery, parsedFromString)) { |
| 992 | // @todo: send NACK? |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 993 | _LOG_ERROR("Cannot parse the JsonQuery"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 994 | return; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 995 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 996 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 997 | std::shared_ptr<ndn::Data> ack = makeAckData(interest, version); |
| 998 | |
| 999 | m_mutex.lock(); |
| 1000 | { // !!! BEGIN CRITICAL SECTION !!! |
| 1001 | // An unusual race-condition case, which requires things like PIT aggregation to be off. |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1002 | auto data = m_activeQueryToFirstResponse.find(interest->getName()); |
| 1003 | if (data) { |
| 1004 | m_face->put(*data); |
| 1005 | m_mutex.unlock(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1006 | return; |
| 1007 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1008 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1009 | // This is where things are expensive so we save them for the lock |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1010 | // note that we ack the query with the cached ACK messages, but we should remove the ACKs |
| 1011 | // that conatin the old version when ChronoSync is updated |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1012 | m_activeQueryToFirstResponse.insert(*ack); |
| 1013 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1014 | m_face->put(*ack); |
| 1015 | } // !!! END CRITICAL SECTION !!! |
| 1016 | m_mutex.unlock(); |
| 1017 | |
| 1018 | // 3) Convert the JSON Query into a MySQL one |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1019 | bool autocomplete = false, lastComponent = false; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1020 | std::stringstream sqlQuery; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1021 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1022 | ndn::Name segmentPrefix(getQueryResultsName(interest, version)); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1023 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1024 | Json::Value tmp; |
| 1025 | // expect the autocomplete and the component-based query are separate |
| 1026 | // if JSON::Value contains ? as key, is autocompletion |
| 1027 | if (parsedFromString.get("?", tmp) != tmp) { |
| 1028 | autocomplete = true; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1029 | if (!json2AutocompletionSql(sqlQuery, parsedFromString, lastComponent)) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1030 | sendNack(segmentPrefix); |
| 1031 | return; |
| 1032 | } |
| 1033 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1034 | else if (parsedFromString.get("??", tmp) != tmp) { |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 1035 | if (!json2PrefixBasedSearchSql(sqlQuery, parsedFromString)) { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1036 | sendNack(segmentPrefix); |
| 1037 | return; |
| 1038 | } |
| 1039 | } |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1040 | else { |
| 1041 | if (!json2Sql(sqlQuery, parsedFromString)) { |
| 1042 | sendNack(segmentPrefix); |
| 1043 | return; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | // 4) Run the Query |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1048 | prepareSegments(segmentPrefix, sqlQuery.str(), autocomplete, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | template <typename DatabaseHandler> |
| 1052 | void |
| 1053 | QueryAdapter<DatabaseHandler>::prepareSegments(const ndn::Name& segmentPrefix, |
| 1054 | const std::string& sqlString, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1055 | bool autocomplete, |
| 1056 | bool lastComponent) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1057 | { |
| 1058 | // empty |
| 1059 | } |
| 1060 | |
| 1061 | // prepareSegments specilization function |
| 1062 | template<> |
| 1063 | void |
| 1064 | QueryAdapter<MYSQL>::prepareSegments(const ndn::Name& segmentPrefix, |
| 1065 | const std::string& sqlString, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1066 | bool autocomplete, |
| 1067 | bool lastComponent) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1068 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1069 | _LOG_DEBUG(">> QueryAdapter::prepareSegments"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1070 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1071 | _LOG_DEBUG(sqlString); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1072 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1073 | std::string errMsg; |
| 1074 | bool success; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1075 | // 4) Run the Query |
| 1076 | std::shared_ptr<MYSQL_RES> results |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1077 | = atmos::util::MySQLPerformQuery(m_databaseHandler, sqlString, util::QUERY, success, errMsg); |
| 1078 | if (!success) |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1079 | _LOG_ERROR(errMsg); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1080 | |
| 1081 | if (!results) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1082 | _LOG_ERROR("NULL MYSQL_RES for" << sqlString); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1083 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1084 | // @todo: throw runtime error or log the error message? |
| 1085 | return; |
| 1086 | } |
| 1087 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1088 | uint64_t resultCount = mysql_num_rows(results.get()); |
| 1089 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1090 | _LOG_DEBUG("Query resuls contain " << resultCount << "rows"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1091 | |
| 1092 | MYSQL_ROW row; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1093 | uint64_t segmentNo = 0; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1094 | Json::Value tmp; |
| 1095 | Json::Value resultJson; |
| 1096 | Json::FastWriter fastWriter; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1097 | |
| 1098 | uint64_t viewStart = 0, viewEnd = 0; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1099 | while ((row = mysql_fetch_row(results.get()))) |
| 1100 | { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1101 | tmp.append(row[0]); |
| 1102 | const std::string tmpString = fastWriter.write(tmp); |
| 1103 | if (tmpString.length() > PAYLOAD_LIMIT) { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1104 | std::shared_ptr<ndn::Data> data |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1105 | = makeReplyData(segmentPrefix, resultJson, segmentNo, false, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1106 | autocomplete, resultCount, viewStart, viewEnd, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1107 | m_mutex.lock(); |
| 1108 | m_cache.insert(*data); |
| 1109 | m_mutex.unlock(); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1110 | tmp.clear(); |
| 1111 | resultJson.clear(); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1112 | segmentNo++; |
| 1113 | viewStart = viewEnd + 1; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1114 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1115 | resultJson.append(row[0]); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1116 | viewEnd++; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1117 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1118 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1119 | std::shared_ptr<ndn::Data> data |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1120 | = makeReplyData(segmentPrefix, resultJson, segmentNo, true, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1121 | autocomplete, resultCount, viewStart, viewEnd, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1122 | m_mutex.lock(); |
| 1123 | m_cache.insert(*data); |
| 1124 | m_mutex.unlock(); |
| 1125 | } |
| 1126 | |
| 1127 | template <typename DatabaseHandler> |
| 1128 | std::shared_ptr<ndn::Data> |
| 1129 | QueryAdapter<DatabaseHandler>::makeReplyData(const ndn::Name& segmentPrefix, |
| 1130 | const Json::Value& value, |
| 1131 | uint64_t segmentNo, |
| 1132 | bool isFinalBlock, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1133 | bool isAutocomplete, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1134 | uint64_t resultCount, |
| 1135 | uint64_t viewStart, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1136 | uint64_t viewEnd, |
| 1137 | bool lastComponent) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1138 | { |
| 1139 | Json::Value entry; |
| 1140 | Json::FastWriter fastWriter; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1141 | |
| 1142 | entry["resultCount"] = Json::UInt64(resultCount);; |
| 1143 | entry["viewStart"] = Json::UInt64(viewStart); |
| 1144 | entry["viewEnd"] = Json::UInt64(viewEnd); |
| 1145 | |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1146 | if (lastComponent) |
| 1147 | entry["lastComponent"] = Json::Value(true); |
| 1148 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1149 | _LOG_DEBUG("resultCount " << resultCount << ";" |
| 1150 | << "viewStart " << viewStart << ";" |
| 1151 | << "viewEnd " << viewEnd); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1152 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1153 | if (isAutocomplete) { |
| 1154 | entry["next"] = value; |
| 1155 | } else { |
| 1156 | entry["results"] = value; |
| 1157 | } |
| 1158 | const std::string jsonMessage = fastWriter.write(entry); |
| 1159 | const char* payload = jsonMessage.c_str(); |
| 1160 | size_t payloadLength = jsonMessage.size() + 1; |
| 1161 | ndn::Name segmentName(segmentPrefix); |
| 1162 | segmentName.appendSegment(segmentNo); |
| 1163 | |
| 1164 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName); |
| 1165 | data->setContent(reinterpret_cast<const uint8_t*>(payload), payloadLength); |
| 1166 | data->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 1167 | |
| 1168 | if (isFinalBlock) { |
| 1169 | data->setFinalBlockId(ndn::Name::Component::fromSegment(segmentNo)); |
| 1170 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1171 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1172 | _LOG_DEBUG(segmentName); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1173 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1174 | signData(*data); |
| 1175 | return data; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 1176 | } |
| 1177 | |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1178 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 1179 | } // namespace query |
| 1180 | } // namespace atmos |
| 1181 | #endif //ATMOS_QUERY_QUERY_ADAPTER_HPP |