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 | |
| 31 | RepoStorage::RepoStorage(const int64_t& nMaxPackets, Storage& store) |
| 32 | : m_index(nMaxPackets) |
| 33 | , m_storage(store) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | RepoStorage::initialize() |
| 39 | { |
Alexander Afanasyev | bb058c0 | 2018-02-15 22:49:24 +0000 | [diff] [blame^] | 40 | NDN_LOG_DEBUG("Initialize"); |
| 41 | m_storage.fullEnumerate(bind(&RepoStorage::insertItemToIndex, this, _1)); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | RepoStorage::insertItemToIndex(const Storage::ItemMeta& item) |
| 46 | { |
| 47 | NDN_LOG_DEBUG("Insert data to index " << item.fullName); |
| 48 | m_index.insert(item.fullName, item.id, item.keyLocatorHash); |
| 49 | afterDataInsertion(item.fullName); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool |
| 53 | RepoStorage::insertData(const Data& data) |
| 54 | { |
| 55 | bool isExist = m_index.hasData(data); |
| 56 | if (isExist) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 57 | BOOST_THROW_EXCEPTION(Error("The Entry Has Already In the Skiplist. Cannot be Inserted!")); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 58 | int64_t id = m_storage.insert(data); |
| 59 | if (id == -1) |
| 60 | return false; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 61 | bool didInsert = m_index.insert(data, id); |
| 62 | if (didInsert) |
| 63 | afterDataInsertion(data.getName()); |
| 64 | return didInsert; |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ssize_t |
| 68 | RepoStorage::deleteData(const Name& name) |
| 69 | { |
| 70 | bool hasError = false; |
| 71 | std::pair<int64_t,ndn::Name> idName = m_index.find(name); |
| 72 | if (idName.first == 0) |
| 73 | return false; |
| 74 | int64_t count = 0; |
| 75 | while (idName.first != 0) { |
| 76 | bool resultDb = m_storage.erase(idName.first); |
| 77 | bool resultIndex = m_index.erase(idName.second); //full name |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 78 | if (resultDb && resultIndex) { |
| 79 | afterDataDeletion(idName.second); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 80 | count++; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 81 | } |
| 82 | else { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 83 | hasError = true; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 84 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 85 | idName = m_index.find(name); |
| 86 | } |
| 87 | if (hasError) |
| 88 | return -1; |
| 89 | else |
| 90 | return count; |
| 91 | } |
| 92 | |
| 93 | ssize_t |
| 94 | RepoStorage::deleteData(const Interest& interest) |
| 95 | { |
| 96 | Interest interestDelete = interest; |
| 97 | interestDelete.setChildSelector(0); //to disable the child selector in delete handle |
| 98 | int64_t count = 0; |
| 99 | bool hasError = false; |
| 100 | std::pair<int64_t,ndn::Name> idName = m_index.find(interestDelete); |
| 101 | while (idName.first != 0) { |
| 102 | bool resultDb = m_storage.erase(idName.first); |
| 103 | bool resultIndex = m_index.erase(idName.second); //full name |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 104 | if (resultDb && resultIndex) { |
| 105 | afterDataDeletion(idName.second); |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 106 | count++; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 107 | } |
| 108 | else { |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 109 | hasError = true; |
Nick Gordon | 190e4dc | 2017-10-04 16:54:10 -0500 | [diff] [blame] | 110 | } |
Weiqi Shi | f0330d5 | 2014-07-09 10:54:27 -0700 | [diff] [blame] | 111 | idName = m_index.find(interestDelete); |
| 112 | } |
| 113 | if (hasError) |
| 114 | return -1; |
| 115 | else |
| 116 | return count; |
| 117 | } |
| 118 | |
| 119 | shared_ptr<Data> |
| 120 | RepoStorage::readData(const Interest& interest) const |
| 121 | { |
| 122 | std::pair<int64_t,ndn::Name> idName = m_index.find(interest); |
| 123 | if (idName.first != 0) { |
| 124 | shared_ptr<Data> data = m_storage.read(idName.first); |
| 125 | if (data) { |
| 126 | return data; |
| 127 | } |
| 128 | } |
| 129 | return shared_ptr<Data>(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | } // namespace repo |