blob: c6ad7c53827d02382f13d4acf63c08732b73eaf3 [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
Weiqi Shif0330d52014-07-09 10:54:27 -070024DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain,
25 Scheduler& scheduler,// RepoStorage& storeindex,
26 ValidatorConfig& 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
39
40void
41DeleteHandle::onRegisterFailed(const Name& prefix, const std::string& reason)
42{
43 throw Error("Delete prefix registration failed");
44}
45
46
47void
48DeleteHandle::onCheckInterest(const Name& prefix, const Interest& interest)
49{
50 BOOST_ASSERT(false); // Deletion progress check, not implemented
51}
52
53
54void
55DeleteHandle::onCheckRegisterFailed(const Name& prefix, const std::string& reason)
56{
57 throw Error("Delete check prefix registration failed");
58}
59
60
61void
62DeleteHandle::onValidated(const shared_ptr<const Interest>& interest, const Name& prefix)
63{
64 RepoCommandParameter parameter;
65
66 try {
67 extractParameter(*interest, prefix, parameter);
68 }
69 catch (RepoCommandParameter::Error) {
70 negativeReply(*interest, 403);
71 return;
72 }
73
74 if (parameter.hasSelectors()) {
75
76 if (parameter.hasStartBlockId() || parameter.hasEndBlockId()) {
77 negativeReply(*interest, 402);
78 return;
79 }
80
81 //choose data with selector and delete it
82 processSelectorDeleteCommand(*interest, parameter);
83 return;
84 }
85
86 if (!parameter.hasStartBlockId() && !parameter.hasEndBlockId()) {
87 processSingleDeleteCommand(*interest, parameter);
88 return;
89 }
90
91 processSegmentDeleteCommand(*interest, parameter);
92}
93
94void
Shuo Chen028dcd32014-06-21 16:36:44 +080095DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest, const string& reason)
Shuo Chen09f09bb2014-03-18 15:37:11 -070096{
Shuo Chen028dcd32014-06-21 16:36:44 +080097 std::cerr << reason << std::endl;
Shuo Chen09f09bb2014-03-18 15:37:11 -070098 negativeReply(*interest, 401);
99}
Weiqi Shif0330d52014-07-09 10:54:27 -0700100//listen change the setinterestfilter
Shuo Chen09f09bb2014-03-18 15:37:11 -0700101void
102DeleteHandle::listen(const Name& prefix)
103{
Weiqi Shif0330d52014-07-09 10:54:27 -0700104 ndn::Name deleteprefix = Name(prefix).append("delete");
105 ndn::InterestFilter filter(deleteprefix);
106 getFace().setInterestFilter(filter,
Shuo Chen09f09bb2014-03-18 15:37:11 -0700107 bind(&DeleteHandle::onInterest, this, _1, _2),
Shuo Chen09f09bb2014-03-18 15:37:11 -0700108 bind(&DeleteHandle::onRegisterFailed, this, _1, _2));
109}
110
111void
112DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
113 uint64_t statusCode, uint64_t nDeletedDatas)
114{
115 RepoCommandResponse response;
116 if (parameter.hasProcessId()) {
117 response.setProcessId(parameter.getProcessId());
118 response.setStatusCode(statusCode);
119 response.setDeleteNum(nDeletedDatas);
120 }
121 else {
122 response.setStatusCode(403);
123 }
124 reply(interest, response);
125}
126
127void
128DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode)
129{
130 RepoCommandResponse response;
131 response.setStatusCode(statusCode);
132 reply(interest, response);
133}
134
135void
136DeleteHandle::processSingleDeleteCommand(const Interest& interest,
137 RepoCommandParameter& parameter)
138{
Weiqi Shif0330d52014-07-09 10:54:27 -0700139 int64_t nDeletedDatas = getStorageHandle().deleteData(parameter.getName());
140 if (nDeletedDatas == -1) {
141 std::cerr << "Deletion Failed!" <<std::endl;
142 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700143 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700144 else
145 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700146}
147
148void
149DeleteHandle::processSelectorDeleteCommand(const Interest& interest,
150 RepoCommandParameter& parameter)
151{
Weiqi Shif0330d52014-07-09 10:54:27 -0700152 int64_t nDeletedDatas = getStorageHandle()
153 .deleteData(Interest(parameter.getName())
154 .setSelectors(parameter.getSelectors()));
155 if (nDeletedDatas == -1) {
156 std::cerr << "Deletion Failed!" <<std::endl;
157 negativeReply(interest, 405); //405 means deletion fail
Shuo Chen09f09bb2014-03-18 15:37:11 -0700158 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700159 else
160 positiveReply(interest, parameter, 200, nDeletedDatas);
Shuo Chen09f09bb2014-03-18 15:37:11 -0700161}
162
163void
164DeleteHandle::processSegmentDeleteCommand(const Interest& interest,
165 RepoCommandParameter& parameter)
166{
167 if (!parameter.hasStartBlockId())
168 parameter.setStartBlockId(0);
169
170 if (parameter.hasEndBlockId()) {
171 SegmentNo startBlockId = parameter.getStartBlockId();
172 SegmentNo endBlockId = parameter.getEndBlockId();
173
174 if (startBlockId > endBlockId) {
175 negativeReply(interest, 403);
176 return;
177 }
178
179 Name prefix = parameter.getName();
180 uint64_t nDeletedDatas = 0;
181 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
182 Name name = prefix;
183 name.appendSegment(i);
184 if (getStorageHandle().deleteData(name)) {
185 nDeletedDatas++;
186 }
187 }
188 //All the data deleted, return 200
189 positiveReply(interest, parameter, 200, nDeletedDatas);
190 }
191 else {
192 BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented
193 }
194}
195
196} //namespace repo