blob: 168cc4150c55c9feeefe097e22a470a4970aabca [file] [log] [blame]
Weiqi Shif0330d52014-07-09 10:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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/>.
18 */
19
20#ifndef REPO_STORAGE_SQLITE_STORAGE_HPP
21#define REPO_STORAGE_SQLITE_STORAGE_HPP
22
23#include "storage.hpp"
24#include "index.hpp"
25#include <string>
26#include <iostream>
27#include <sqlite3.h>
28#include <stdlib.h>
29#include <vector>
30#include <queue>
31#include <algorithm>
32
33namespace repo {
34
35using std::queue;
36
37class SqliteStorage : public Storage
38{
39public:
40 class Error : public std::runtime_error
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : std::runtime_error(what)
46 {
47 }
48 };
49
50 explicit
Wentao Shanga8f3c402014-10-30 14:03:27 -070051 SqliteStorage(const std::string& dbPath);
Weiqi Shif0330d52014-07-09 10:54:27 -070052
53 virtual
54 ~SqliteStorage();
55
56 /**
57 * @brief put the data into database
58 * @param data the data should be inserted into databse
59 * @return int64_t the id number of each entry in the database
60 */
61 virtual int64_t
62 insert(const Data& data);
63
64 /**
65 * @brief remove the entry in the database by using id
66 * @param id id number of each entry in the database
67 */
68 virtual bool
69 erase(const int64_t id);
70
71 /**
72 * @brief get the data from database
73 * @para id id number of each entry in the database, used to find the data
74 */
Wentao Shanga8f3c402014-10-30 14:03:27 -070075 virtual std::shared_ptr<Data>
Weiqi Shif0330d52014-07-09 10:54:27 -070076 read(const int64_t id);
77
78 /**
79 * @brief return the size of database
80 */
81 virtual int64_t
82 size();
83
84 /**
85 * @brief enumerate each entry in database and call the function
86 * insertItemToIndex to reubuild index from database
87 */
88 void
Wentao Shanga8f3c402014-10-30 14:03:27 -070089 fullEnumerate(const std::function<void(const Storage::ItemMeta)>& f);
Weiqi Shif0330d52014-07-09 10:54:27 -070090
91private:
92 void
93 initializeRepo();
94
95private:
96 sqlite3* m_db;
Wentao Shanga8f3c402014-10-30 14:03:27 -070097 std::string m_dbPath;
Weiqi Shif0330d52014-07-09 10:54:27 -070098 int64_t m_size;
99};
100
101
102} // namespace repo
103
104#endif // REPO_STORAGE_SQLITE_STORAGE_HPP