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