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