blob: a5952425d7baca2277a0f449f07628e040de5c08 [file] [log] [blame]
Shuo Chen7c6b4d72014-03-26 15:20:30 -07001/* -*- 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
20namespace repo {
21
22using std::queue;
23
24class SqliteHandle : public StorageHandle
25{
26public:
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
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070063 virtual size_t
64 size();
65
Shuo Chen7c6b4d72014-03-26 15:20:30 -070066private:
67 void
68 initializeRepo();
69
70 /**
71 * @brief find data with the exact name matched
72 * @param[out] data Data matching Interest.
73 * @return if no data or something is wrong, return false
74 */
75 bool
76 readData(const Name& name, Data& data);
77
78 /**
79 * @brief check whether there is one row with this parentName = parentName in database
80 * @return if no data or something is wrong, return false.
81 */
82 bool
83 hasParentName(const Name& parentName) const;
84
85 /**
86 * @brief This function is for no selector, it will reply the leftmost data
87 * @param[out] data Data matching Interest.
88 * @return if no data or something is wrong, return false.
89 */
90 bool
91 readDataPlain(const Name& name, Data& data);
92
93 /**
94 * @brief read data with this prefix or name
95 * @param name indicates name or prefix of interest
96 * @param[out] names is vector to contain the result of this function.
97 * @return success return true, error return false
98 */
99 bool
100 readDataName(const Name& name, vector<Name>& names) const;
101
102 /**
103 * @brief read data with this prefix or name and selectors including MinSuffixComponents,
104 * MaxSuffixComponents, PublisherPublicKeyLocator, and Exclude.
105 * This method does not consider ChildSelector and MustBeFresh.
106 *
107 * @param name indicates name or prefix of interest
108 * @param[out] names is vector to contain the result of this function.
109 * @return success return true, error return false
110 */
111 bool
112 readNameSelector(const Interest& interest, vector<Name>& names) const;
113
114 /**
115 * @brief ChildSelector filter
116 * @param names list of candidate names for ChildSelector filter
117 * @param[out] resultName is the result of selected name
118 * @return success return true, error return false
119 */
120 bool
121 filterNameChild(const Name& name, int childSelector,
122 const vector<Name>& names, Name& resultName);
123
124private:
125 sqlite3* m_db;
126 string m_dbPath;
127};
128
129} // namespace repo
130
131#endif