blob: 334b4d36b857df09a1fcbf38594fe6560fec3d82 [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
weijia yuan3aa8d2b2018-03-06 15:35:57 -080031RepoStorage::RepoStorage(Storage& store)
32 : m_storage(store)
Weiqi Shif0330d52014-07-09 10:54:27 -070033{
34}
35
Weiqi Shif0330d52014-07-09 10:54:27 -070036bool
37RepoStorage::insertData(const Data& data)
38{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080039 bool isExist = m_storage.has(data.getFullName());
40
41 if (isExist) {
42 NDN_LOG_DEBUG("Data already in storage, regarded as successful data insertion");
43 return true;
44 }
45
46 int64_t id = m_storage.insert(data);
47 NDN_LOG_DEBUG("Insert ID: " << id << ", full name:" << data.getFullName());
48 if (id == NOTFOUND)
49 return false;
50
51 afterDataInsertion(data.getName());
52 return true;
Weiqi Shif0330d52014-07-09 10:54:27 -070053}
54
55ssize_t
56RepoStorage::deleteData(const Name& name)
57{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080058 NDN_LOG_DEBUG("Delete: " << name);
Weiqi Shif0330d52014-07-09 10:54:27 -070059 bool hasError = false;
weijia yuan3aa8d2b2018-03-06 15:35:57 -080060
Weiqi Shif0330d52014-07-09 10:54:27 -070061 int64_t count = 0;
weijia yuan3aa8d2b2018-03-06 15:35:57 -080062 std::shared_ptr<Data> foundData;
63 Name foundName;
64 while ((foundData = m_storage.find(name))) {
65 foundName = foundData->getFullName();
66 bool resultDb = m_storage.erase(foundName);
67 if (resultDb) {
68 afterDataDeletion(foundName);
Weiqi Shif0330d52014-07-09 10:54:27 -070069 count++;
Nick Gordon190e4dc2017-10-04 16:54:10 -050070 }
71 else {
Weiqi Shif0330d52014-07-09 10:54:27 -070072 hasError = true;
Nick Gordon190e4dc2017-10-04 16:54:10 -050073 }
weijia yuan3aa8d2b2018-03-06 15:35:57 -080074 NDN_LOG_DEBUG("Delete: " << name << ", found " << foundName << ", count " << count << ", result " << resultDb);
Weiqi Shif0330d52014-07-09 10:54:27 -070075 }
weijia yuan3aa8d2b2018-03-06 15:35:57 -080076
Weiqi Shif0330d52014-07-09 10:54:27 -070077 if (hasError)
78 return -1;
79 else
80 return count;
81}
82
83ssize_t
84RepoStorage::deleteData(const Interest& interest)
85{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080086 return deleteData(interest.getName());
Weiqi Shif0330d52014-07-09 10:54:27 -070087}
88
weijia yuan3aa8d2b2018-03-06 15:35:57 -080089std::shared_ptr<Data>
Weiqi Shif0330d52014-07-09 10:54:27 -070090RepoStorage::readData(const Interest& interest) const
91{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080092 NDN_LOG_DEBUG("Reading data for " << interest.getName());
93
94 return m_storage.read(interest.getName());
Weiqi Shif0330d52014-07-09 10:54:27 -070095}
96
97
98} // namespace repo