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