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