weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, 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/>. |
| 18 | */ |
| 19 | |
| 20 | #include "repo-command.hpp" |
| 21 | |
| 22 | namespace repo { |
| 23 | |
| 24 | void |
| 25 | RepoCommand::validateRequest(const RepoCommandParameter& parameters) { |
| 26 | m_requestValidator.validate(parameters); |
| 27 | check(parameters); |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | RepoCommand::FieldValidator::validate(const RepoCommandParameter& parameters) const |
| 32 | { |
| 33 | const std::vector<bool>& presentFields = parameters.getPresentFields(); |
| 34 | |
| 35 | for (size_t i = 0; i < REPO_PARAMETER_UBOUND; i++) { |
| 36 | bool isPresent = presentFields[i]; |
| 37 | if (m_required[i]) { |
| 38 | if (!isPresent) { |
| 39 | BOOST_THROW_EXCEPTION(ArgumentError(REPO_PARAMETER_FIELD[i] + " is required but missing")); |
| 40 | } |
| 41 | } |
| 42 | else if (isPresent && !m_optional[i]) { |
| 43 | BOOST_THROW_EXCEPTION(ArgumentError(REPO_PARAMETER_FIELD[i] + " is forbidden but present")); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | RepoCommand::FieldValidator::FieldValidator() |
| 49 | : m_required(REPO_PARAMETER_UBOUND) |
| 50 | , m_optional(REPO_PARAMETER_UBOUND) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | InsertCommand::InsertCommand() |
| 55 | : RepoCommand() |
| 56 | { |
| 57 | m_requestValidator |
| 58 | .required(REPO_PARAMETER_NAME) |
| 59 | .required(REPO_PARAMETER_START_BLOCK_ID) |
| 60 | .required(REPO_PARAMETER_END_BLOCK_ID); |
| 61 | } |
| 62 | |
| 63 | InsertCheckCommand::InsertCheckCommand() |
| 64 | { |
| 65 | m_requestValidator |
| 66 | .required(REPO_PARAMETER_NAME) |
| 67 | .required(REPO_PARAMETER_PROCESS_ID); |
| 68 | } |
| 69 | |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 70 | DeleteCommand::DeleteCommand() |
| 71 | { |
| 72 | m_requestValidator |
| 73 | .required(REPO_PARAMETER_NAME) |
| 74 | .required(REPO_PARAMETER_START_BLOCK_ID) |
| 75 | .required(REPO_PARAMETER_END_BLOCK_ID) |
| 76 | .required(REPO_PARAMETER_PROCESS_ID); |
| 77 | } |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 78 | |
| 79 | void |
| 80 | DeleteCommand::check(const RepoCommandParameter& parameters) const |
| 81 | { |
| 82 | if (parameters.hasStartBlockId() || parameters.hasEndBlockId()) { |
| 83 | if (parameters.hasEndBlockId()) { |
| 84 | SegmentNo startBlockId = parameters.getStartBlockId(); |
| 85 | SegmentNo endBlockId = parameters.getEndBlockId(); |
| 86 | |
| 87 | if (startBlockId > endBlockId) { |
| 88 | BOOST_THROW_EXCEPTION(ArgumentError("start block Id is bigger than end block Id")); |
| 89 | } |
| 90 | } |
| 91 | else { |
| 92 | BOOST_THROW_EXCEPTION(ArgumentError("Segmented deletion without EndBlockId, not implemented")); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } // namespace repo |