Skip to content

Commit

Permalink
Switch to std::filesystem
Browse files Browse the repository at this point in the history
Change-Id: If70258048d6f54c3e9f0a25bf8f3e327411c42ac
  • Loading branch information
Pesa committed Dec 13, 2024
1 parent c1f1362 commit 01a2a8c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 52 deletions.
1 change: 0 additions & 1 deletion .jenkins.d/00-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ APT_PKGS=(
libboost-chrono-dev
libboost-date-time-dev
libboost-dev
libboost-filesystem-dev
libboost-iostreams-dev
libboost-log-dev
libboost-program-options-dev
Expand Down
2 changes: 1 addition & 1 deletion .waf-tools/default-compiler-flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def getDebugFlags(self, conf):
return {
'CXXFLAGS': [],
'LINKFLAGS': [],
'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
}

def getOptimizedFlags(self, conf):
Expand Down
39 changes: 15 additions & 24 deletions src/storage/sqlite-storage.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2019, Regents of the University of California.
* Copyright (c) 2014-2024, 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.
Expand All @@ -18,15 +18,12 @@
*/

#include "sqlite-storage.hpp"
#include "config.hpp"

#include <ndn-cxx/util/logger.hpp>
#include <ndn-cxx/util/sha256.hpp>
#include <ndn-cxx/util/sqlite3-statement.hpp>

#include <boost/filesystem.hpp>
#include <istream>

#include <ndn-cxx/util/logger.hpp>
#include <filesystem>

namespace repo {

Expand All @@ -35,25 +32,22 @@ NDN_LOG_INIT(repo.SqliteStorage);
SqliteStorage::SqliteStorage(const std::string& dbPath)
{
if (dbPath.empty()) {
m_dbPath = std::string("ndn_repo.db");
NDN_LOG_DEBUG("Create db file in local location [" << m_dbPath << "]. " );
NDN_LOG_DEBUG("You can assign the path using -d option" );
m_dbPath = "ndn_repo.db";
}
else {
boost::filesystem::path fsPath(dbPath);
boost::filesystem::file_status fsPathStatus = boost::filesystem::status(fsPath);
if (!boost::filesystem::is_directory(fsPathStatus)) {
if (!boost::filesystem::create_directory(boost::filesystem::path(fsPath))) {
NDN_THROW(Error("Folder '" + dbPath + "' does not exists and cannot be created"));
std::filesystem::path fsPath(dbPath);
if (!std::filesystem::is_directory(fsPath)) {
if (!std::filesystem::create_directory(fsPath)) {
NDN_THROW(Error("Directory '" + dbPath + "' does not exists and cannot be created"));
}
}

m_dbPath = dbPath + "/ndn_repo.db";
}

NDN_LOG_DEBUG("Using database file " << m_dbPath);
initializeRepo();
}


void
SqliteStorage::initializeRepo()
{
Expand Down Expand Up @@ -95,7 +89,7 @@ SqliteStorage::insert(const Data& data)
Name name = data.getFullName(); // store the full name
ndn::util::Sqlite3Statement stmt(m_db, "INSERT INTO NDN_REPO_V2 (name, data) VALUES (?, ?);");

//Insert
// Insert
// Bind NULL to name value in NDN_REPO_V2 when initialize result.
auto result = sqlite3_bind_null(stmt, 1);
if (result == SQLITE_OK) {
Expand All @@ -106,7 +100,6 @@ SqliteStorage::insert(const Data& data)
result = stmt.bind(2, data.wireEncode(), SQLITE_STATIC);
}

int id = 0;
if (result == SQLITE_OK) {
int rc = 0;
rc = stmt.step();
Expand All @@ -115,12 +108,11 @@ SqliteStorage::insert(const Data& data)
NDN_THROW(Error("Insert failed"));
}
sqlite3_reset(stmt);
id = sqlite3_last_insert_rowid(m_db);
return sqlite3_last_insert_rowid(m_db);
}
else {
NDN_THROW(Error("Some error with insert"));
NDN_THROW(Error("Database insert failure (code: " + std::to_string(result)));
}
return id;
}

bool
Expand Down Expand Up @@ -250,7 +242,7 @@ SqliteStorage::forEach(const std::function<void(const Name&)>& f)
break;
}
else {
NDN_THROW(Error("Database query failure (code: " + ndn::to_string(rc)));
NDN_THROW(Error("Database query failure (code: " + std::to_string(rc)));
}
}
}
Expand All @@ -266,8 +258,7 @@ SqliteStorage::size()
NDN_THROW(Error("Database query failure"));
}

uint64_t nData = stmt.getInt(0);
return nData;
return stmt.getInt(0);
}

} // namespace repo
8 changes: 4 additions & 4 deletions tests/identity-management-fixture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California.
* Copyright (c) 2014-2024, 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.
Expand All @@ -25,7 +25,7 @@
#include <ndn-cxx/security/certificate.hpp>
#include <ndn-cxx/util/io.hpp>

#include <boost/filesystem.hpp>
#include <filesystem>

namespace repo::tests {

Expand All @@ -37,9 +37,9 @@ IdentityManagementFixture::IdentityManagementFixture()

IdentityManagementFixture::~IdentityManagementFixture()
{
boost::system::error_code ec;
std::error_code ec;
for (const auto& certFile : m_certFiles) {
boost::filesystem::remove(certFile, ec); // ignore error
std::filesystem::remove(certFile, ec); // ignore error
}
}

Expand Down
17 changes: 6 additions & 11 deletions tests/repo-storage-fixture.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2018-2022, Regents of the University of California.
* Copyright (c) 2018-2024, 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.
Expand All @@ -23,27 +23,22 @@
#include "storage/repo-storage.hpp"
#include "storage/sqlite-storage.hpp"

#include <boost/filesystem.hpp>
#include <filesystem>

namespace repo::tests {

class RepoStorageFixture
{
public:
RepoStorageFixture()
: store(std::make_shared<SqliteStorage>("unittestdb"))
, handle(std::make_shared<RepoStorage>(*store))
{
}

~RepoStorageFixture()
{
boost::filesystem::remove_all(boost::filesystem::path("unittestdb"));
std::error_code ec;
std::filesystem::remove_all(std::filesystem::path("unittestdb"), ec);
}

public:
std::shared_ptr<Storage> store;
std::shared_ptr<RepoStorage> handle;
std::shared_ptr<Storage> store = std::make_shared<SqliteStorage>("unittestdb");
std::shared_ptr<RepoStorage> handle = std::make_shared<RepoStorage>(*store);
};

} // namespace repo::tests
Expand Down
16 changes: 6 additions & 10 deletions tests/sqlite-fixture.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California.
* Copyright (c) 2014-2024, 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.
Expand All @@ -22,26 +22,22 @@

#include "storage/sqlite-storage.hpp"

#include <boost/filesystem.hpp>
#include <filesystem>

namespace repo::tests {

class SqliteFixture
{
public:
SqliteFixture()
: handle(new SqliteStorage("unittestdb"))
{
}

~SqliteFixture()
{
delete handle;
boost::filesystem::remove_all(boost::filesystem::path("unittestdb"));
handle.reset();
std::error_code ec;
std::filesystem::remove_all(std::filesystem::path("unittestdb"), ec);
}

public:
SqliteStorage* handle;
std::unique_ptr<SqliteStorage> handle = std::make_unique<SqliteStorage>("unittestdb");
};

} // namespace repo::tests
Expand Down
2 changes: 1 addition & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def configure(conf):

conf.check_sqlite3()

conf.check_boost(lib='filesystem program_options', mt=True)
conf.check_boost(lib='program_options', mt=True)

if conf.env.WITH_TESTS:
conf.check_boost(lib='unit_test_framework', mt=True, uselib_store='BOOST_TESTS')
Expand Down

0 comments on commit 01a2a8c

Please sign in to comment.