blob: 281e4b48cbc23ace9e5f4bc78dbb1a05cd900cf2 [file] [log] [blame]
Shuo Chen7c6b4d72014-03-26 15:20:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07003 * 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 Chen7c6b4d72014-03-26 15:20:30 -070018 */
19
20#ifndef REPO_STORAGE_STORAGE_HANDLE_HPP
21#define REPO_STORAGE_STORAGE_HANDLE_HPP
22
23#include <string>
24#include <stdexcept>
25
Alexander Afanasyeve291caf2014-04-25 11:17:36 -070026#include <ndn-cxx/interest.hpp>
27#include <ndn-cxx/name.hpp>
28#include <ndn-cxx/data.hpp>
29#include <ndn-cxx/selectors.hpp>
30#include <ndn-cxx/key-locator.hpp>
Shuo Chen7c6b4d72014-03-26 15:20:30 -070031
32#include "storage-method.hpp"
33
34namespace repo {
35
36using ndn::Interest;
37using ndn::Name;
38using ndn::Data;
39using ndn::Selectors;
40using ndn::KeyLocator;
41using ndn::Block;
42using ndn::Exclude;
43
44using std::vector;
45using std::string;
46using boost::noncopyable;
47
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -070048/**
49 * @brief this class defines handles to read, insert and delete data packets in storage media
Shuo Chen7c6b4d72014-03-26 15:20:30 -070050 */
51
52class StorageHandle : noncopyable
53{
54public:
55 class Error : public std::runtime_error
56 {
57 public:
58 explicit
59 Error(const std::string& what)
60 : std::runtime_error(what)
61 {
62 }
63 };
64
65 /**
66 * @brief Create a basic class object the specified storage type
67 */
68 explicit
69 StorageHandle(StorageMethod storageMethod);
70
71 virtual
72 ~StorageHandle();
73
74 /**
75 * @return storage method defined in storage-define.hpp
76 */
77 StorageMethod
78 getStorageMethod() const;
79
80 /**
81 * @brief insert data
82 * @return true on success, false otherwise
83 */
84 virtual bool
85 insertData(const Data& data) = 0;
86
87 /**
88 * @brief delete the data that exactly matches the name
89 * @return true on success, false otherwise
90 * @note It's considered successful if Data doesn't exist.
91 */
92 virtual bool
93 deleteData(const Name& name) = 0;
94
95 /**
96 * @brief find data according to the interest. This interest may contain selectors.
97 * @param[out] data Data matching Interest.
98 * @return true if Data is found, false otherwise
99 */
100 virtual bool
101 readData(const Interest& interest, Data& data) = 0;
102
103 /**
104 * @return if storage media has data packets with this name, return true, else return false
105 */
106 virtual bool
107 hasName(const Name& name) = 0;
108
109 /**
110 * @brief select any data conforms to the selector
111 * @param[out] names Data names matching @p name and @p selectors.
112 * @return true if at least one Data is found, false otherwise
113 */
114 virtual bool
115 readNameAny(const Name& name, const Selectors& selectors, vector<Name>& names) = 0;
116
Alexander Afanasyevb0c78ea2014-04-15 18:12:04 -0700117 /**
118 * @brief Get the number of Data packets stored
119 */
120 virtual size_t
121 size() = 0;
122
Shuo Chen7c6b4d72014-03-26 15:20:30 -0700123private:
124 StorageMethod m_storageMethod;
125};
126
127inline
128StorageHandle::StorageHandle(StorageMethod storageMethod)
129 : m_storageMethod(storageMethod)
130{
131}
132
133inline
134StorageHandle::~StorageHandle()
135{
136}
137
138inline StorageMethod
139StorageHandle::getStorageMethod() const
140{
141 return m_storageMethod;
142}
143
144} // namespace repo
145
146#endif // REPO_STORAGE_STORAGE_HANDLE_HPP