Skip to content

Commit 4f60c64

Browse files
committed
Directory as database parameter.
1 parent 1e596c8 commit 4f60c64

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/repository/rocksdb_repository.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
#include "error.h"
77
#include "rocksdb_repository.h"
88

9-
repository::rocksdb_repository::rocksdb_repository()
9+
repository::rocksdb_repository::rocksdb_repository(const std::string &directory)
1010
{
1111
rocksdb::Options options;
1212
options.create_if_missing = true;
1313

14-
std::filesystem::create_directory("/tmp/asyncdb");
15-
rocksdb::Status status = rocksdb::DB::Open(options, "/tmp/asyncdb/" + std::to_string(std::rand()), &database);
14+
std::filesystem::path directory_path = std::filesystem::path(directory);
15+
std::filesystem::create_directories(directory_path);
16+
17+
std::filesystem::path database_path = directory_path / std::to_string(std::rand());
18+
rocksdb::Status status = rocksdb::DB::Open(options, database_path.string(), &database);
1619

1720
if (!status.ok())
1821
{

src/repository/rocksdb_repository.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef REPOSITORY_ROCSKDB_REPOSITORY_H
22
#define REPOSITORY_ROCSKDB_REPOSITORY_H
33

4+
#include <string>
5+
46
#include "repository.h"
57

68
namespace repository
@@ -10,7 +12,7 @@ namespace repository
1012
rocksdb::DB *database;
1113

1214
public:
13-
rocksdb_repository();
15+
explicit rocksdb_repository(const std::string &directory);
1416

1517
void create_table(const table::table &table) override;
1618

src/server/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ server::server::server(
1313
thread_count(threads),
1414
io_context(thread_count),
1515
acceptor(boost::asio::make_strand(io_context)),
16-
repository(repository::rocksdb_repository()),
16+
repository(repository::rocksdb_repository("/tmp/asyncdb")),
1717
router(router::router(repository))
1818
{
1919
boost::beast::error_code error;

test/repository/rocksdb_repository_test.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <set>
22
#include <vector>
3+
#include <filesystem>
34

45
#include <gtest/gtest.h>
56
#include <rocksdb/db.h>
@@ -13,15 +14,33 @@ class repository_test: public ::testing::Test
1314
protected:
1415
repository::rocksdb_repository repository;
1516

16-
void SetUp()
17+
repository::rocksdb_repository faulty_repository;
18+
19+
repository_test():
20+
repository("/tmp/asyncdb"),
21+
faulty_repository("/tmp/faulty")
1722
{
1823
}
1924

20-
void TearDown()
25+
void SetUp() override
26+
{
27+
}
28+
29+
void TearDown() override
2130
{
2231
}
2332
};
2433

34+
TEST_F(repository_test, fail_to_create_table_with_faulty_repository)
35+
{
36+
faulty_repository.create_table(table::valid_table("first table", std::vector<std::string>()));
37+
38+
std::set<table::table> table_set = faulty_repository.list_tables();
39+
std::vector<table::table> tables(table_set.begin(), table_set.end());
40+
41+
EXPECT_EQ(tables.size(), 1);
42+
}
43+
2544
TEST_F(repository_test, create_and_read_tables)
2645
{
2746
repository.create_table(table::valid_table("first table", std::vector<std::string>()));

0 commit comments

Comments
 (0)