blob: c892583d5aebb5d66121e8f6aa87557d179eff5a [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2018, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
*
* repo-ng is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "delete-handle.hpp"
#include <ndn-cxx/util/logger.hpp>
namespace repo {
NDN_LOG_INIT(repo.DeleteHandle);
DeleteHandle::DeleteHandle(Face& face, RepoStorage& storageHandle,
ndn::mgmt::Dispatcher& dispatcher, Scheduler& scheduler,
Validator& validator)
: CommandBaseHandle(face, storageHandle, scheduler, validator)
{
dispatcher.addControlCommand<RepoCommandParameter>(ndn::PartialName("delete"),
makeAuthorization(),
std::bind(&DeleteHandle::validateParameters<DeleteCommand>, this, _1),
std::bind(&DeleteHandle::handleDeleteCommand, this, _1, _2, _3, _4));
}
void
DeleteHandle::handleDeleteCommand(const Name& prefix, const Interest& interest,
const ndn::mgmt::ControlParameters& parameter,
const ndn::mgmt::CommandContinuation& done)
{
const RepoCommandParameter& repoParameter = dynamic_cast<const RepoCommandParameter&>(parameter);
if (!repoParameter.hasStartBlockId() && !repoParameter.hasEndBlockId()) {
processSingleDeleteCommand(interest, repoParameter, done);
return;
}
processSegmentDeleteCommand(interest, repoParameter, done);
}
RepoCommandResponse
DeleteHandle::positiveReply(const Interest& interest, const RepoCommandParameter& parameter,
uint64_t statusCode, uint64_t nDeletedData) const
{
RepoCommandResponse response(statusCode, "Deletion Successful");
if (parameter.hasProcessId()) {
response.setProcessId(parameter.getProcessId());
response.setDeleteNum(nDeletedData);
response.setBody(response.wireEncode());
}
else {
response.setCode(403);
response.setText("Malformed Command");
response.setBody(response.wireEncode());
}
return response;
}
RepoCommandResponse
DeleteHandle::negativeReply(const Interest& interest, uint64_t statusCode, std::string text) const
{
RepoCommandResponse response(statusCode, text);
response.setBody(response.wireEncode());
return response;
}
void
DeleteHandle::processSingleDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
const ndn::mgmt::CommandContinuation& done) const
{
int64_t nDeletedData = storageHandle.deleteData(parameter.getName());
if (nDeletedData == -1) {
NDN_LOG_DEBUG("Deletion Failed");
done(negativeReply(interest, 405, "Deletion Failed"));
}
else
done(positiveReply(interest, parameter, 200, nDeletedData));
}
void
DeleteHandle::processSegmentDeleteCommand(const Interest& interest, const RepoCommandParameter& parameter,
const ndn::mgmt::CommandContinuation& done) const
{
SegmentNo startBlockId = parameter.hasStartBlockId() ? parameter.getStartBlockId() : 0;
SegmentNo endBlockId = parameter.getEndBlockId();
Name prefix = parameter.getName();
uint64_t nDeletedData = 0;
for (SegmentNo i = startBlockId; i <= endBlockId; i++) {
Name name = prefix;
name.appendSegment(i);
if (storageHandle.deleteData(name)) {
nDeletedData++;
}
}
//All the data deleted, return 200
done(positiveReply(interest, parameter, 200, nDeletedData));
}
} // namespace repo