Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "../src/common.hpp" |
| 21 | #include "config.hpp" |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 22 | |
| 23 | #include <iostream> |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 24 | #include <string> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 25 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 26 | #include <ndn-cxx/util/sqlite3-statement.hpp> |
| 27 | |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 28 | #include <boost/property_tree/info_parser.hpp> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 29 | #include <boost/property_tree/ptree.hpp> |
| 30 | |
| 31 | #include <sqlite3.h> |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 32 | |
| 33 | namespace repo { |
| 34 | |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame] | 35 | static void |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 36 | printUsage(const char* programName) |
| 37 | { |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 38 | std::cout |
| 39 | << "Usage:\n" |
| 40 | << " " << programName << " [-c <path/to/repo-ng.conf>] [-n] [-h]\n" |
| 41 | << "\n" |
| 42 | << "List names of Data packets in NDN repository. " |
| 43 | << "By default, all names will include the implicit digest of Data packets\n" |
| 44 | << "\n" |
| 45 | << "Options:\n" |
| 46 | << " -h: show help message\n" |
| 47 | << " -c: set config file path\n" |
| 48 | << " -n: do not show implicit digest\n" |
| 49 | << std::endl; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | class RepoEnumerator |
| 53 | { |
| 54 | public: |
| 55 | class Error : public std::runtime_error |
| 56 | { |
| 57 | public: |
| 58 | explicit |
| 59 | Error(const std::string& what) |
| 60 | : std::runtime_error(what) |
| 61 | { |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | public: |
| 66 | RepoEnumerator(const std::string& configFile); |
| 67 | |
| 68 | uint64_t |
| 69 | enumerate(bool showImplicitDigest); |
| 70 | |
| 71 | private: |
| 72 | void |
| 73 | readConfig(const std::string& configFile); |
| 74 | |
| 75 | private: |
| 76 | sqlite3* m_db; |
| 77 | std::string m_dbPath; |
| 78 | }; |
| 79 | |
| 80 | RepoEnumerator::RepoEnumerator(const std::string& configFile) |
| 81 | { |
| 82 | readConfig(configFile); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 83 | char* errMsg = nullptr; |
| 84 | int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db, SQLITE_OPEN_READONLY, |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 85 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 86 | "unix-dotfile" |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 87 | #else |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 88 | nullptr |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 89 | #endif |
| 90 | ); |
| 91 | if (rc != SQLITE_OK) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 92 | BOOST_THROW_EXCEPTION(Error("Database file open failure")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 93 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 94 | sqlite3_exec(m_db, "PRAGMA synchronous = OFF", nullptr, nullptr, &errMsg); |
| 95 | sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", nullptr, nullptr, &errMsg); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | RepoEnumerator::readConfig(const std::string& configFile) |
| 100 | { |
| 101 | if (configFile.empty()) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 102 | BOOST_THROW_EXCEPTION(Error("Invalid configuration file name")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | std::ifstream fin(configFile.c_str()); |
| 106 | if (!fin.is_open()) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 107 | BOOST_THROW_EXCEPTION(Error("failed to open configuration file '" + configFile + "'")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 108 | |
| 109 | using namespace boost::property_tree; |
| 110 | ptree propertyTree; |
| 111 | try { |
| 112 | read_info(fin, propertyTree); |
| 113 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 114 | catch (const ptree_error& e) { |
| 115 | BOOST_THROW_EXCEPTION(Error("failed to read configuration file '" + configFile + "'")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 116 | } |
| 117 | ptree repoConf = propertyTree.get_child("repo"); |
| 118 | m_dbPath = repoConf.get<std::string>("storage.path"); |
| 119 | m_dbPath += "/ndn_repo.db"; |
| 120 | } |
| 121 | |
| 122 | uint64_t |
| 123 | RepoEnumerator::enumerate(bool showImplicitDigest) |
| 124 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 125 | ndn::util::Sqlite3Statement stmt(m_db, "SELECT data FROM NDN_REPO_V2;"); |
| 126 | uint64_t nEntries = 0; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 127 | while (true) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 128 | int rc = stmt.step(); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 129 | if (rc == SQLITE_ROW) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 130 | Data data(stmt.getBlock(0)); |
| 131 | if (showImplicitDigest) { |
| 132 | std::cout << data.getFullName() << std::endl; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 133 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 134 | else { |
| 135 | std::cout << data.getName() << std::endl; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 136 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 137 | nEntries++; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 138 | } |
| 139 | else if (rc == SQLITE_DONE) { |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | else { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 143 | BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 146 | return nEntries; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int |
| 150 | main(int argc, char** argv) |
| 151 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 152 | std::string configPath = DEFAULT_CONFIG_FILE; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 153 | bool showImplicitDigest = true; |
| 154 | int opt; |
| 155 | while ((opt = getopt(argc, argv, "hc:n")) != -1) { |
| 156 | switch (opt) { |
| 157 | case 'h': |
| 158 | printUsage(argv[0]); |
| 159 | return 0; |
| 160 | case 'c': |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 161 | configPath = std::string(optarg); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 162 | break; |
| 163 | case 'n': |
| 164 | showImplicitDigest = false; |
| 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | RepoEnumerator instance(configPath); |
| 172 | uint64_t count = instance.enumerate(showImplicitDigest); |
| 173 | std::cerr << "Total number of data = " << count << std::endl; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | } // namespace repo |
| 178 | |
| 179 | |
| 180 | int |
| 181 | main(int argc, char** argv) |
| 182 | { |
| 183 | try { |
| 184 | return repo::main(argc, argv); |
| 185 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 186 | catch (const std::exception& e) { |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 187 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 188 | return 2; |
| 189 | } |
| 190 | } |