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