blob: 4d7142bc0cdbf8ac78bc8d16ab57d71b43e13528 [file] [log] [blame]
Weiqi Shif0330d52014-07-09 10:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
weijia yuan3aa8d2b2018-03-06 15:35:57 -08003 * Copyright (c) 2018, Regents of the University of California.
Weiqi Shif0330d52014-07-09 10:54:27 -07004 *
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 yuan3aa8d2b2018-03-06 15:35:57 -080024
Weiqi Shif0330d52014-07-09 10:54:27 -070025#include <algorithm>
weijia yuan3aa8d2b2018-03-06 15:35:57 -080026#include <iostream>
27#include <queue>
28#include <stdlib.h>
29#include <string>
30#include <sqlite3.h>
31#include <vector>
Weiqi Shif0330d52014-07-09 10:54:27 -070032
33namespace repo {
34
Weiqi Shif0330d52014-07-09 10:54:27 -070035class SqliteStorage : public Storage
36{
37public:
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 Shanga8f3c402014-10-30 14:03:27 -070049 SqliteStorage(const std::string& dbPath);
Weiqi Shif0330d52014-07-09 10:54:27 -070050
Weiqi Shif0330d52014-07-09 10:54:27 -070051 ~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 yuan3aa8d2b2018-03-06 15:35:57 -080058 int64_t
59 insert(const Data& data) override;
Weiqi Shif0330d52014-07-09 10:54:27 -070060
61 /**
weijia yuan3aa8d2b2018-03-06 15:35:57 -080062 * @brief remove the entry in the database by using name as index
63 * @param name name of the data
Weiqi Shif0330d52014-07-09 10:54:27 -070064 */
weijia yuan3aa8d2b2018-03-06 15:35:57 -080065 bool
66 erase(const Name& name) override;
Weiqi Shif0330d52014-07-09 10:54:27 -070067
weijia yuan3aa8d2b2018-03-06 15:35:57 -080068 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 Shif0330d52014-07-09 10:54:27 -070076
77 /**
78 * @brief return the size of database
79 */
weijia yuan3aa8d2b2018-03-06 15:35:57 -080080 uint64_t
81 size() override;
Weiqi Shif0330d52014-07-09 10:54:27 -070082
83private:
84 void
85 initializeRepo();
86
87private:
88 sqlite3* m_db;
Wentao Shanga8f3c402014-10-30 14:03:27 -070089 std::string m_dbPath;
Weiqi Shif0330d52014-07-09 10:54:27 -070090};
91
92
93} // namespace repo
94
95#endif // REPO_STORAGE_SQLITE_STORAGE_HPP