Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -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 |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef REPO_STORAGE_SQLITE_STORAGE_HPP |
| 21 | #define REPO_STORAGE_SQLITE_STORAGE_HPP |
| 22 | |
| 23 | #include "storage.hpp" |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 24 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 25 | #include <sqlite3.h> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 26 | |
| 27 | namespace repo { |
| 28 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 29 | class SqliteStorage : public Storage |
| 30 | { |
| 31 | public: |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 32 | explicit |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 33 | SqliteStorage(const std::string& dbPath); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 35 | ~SqliteStorage() override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @brief put the data into database |
| 39 | * @param data the data should be inserted into databse |
| 40 | * @return int64_t the id number of each entry in the database |
| 41 | */ |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 42 | int64_t |
| 43 | insert(const Data& data) override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 46 | * @brief remove the entry in the database by using name as index |
| 47 | * @param name name of the data |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 48 | */ |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 49 | bool |
| 50 | erase(const Name& name) override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 51 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 52 | std::shared_ptr<Data> |
| 53 | read(const Name& name) override; |
| 54 | |
| 55 | bool |
| 56 | has(const Name& name) override; |
| 57 | |
| 58 | std::shared_ptr<Data> |
| 59 | find(const Name& name, bool exactMatch = false) override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 61 | void |
| 62 | forEach(const std::function<void(const Name&)>& f) override; |
| 63 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 64 | /** |
| 65 | * @brief return the size of database |
| 66 | */ |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 67 | uint64_t |
| 68 | size() override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | void |
| 72 | initializeRepo(); |
| 73 | |
| 74 | private: |
| 75 | sqlite3* m_db; |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 76 | std::string m_dbPath; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 79 | } // namespace repo |
| 80 | |
| 81 | #endif // REPO_STORAGE_SQLITE_STORAGE_HPP |