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