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 | |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 39 | void |
| 40 | DeleteHandle::onValidated(const shared_ptr<const Interest>& interest, const Name& prefix) |
| 41 | { |
| 42 | RepoCommandParameter parameter; |
| 43 | |
| 44 | try { |
| 45 | extractParameter(*interest, prefix, parameter); |
| 46 | } |
| 47 | catch (RepoCommandParameter::Error) { |
| 48 | negativeReply(*interest, 403); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (parameter.hasSelectors()) { |
| 53 | |
| 54 | if (parameter.hasStartBlockId() || parameter.hasEndBlockId()) { |
| 55 | negativeReply(*interest, 402); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | //choose data with selector and delete it |
| 60 | processSelectorDeleteCommand(*interest, parameter); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (!parameter.hasStartBlockId() && !parameter.hasEndBlockId()) { |
| 65 | processSingleDeleteCommand(*interest, parameter); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | processSegmentDeleteCommand(*interest, parameter); |
| 70 | } |
| 71 | |
| 72 | void |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 73 | DeleteHandle::onValidationFailed(const shared_ptr<const Interest>& interest, |
| 74 | const std::string& reason) |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 75 | { |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 76 | std::cerr << reason << std::endl; |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 77 | negativeReply(*interest, 401); |
| 78 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 79 | //listen change the setinterestfilter |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 80 | void |
| 81 | DeleteHandle::listen(const Name& prefix) |
| 82 | { |
Junxiao Shi | 2b7b831 | 2017-06-16 03:43:24 +0000 | [diff] [blame^] | 83 | getFace().setInterestFilter(Name(prefix).append("delete"), |
| 84 | bind(&DeleteHandle::onInterest, this, _1, _2)); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter, |
| 89 | uint64_t statusCode, uint64_t nDeletedDatas) |
| 90 | { |
| 91 | RepoCommandResponse response; |
| 92 | if (parameter.hasProcessId()) { |
| 93 | response.setProcessId(parameter.getProcessId()); |
| 94 | response.setStatusCode(statusCode); |
| 95 | response.setDeleteNum(nDeletedDatas); |
| 96 | } |
| 97 | else { |
| 98 | response.setStatusCode(403); |
| 99 | } |
| 100 | reply(interest, response); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode) |
| 105 | { |
| 106 | RepoCommandResponse response; |
| 107 | response.setStatusCode(statusCode); |
| 108 | reply(interest, response); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | DeleteHandle::processSingleDeleteCommand(const Interest& interest, |
| 113 | RepoCommandParameter& parameter) |
| 114 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 115 | int64_t nDeletedDatas = getStorageHandle().deleteData(parameter.getName()); |
| 116 | if (nDeletedDatas == -1) { |
| 117 | std::cerr << "Deletion Failed!" <<std::endl; |
| 118 | negativeReply(interest, 405); //405 means deletion fail |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 119 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 120 | else |
| 121 | positiveReply(interest, parameter, 200, nDeletedDatas); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
| 125 | DeleteHandle::processSelectorDeleteCommand(const Interest& interest, |
| 126 | RepoCommandParameter& parameter) |
| 127 | { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 128 | int64_t nDeletedDatas = getStorageHandle() |
| 129 | .deleteData(Interest(parameter.getName()) |
| 130 | .setSelectors(parameter.getSelectors())); |
| 131 | if (nDeletedDatas == -1) { |
| 132 | std::cerr << "Deletion Failed!" <<std::endl; |
| 133 | negativeReply(interest, 405); //405 means deletion fail |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 134 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 135 | else |
| 136 | positiveReply(interest, parameter, 200, nDeletedDatas); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
| 140 | DeleteHandle::processSegmentDeleteCommand(const Interest& interest, |
| 141 | RepoCommandParameter& parameter) |
| 142 | { |
| 143 | if (!parameter.hasStartBlockId()) |
| 144 | parameter.setStartBlockId(0); |
| 145 | |
| 146 | if (parameter.hasEndBlockId()) { |
| 147 | SegmentNo startBlockId = parameter.getStartBlockId(); |
| 148 | SegmentNo endBlockId = parameter.getEndBlockId(); |
| 149 | |
| 150 | if (startBlockId > endBlockId) { |
| 151 | negativeReply(interest, 403); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | Name prefix = parameter.getName(); |
| 156 | uint64_t nDeletedDatas = 0; |
| 157 | for (SegmentNo i = startBlockId; i <= endBlockId; i++) { |
| 158 | Name name = prefix; |
| 159 | name.appendSegment(i); |
| 160 | if (getStorageHandle().deleteData(name)) { |
| 161 | nDeletedDatas++; |
| 162 | } |
| 163 | } |
| 164 | //All the data deleted, return 200 |
| 165 | positiveReply(interest, parameter, 200, nDeletedDatas); |
| 166 | } |
| 167 | else { |
| 168 | BOOST_ASSERT(false); // segmented deletion without EndBlockId, not implemented |
| 169 | } |
| 170 | } |
| 171 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 172 | } // namespace repo |