Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | bb058c0 | 2018-02-15 22:49:24 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California. |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -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-storage.hpp" |
Davide Pesavento | 5d66961 | 2017-09-22 23:49:37 -0400 | [diff] [blame] | 21 | #include "config.hpp" |
| 22 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 23 | #include <istream> |
| 24 | |
Alexander Afanasyev | bb058c0 | 2018-02-15 22:49:24 +0000 | [diff] [blame] | 25 | #include <ndn-cxx/util/logger.hpp> |
| 26 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 27 | namespace repo { |
| 28 | |
Alexander Afanasyev | bb058c0 | 2018-02-15 22:49:24 +0000 | [diff] [blame] | 29 | NDN_LOG_INIT(repo.RepoStorage); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 30 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 31 | RepoStorage::RepoStorage(Storage& store) |
| 32 | : m_storage(store) |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 36 | bool |
| 37 | RepoStorage::insertData(const Data& data) |
| 38 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 39 | bool isExist = m_storage.has(data.getFullName()); |
| 40 | |
| 41 | if (isExist) { |
| 42 | NDN_LOG_DEBUG("Data already in storage, regarded as successful data insertion"); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | int64_t id = m_storage.insert(data); |
| 47 | NDN_LOG_DEBUG("Insert ID: " << id << ", full name:" << data.getFullName()); |
| 48 | if (id == NOTFOUND) |
| 49 | return false; |
| 50 | |
| 51 | afterDataInsertion(data.getName()); |
| 52 | return true; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ssize_t |
| 56 | RepoStorage::deleteData(const Name& name) |
| 57 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 58 | NDN_LOG_DEBUG("Delete: " << name); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 59 | bool hasError = false; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 60 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 61 | int64_t count = 0; |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 62 | std::shared_ptr<Data> foundData; |
| 63 | Name foundName; |
| 64 | while ((foundData = m_storage.find(name))) { |
| 65 | foundName = foundData->getFullName(); |
| 66 | bool resultDb = m_storage.erase(foundName); |
| 67 | if (resultDb) { |
| 68 | afterDataDeletion(foundName); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 69 | count++; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 70 | } |
| 71 | else { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 72 | hasError = true; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 73 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 74 | NDN_LOG_DEBUG("Delete: " << name << ", found " << foundName << ", count " << count << ", result " << resultDb); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 75 | } |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 76 | |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 77 | if (hasError) |
| 78 | return -1; |
| 79 | else |
| 80 | return count; |
| 81 | } |
| 82 | |
| 83 | ssize_t |
| 84 | RepoStorage::deleteData(const Interest& interest) |
| 85 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 86 | return deleteData(interest.getName()); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 89 | std::shared_ptr<Data> |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 90 | RepoStorage::readData(const Interest& interest) const |
| 91 | { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame^] | 92 | NDN_LOG_DEBUG("Reading data for " << interest.getName()); |
| 93 | |
| 94 | return m_storage.read(interest.getName()); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | } // namespace repo |