Shuo Chen | 7c6b4d7 | 2014-03-26 15:20:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California. |
| 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/>. |
Shuo Chen | 7c6b4d7 | 2014-03-26 15:20:30 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 20 | #ifndef REPO_STORAGE_SQLITE_HANDLE_HPP |
| 21 | #define REPO_STORAGE_SQLITE_HANDLE_HPP |
Shuo Chen | 7c6b4d7 | 2014-03-26 15:20:30 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 23 | #include "storage-handle.hpp" |
Shuo Chen | 7c6b4d7 | 2014-03-26 15:20:30 -0700 | [diff] [blame] | 24 | |
| 25 | #include <string> |
| 26 | #include <iostream> |
| 27 | #include <sqlite3.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <vector> |
| 30 | #include <queue> |
| 31 | #include <algorithm> |
| 32 | |
| 33 | namespace repo { |
| 34 | |
| 35 | using std::queue; |
| 36 | |
| 37 | class SqliteHandle : public StorageHandle |
| 38 | { |
| 39 | public: |
| 40 | class Error : public StorageHandle::Error |
| 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | Error(const std::string& what) |
| 45 | : StorageHandle::Error(what) |
| 46 | { |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | SqliteHandle(); |
| 51 | |
| 52 | explicit |
| 53 | SqliteHandle(const string& dbPath); |
| 54 | |
| 55 | virtual |
| 56 | ~SqliteHandle(); |
| 57 | |
| 58 | |
| 59 | // from StorageHandle |
| 60 | |
| 61 | virtual bool |
| 62 | insertData(const Data& data); |
| 63 | |
| 64 | virtual bool |
| 65 | deleteData(const Name& name); |
| 66 | |
| 67 | virtual bool |
| 68 | readData(const Interest& interest, Data& data); |
| 69 | |
| 70 | virtual bool |
| 71 | hasName(const Name& name); |
| 72 | |
| 73 | virtual bool |
| 74 | readNameAny(const Name& name, const Selectors& selectors, vector<Name>& names); |
| 75 | |
Alexander Afanasyev | b0c78ea | 2014-04-15 18:12:04 -0700 | [diff] [blame] | 76 | virtual size_t |
| 77 | size(); |
| 78 | |
Shuo Chen | 7c6b4d7 | 2014-03-26 15:20:30 -0700 | [diff] [blame] | 79 | private: |
| 80 | void |
| 81 | initializeRepo(); |
| 82 | |
| 83 | /** |
| 84 | * @brief find data with the exact name matched |
| 85 | * @param[out] data Data matching Interest. |
| 86 | * @return if no data or something is wrong, return false |
| 87 | */ |
| 88 | bool |
| 89 | readData(const Name& name, Data& data); |
| 90 | |
| 91 | /** |
| 92 | * @brief check whether there is one row with this parentName = parentName in database |
| 93 | * @return if no data or something is wrong, return false. |
| 94 | */ |
| 95 | bool |
| 96 | hasParentName(const Name& parentName) const; |
| 97 | |
| 98 | /** |
| 99 | * @brief This function is for no selector, it will reply the leftmost data |
| 100 | * @param[out] data Data matching Interest. |
| 101 | * @return if no data or something is wrong, return false. |
| 102 | */ |
| 103 | bool |
| 104 | readDataPlain(const Name& name, Data& data); |
| 105 | |
| 106 | /** |
| 107 | * @brief read data with this prefix or name |
| 108 | * @param name indicates name or prefix of interest |
| 109 | * @param[out] names is vector to contain the result of this function. |
| 110 | * @return success return true, error return false |
| 111 | */ |
| 112 | bool |
| 113 | readDataName(const Name& name, vector<Name>& names) const; |
| 114 | |
| 115 | /** |
| 116 | * @brief read data with this prefix or name and selectors including MinSuffixComponents, |
| 117 | * MaxSuffixComponents, PublisherPublicKeyLocator, and Exclude. |
| 118 | * This method does not consider ChildSelector and MustBeFresh. |
| 119 | * |
| 120 | * @param name indicates name or prefix of interest |
| 121 | * @param[out] names is vector to contain the result of this function. |
| 122 | * @return success return true, error return false |
| 123 | */ |
| 124 | bool |
| 125 | readNameSelector(const Interest& interest, vector<Name>& names) const; |
| 126 | |
| 127 | /** |
| 128 | * @brief ChildSelector filter |
| 129 | * @param names list of candidate names for ChildSelector filter |
| 130 | * @param[out] resultName is the result of selected name |
| 131 | * @return success return true, error return false |
| 132 | */ |
| 133 | bool |
| 134 | filterNameChild(const Name& name, int childSelector, |
| 135 | const vector<Name>& names, Name& resultName); |
| 136 | |
| 137 | private: |
| 138 | sqlite3* m_db; |
| 139 | string m_dbPath; |
| 140 | }; |
| 141 | |
| 142 | } // namespace repo |
| 143 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 144 | #endif // REPO_STORAGE_SQLITE_HANDLE_HPP |