blob: 381ccaa2a63a88b1d1412c609f5d29bf1260d0ef [file] [log] [blame]
Weiqi Shif0330d52014-07-09 10:54:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
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"
21#include "../../build/src/config.hpp"
22#include <istream>
23
24namespace repo {
25
26static void
27insertItemToIndex(Index* index, const Storage::ItemMeta& item)
28{
29 index->insert(item.fullName, item.id, item.keyLocatorHash);
30}
31
32RepoStorage::RepoStorage(const int64_t& nMaxPackets, Storage& store)
33 : m_index(nMaxPackets)
34 , m_storage(store)
35{
36}
37
38void
39RepoStorage::initialize()
40{
41 m_storage.fullEnumerate(bind(&insertItemToIndex, &m_index, _1));
42}
43
44bool
45RepoStorage::insertData(const Data& data)
46{
47 bool isExist = m_index.hasData(data);
48 if (isExist)
49 throw Error("The Entry Has Already In the Skiplist. Cannot be Inserted!");
50 int64_t id = m_storage.insert(data);
51 if (id == -1)
52 return false;
53 return m_index.insert(data, id);
54}
55
56ssize_t
57RepoStorage::deleteData(const Name& name)
58{
59 bool hasError = false;
60 std::pair<int64_t,ndn::Name> idName = m_index.find(name);
61 if (idName.first == 0)
62 return false;
63 int64_t count = 0;
64 while (idName.first != 0) {
65 bool resultDb = m_storage.erase(idName.first);
66 bool resultIndex = m_index.erase(idName.second); //full name
67 if (resultDb && resultIndex)
68 count++;
69 else
70 hasError = true;
71 idName = m_index.find(name);
72 }
73 if (hasError)
74 return -1;
75 else
76 return count;
77}
78
79ssize_t
80RepoStorage::deleteData(const Interest& interest)
81{
82 Interest interestDelete = interest;
83 interestDelete.setChildSelector(0); //to disable the child selector in delete handle
84 int64_t count = 0;
85 bool hasError = false;
86 std::pair<int64_t,ndn::Name> idName = m_index.find(interestDelete);
87 while (idName.first != 0) {
88 bool resultDb = m_storage.erase(idName.first);
89 bool resultIndex = m_index.erase(idName.second); //full name
90 if (resultDb && resultIndex)
91 count++;
92 else
93 hasError = true;
94 idName = m_index.find(interestDelete);
95 }
96 if (hasError)
97 return -1;
98 else
99 return count;
100}
101
102shared_ptr<Data>
103RepoStorage::readData(const Interest& interest) const
104{
105 std::pair<int64_t,ndn::Name> idName = m_index.find(interest);
106 if (idName.first != 0) {
107 shared_ptr<Data> data = m_storage.read(idName.first);
108 if (data) {
109 return data;
110 }
111 }
112 return shared_ptr<Data>();
113}
114
115
116} // namespace repo