Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 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 Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "delete-handle.hpp" |
| 21 | |
| 22 | namespace repo { |
| 23 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 24 | DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle, KeyChain& keyChain, |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame^] | 25 | Scheduler& scheduler, |
| 26 | Validator& validator) |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 27 | : BaseHandle(face, storageHandle, keyChain, scheduler) |
| 28 | , m_validator(validator) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | DeleteHandle::onInterest(const Name& prefix, const Interest& interest) |
| 34 | { |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 35 | m_validator.validate(interest, bind(&DeleteHandle::onValidated, this, _1, prefix), |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 36 | bind(&DeleteHandle::onValidationFailed, this, _1, _2)); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
| 40 | void |
| 41 | DeleteHandle::onRegisterFailed(const Name& prefix, const std::string& reason) |
| 42 | { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 43 | BOOST_THROW_EXCEPTION(Error("Delete prefix registration failed")); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | void |
| 48 | DeleteHandle::onCheckInterest(const Name& prefix, const Interest& interest) |
| 49 | { |
| 50 | BOOST_ASSERT(false); // Deletion progress check, not implemented |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void |
| 55 | DeleteHandle::onCheckRegisterFailed(const Name& prefix, const std::string& reason) |
| 56 | { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 57 | BOOST_THROW_EXCEPTION(Error("Delete check prefix registration failed")); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | void |
| 62 | DeleteHandle::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 | |
| 94 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 95 | DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest, |
| 96 | const std::string& reason) |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 97 | { |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 98 | std::cerr << reason << std::endl; |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 99 | negativeReply(*interest, 401); |
| 100 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 101 | //listen change the setinterestfilter |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 102 | void |
| 103 | DeleteHandle::listen(const Name& prefix) |
| 104 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 105 | ndn::Name deleteprefix = Name(prefix).append("delete"); |
| 106 | ndn::InterestFilter filter(deleteprefix); |
| 107 | getFace().setInterestFilter(filter, |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 108 | bind(&DeleteHandle::onInterest, this, _1, _2), |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 109 | bind(&DeleteHandle::onRegisterFailed, this, _1, _2)); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter, |
| 114 | uint64_t statusCode, uint64_t nDeletedDatas) |
| 115 | { |
| 116 | RepoCommandResponse response; |
| 117 | if (parameter.hasProcessId()) { |
| 118 | response.setProcessId(parameter.getProcessId()); |
| 119 | response.setStatusCode(statusCode); |
| 120 | response.setDeleteNum(nDeletedDatas); |
| 121 | } |
| 122 | else { |
| 123 | response.setStatusCode(403); |
| 124 | } |
| 125 | reply(interest, response); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode) |
| 130 | { |
| 131 | RepoCommandResponse response; |
| 132 | response.setStatusCode(statusCode); |
| 133 | reply(interest, response); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | DeleteHandle::processSingleDeleteCommand(const Interest& interest, |
| 138 | RepoCommandParameter& parameter) |
| 139 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 140 | int64_t nDeletedDatas = getStorageHandle().deleteData(parameter.getName()); |
| 141 | if (nDeletedDatas == -1) { |
| 142 | std::cerr << "Deletion Failed!" <<std::endl; |
| 143 | negativeReply(interest, 405); //405 means deletion fail |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 144 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 145 | else |
| 146 | positiveReply(interest, parameter, 200, nDeletedDatas); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | DeleteHandle::processSelectorDeleteCommand(const Interest& interest, |
| 151 | RepoCommandParameter& parameter) |
| 152 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 153 | int64_t nDeletedDatas = getStorageHandle() |
| 154 | .deleteData(Interest(parameter.getName()) |
| 155 | .setSelectors(parameter.getSelectors())); |
| 156 | if (nDeletedDatas == -1) { |
| 157 | std::cerr << "Deletion Failed!" <<std::endl; |
| 158 | negativeReply(interest, 405); //405 means deletion fail |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 159 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 160 | else |
| 161 | positiveReply(interest, parameter, 200, nDeletedDatas); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void |
| 165 | DeleteHandle::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 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 197 | } // namespace repo |