blob: d3834bae04efa6a09d24019579486f19ab49ce49 [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 Afanasyev42290b22017-03-09 12:58:29 -08003 * Copyright (c) 2014-2017, 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
25namespace repo {
26
27static void
28insertItemToIndex(Index* index, const Storage::ItemMeta& item)
29{
30 index->insert(item.fullName, item.id, item.keyLocatorHash);
31}
32
33RepoStorage::RepoStorage(const int64_t& nMaxPackets, Storage& store)
34 : m_index(nMaxPackets)
35 , m_storage(store)
36{
37}
38
39void
40RepoStorage::initialize()
41{
42 m_storage.fullEnumerate(bind(&insertItemToIndex, &m_index, _1));
43}
44
45bool
46RepoStorage::insertData(const Data& data)
47{
48 bool isExist = m_index.hasData(data);
49 if (isExist)
Alexander Afanasyev42290b22017-03-09 12:58:29 -080050 BOOST_THROW_EXCEPTION(Error("The Entry Has Already In the Skiplist. Cannot be Inserted!"));
Weiqi Shif0330d52014-07-09 10:54:27 -070051 int64_t id = m_storage.insert(data);
52 if (id == -1)
53 return false;
Nick Gordon190e4dc2017-10-04 16:54:10 -050054 bool didInsert = m_index.insert(data, id);
55 if (didInsert)
56 afterDataInsertion(data.getName());
57 return didInsert;
Weiqi Shif0330d52014-07-09 10:54:27 -070058}
59
60ssize_t
61RepoStorage::deleteData(const Name& name)
62{
63 bool hasError = false;
64 std::pair<int64_t,ndn::Name> idName = m_index.find(name);
65 if (idName.first == 0)
66 return false;
67 int64_t count = 0;
68 while (idName.first != 0) {
69 bool resultDb = m_storage.erase(idName.first);
70 bool resultIndex = m_index.erase(idName.second); //full name
Nick Gordon190e4dc2017-10-04 16:54:10 -050071 if (resultDb && resultIndex) {
72 afterDataDeletion(idName.second);
Weiqi Shif0330d52014-07-09 10:54:27 -070073 count++;
Nick Gordon190e4dc2017-10-04 16:54:10 -050074 }
75 else {
Weiqi Shif0330d52014-07-09 10:54:27 -070076 hasError = true;
Nick Gordon190e4dc2017-10-04 16:54:10 -050077 }
Weiqi Shif0330d52014-07-09 10:54:27 -070078 idName = m_index.find(name);
79 }
80 if (hasError)
81 return -1;
82 else
83 return count;
84}
85
86ssize_t
87RepoStorage::deleteData(const Interest& interest)
88{
89 Interest interestDelete = interest;
90 interestDelete.setChildSelector(0); //to disable the child selector in delete handle
91 int64_t count = 0;
92 bool hasError = false;
93 std::pair<int64_t,ndn::Name> idName = m_index.find(interestDelete);
94 while (idName.first != 0) {
95 bool resultDb = m_storage.erase(idName.first);
96 bool resultIndex = m_index.erase(idName.second); //full name
Nick Gordon190e4dc2017-10-04 16:54:10 -050097 if (resultDb && resultIndex) {
98 afterDataDeletion(idName.second);
Weiqi Shif0330d52014-07-09 10:54:27 -070099 count++;
Nick Gordon190e4dc2017-10-04 16:54:10 -0500100 }
101 else {
Weiqi Shif0330d52014-07-09 10:54:27 -0700102 hasError = true;
Nick Gordon190e4dc2017-10-04 16:54:10 -0500103 }
Weiqi Shif0330d52014-07-09 10:54:27 -0700104 idName = m_index.find(interestDelete);
105 }
106 if (hasError)
107 return -1;
108 else
109 return count;
110}
111
112shared_ptr<Data>
113RepoStorage::readData(const Interest& interest) const
114{
115 std::pair<int64_t,ndn::Name> idName = m_index.find(interest);
116 if (idName.first != 0) {
117 shared_ptr<Data> data = m_storage.read(idName.first);
118 if (data) {
119 return data;
120 }
121 }
122 return shared_ptr<Data>();
123}
124
125
126} // namespace repo