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 | /* |
Davide Pesavento | 399f4d9 | 2023-09-17 14:03:51 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | 399f4d9 | 2023-09-17 14:03:51 -0400 | [diff] [blame] | 20 | #include "common.hpp" |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 21 | |
| 22 | #include <iostream> |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 23 | #include <string> |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 24 | #include <unistd.h> |
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 |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 36 | usage(const char* programName) |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 37 | { |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 38 | std::cerr << "Usage: " |
| 39 | << programName << " [-c <path/to/repo-ng.conf>] [-n] [-h]\n" |
| 40 | << "\n" |
| 41 | << "List names of Data packets in NDN repository.\n" |
| 42 | << "By default, all names will include the implicit digest of Data packets.\n" |
| 43 | << "\n" |
| 44 | << "Options:\n" |
| 45 | << " -h: show help message\n" |
| 46 | << " -c: set config file path\n" |
| 47 | << " -n: do not show implicit digest\n" |
| 48 | << std::endl; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | class RepoEnumerator |
| 52 | { |
| 53 | public: |
| 54 | class Error : public std::runtime_error |
| 55 | { |
| 56 | public: |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 57 | using std::runtime_error::runtime_error; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 60 | explicit |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 61 | RepoEnumerator(const std::string& configFile); |
| 62 | |
| 63 | uint64_t |
| 64 | enumerate(bool showImplicitDigest); |
| 65 | |
| 66 | private: |
| 67 | void |
| 68 | readConfig(const std::string& configFile); |
| 69 | |
| 70 | private: |
| 71 | sqlite3* m_db; |
| 72 | std::string m_dbPath; |
| 73 | }; |
| 74 | |
| 75 | RepoEnumerator::RepoEnumerator(const std::string& configFile) |
| 76 | { |
| 77 | readConfig(configFile); |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 78 | char* errMsg = nullptr; |
| 79 | 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] | 80 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 81 | "unix-dotfile" |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 82 | #else |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 83 | nullptr |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 84 | #endif |
| 85 | ); |
| 86 | if (rc != SQLITE_OK) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 87 | BOOST_THROW_EXCEPTION(Error("Database file open failure")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 88 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 89 | sqlite3_exec(m_db, "PRAGMA synchronous = OFF", nullptr, nullptr, &errMsg); |
| 90 | sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", nullptr, nullptr, &errMsg); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
| 94 | RepoEnumerator::readConfig(const std::string& configFile) |
| 95 | { |
| 96 | if (configFile.empty()) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("Invalid configuration file name")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | std::ifstream fin(configFile.c_str()); |
| 101 | if (!fin.is_open()) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 102 | BOOST_THROW_EXCEPTION(Error("failed to open configuration file '" + configFile + "'")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 103 | |
| 104 | using namespace boost::property_tree; |
| 105 | ptree propertyTree; |
| 106 | try { |
| 107 | read_info(fin, propertyTree); |
| 108 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 109 | catch (const ptree_error& e) { |
| 110 | BOOST_THROW_EXCEPTION(Error("failed to read configuration file '" + configFile + "'")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 111 | } |
| 112 | ptree repoConf = propertyTree.get_child("repo"); |
| 113 | m_dbPath = repoConf.get<std::string>("storage.path"); |
| 114 | m_dbPath += "/ndn_repo.db"; |
| 115 | } |
| 116 | |
| 117 | uint64_t |
| 118 | RepoEnumerator::enumerate(bool showImplicitDigest) |
| 119 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 120 | ndn::util::Sqlite3Statement stmt(m_db, "SELECT data FROM NDN_REPO_V2;"); |
| 121 | uint64_t nEntries = 0; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 122 | while (true) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 123 | int rc = stmt.step(); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 124 | if (rc == SQLITE_ROW) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 125 | Data data(stmt.getBlock(0)); |
| 126 | if (showImplicitDigest) { |
| 127 | std::cout << data.getFullName() << std::endl; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 128 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 129 | else { |
| 130 | std::cout << data.getName() << std::endl; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 131 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 132 | nEntries++; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 133 | } |
| 134 | else if (rc == SQLITE_DONE) { |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | else { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(Error("Initiation Read Entries error")); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 141 | return nEntries; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 144 | static int |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 145 | main(int argc, char** argv) |
| 146 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 147 | std::string configPath = DEFAULT_CONFIG_FILE; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 148 | bool showImplicitDigest = true; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 149 | |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 150 | int opt; |
| 151 | while ((opt = getopt(argc, argv, "hc:n")) != -1) { |
| 152 | switch (opt) { |
| 153 | case 'h': |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 154 | usage(argv[0]); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | case 'c': |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 157 | configPath = std::string(optarg); |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 158 | break; |
| 159 | case 'n': |
| 160 | showImplicitDigest = false; |
| 161 | break; |
| 162 | default: |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 163 | usage(argv[0]); |
| 164 | return 2; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | RepoEnumerator instance(configPath); |
| 169 | uint64_t count = instance.enumerate(showImplicitDigest); |
| 170 | std::cerr << "Total number of data = " << count << std::endl; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | } // namespace repo |
| 175 | |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 176 | int |
| 177 | main(int argc, char** argv) |
| 178 | { |
| 179 | try { |
| 180 | return repo::main(argc, argv); |
| 181 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 182 | catch (const std::exception& e) { |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 183 | std::cerr << "ERROR: " << e.what() << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 184 | return 1; |
Weiqi Shi | 9a22389 | 2014-08-24 18:42:58 -0700 | [diff] [blame] | 185 | } |
| 186 | } |