Fix regression of repo not registering prefixes for previously inserted data
Change-Id: I90e47def6219560cdadd281b8f65b16036aa0801
Refs: #4247
diff --git a/src/storage/repo-storage.cpp b/src/storage/repo-storage.cpp
index 334b4d3..6029a05 100644
--- a/src/storage/repo-storage.cpp
+++ b/src/storage/repo-storage.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -22,6 +22,7 @@
#include <istream>
+#include <ndn-cxx/util/exception.hpp>
#include <ndn-cxx/util/logger.hpp>
namespace repo {
@@ -33,6 +34,14 @@
{
}
+void
+RepoStorage::notifyAboutExistingData()
+{
+ m_storage.forEach([this] (const Name& name) {
+ afterDataInsertion(name);
+ });
+}
+
bool
RepoStorage::insertData(const Data& data)
{
diff --git a/src/storage/repo-storage.hpp b/src/storage/repo-storage.hpp
index 5934b12..fe355ad 100644
--- a/src/storage/repo-storage.hpp
+++ b/src/storage/repo-storage.hpp
@@ -47,9 +47,18 @@
};
public:
+ explicit
RepoStorage(Storage& store);
/**
+ * @brief Notify about existing data
+ *
+ * Note, this cannot be in constructor, as have to be called after signal is connected
+ */
+ void
+ notifyAboutExistingData();
+
+ /**
* @brief insert data into repo
*/
bool
diff --git a/src/storage/sqlite-storage.cpp b/src/storage/sqlite-storage.cpp
index 91016ea..a9627ff 100644
--- a/src/storage/sqlite-storage.cpp
+++ b/src/storage/sqlite-storage.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -44,7 +44,7 @@
boost::filesystem::file_status fsPathStatus = boost::filesystem::status(fsPath);
if (!boost::filesystem::is_directory(fsPathStatus)) {
if (!boost::filesystem::create_directory(boost::filesystem::path(fsPath))) {
- BOOST_THROW_EXCEPTION(Error("Folder '" + dbPath + "' does not exists and cannot be created"));
+ NDN_THROW(Error("Folder '" + dbPath + "' does not exists and cannot be created"));
}
}
@@ -75,7 +75,7 @@
}
else {
NDN_LOG_DEBUG("Database file open failure rc:" << rc);
- BOOST_THROW_EXCEPTION(Error("Database file open failure"));
+ NDN_THROW(Error("Database file open failure"));
}
// SQLite continues without syncing as soon as it has handed data off to the operating system
@@ -112,13 +112,13 @@
rc = stmt.step();
if (rc == SQLITE_CONSTRAINT) {
NDN_LOG_DEBUG("Insert failed");
- BOOST_THROW_EXCEPTION(Error("Insert failed"));
+ NDN_THROW(Error("Insert failed"));
}
sqlite3_reset(stmt);
id = sqlite3_last_insert_rowid(m_db);
}
else {
- BOOST_THROW_EXCEPTION(Error("Some error with insert"));
+ NDN_THROW(Error("Some error with insert"));
}
return id;
}
@@ -136,7 +136,7 @@
int rc = stmt.step();
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
NDN_LOG_DEBUG("Node delete error rc:" << rc);
- BOOST_THROW_EXCEPTION(Error("Node delete error"));
+ NDN_THROW(Error("Node delete error"));
}
if (sqlite3_changes(m_db) != 1) {
return false;
@@ -144,7 +144,7 @@
}
else {
NDN_LOG_DEBUG("delete bind error");
- BOOST_THROW_EXCEPTION(Error("delete bind error"));
+ NDN_THROW(Error("delete bind error"));
}
return true;
}
@@ -218,16 +218,43 @@
}
else {
NDN_LOG_DEBUG("Database query failure rc:" << rc);
- BOOST_THROW_EXCEPTION(Error("Database query failure"));
+ NDN_THROW(Error("Database query failure"));
}
}
else {
NDN_LOG_DEBUG("select bind error");
- BOOST_THROW_EXCEPTION(Error("select bind error"));
+ NDN_THROW(Error("select bind error"));
}
return nullptr;
}
+void
+SqliteStorage::forEach(const std::function<void(const Name&)>& f)
+{
+ ndn::util::Sqlite3Statement stmt(m_db, "SELECT data FROM NDN_REPO_V2;");
+
+ while (true) {
+ int rc = stmt.step();
+ if (rc == SQLITE_ROW) {
+ Data data;
+ try {
+ data.wireDecode(stmt.getBlock(0));
+ }
+ catch (const ndn::Block::Error& error) {
+ NDN_LOG_DEBUG("Error while decoding data from the database: " << error.what());
+ continue;
+ }
+ f(data.getName());
+ }
+ else if (rc == SQLITE_DONE) {
+ break;
+ }
+ else {
+ NDN_THROW(Error("Database query failure (code: " + ndn::to_string(rc)));
+ }
+ }
+}
+
uint64_t
SqliteStorage::size()
{
@@ -236,7 +263,7 @@
int rc = stmt.step();
if (rc != SQLITE_ROW) {
NDN_LOG_DEBUG("Database query failure rc:" << rc);
- BOOST_THROW_EXCEPTION(Error("Database query failure"));
+ NDN_THROW(Error("Database query failure"));
}
uint64_t nData = stmt.getInt(0);
diff --git a/src/storage/sqlite-storage.hpp b/src/storage/sqlite-storage.hpp
index 4d7142b..217f9d0 100644
--- a/src/storage/sqlite-storage.hpp
+++ b/src/storage/sqlite-storage.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2018, Regents of the University of California.
+/*
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -74,6 +74,9 @@
std::shared_ptr<Data>
find(const Name& name, bool exactMatch = false) override;
+ void
+ forEach(const std::function<void(const Name&)>& f) override;
+
/**
* @brief return the size of database
*/
diff --git a/src/storage/storage.hpp b/src/storage/storage.hpp
index 146368f..d724eb1 100644
--- a/src/storage/storage.hpp
+++ b/src/storage/storage.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -82,6 +82,12 @@
virtual std::shared_ptr<Data>
find(const Name& name, bool exactMatch = false) = 0;
+ /**
+ * @brief Enumerate each entry in database and call @p f with name of stored data
+ */
+ virtual void
+ forEach(const std::function<void(const Name&)>& f) = 0;
+
/**
* @brief return the size of database
*/