blob: 1828eb9d5642c1f129a3560695a00fb72ae50a02 [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;
54 return m_index.insert(data, id);
55}
56
57ssize_t
58RepoStorage::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
80ssize_t
81RepoStorage::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
103shared_ptr<Data>
104RepoStorage::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