blob: d06239964b84782c368ac58983062438a4d04fff [file] [log] [blame]
Shuo Chen09f09bb2014-03-18 15:37:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -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/>.
Shuo Chen09f09bb2014-03-18 15:37:11 -070018 */
19
20#include "delete-handle.hpp"
21
22namespace repo {
23
Weiqi Shif0330d52014-07-09 10:54:27 -070024DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain,
Junxiao Shi047a6fb2017-06-08 16:16:05 +000025 Scheduler& scheduler,
26 Validator& validator)
Shuo Chen09f09bb2014-03-18 15:37:11 -070027 : BaseHandle(face, storageHandle, keyChain, scheduler)
28 , m_validator(validator)
29{
30}
31
32void
33DeleteHandle::onInterest(const Name& prefix, const Interest& interest)
34{
Shuo Chen09f09bb2014-03-18 15:37:11 -070035 m_validator.validate(interest, bind(&DeleteHandle::onValidated, this, _1, prefix),
Shuo Chen028dcd32014-06-21 16:36:44 +080036 bind(&DeleteHandle::onValidationFailed, this, _1, _2));
Shuo Chen09f09bb2014-03-18 15:37:11 -070037}
38
Shuo Chen09f09bb2014-03-18 15:37:11 -070039void
40DeleteHandle::onValidated(const shared_ptr<const Interest>& interest, const Name& prefix)
41{
42 RepoCommandParameter parameter;
43
44 try {
45 extractParameter(*interest, prefix, parameter);
46 }
47 catch (RepoCommandParameter::Error) {
48 negativeReply(*interest, 403);
49 return;
50 }
51
52 if (parameter.hasSelectors()) {
53
54 if (parameter.hasStartBlockId() || parameter.hasEndBlockId()) {
55 negativeReply(*interest, 402);
56 return;
57 }
58
59 //choose data with selector and delete it
60 processSelectorDeleteCommand(*interest, parameter);
61 return;
62 }
63
64 if (!parameter.hasStartBlockId() && !parameter.hasEndBlockId()) {
65 processSingleDeleteCommand(*interest, parameter);
66 return;
67 }
68
69 processSegmentDeleteCommand(*interest, parameter);
70}
71
72void
Wentao Shanga8f3c402014-10-30 14:03:27 -070073DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest,
74 const std::string& reason)
Shuo Chen09f09bb2014-03-18 15:37:11 -070075{
Shuo Chen028dcd32014-06-21 16:36:44 +080076 std::cerr << reason << std::endl;
Shuo Chen09f09bb2014-03-18 15:37:11 -070077 negativeReply(*interest, 401);
78}
Weiqi Shif0330d52014-07-09 10:54:27 -070079//listen change the setinterestfilter
Shuo Chen09f09bb2014-03-18 15:37:11 -070080void
81DeleteHandle::listen(const Name& prefix)
82{
Junxiao Shi2b7b8312017-06-16 03:43:24 +000083 getFace().setInterestFilter(Name(prefix).append("delete"),
84 bind(&DeleteHandle::onInterest, this, _1, _2));
Shuo Chen09f09bb2014-03-18 15:37:11 -070085}
86
87void
88DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
89 uint64_t statusCode, uint64_t nDeletedDatas)
90{
91 RepoCommandResponse response;
92 if (parameter.hasProcessId()) {
93 response.setProcessId(parameter.getProcessId());
94 response.setStatusCode(statusCode);
95 response.setDeleteNum(nDeletedDatas);
96 }
97 else {
98 response.setStatusCode(403);
99 }
100 reply(interest, response);
101}
102
103void
104DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode)
105{
106 RepoCommandResponse response;
107 response.setStatusCode(statusCode);
108 reply(interest, response);
109}
110
111void
112DeleteHandle::processSingleDeleteCommand(const Interest& interest,
113 RepoCommandParameter& parameter)
114{
Weiqi Shif0330d52014-07-09 10:54:27 -0700115 int64_t nDeletedDatas = getStorageHandle().deleteData(parameter.getName());
116 if (nDeletedDatas == -1) {
117 std::cerr << "Deletion Failed!" <<std::endl;
118 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700119 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700120 else
121 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700122}
123
124void
125DeleteHandle::processSelectorDeleteCommand(const Interest& interest,
126 RepoCommandParameter& parameter)
127{
Weiqi Shif0330d52014-07-09 10:54:27 -0700128 int64_t nDeletedDatas = getStorageHandle()
129 .deleteData(Interest(parameter.getName())
130 .setSelectors(parameter.getSelectors()));
131 if (nDeletedDatas == -1) {
132 std::cerr << "Deletion Failed!" <<std::endl;
133 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700134 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700135 else
136 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700137}
138
139void
140DeleteHandle::processSegmentDeleteCommand(const Interest& interest,
141 RepoCommandParameter& parameter)
142{
143 if (!parameter.hasStartBlockId())
144 parameter.setStartBlockId(0);
145
146 if (parameter.hasEndBlockId()) {
147 SegmentNo startBlockId = parameter.getStartBlockId();
148 SegmentNo endBlockId = parameter.getEndBlockId();
149
150 if (startBlockId > endBlockId) {
151 negativeReply(interest, 403);
152 return;
153 }
154
155 Name prefix = parameter.getName();
156 uint64_t nDeletedDatas = 0;
157 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
158 Name name = prefix;
159 name.appendSegment(i);
160 if (getStorageHandle().deleteData(name)) {
161 nDeletedDatas++;
162 }
163 }
164 //All the data deleted, return 200
165 positiveReply(interest, parameter, 200, nDeletedDatas);
166 }
167 else {
168 BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented
169 }
170}
171
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800172} // namespace repo