blob: 6029a05f3168bb3b407cc866af185ed902b32691 [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 Afanasyev5c16cc22019-04-02 14:17:12 -04003 * Copyright (c) 2014-2019, 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 Afanasyev5c16cc22019-04-02 14:17:12 -040025#include <ndn-cxx/util/exception.hpp>
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000026#include <ndn-cxx/util/logger.hpp>
27
Weiqi Shif0330d52014-07-09 10:54:27 -070028namespace repo {
29
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000030NDN_LOG_INIT(repo.RepoStorage);
Weiqi Shif0330d52014-07-09 10:54:27 -070031
weijia yuan3aa8d2b2018-03-06 15:35:57 -080032RepoStorage::RepoStorage(Storage& store)
33 : m_storage(store)
Weiqi Shif0330d52014-07-09 10:54:27 -070034{
35}
36
Alexander Afanasyev5c16cc22019-04-02 14:17:12 -040037void
38RepoStorage::notifyAboutExistingData()
39{
40 m_storage.forEach([this] (const Name& name) {
41 afterDataInsertion(name);
42 });
43}
44
Weiqi Shif0330d52014-07-09 10:54:27 -070045bool
46RepoStorage::insertData(const Data& data)
47{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080048 bool isExist = m_storage.has(data.getFullName());
49
50 if (isExist) {
51 NDN_LOG_DEBUG("Data already in storage, regarded as successful data insertion");
52 return true;
53 }
54
55 int64_t id = m_storage.insert(data);
56 NDN_LOG_DEBUG("Insert ID: " << id << ", full name:" << data.getFullName());
57 if (id == NOTFOUND)
58 return false;
59
60 afterDataInsertion(data.getName());
61 return true;
Weiqi Shif0330d52014-07-09 10:54:27 -070062}
63
64ssize_t
65RepoStorage::deleteData(const Name& name)
66{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080067 NDN_LOG_DEBUG("Delete: " << name);
Weiqi Shif0330d52014-07-09 10:54:27 -070068 bool hasError = false;
weijia yuan3aa8d2b2018-03-06 15:35:57 -080069
Weiqi Shif0330d52014-07-09 10:54:27 -070070 int64_t count = 0;
weijia yuan3aa8d2b2018-03-06 15:35:57 -080071 std::shared_ptr<Data> foundData;
72 Name foundName;
73 while ((foundData = m_storage.find(name))) {
74 foundName = foundData->getFullName();
75 bool resultDb = m_storage.erase(foundName);
76 if (resultDb) {
77 afterDataDeletion(foundName);
Weiqi Shif0330d52014-07-09 10:54:27 -070078 count++;
Nick Gordon190e4dc2017-10-04 16:54:10 -050079 }
80 else {
Weiqi Shif0330d52014-07-09 10:54:27 -070081 hasError = true;
Nick Gordon190e4dc2017-10-04 16:54:10 -050082 }
weijia yuan3aa8d2b2018-03-06 15:35:57 -080083 NDN_LOG_DEBUG("Delete: " << name << ", found " << foundName << ", count " << count << ", result " << resultDb);
Weiqi Shif0330d52014-07-09 10:54:27 -070084 }
weijia yuan3aa8d2b2018-03-06 15:35:57 -080085
Weiqi Shif0330d52014-07-09 10:54:27 -070086 if (hasError)
87 return -1;
88 else
89 return count;
90}
91
92ssize_t
93RepoStorage::deleteData(const Interest& interest)
94{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080095 return deleteData(interest.getName());
Weiqi Shif0330d52014-07-09 10:54:27 -070096}
97
weijia yuan3aa8d2b2018-03-06 15:35:57 -080098std::shared_ptr<Data>
Weiqi Shif0330d52014-07-09 10:54:27 -070099RepoStorage::readData(const Interest& interest) const
100{
weijia yuan3aa8d2b2018-03-06 15:35:57 -0800101 NDN_LOG_DEBUG("Reading data for " << interest.getName());
102
103 return m_storage.read(interest.getName());
Weiqi Shif0330d52014-07-09 10:54:27 -0700104}
105
106
107} // namespace repo