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 |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 114 | onIncomingQueryInterest(const ndn::InterestFilter& filter, const ndn::Interest& interest); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 115 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 116 | /** |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 117 | * Handles requests for responses to an filter initialization request |
| 118 | * |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 119 | * @param interest: Interest that needs to be handled |
| 120 | */ |
| 121 | virtual void |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 122 | onFiltersInitializationInterest(std::shared_ptr<const ndn::Interest> interest); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 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 | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 245 | std::string |
| 246 | getChronoSyncDigest(); |
| 247 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 248 | protected: |
| 249 | typedef std::unordered_map<ndn::Name, const ndn::RegisteredPrefixId*> RegisteredPrefixList; |
| 250 | // Handle to the Catalog's database |
| 251 | std::shared_ptr<DatabaseHandler> m_databaseHandler; |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 252 | const std::shared_ptr<chronosync::Socket>& m_socket; |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 253 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 254 | // mutex to control critical sections |
| 255 | std::mutex m_mutex; |
| 256 | // @{ needs m_mutex protection |
| 257 | // The Queries we are currently writing to |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 258 | //std::map<std::string, std::shared_ptr<ndn::Data>> m_activeQueryToFirstResponse; |
| 259 | ndn::util::InMemoryStorageLru m_activeQueryToFirstResponse; |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 260 | ndn::util::InMemoryStorageLru m_cache; |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 261 | std::string m_chronosyncDigest; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 262 | // @} |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 263 | RegisteredPrefixList m_registeredPrefixList; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 264 | ndn::Name m_catalogId; // should be replaced with the PK digest |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 265 | std::vector<std::string> m_filterCategoryNames; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 266 | }; |
| 267 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 268 | template <typename DatabaseHandler> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 269 | QueryAdapter<DatabaseHandler>::QueryAdapter(const std::shared_ptr<ndn::Face>& face, |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 270 | const std::shared_ptr<ndn::KeyChain>& keyChain, |
| 271 | const std::shared_ptr<chronosync::Socket>& syncSocket) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 272 | : util::CatalogAdapter(face, keyChain) |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 273 | , m_socket(syncSocket) |
| 274 | , m_activeQueryToFirstResponse(100000) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 275 | , m_cache(250000) |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 276 | , m_chronosyncDigest("0") |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 277 | , m_catalogId("catalogIdPlaceHolder") // initialize for unitests |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 278 | { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | template <typename DatabaseHandler> |
| 282 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 283 | QueryAdapter<DatabaseHandler>::setFilters() |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 284 | { |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 285 | m_registeredPrefixList[m_prefix] = m_face->setInterestFilter(ndn::InterestFilter(m_prefix), |
| 286 | bind(&query::QueryAdapter<DatabaseHandler>::onIncomingQueryInterest, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 287 | this, _1, _2), |
| 288 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterSuccess, |
| 289 | this, _1), |
| 290 | bind(&query::QueryAdapter<DatabaseHandler>::onRegisterFailure, |
| 291 | this, _1, _2)); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | template <typename DatabaseHandler> |
| 295 | void |
| 296 | QueryAdapter<DatabaseHandler>::setConfigFile(util::ConfigFile& config, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 297 | const ndn::Name& prefix, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 298 | const std::vector<std::string>& nameFields, |
| 299 | const std::string& databaseTable) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 300 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 301 | m_nameFields = nameFields; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 302 | m_databaseTable = databaseTable; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 303 | config.addSectionHandler("queryAdapter", bind(&QueryAdapter<DatabaseHandler>::onConfig, this, |
| 304 | _1, _2, _3, prefix)); |
| 305 | } |
| 306 | |
| 307 | template <typename DatabaseHandler> |
| 308 | void |
| 309 | QueryAdapter<DatabaseHandler>::onConfig(const util::ConfigSection& section, |
| 310 | bool isDryRun, |
| 311 | const std::string& filename, |
| 312 | const ndn::Name& prefix) |
| 313 | { |
| 314 | using namespace util; |
| 315 | if (isDryRun) { |
| 316 | return; |
| 317 | } |
| 318 | std::string signingId, dbServer, dbName, dbUser, dbPasswd; |
| 319 | for (auto item = section.begin(); |
| 320 | item != section.end(); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 321 | ++item) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 322 | { |
| 323 | if (item->first == "signingId") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 324 | signingId = item->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 325 | if (signingId.empty()) { |
| 326 | throw Error("Empty value for \"signingId\"" |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 327 | " in \"query\" section"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 328 | } |
| 329 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 330 | if (item->first == "filterCategoryNames") { |
| 331 | std::istringstream ss(item->second.get_value<std::string>()); |
| 332 | std::string token; |
| 333 | while(std::getline(ss, token, ',')) { |
| 334 | m_filterCategoryNames.push_back(token); |
| 335 | } |
| 336 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 337 | if (item->first == "database") { |
| 338 | const util::ConfigSection& dataSection = item->second; |
| 339 | for (auto subItem = dataSection.begin(); |
| 340 | subItem != dataSection.end(); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 341 | ++subItem) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 342 | { |
| 343 | if (subItem->first == "dbServer") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 344 | dbServer = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 345 | } |
| 346 | if (subItem->first == "dbName") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 347 | dbName = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 348 | } |
| 349 | if (subItem->first == "dbUser") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 350 | dbUser = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 351 | } |
| 352 | if (subItem->first == "dbPasswd") { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 353 | dbPasswd = subItem->second.get_value<std::string>(); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 354 | } |
| 355 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 356 | |
| 357 | if (dbServer.empty()){ |
| 358 | throw Error("Invalid value for \"dbServer\"" |
| 359 | " in \"query\" section"); |
| 360 | } |
| 361 | if (dbName.empty()){ |
| 362 | throw Error("Invalid value for \"dbName\"" |
| 363 | " in \"query\" section"); |
| 364 | } |
| 365 | if (dbUser.empty()){ |
| 366 | throw Error("Invalid value for \"dbUser\"" |
| 367 | " in \"query\" section"); |
| 368 | } |
| 369 | if (dbPasswd.empty()){ |
| 370 | throw Error("Invalid value for \"dbPasswd\"" |
| 371 | " in \"query\" section"); |
| 372 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 376 | if (m_filterCategoryNames.empty()) { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 377 | throw Error("Empty value for \"filterCategoryNames\" in \"query\" section"); |
| 378 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 379 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 380 | m_prefix = prefix; |
| 381 | |
| 382 | m_signingId = ndn::Name(signingId); |
| 383 | setCatalogId(); |
| 384 | |
| 385 | util::ConnectionDetails mysqlId(dbServer, dbUser, dbPasswd, dbName); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 386 | setDatabaseHandler(mysqlId); |
| 387 | setFilters(); |
| 388 | } |
| 389 | |
| 390 | template <typename DatabaseHandler> |
| 391 | void |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 392 | QueryAdapter<DatabaseHandler>::setCatalogId() |
| 393 | { |
| 394 | //empty |
| 395 | } |
| 396 | |
| 397 | template <> |
| 398 | void |
| 399 | QueryAdapter<MYSQL>::setCatalogId() |
| 400 | { |
| 401 | // use public key digest as the catalog ID |
| 402 | ndn::Name keyId; |
| 403 | if (m_signingId.empty()) { |
| 404 | keyId = m_keyChain->getDefaultKeyNameForIdentity(m_keyChain->getDefaultIdentity()); |
| 405 | } else { |
| 406 | keyId = m_keyChain->getDefaultKeyNameForIdentity(m_signingId); |
| 407 | } |
| 408 | |
| 409 | std::shared_ptr<ndn::PublicKey> pKey = m_keyChain->getPib().getPublicKey(keyId); |
| 410 | ndn::Block keyDigest = pKey->computeDigest(); |
| 411 | m_catalogId.clear(); |
| 412 | m_catalogId.append(ndn::toHex(*keyDigest.getBuffer())); |
| 413 | } |
| 414 | |
| 415 | template <typename DatabaseHandler> |
| 416 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 417 | QueryAdapter<DatabaseHandler>::setDatabaseHandler(const util::ConnectionDetails& databaseId) |
| 418 | { |
| 419 | //empty |
| 420 | } |
| 421 | |
| 422 | template <> |
| 423 | void |
| 424 | QueryAdapter<MYSQL>::setDatabaseHandler(const util::ConnectionDetails& databaseId) |
| 425 | { |
| 426 | std::shared_ptr<MYSQL> conn = atmos::util::MySQLConnectionSetup(databaseId); |
| 427 | |
| 428 | m_databaseHandler = conn; |
| 429 | } |
| 430 | |
| 431 | template <typename DatabaseHandler> |
| 432 | QueryAdapter<DatabaseHandler>::~QueryAdapter() |
| 433 | { |
| 434 | for (const auto& itr : m_registeredPrefixList) { |
| 435 | if (static_cast<bool>(itr.second)) |
| 436 | m_face->unsetInterestFilter(itr.second); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | template <typename DatabaseHandler> |
| 441 | void |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 442 | QueryAdapter<DatabaseHandler>::onIncomingQueryInterest(const ndn::InterestFilter& filter, |
| 443 | const ndn::Interest& interest) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 444 | { |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 445 | _LOG_DEBUG(">> QueryAdapter::onIncomingQueryInterest"); |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 446 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 447 | // Interest must carry component "initialization" or "query" |
| 448 | if (interest.getName().size() < filter.getPrefix().size()) { |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 449 | // @todo: return a nack |
| 450 | return; |
| 451 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 452 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 453 | _LOG_DEBUG("Interest : " << interest.getName()); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 454 | std::shared_ptr<const ndn::Interest> interestPtr = interest.shared_from_this(); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 455 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 456 | if (interest.getName()[filter.getPrefix().size()] == ndn::Name::Component("filters-initialization")) { |
| 457 | std::thread queryThread(&QueryAdapter<DatabaseHandler>::onFiltersInitializationInterest, |
| 458 | this, |
| 459 | interestPtr); |
| 460 | queryThread.join(); |
| 461 | } |
| 462 | else if (interest.getName()[filter.getPrefix().size()] == ndn::Name::Component("query")) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 463 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 464 | auto data = m_cache.find(interest); |
| 465 | if (data) { |
| 466 | m_face->put(*data); |
| 467 | return; |
| 468 | } |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 469 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 470 | // catalog must strip sequence number in an Interest for further process |
| 471 | if (interest.getName().size() > (filter.getPrefix().size() + 2)) { |
| 472 | // Interest carries sequence number, only grip the main part |
| 473 | // e.g., /hep/query/<query-params>/<version>/#seq |
| 474 | ndn::Interest queryInterest(interest.getName().getPrefix(filter.getPrefix().size() + 2)); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 475 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 476 | auto data = m_cache.find(queryInterest); |
| 477 | if (data) { |
| 478 | // catalog has generated some data, but still working on it |
| 479 | return; |
| 480 | } |
| 481 | interestPtr = queryInterest.shared_from_this(); |
| 482 | } |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 483 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 484 | std::thread queryThread(&QueryAdapter<DatabaseHandler>::runJsonQuery, |
| 485 | this, |
| 486 | interestPtr); |
| 487 | queryThread.join(); |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 488 | } |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 489 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 490 | // ignore other Interests |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | template <typename DatabaseHandler> |
| 494 | void |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 495 | QueryAdapter<DatabaseHandler>::onFiltersInitializationInterest(std::shared_ptr<const ndn::Interest> interest) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 496 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 497 | _LOG_DEBUG(">> QueryAdapter::onFiltersInitializationInterest"); |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 498 | |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 499 | if(m_socket != nullptr) { |
| 500 | const ndn::ConstBufferPtr digestPtr = m_socket->getRootDigest(); |
| 501 | std::string digestStr = ndn::toHex(digestPtr->buf(), digestPtr->size()); |
| 502 | _LOG_DEBUG("Original digest :" << m_chronosyncDigest); |
| 503 | _LOG_DEBUG("New digest : " << digestStr); |
| 504 | // if the m_chronosyncDigest and the rootdigest are not equal |
| 505 | if (digestStr != m_chronosyncDigest) { |
| 506 | // (1) update chronosyncDigest |
| 507 | // (2) clear all staled ACK data |
| 508 | m_mutex.lock(); |
| 509 | m_chronosyncDigest = digestStr; |
| 510 | m_activeQueryToFirstResponse.erase(ndn::Name("/")); |
| 511 | m_mutex.unlock(); |
| 512 | _LOG_DEBUG("Change digest to " << m_chronosyncDigest); |
| 513 | } |
| 514 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 515 | |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 516 | auto data = m_activeQueryToFirstResponse.find(*interest); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 517 | if (data) { |
| 518 | m_face->put(*data); |
| 519 | } |
| 520 | else { |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 521 | populateFiltersMenu(interest); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 522 | } |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 523 | |
| 524 | _LOG_DEBUG("<< QueryAdapter::onFiltersInitializationInterest"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | template <typename DatabaseHandler> |
| 528 | void |
| 529 | QueryAdapter<DatabaseHandler>::populateFiltersMenu(std::shared_ptr<const ndn::Interest> interest) |
| 530 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 531 | _LOG_DEBUG(">> QueryAdapter::populateFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 532 | Json::Value filters; |
| 533 | Json::FastWriter fastWriter; |
| 534 | getFiltersMenu(filters); |
| 535 | |
| 536 | const std::string filterValue = fastWriter.write(filters); |
| 537 | |
| 538 | if (!filters.empty()) { |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 539 | // use /<prefix>/filters-initialization/<seg> as data name |
| 540 | ndn::Name filterDataName(interest->getName().getPrefix(-1)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 541 | |
| 542 | const char* payload = filterValue.c_str(); |
| 543 | size_t payloadLength = filterValue.size(); |
| 544 | size_t startIndex = 0, seqNo = 0; |
| 545 | |
| 546 | if (filterValue.length() > PAYLOAD_LIMIT) { |
| 547 | payloadLength = PAYLOAD_LIMIT; |
| 548 | ndn::Name segmentName = ndn::Name(filterDataName).appendSegment(seqNo); |
| 549 | std::shared_ptr<ndn::Data> filterData = std::make_shared<ndn::Data>(segmentName); |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 550 | // freshnessPeriod 0 means permanent? |
| 551 | filterData->setFreshnessPeriod(ndn::time::milliseconds(10)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 552 | filterData->setContent(reinterpret_cast<const uint8_t*>(payload + startIndex), payloadLength); |
| 553 | |
| 554 | signData(*filterData); |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 555 | |
| 556 | _LOG_DEBUG("Populate Filter Data :" << segmentName); |
| 557 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 558 | m_mutex.lock(); |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 559 | // save the filter results in the activeQueryToFirstResponse structure |
| 560 | // when version changes, the activeQueryToFirstResponse should be cleaned |
| 561 | m_activeQueryToFirstResponse.insert(*filterData); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 562 | try { |
| 563 | m_face->put(*filterData); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 564 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 565 | catch (std::exception& e) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 566 | _LOG_ERROR(e.what()); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 567 | } |
| 568 | m_mutex.unlock(); |
| 569 | |
| 570 | seqNo++; |
| 571 | startIndex = payloadLength * seqNo + 1; |
| 572 | } |
| 573 | payloadLength = filterValue.size() - PAYLOAD_LIMIT * seqNo; |
| 574 | |
| 575 | ndn::Name lastSegment = ndn::Name(filterDataName).appendSegment(seqNo); |
| 576 | std::shared_ptr<ndn::Data> filterData = std::make_shared<ndn::Data>(lastSegment); |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 577 | filterData->setFreshnessPeriod(ndn::time::milliseconds(10)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 578 | filterData->setContent(reinterpret_cast<const uint8_t*>(payload + startIndex), payloadLength); |
| 579 | filterData->setFinalBlockId(ndn::Name::Component::fromSegment(seqNo)); |
| 580 | |
| 581 | signData(*filterData); |
| 582 | m_mutex.lock(); |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 583 | m_activeQueryToFirstResponse.insert(*filterData); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 584 | m_face->put(*filterData); |
| 585 | m_mutex.unlock(); |
| 586 | } |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 587 | _LOG_DEBUG("<< QueryAdapter::populateFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | template <typename DatabaseHandler> |
| 591 | void |
| 592 | QueryAdapter<DatabaseHandler>::getFiltersMenu(Json::Value& value) |
| 593 | { |
| 594 | // empty |
| 595 | } |
| 596 | |
| 597 | // get distinct value of each column |
| 598 | template <> |
| 599 | void |
| 600 | QueryAdapter<MYSQL>::getFiltersMenu(Json::Value& value) |
| 601 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 602 | _LOG_DEBUG(">> QueryAdapter::getFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 603 | Json::Value tmp; |
| 604 | |
| 605 | for (size_t i = 0; i < m_filterCategoryNames.size(); i++) { |
| 606 | std::string columnName = m_filterCategoryNames[i]; |
| 607 | std::string getFilterSql("SELECT DISTINCT " + columnName + |
| 608 | " FROM " + m_databaseTable + ";"); |
| 609 | std::string errMsg; |
| 610 | bool success; |
| 611 | |
| 612 | std::shared_ptr<MYSQL_RES> results |
| 613 | = atmos::util::MySQLPerformQuery(m_databaseHandler, getFilterSql, |
| 614 | util::QUERY, success, errMsg); |
| 615 | if (!success) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 616 | _LOG_ERROR(errMsg); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 617 | value.clear(); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | while (MYSQL_ROW row = mysql_fetch_row(results.get())) |
| 622 | { |
| 623 | tmp[columnName].append(row[0]); |
| 624 | } |
| 625 | value.append(tmp); |
| 626 | tmp.clear(); |
| 627 | } |
| 628 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 629 | _LOG_DEBUG("<< QueryAdapter::getFiltersMenu"); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | template <typename DatabaseHandler> |
| 633 | void |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 634 | QueryAdapter<DatabaseHandler>::signData(ndn::Data& data) |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 635 | { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 636 | if (m_signingId.empty()) |
| 637 | m_keyChain->sign(data); |
| 638 | else { |
| 639 | ndn::Name keyName = m_keyChain->getDefaultKeyNameForIdentity(m_signingId); |
| 640 | ndn::Name certName = m_keyChain->getDefaultCertificateNameForKey(keyName); |
| 641 | m_keyChain->sign(data, certName); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 642 | } |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | template <typename DatabaseHandler> |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 646 | ndn::Name |
| 647 | QueryAdapter<DatabaseHandler>::getQueryResultsName(std::shared_ptr<const ndn::Interest> interest, |
| 648 | const ndn::Name::Component& version) |
| 649 | { |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 650 | // use generic name, instead of specific one |
| 651 | ndn::Name queryResultName = interest->getName(); |
| 652 | queryResultName.append(version); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 653 | return queryResultName; |
| 654 | } |
| 655 | |
| 656 | template <typename DatabaseHandler> |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 657 | std::shared_ptr<ndn::Data> |
| 658 | QueryAdapter<DatabaseHandler>::makeAckData(std::shared_ptr<const ndn::Interest> interest, |
| 659 | const ndn::Name::Component& version) |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 660 | { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 661 | std::string queryResultNameStr(getQueryResultsName(interest, version).toUri()); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 662 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 663 | std::shared_ptr<ndn::Data> ack = std::make_shared<ndn::Data>(interest->getName()); |
| 664 | ack->setContent(reinterpret_cast<const uint8_t*>(queryResultNameStr.c_str()), |
| 665 | queryResultNameStr.length()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 666 | ack->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 667 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 668 | signData(*ack); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 669 | |
| 670 | _LOG_DEBUG("Make ACK : " << queryResultNameStr); |
| 671 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 672 | return ack; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | template <typename DatabaseHandler> |
| 676 | void |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 677 | QueryAdapter<DatabaseHandler>::sendNack(const ndn::Name& dataPrefix) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 678 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 679 | uint64_t segmentNo = 0; |
| 680 | |
| 681 | std::shared_ptr<ndn::Data> nack = |
| 682 | std::make_shared<ndn::Data>(ndn::Name(dataPrefix).appendSegment(segmentNo)); |
| 683 | nack->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 684 | nack->setFinalBlockId(ndn::Name::Component::fromSegment(segmentNo)); |
| 685 | |
| 686 | signData(*nack); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 687 | |
| 688 | _LOG_DEBUG("Send Nack: " << ndn::Name(dataPrefix).appendSegment(segmentNo)); |
| 689 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 690 | m_mutex.lock(); |
| 691 | m_cache.insert(*nack); |
| 692 | m_mutex.unlock(); |
| 693 | } |
| 694 | |
| 695 | |
| 696 | template <typename DatabaseHandler> |
| 697 | bool |
| 698 | QueryAdapter<DatabaseHandler>::json2Sql(std::stringstream& sqlQuery, |
| 699 | Json::Value& jsonValue) |
| 700 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 701 | _LOG_DEBUG(">> QueryAdapter::json2Sql"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 702 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 703 | _LOG_DEBUG(jsonValue.toStyledString()); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 704 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 705 | if (jsonValue.type() != Json::objectValue) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 709 | sqlQuery << "SELECT name FROM " << m_databaseTable; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 710 | bool input = false; |
| 711 | for (Json::Value::iterator iter = jsonValue.begin(); iter != jsonValue.end(); ++iter) |
| 712 | { |
| 713 | Json::Value key = iter.key(); |
| 714 | Json::Value value = (*iter); |
| 715 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 716 | if (key == Json::nullValue || value == Json::nullValue) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 717 | _LOG_ERROR("Null key or value in JsonValue"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | |
| 721 | // cannot convert to string |
| 722 | if (!key.isConvertibleTo(Json::stringValue) || !value.isConvertibleTo(Json::stringValue)) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 723 | _LOG_ERROR("Malformed JsonQuery string"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
| 727 | if (key.asString().compare("?") == 0) { |
| 728 | continue; |
| 729 | } |
| 730 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 731 | if (input) { |
| 732 | sqlQuery << " AND"; |
| 733 | } else { |
| 734 | sqlQuery << " WHERE"; |
| 735 | } |
| 736 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 737 | sqlQuery << " " << key.asString() << "='" << value.asString() << "'"; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 738 | input = true; |
| 739 | } |
| 740 | |
| 741 | if (!input) { // Force it to be the empty set |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 742 | return false; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 743 | } |
| 744 | sqlQuery << ";"; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 745 | return true; |
| 746 | } |
| 747 | |
| 748 | template <typename DatabaseHandler> |
| 749 | bool |
| 750 | QueryAdapter<DatabaseHandler>::json2AutocompletionSql(std::stringstream& sqlQuery, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 751 | Json::Value& jsonValue, |
| 752 | bool& lastComponent) |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 753 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 754 | _LOG_DEBUG(">> QueryAdapter::json2AutocompletionSql"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 755 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 756 | _LOG_DEBUG(jsonValue.toStyledString()); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 757 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 758 | if (jsonValue.type() != Json::objectValue) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | |
| 762 | std::string typedString; |
| 763 | // get the string in the jsonValue |
| 764 | for (Json::Value::iterator iter = jsonValue.begin(); iter != jsonValue.end(); ++iter) |
| 765 | { |
| 766 | Json::Value key = iter.key(); |
| 767 | Json::Value value = (*iter); |
| 768 | |
| 769 | if (key == Json::nullValue || value == Json::nullValue) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 770 | _LOG_ERROR("Null key or value in JsonValue"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 771 | return false; |
| 772 | } |
| 773 | |
| 774 | // cannot convert to string |
| 775 | if (!key.isConvertibleTo(Json::stringValue) || !value.isConvertibleTo(Json::stringValue)) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 776 | _LOG_ERROR("Malformed JsonQuery string"); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 777 | return false; |
| 778 | } |
| 779 | |
| 780 | if (key.asString().compare("?") == 0) { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 781 | typedString = value.asString(); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 782 | // since the front end triggers the autocompletion when users typed '/', |
| 783 | // there must be a '/' at the end, and the first char must be '/' |
| 784 | if (typedString.at(typedString.length() - 1) != '/' || typedString.find("/") != 0) |
| 785 | return false; |
| 786 | break; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // 1. get the expected column number by parsing the typedString, so we can get the filed name |
| 791 | size_t pos = 0; |
| 792 | size_t start = 1; // start from the 1st char which is not '/' |
| 793 | size_t count = 0; // also the name to query for |
| 794 | std::string token; |
| 795 | std::string delimiter = "/"; |
| 796 | std::map<std::string, std::string> typedComponents; |
| 797 | while ((pos = typedString.find(delimiter, start)) != std::string::npos) { |
| 798 | token = typedString.substr(start, pos - start); |
| 799 | if (count >= m_nameFields.size() - 1) { |
| 800 | return false; |
| 801 | } |
| 802 | |
| 803 | // add column name and value (token) into map |
| 804 | typedComponents.insert(std::pair<std::string, std::string>(m_nameFields[count], token)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 805 | count++; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 806 | start = pos + 1; |
| 807 | } |
| 808 | |
| 809 | // 2. generate the sql string (append what appears in the typed string, like activity='xxx'), |
| 810 | // return true |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 811 | if (count == m_nameFields.size() - 1) |
| 812 | lastComponent = true; // indicate this query is to query the last component |
| 813 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 814 | bool more = false; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 815 | sqlQuery << "SELECT DISTINCT " << m_nameFields[count] << " FROM " << m_databaseTable; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 816 | for (std::map<std::string, std::string>::iterator it = typedComponents.begin(); |
| 817 | it != typedComponents.end(); ++it) { |
| 818 | if (more) |
| 819 | sqlQuery << " AND"; |
| 820 | else |
| 821 | sqlQuery << " WHERE"; |
| 822 | |
| 823 | sqlQuery << " " << it->first << "='" << it->second << "'"; |
| 824 | |
| 825 | more = true; |
| 826 | } |
| 827 | sqlQuery << ";"; |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | template <typename DatabaseHandler> |
| 832 | bool |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 833 | QueryAdapter<DatabaseHandler>::json2PrefixBasedSearchSql(std::stringstream& sqlQuery, |
| 834 | Json::Value& jsonValue) |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 835 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 836 | _LOG_DEBUG(">> QueryAdapter::json2CompleteSearchSql"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 837 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 838 | _LOG_DEBUG(jsonValue.toStyledString()); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 839 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 840 | if (jsonValue.type() != Json::objectValue) { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 841 | return false; |
| 842 | } |
| 843 | |
| 844 | std::string typedString; |
| 845 | // get the string in the jsonValue |
| 846 | for (Json::Value::iterator iter = jsonValue.begin(); iter != jsonValue.end(); ++iter) |
| 847 | { |
| 848 | Json::Value key = iter.key(); |
| 849 | Json::Value value = (*iter); |
| 850 | |
| 851 | if (key == Json::nullValue || value == Json::nullValue) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 852 | _LOG_ERROR("Null key or value in JsonValue"); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 853 | return false; |
| 854 | } |
| 855 | |
| 856 | // cannot convert to string |
| 857 | if (!key.isConvertibleTo(Json::stringValue) || !value.isConvertibleTo(Json::stringValue)) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 858 | _LOG_ERROR("Malformed JsonQuery string"); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 859 | return false; |
| 860 | } |
| 861 | |
| 862 | if (key.asString().compare("??") == 0) { |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 863 | typedString = value.asString(); |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 864 | if (typedString.empty() || typedString.find("/") != 0) |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 865 | return false; |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | // 1. get the expected column number by parsing the typedString, so we can get the filed name |
| 871 | size_t pos = 0; |
| 872 | size_t start = 1; // start from the 1st char which is not '/' |
| 873 | size_t count = 0; // also the name to query for |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 874 | size_t typedStringLen = typedString.length(); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 875 | std::string token; |
| 876 | std::string delimiter = "/"; |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 877 | std::vector<std::pair<std::string, std::string>> typedComponents; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 878 | while ((pos = typedString.find(delimiter, start)) != std::string::npos) { |
| 879 | token = typedString.substr(start, pos - start); |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 880 | if (count >= m_nameFields.size()) { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | |
| 884 | // add column name and value (token) into map |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 885 | typedComponents.push_back(std::make_pair(m_nameFields[count], token)); |
| 886 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 887 | count++; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 888 | start = pos + 1; |
| 889 | } |
| 890 | |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 891 | // we may have a component after the last "/" |
| 892 | if (start < typedStringLen) { |
| 893 | typedComponents.push_back(std::make_pair(m_nameFields[count], |
| 894 | typedString.substr(start, typedStringLen - start))); |
| 895 | } |
| 896 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 897 | // 2. generate the sql string (append what appears in the typed string, like activity='xxx'), |
| 898 | // return true |
| 899 | bool more = false; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 900 | sqlQuery << "SELECT name FROM " << m_databaseTable; |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 901 | 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] | 902 | it != typedComponents.end(); ++it) { |
| 903 | if (more) |
| 904 | sqlQuery << " AND"; |
| 905 | else |
| 906 | sqlQuery << " WHERE"; |
| 907 | |
| 908 | sqlQuery << " " << it->first << "='" << it->second << "'"; |
| 909 | |
| 910 | more = true; |
| 911 | } |
| 912 | sqlQuery << ";"; |
| 913 | return true; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | template <typename DatabaseHandler> |
| 917 | void |
| 918 | QueryAdapter<DatabaseHandler>::runJsonQuery(std::shared_ptr<const ndn::Interest> interest) |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 919 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 920 | _LOG_DEBUG(">> QueryAdapter::runJsonQuery"); |
| 921 | |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 922 | // 1) Strip the prefix off the ndn::Interest's ndn::Name |
| 923 | // +1 to grab JSON component after "query" component |
Alison Craig | 1aced7d | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 924 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 925 | ndn::Name::Component jsonStr = interest->getName()[m_prefix.size()+1]; |
| 926 | // This one cannot parse the JsonQuery correctly, and should be moved to runJsonQuery |
| 927 | 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] | 928 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 929 | if (jsonQuery.length() <= 0) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 930 | // no JSON query, send Nack? |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 931 | return; |
| 932 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 933 | |
| 934 | // the version should be replaced with ChronoSync state digest |
| 935 | ndn::name::Component version; |
| 936 | |
| 937 | if(m_socket != nullptr) { |
| 938 | const ndn::ConstBufferPtr digestPtr = m_socket->getRootDigest(); |
| 939 | std::string digestStr = ndn::toHex(digestPtr->buf(), digestPtr->size()); |
Chengyu Fan | 36dae3a | 2015-11-02 22:39:24 -0700 | [diff] [blame] | 940 | _LOG_DEBUG("Original digest " << m_chronosyncDigest); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 941 | _LOG_DEBUG("New digest : " << digestStr); |
| 942 | // if the m_chronosyncDigest and the rootdigest are not equal |
| 943 | if (digestStr != m_chronosyncDigest) { |
| 944 | // (1) update chronosyncDigest |
| 945 | // (2) clear all staled ACK data |
| 946 | m_mutex.lock(); |
| 947 | m_chronosyncDigest = digestStr; |
| 948 | m_activeQueryToFirstResponse.erase(ndn::Name("/")); |
| 949 | m_mutex.unlock(); |
| 950 | _LOG_DEBUG("Change digest to " << m_chronosyncDigest); |
| 951 | } |
| 952 | version = ndn::name::Component::fromEscapedString(digestStr); |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 953 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 954 | else { |
| 955 | version = ndn::name::Component::fromEscapedString(m_chronosyncDigest); |
| 956 | } |
| 957 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 958 | // 2) From the remainder of the ndn::Interest's ndn::Name, get the JSON out |
| 959 | Json::Value parsedFromString; |
| 960 | Json::Reader reader; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 961 | if (!reader.parse(jsonQuery, parsedFromString)) { |
| 962 | // @todo: send NACK? |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 963 | _LOG_ERROR("Cannot parse the JsonQuery"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 964 | return; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 965 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 966 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 967 | // 3) Convert the JSON Query into a MySQL one |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 968 | bool autocomplete = false, lastComponent = false; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 969 | std::stringstream sqlQuery; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 970 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 971 | ndn::Name segmentPrefix(getQueryResultsName(interest, version)); |
Chengyu Fan | 7b978f8 | 2015-12-09 17:03:23 -0700 | [diff] [blame^] | 972 | _LOG_DEBUG("segmentPrefix :" << segmentPrefix); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 973 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 974 | Json::Value tmp; |
| 975 | // expect the autocomplete and the component-based query are separate |
| 976 | // if JSON::Value contains ? as key, is autocompletion |
| 977 | if (parsedFromString.get("?", tmp) != tmp) { |
| 978 | autocomplete = true; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 979 | if (!json2AutocompletionSql(sqlQuery, parsedFromString, lastComponent)) { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 980 | sendNack(segmentPrefix); |
| 981 | return; |
| 982 | } |
| 983 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 984 | else if (parsedFromString.get("??", tmp) != tmp) { |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 985 | if (!json2PrefixBasedSearchSql(sqlQuery, parsedFromString)) { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 986 | sendNack(segmentPrefix); |
| 987 | return; |
| 988 | } |
| 989 | } |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 990 | else { |
| 991 | if (!json2Sql(sqlQuery, parsedFromString)) { |
| 992 | sendNack(segmentPrefix); |
| 993 | return; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | // 4) Run the Query |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 998 | prepareSegments(segmentPrefix, sqlQuery.str(), autocomplete, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | template <typename DatabaseHandler> |
| 1002 | void |
| 1003 | QueryAdapter<DatabaseHandler>::prepareSegments(const ndn::Name& segmentPrefix, |
| 1004 | const std::string& sqlString, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1005 | bool autocomplete, |
| 1006 | bool lastComponent) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1007 | { |
| 1008 | // empty |
| 1009 | } |
| 1010 | |
| 1011 | // prepareSegments specilization function |
| 1012 | template<> |
| 1013 | void |
| 1014 | QueryAdapter<MYSQL>::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 | { |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1019 | _LOG_DEBUG(">> QueryAdapter::prepareSegments"); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1020 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1021 | _LOG_DEBUG(sqlString); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1022 | |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1023 | std::string errMsg; |
| 1024 | bool success; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1025 | // 4) Run the Query |
| 1026 | std::shared_ptr<MYSQL_RES> results |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1027 | = atmos::util::MySQLPerformQuery(m_databaseHandler, sqlString, util::QUERY, success, errMsg); |
| 1028 | if (!success) |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1029 | _LOG_ERROR(errMsg); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1030 | |
| 1031 | if (!results) { |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1032 | _LOG_ERROR("NULL MYSQL_RES for" << sqlString); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1033 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1034 | // @todo: throw runtime error or log the error message? |
| 1035 | return; |
| 1036 | } |
| 1037 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1038 | uint64_t resultCount = mysql_num_rows(results.get()); |
| 1039 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1040 | _LOG_DEBUG("Query resuls contain " << resultCount << "rows"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1041 | |
| 1042 | MYSQL_ROW row; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1043 | uint64_t segmentNo = 0; |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1044 | Json::Value tmp; |
| 1045 | Json::Value resultJson; |
| 1046 | Json::FastWriter fastWriter; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1047 | |
| 1048 | uint64_t viewStart = 0, viewEnd = 0; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1049 | while ((row = mysql_fetch_row(results.get()))) |
| 1050 | { |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1051 | tmp.append(row[0]); |
| 1052 | const std::string tmpString = fastWriter.write(tmp); |
| 1053 | if (tmpString.length() > PAYLOAD_LIMIT) { |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1054 | std::shared_ptr<ndn::Data> data |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1055 | = makeReplyData(segmentPrefix, resultJson, segmentNo, false, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1056 | autocomplete, resultCount, viewStart, viewEnd, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1057 | m_mutex.lock(); |
| 1058 | m_cache.insert(*data); |
| 1059 | m_mutex.unlock(); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1060 | tmp.clear(); |
| 1061 | resultJson.clear(); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1062 | segmentNo++; |
| 1063 | viewStart = viewEnd + 1; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1064 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1065 | resultJson.append(row[0]); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1066 | viewEnd++; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1067 | } |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 1068 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1069 | std::shared_ptr<ndn::Data> data |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1070 | = makeReplyData(segmentPrefix, resultJson, segmentNo, true, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1071 | autocomplete, resultCount, viewStart, viewEnd, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1072 | m_mutex.lock(); |
| 1073 | m_cache.insert(*data); |
| 1074 | m_mutex.unlock(); |
| 1075 | } |
| 1076 | |
| 1077 | template <typename DatabaseHandler> |
| 1078 | std::shared_ptr<ndn::Data> |
| 1079 | QueryAdapter<DatabaseHandler>::makeReplyData(const ndn::Name& segmentPrefix, |
| 1080 | const Json::Value& value, |
| 1081 | uint64_t segmentNo, |
| 1082 | bool isFinalBlock, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 1083 | bool isAutocomplete, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1084 | uint64_t resultCount, |
| 1085 | uint64_t viewStart, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1086 | uint64_t viewEnd, |
| 1087 | bool lastComponent) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1088 | { |
| 1089 | Json::Value entry; |
| 1090 | Json::FastWriter fastWriter; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1091 | |
| 1092 | entry["resultCount"] = Json::UInt64(resultCount);; |
| 1093 | entry["viewStart"] = Json::UInt64(viewStart); |
| 1094 | entry["viewEnd"] = Json::UInt64(viewEnd); |
| 1095 | |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 1096 | if (lastComponent) |
| 1097 | entry["lastComponent"] = Json::Value(true); |
| 1098 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1099 | _LOG_DEBUG("resultCount " << resultCount << ";" |
| 1100 | << "viewStart " << viewStart << ";" |
| 1101 | << "viewEnd " << viewEnd); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 1102 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1103 | if (isAutocomplete) { |
| 1104 | entry["next"] = value; |
| 1105 | } else { |
| 1106 | entry["results"] = value; |
| 1107 | } |
| 1108 | const std::string jsonMessage = fastWriter.write(entry); |
| 1109 | const char* payload = jsonMessage.c_str(); |
| 1110 | size_t payloadLength = jsonMessage.size() + 1; |
| 1111 | ndn::Name segmentName(segmentPrefix); |
| 1112 | segmentName.appendSegment(segmentNo); |
| 1113 | |
| 1114 | std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(segmentName); |
| 1115 | data->setContent(reinterpret_cast<const uint8_t*>(payload), payloadLength); |
| 1116 | data->setFreshnessPeriod(ndn::time::milliseconds(10000)); |
| 1117 | |
| 1118 | if (isFinalBlock) { |
| 1119 | data->setFinalBlockId(ndn::Name::Component::fromSegment(segmentNo)); |
| 1120 | } |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1121 | |
Chengyu Fan | 71b712b | 2015-09-09 22:13:56 -0600 | [diff] [blame] | 1122 | _LOG_DEBUG(segmentName); |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1123 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1124 | signData(*data); |
| 1125 | return data; |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 1126 | } |
| 1127 | |
Chengyu Fan | cfb80c7 | 2015-10-19 16:50:04 -0600 | [diff] [blame] | 1128 | |
Alison Craig | 2a4d528 | 2015-04-10 12:00:02 -0600 | [diff] [blame] | 1129 | } // namespace query |
| 1130 | } // namespace atmos |
| 1131 | #endif //ATMOS_QUERY_QUERY_ADAPTER_HPP |