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 | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 25 | namespace repo { |
| 26 | |
| 27 | static void |
| 28 | insertItemToIndex(Index* index, const Storage::ItemMeta& item) |
| 29 | { |
| 30 | index->insert(item.fullName, item.id, item.keyLocatorHash); |
| 31 | } |
| 32 | |
| 33 | RepoStorage::RepoStorage(const int64_t& nMaxPackets, Storage& store) |
| 34 | : m_index(nMaxPackets) |
| 35 | , m_storage(store) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | RepoStorage::initialize() |
| 41 | { |
| 42 | m_storage.fullEnumerate(bind(&insertItemToIndex, &m_index, _1)); |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | RepoStorage::insertData(const Data& data) |
| 47 | { |
| 48 | bool isExist = m_index.hasData(data); |
| 49 | if (isExist) |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 50 | 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] | 51 | int64_t id = m_storage.insert(data); |
| 52 | if (id == -1) |
| 53 | return false; |
| 54 | return m_index.insert(data, id); |
| 55 | } |
| 56 | |
| 57 | ssize_t |
| 58 | RepoStorage::deleteData(const Name& name) |
| 59 | { |
| 60 | bool hasError = false; |
| 61 | std::pair<int64_t,ndn::Name> idName = m_index.find(name); |
| 62 | if (idName.first == 0) |
| 63 | return false; |
| 64 | int64_t count = 0; |
| 65 | while (idName.first != 0) { |
| 66 | bool resultDb = m_storage.erase(idName.first); |
| 67 | bool resultIndex = m_index.erase(idName.second); //full name |
| 68 | if (resultDb && resultIndex) |
| 69 | count++; |
| 70 | else |
| 71 | hasError = true; |
| 72 | idName = m_index.find(name); |
| 73 | } |
| 74 | if (hasError) |
| 75 | return -1; |
| 76 | else |
| 77 | return count; |
| 78 | } |
| 79 | |
| 80 | ssize_t |
| 81 | RepoStorage::deleteData(const Interest& interest) |
| 82 | { |
| 83 | Interest interestDelete = interest; |
| 84 | interestDelete.setChildSelector(0); //to disable the child selector in delete handle |
| 85 | int64_t count = 0; |
| 86 | bool hasError = false; |
| 87 | std::pair<int64_t,ndn::Name> idName = m_index.find(interestDelete); |
| 88 | while (idName.first != 0) { |
| 89 | bool resultDb = m_storage.erase(idName.first); |
| 90 | bool resultIndex = m_index.erase(idName.second); //full name |
| 91 | if (resultDb && resultIndex) |
| 92 | count++; |
| 93 | else |
| 94 | hasError = true; |
| 95 | idName = m_index.find(interestDelete); |
| 96 | } |
| 97 | if (hasError) |
| 98 | return -1; |
| 99 | else |
| 100 | return count; |
| 101 | } |
| 102 | |
| 103 | shared_ptr<Data> |
| 104 | RepoStorage::readData(const Interest& interest) const |
| 105 | { |
| 106 | std::pair<int64_t,ndn::Name> idName = m_index.find(interest); |
| 107 | if (idName.first != 0) { |
| 108 | shared_ptr<Data> data = m_storage.read(idName.first); |
| 109 | if (data) { |
| 110 | return data; |
| 111 | } |
| 112 | } |
| 113 | return shared_ptr<Data>(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | } // namespace repo |