blob: 5c0601563869c744b4aebb0847c66c8d0e5ffceb [file] [log] [blame]
Shuo Chen09f09bb2014-03-18 15:37:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc0e26582017-08-13 21:16:49 -04002/*
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),
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040036 bind(&DeleteHandle::onValidationFailed, this, _1, _2));
Shuo Chen09f09bb2014-03-18 15:37:11 -070037}
38
Shuo Chen09f09bb2014-03-18 15:37:11 -070039void
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040040DeleteHandle::onValidated(const Interest& interest, const Name& prefix)
Shuo Chen09f09bb2014-03-18 15:37:11 -070041{
42 RepoCommandParameter parameter;
43
44 try {
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040045 extractParameter(interest, prefix, parameter);
Shuo Chen09f09bb2014-03-18 15:37:11 -070046 }
47 catch (RepoCommandParameter::Error) {
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040048 negativeReply(interest, 403);
Shuo Chen09f09bb2014-03-18 15:37:11 -070049 return;
50 }
51
52 if (parameter.hasSelectors()) {
53
54 if (parameter.hasStartBlockId() || parameter.hasEndBlockId()) {
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040055 negativeReply(interest, 402);
Shuo Chen09f09bb2014-03-18 15:37:11 -070056 return;
57 }
58
59 //choose data with selector and delete it
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040060 processSelectorDeleteCommand(interest, parameter);
Shuo Chen09f09bb2014-03-18 15:37:11 -070061 return;
62 }
63
64 if (!parameter.hasStartBlockId() && !parameter.hasEndBlockId()) {
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040065 processSingleDeleteCommand(interest, parameter);
Shuo Chen09f09bb2014-03-18 15:37:11 -070066 return;
67 }
68
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040069 processSegmentDeleteCommand(interest, parameter);
Shuo Chen09f09bb2014-03-18 15:37:11 -070070}
71
72void
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040073DeleteHandle::onValidationFailed(const Interest& interest, const ValidationError& error)
Shuo Chen09f09bb2014-03-18 15:37:11 -070074{
Alexander Afanasyevc0e26582017-08-13 21:16:49 -040075 std::cerr << error << std::endl;
76 negativeReply(interest, 401);
Shuo Chen09f09bb2014-03-18 15:37:11 -070077}
Weiqi Shif0330d52014-07-09 10:54:27 -070078//listen change the setinterestfilter
Shuo Chen09f09bb2014-03-18 15:37:11 -070079void
80DeleteHandle::listen(const Name& prefix)
81{
Junxiao Shi2b7b8312017-06-16 03:43:24 +000082 getFace().setInterestFilter(Name(prefix).append("delete"),
83 bind(&DeleteHandle::onInterest, this, _1, _2));
Shuo Chen09f09bb2014-03-18 15:37:11 -070084}
85
86void
87DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
88 uint64_t statusCode, uint64_t nDeletedDatas)
89{
90 RepoCommandResponse response;
91 if (parameter.hasProcessId()) {
92 response.setProcessId(parameter.getProcessId());
93 response.setStatusCode(statusCode);
94 response.setDeleteNum(nDeletedDatas);
95 }
96 else {
97 response.setStatusCode(403);
98 }
99 reply(interest, response);
100}
101
102void
103DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode)
104{
105 RepoCommandResponse response;
106 response.setStatusCode(statusCode);
107 reply(interest, response);
108}
109
110void
111DeleteHandle::processSingleDeleteCommand(const Interest& interest,
112 RepoCommandParameter& parameter)
113{
Weiqi Shif0330d52014-07-09 10:54:27 -0700114 int64_t nDeletedDatas = getStorageHandle().deleteData(parameter.getName());
115 if (nDeletedDatas == -1) {
116 std::cerr << "Deletion Failed!" <<std::endl;
117 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700118 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700119 else
120 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700121}
122
123void
124DeleteHandle::processSelectorDeleteCommand(const Interest& interest,
125 RepoCommandParameter& parameter)
126{
Weiqi Shif0330d52014-07-09 10:54:27 -0700127 int64_t nDeletedDatas = getStorageHandle()
128 .deleteData(Interest(parameter.getName())
129 .setSelectors(parameter.getSelectors()));
130 if (nDeletedDatas == -1) {
131 std::cerr << "Deletion Failed!" <<std::endl;
132 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700133 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700134 else
135 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700136}
137
138void
139DeleteHandle::processSegmentDeleteCommand(const Interest& interest,
140 RepoCommandParameter& parameter)
141{
142 if (!parameter.hasStartBlockId())
143 parameter.setStartBlockId(0);
144
145 if (parameter.hasEndBlockId()) {
146 SegmentNo startBlockId = parameter.getStartBlockId();
147 SegmentNo endBlockId = parameter.getEndBlockId();
148
149 if (startBlockId > endBlockId) {
150 negativeReply(interest, 403);
151 return;
152 }
153
154 Name prefix = parameter.getName();
155 uint64_t nDeletedDatas = 0;
156 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
157 Name name = prefix;
158 name.appendSegment(i);
159 if (getStorageHandle().deleteData(name)) {
160 nDeletedDatas++;
161 }
162 }
163 //All the data deleted, return 200
164 positiveReply(interest, parameter, 200, nDeletedDatas);
165 }
166 else {
167 BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented
168 }
169}
170
Alexander Afanasyev42290b22017-03-09 12:58:29 -0800171} // namespace repo