Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 22 | #include <ndn-cxx/util/logger.hpp> |
| 23 | |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 24 | namespace repo { |
| 25 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 26 | NDN_LOG_INIT(repo.DeleteHandle); |
| 27 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 28 | DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle, |
| 29 | ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler, |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 30 | Validator& validator) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 31 | : CommandBaseHandle(face, storageHandle, scheduler, validator) |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 32 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 33 | dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("delete"), |
| 34 | makeAuthorization(), |
| 35 | std::bind(&DeleteHandle::validateParameters<DeleteCommand>, this, _1), |
| 36 | std::bind(&DeleteHandle::handleDeleteCommand, this, _1, _2, _3, _4)); |
| 37 | |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 41 | DeleteHandle::handleDeleteCommand(const Name& prefix, const Interest& interest, |
| 42 | const ndn::mgmt::ControlParameters& parameter, |
| 43 | const ndn::mgmt::CommandContinuation& done) |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 44 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 45 | const RepoCommandParameter& repoParameter = dynamic_cast<const RepoCommandParameter&>(parameter); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 46 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 47 | if (!repoParameter.hasStartBlockId() && !repoParameter.hasEndBlockId()) { |
| 48 | processSingleDeleteCommand(interest, repoParameter, done); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 52 | processSegmentDeleteCommand(interest, repoParameter, done); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 53 | } |
| 54 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 55 | RepoCommandResponse |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 56 | DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter, |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 57 | uint64_t statusCode, uint64_t nDeletedData) const |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 58 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 59 | RepoCommandResponse response(statusCode, "Deletion Successful"); |
| 60 | |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 61 | if (parameter.hasProcessId()) { |
| 62 | response.setProcessId(parameter.getProcessId()); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 63 | response.setDeleteNum(nDeletedData); |
| 64 | response.setBody(response.wireEncode()); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 65 | } |
| 66 | else { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 67 | response.setCode(403); |
| 68 | response.setText("Malformed Command"); |
| 69 | response.setBody(response.wireEncode()); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 70 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 71 | return response; |
| 72 | } |
| 73 | |
| 74 | RepoCommandResponse |
| 75 | DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode, std::string text) const |
| 76 | { |
| 77 | RepoCommandResponse response(statusCode, text); |
| 78 | response.setBody(response.wireEncode()); |
| 79 | return response; |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 83 | DeleteHandle::processSingleDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter, |
| 84 | const ndn::mgmt::CommandContinuation& done) const |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 85 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 86 | int64_t nDeletedData = storageHandle.deleteData(parameter.getName()); |
| 87 | if (nDeletedData == -1) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 88 | NDN_LOG_DEBUG("Deletion Failed"); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 89 | done(negativeReply(interest, 405, "Deletion Failed")); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 90 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 91 | else |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 92 | done(positiveReply(interest, parameter, 200, nDeletedData)); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 96 | DeleteHandle::processSegmentDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter, |
| 97 | const ndn::mgmt::CommandContinuation& done) const |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 98 | { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 99 | SegmentNo startBlockId = parameter.hasStartBlockId() ? parameter.getStartBlockId() : 0; |
| 100 | SegmentNo endBlockId = parameter.getEndBlockId(); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 101 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 102 | Name prefix = parameter.getName(); |
| 103 | uint64_t nDeletedData = 0; |
| 104 | for (SegmentNo i = startBlockId; i <= endBlockId; i++) { |
| 105 | Name name = prefix; |
| 106 | name.appendSegment(i); |
| 107 | if (storageHandle.deleteData(name)) { |
| 108 | nDeletedData++; |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 109 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 110 | } |
| 111 | //All the data deleted, return 200 |
| 112 | done(positiveReply(interest, parameter, 200, nDeletedData)); |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 113 | |
Shuo Chen | 09f09bb | 2014-03-18 15:37:11 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 116 | } // namespace repo |