Skip to content

Commit 1e596c8

Browse files
committed
Remove table from repository.
1 parent 2e1229a commit 1e596c8

6 files changed

Lines changed: 58 additions & 6 deletions

File tree

src/repository/repository.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace repository
1919
virtual bool has_table(const std::string &table_name) const = 0;
2020

2121
virtual table::table read_table(const std::string &table_name) const = 0;
22+
23+
virtual void delete_table(const std::string &table_name) = 0;
2224
};
2325
}
2426

src/repository/rocksdb_repository.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ table::table repository::rocksdb_repository::read_table(const std::string &table
6868
}
6969
}
7070

71+
void repository::rocksdb_repository::delete_table(const std::string &table_name)
72+
{
73+
std::string value;
74+
75+
database->Get(rocksdb::ReadOptions(), "TABLE_" + table_name, &value);
76+
77+
if (value.empty())
78+
{
79+
return;
80+
}
81+
else
82+
{
83+
database->Delete(rocksdb::WriteOptions(), "TABLE_" + table_name);
84+
}
85+
}
86+
7187
repository::rocksdb_repository::~rocksdb_repository()
7288
{
7389
delete database;

src/repository/rocksdb_repository.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace repository
2020

2121
table::table read_table(const std::string &table_name) const override;
2222

23+
void delete_table(const std::string &table_name) override;
24+
2325
~rocksdb_repository();
2426
};
2527
}

test/repository/fake_repository.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,27 @@ std::set<table::table> repository::fake_repository::list_tables() const
2424
return table_list;
2525
}
2626

27-
bool repository::fake_repository::has_table(const std::string &tableName) const
27+
bool repository::fake_repository::has_table(const std::string &table_name) const
2828
{
29-
return tables.count(tableName) > 0;
29+
return tables.count(table_name) > 0;
3030
}
3131

32-
table::table repository::fake_repository::read_table(const std::string &tableName) const
32+
table::table repository::fake_repository::read_table(const std::string &table_name) const
3333
{
34-
if (tables.count(tableName))
34+
if (tables.count(table_name))
3535
{
36-
return table::to_table(tables.at(tableName));
36+
return table::to_table(tables.at(table_name));
3737
}
3838
else
3939
{
40-
return table::invalid_table("Fake data store didn't contain table " + tableName + ".");
40+
return table::invalid_table("Fake data store didn't contain table " + table_name + ".");
41+
}
42+
}
43+
44+
void repository::fake_repository::delete_table(const std::string &table_name)
45+
{
46+
if (tables.count(table_name))
47+
{
48+
return;
4149
}
4250
}

test/repository/fake_repository.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace repository
2222
bool has_table(const std::string &tableName) const override;
2323

2424
table::table read_table(const std::string &tableName) const override;
25+
26+
void delete_table(const std::string &table_name) override;
2527
};
2628
}
2729

test/repository/rocksdb_repository_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,25 @@ TEST_F(repository_test, does_not_have_table)
8585

8686
EXPECT_FALSE(has_table);
8787
}
88+
89+
TEST_F(repository_test, does_not_delete_invalid_table)
90+
{
91+
repository.create_table(table::valid_table("a table", std::vector<std::string>()));
92+
93+
repository.delete_table("not a table");
94+
95+
bool has_table = repository.has_table("a table");
96+
97+
EXPECT_TRUE(has_table);
98+
}
99+
100+
TEST_F(repository_test, delete_table_from_repository)
101+
{
102+
repository.create_table(table::valid_table("a table", std::vector<std::string>()));
103+
104+
repository.delete_table("a table");
105+
106+
bool has_table = repository.has_table("a table");
107+
108+
EXPECT_FALSE(has_table);
109+
}

0 commit comments

Comments
 (0)