blob: ad4a474219d542e843c4f634c87830bfc7dfccea [file] [log] [blame]
Shuo Chen09f09bb2014-03-18 15:37:11 -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 Chen09f09bb2014-03-18 15:37:11 -070018 */
19
20#include "delete-handle.hpp"
21
22namespace repo {
23
24DeleteHandle::DeleteHandle(Face& face, StorageHandle& storageHandle, KeyChain& keyChain,
Shuo Chen028dcd32014-06-21 16:36:44 +080025 Scheduler& scheduler, ValidatorConfig& validator)
Shuo Chen09f09bb2014-03-18 15:37:11 -070026 : BaseHandle(face, storageHandle, keyChain, scheduler)
27 , m_validator(validator)
28{
29}
30
31void
32DeleteHandle::onInterest(const Name& prefix, const Interest& interest)
33{
Shuo Chen09f09bb2014-03-18 15:37:11 -070034 m_validator.validate(interest, bind(&DeleteHandle::onValidated, this, _1, prefix),
Shuo Chen028dcd32014-06-21 16:36:44 +080035 bind(&DeleteHandle::onValidationFailed, this, _1, _2));
Shuo Chen09f09bb2014-03-18 15:37:11 -070036}
37
Wentao Shang91fb4f22014-05-20 10:55:22 -070038void
39DeleteHandle::onRegisterSuccess(const Name& prefix)
40{
41 std::cerr << "Successfully registered prefix " << prefix << std::endl;
42}
Shuo Chen09f09bb2014-03-18 15:37:11 -070043
44void
45DeleteHandle::onRegisterFailed(const Name& prefix, const std::string& reason)
46{
47 throw Error("Delete prefix registration failed");
48}
49
50
51void
52DeleteHandle::onCheckInterest(const Name& prefix, const Interest& interest)
53{
54 BOOST_ASSERT(false); // Deletion progress check, not implemented
55}
56
57
58void
59DeleteHandle::onCheckRegisterFailed(const Name& prefix, const std::string& reason)
60{
61 throw Error("Delete check prefix registration failed");
62}
63
64
65void
66DeleteHandle::onValidated(const shared_ptr<const Interest>& interest, const Name& prefix)
67{
68 RepoCommandParameter parameter;
69
70 try {
71 extractParameter(*interest, prefix, parameter);
72 }
73 catch (RepoCommandParameter::Error) {
74 negativeReply(*interest, 403);
75 return;
76 }
77
78 if (parameter.hasSelectors()) {
79
80 if (parameter.hasStartBlockId() || parameter.hasEndBlockId()) {
81 negativeReply(*interest, 402);
82 return;
83 }
84
85 //choose data with selector and delete it
86 processSelectorDeleteCommand(*interest, parameter);
87 return;
88 }
89
90 if (!parameter.hasStartBlockId() && !parameter.hasEndBlockId()) {
91 processSingleDeleteCommand(*interest, parameter);
92 return;
93 }
94
95 processSegmentDeleteCommand(*interest, parameter);
96}
97
98void
Shuo Chen028dcd32014-06-21 16:36:44 +080099DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest, const string& reason)
Shuo Chen09f09bb2014-03-18 15:37:11 -0700100{
Shuo Chen028dcd32014-06-21 16:36:44 +0800101 std::cerr << reason << std::endl;
Shuo Chen09f09bb2014-03-18 15:37:11 -0700102 negativeReply(*interest, 401);
103}
104
105void
106DeleteHandle::listen(const Name& prefix)
107{
Shuo Chen478204c2014-03-18 18:27:04 -0700108 getFace().setInterestFilter(Name(prefix).append("delete"),
Shuo Chen09f09bb2014-03-18 15:37:11 -0700109 bind(&DeleteHandle::onInterest, this, _1, _2),
Wentao Shang91fb4f22014-05-20 10:55:22 -0700110 bind(&DeleteHandle::onRegisterSuccess, this, _1),
Shuo Chen09f09bb2014-03-18 15:37:11 -0700111 bind(&DeleteHandle::onRegisterFailed, this, _1, _2));
112}
113
114void
115DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
116 uint64_t statusCode, uint64_t nDeletedDatas)
117{
118 RepoCommandResponse response;
119 if (parameter.hasProcessId()) {
120 response.setProcessId(parameter.getProcessId());
121 response.setStatusCode(statusCode);
122 response.setDeleteNum(nDeletedDatas);
123 }
124 else {
125 response.setStatusCode(403);
126 }
127 reply(interest, response);
128}
129
130void
131DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode)
132{
133 RepoCommandResponse response;
134 response.setStatusCode(statusCode);
135 reply(interest, response);
136}
137
138void
139DeleteHandle::processSingleDeleteCommand(const Interest& interest,
140 RepoCommandParameter& parameter)
141{
142 uint64_t nDeletedDatas = 0;
143 if (getStorageHandle().deleteData(parameter.getName())) {
144 nDeletedDatas++;
145 }
146 positiveReply(interest, parameter, 200, nDeletedDatas);
147}
148
149void
150DeleteHandle::processSelectorDeleteCommand(const Interest& interest,
151 RepoCommandParameter& parameter)
152{
153 uint64_t nDeletedDatas = 0;
154 Name name = parameter.getName();
155 Selectors selectors = parameter.getSelectors();
156 vector<Name> names;
157 getStorageHandle().readNameAny(name, selectors, names);
158
159 for (vector<Name>::iterator it = names.begin(); it != names.end(); ++it) {
160 if (getStorageHandle().deleteData(*it)) {
161 nDeletedDatas++;
162 }
163 }
164
165 //All data has been deleted, return 200
166 positiveReply(interest, parameter, 200, nDeletedDatas);
167}
168
169void
170DeleteHandle::processSegmentDeleteCommand(const Interest& interest,
171 RepoCommandParameter& parameter)
172{
173 if (!parameter.hasStartBlockId())
174 parameter.setStartBlockId(0);
175
176 if (parameter.hasEndBlockId()) {
177 SegmentNo startBlockId = parameter.getStartBlockId();
178 SegmentNo endBlockId = parameter.getEndBlockId();
179
180 if (startBlockId > endBlockId) {
181 negativeReply(interest, 403);
182 return;
183 }
184
185 Name prefix = parameter.getName();
186 uint64_t nDeletedDatas = 0;
187 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
188 Name name = prefix;
189 name.appendSegment(i);
190 if (getStorageHandle().deleteData(name)) {
191 nDeletedDatas++;
192 }
193 }
194 //All the data deleted, return 200
195 positiveReply(interest, parameter, 200, nDeletedDatas);
196 }
197 else {
198 BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented
199 }
200}
201
202} //namespace repo