blob: 148116b5af889c11e1f3b391007f321eb7f68043 [file] [log] [blame]
Weiqi Shif0330d52014-07-09 10:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d669612017-09-22 23:49:37 -04002/*
Alexander Afanasyevbb058c02018-02-15 22:49:24 +00003 * Copyright (c) 2014-2018, Regents of the University of California.
Weiqi Shif0330d52014-07-09 10:54:27 -07004 *
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 Pesavento5d669612017-09-22 23:49:37 -040021#include "config.hpp"
22
Weiqi Shif0330d52014-07-09 10:54:27 -070023#include <istream>
24
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000025#include <ndn-cxx/util/logger.hpp>
26
Weiqi Shif0330d52014-07-09 10:54:27 -070027namespace repo {
28
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000029NDN_LOG_INIT(repo.RepoStorage);
Weiqi Shif0330d52014-07-09 10:54:27 -070030
31RepoStorage::RepoStorage(const int64_t& nMaxPackets, Storage& store)
32 : m_index(nMaxPackets)
33 , m_storage(store)
34{
35}
36
37void
38RepoStorage::initialize()
39{
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000040 NDN_LOG_DEBUG("Initialize");
41 m_storage.fullEnumerate(bind(&RepoStorage::insertItemToIndex, this, _1));
42}
43
44void
45RepoStorage::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 Shif0330d52014-07-09 10:54:27 -070050}
51
52bool
53RepoStorage::insertData(const Data& data)
54{
55 bool isExist = m_index.hasData(data);
56 if (isExist)
Alexander Afanasyev42290b22017-03-09 12:58:29 -080057 BOOST_THROW_EXCEPTION(Error("The Entry Has Already In the Skiplist. Cannot be Inserted!"));
Weiqi Shif0330d52014-07-09 10:54:27 -070058 int64_t id = m_storage.insert(data);
59 if (id == -1)
60 return false;
Nick Gordon190e4dc2017-10-04 16:54:10 -050061 bool didInsert = m_index.insert(data, id);
62 if (didInsert)
63 afterDataInsertion(data.getName());
64 return didInsert;
Weiqi Shif0330d52014-07-09 10:54:27 -070065}
66
67ssize_t
68RepoStorage::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 Gordon190e4dc2017-10-04 16:54:10 -050078 if (resultDb && resultIndex) {
79 afterDataDeletion(idName.second);
Weiqi Shif0330d52014-07-09 10:54:27 -070080 count++;
Nick Gordon190e4dc2017-10-04 16:54:10 -050081 }
82 else {
Weiqi Shif0330d52014-07-09 10:54:27 -070083 hasError = true;
Nick Gordon190e4dc2017-10-04 16:54:10 -050084 }
Weiqi Shif0330d52014-07-09 10:54:27 -070085 idName = m_index.find(name);
86 }
87 if (hasError)
88 return -1;
89 else
90 return count;
91}
92
93ssize_t
94RepoStorage::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 Gordon190e4dc2017-10-04 16:54:10 -0500104 if (resultDb && resultIndex) {
105 afterDataDeletion(idName.second);
Weiqi Shif0330d52014-07-09 10:54:27 -0700106 count++;
Nick Gordon190e4dc2017-10-04 16:54:10 -0500107 }
108 else {
Weiqi Shif0330d52014-07-09 10:54:27 -0700109 hasError = true;
Nick Gordon190e4dc2017-10-04 16:54:10 -0500110 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700111 idName = m_index.find(interestDelete);
112 }
113 if (hasError)
114 return -1;
115 else
116 return count;
117}
118
119shared_ptr<Data>
120RepoStorage::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