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 | /* |
| 3 | * Copyright (c) 2014-2019, 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 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 25 | #include <algorithm> |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 26 | #include <iostream> |
| 27 | #include <queue> |
| 28 | #include <stdlib.h> |
| 29 | #include <string> |
| 30 | #include <sqlite3.h> |
| 31 | #include <vector> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 32 | |
| 33 | namespace repo { |
| 34 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 35 | class SqliteStorage : public Storage |
| 36 | { |
| 37 | public: |
| 38 | class Error : public std::runtime_error |
| 39 | { |
| 40 | public: |
| 41 | explicit |
| 42 | Error(const std::string& what) |
| 43 | : std::runtime_error(what) |
| 44 | { |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | explicit |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 49 | SqliteStorage(const std::string& dbPath); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 50 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 51 | ~SqliteStorage(); |
| 52 | |
| 53 | /** |
| 54 | * @brief put the data into database |
| 55 | * @param data the data should be inserted into databse |
| 56 | * @return int64_t the id number of each entry in the database |
| 57 | */ |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 58 | int64_t |
| 59 | insert(const Data& data) override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 62 | * @brief remove the entry in the database by using name as index |
| 63 | * @param name name of the data |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 64 | */ |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 65 | bool |
| 66 | erase(const Name& name) override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 67 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 68 | std::shared_ptr<Data> |
| 69 | read(const Name& name) override; |
| 70 | |
| 71 | bool |
| 72 | has(const Name& name) override; |
| 73 | |
| 74 | std::shared_ptr<Data> |
| 75 | find(const Name& name, bool exactMatch = false) override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | 5c16cc2 | 2019-04-02 14:17:12 -0400 | [diff] [blame] | 77 | void |
| 78 | forEach(const std::function<void(const Name&)>& f) override; |
| 79 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 80 | /** |
| 81 | * @brief return the size of database |
| 82 | */ |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 83 | uint64_t |
| 84 | size() override; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | void |
| 88 | initializeRepo(); |
| 89 | |
| 90 | private: |
| 91 | sqlite3* m_db; |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 92 | std::string m_dbPath; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | |
| 96 | } // namespace repo |
| 97 | |
| 98 | #endif // REPO_STORAGE_SQLITE_STORAGE_HPP |