blob: 49e4d9855ca1bfeb333aeec0e22551d5400735f4 [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,
25 Scheduler& scheduler, CommandInterestValidator& validator)
26 : BaseHandle(face, storageHandle, keyChain, scheduler)
27 , m_validator(validator)
28{
29}
30
31void
32DeleteHandle::onInterest(const Name& prefix, const Interest& interest)
33{
34 //std::cout << "call DeleteHandle" << std::endl;
35 m_validator.validate(interest, bind(&DeleteHandle::onValidated, this, _1, prefix),
36 bind(&DeleteHandle::onValidationFailed, this, _1, prefix));
37}
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
95DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest, const Name& prefix)
96{
97 std::cout << "invalidated" << std::endl;
98 negativeReply(*interest, 401);
99}
100
101void
102DeleteHandle::listen(const Name& prefix)
103{
Shuo Chen478204c2014-03-18 18:27:04 -0700104 getFace().setInterestFilter(Name(prefix).append("delete"),
Shuo Chen09f09bb2014-03-18 15:37:11 -0700105 bind(&DeleteHandle::onInterest, this, _1, _2),
106 bind(&DeleteHandle::onRegisterFailed, this, _1, _2));
107}
108
109void
110DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
111 uint64_t statusCode, uint64_t nDeletedDatas)
112{
113 RepoCommandResponse response;
114 if (parameter.hasProcessId()) {
115 response.setProcessId(parameter.getProcessId());
116 response.setStatusCode(statusCode);
117 response.setDeleteNum(nDeletedDatas);
118 }
119 else {
120 response.setStatusCode(403);
121 }
122 reply(interest, response);
123}
124
125void
126DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode)
127{
128 RepoCommandResponse response;
129 response.setStatusCode(statusCode);
130 reply(interest, response);
131}
132
133void
134DeleteHandle::processSingleDeleteCommand(const Interest& interest,
135 RepoCommandParameter& parameter)
136{
137 uint64_t nDeletedDatas = 0;
138 if (getStorageHandle().deleteData(parameter.getName())) {
139 nDeletedDatas++;
140 }
141 positiveReply(interest, parameter, 200, nDeletedDatas);
142}
143
144void
145DeleteHandle::processSelectorDeleteCommand(const Interest& interest,
146 RepoCommandParameter& parameter)
147{
148 uint64_t nDeletedDatas = 0;
149 Name name = parameter.getName();
150 Selectors selectors = parameter.getSelectors();
151 vector<Name> names;
152 getStorageHandle().readNameAny(name, selectors, names);
153
154 for (vector<Name>::iterator it = names.begin(); it != names.end(); ++it) {
155 if (getStorageHandle().deleteData(*it)) {
156 nDeletedDatas++;
157 }
158 }
159
160 //All data has been deleted, return 200
161 positiveReply(interest, parameter, 200, nDeletedDatas);
162}
163
164void
165DeleteHandle::processSegmentDeleteCommand(const Interest& interest,
166 RepoCommandParameter& parameter)
167{
168 if (!parameter.hasStartBlockId())
169 parameter.setStartBlockId(0);
170
171 if (parameter.hasEndBlockId()) {
172 SegmentNo startBlockId = parameter.getStartBlockId();
173 SegmentNo endBlockId = parameter.getEndBlockId();
174
175 if (startBlockId > endBlockId) {
176 negativeReply(interest, 403);
177 return;
178 }
179
180 Name prefix = parameter.getName();
181 uint64_t nDeletedDatas = 0;
182 for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
183 Name name = prefix;
184 name.appendSegment(i);
185 if (getStorageHandle().deleteData(name)) {
186 nDeletedDatas++;
187 }
188 }
189 //All the data deleted, return 200
190 positiveReply(interest, parameter, 200, nDeletedDatas);
191 }
192 else {
193 BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented
194 }
195}
196
197} //namespace repo