Skip to content

Commit

Permalink
comment && rm
Browse files Browse the repository at this point in the history
  • Loading branch information
kkli08 committed Sep 2, 2024
1 parent aa9f6d5 commit 716162d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 37 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ MyDB->Close();
**kvdb::API::Scan(int_64, int_64)**
> Retrieves all KV-pairs in a key range in key order (key1 < key2)
```c++
KV-pairs = Scan(Key1, Key2)
auto MyDB = new kvdb::API();
unordered_map<long long, long long> KvPairs;
MyDB->Open("database name");
KvPairs = MyDB->Scan(Key1, Key2);
```
**kvdb::API::Update()**
> Update the data.
Expand Down
119 changes: 83 additions & 36 deletions api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace fs = std::filesystem;
namespace kvdb {
/*
* void API::Open(string)
*
* Open db by name
*/
void API::Open(string db_name){
std::cout << "Opening database " << db_name << std::endl;

Expand All @@ -33,6 +38,11 @@ namespace kvdb {
index->getAllSSTs();
}

/*
* void API::Close()
*
* Close and cleanup the database
*/
void API::Close(){
check_if_open();
std::cout << "Closing database " << std::endl;
Expand All @@ -53,42 +63,11 @@ namespace kvdb {
cleanup();
}

void API::cleanup() {
// Check if memtable is not null
if (memtable) {
delete memtable; // Delete the dynamically allocated memtable
memtable = nullptr; // Set pointer to nullptr to avoid dangling pointer
}
std::cout << "Cleanup completed." << std::endl;
}

API::~API() {
// Check if memtable is not null
if (memtable) {
delete memtable; // Delete the dynamically allocated memtable
memtable = nullptr; // Set pointer to nullptr to avoid dangling pointer
}
}
void API::set_path(fs::path _path) {
path = _path;
memtable->set_path(_path);
index->set_path(_path);

// Construct the full path to the "Index.sst" file
fs::path indexFilePath = path / "Index.sst";

// Check if the "Index.sst" file exists
if (!fs::exists(indexFilePath)) {
// If the file does not exist, create an empty "Index.sst" file
std::ofstream outfile(indexFilePath);
if (!outfile.is_open()) {
throw std::runtime_error("Failed to create Index.sst file at: " + indexFilePath.string());
}
outfile.close(); // Close the file after creation
std::cout << "Created new Index.sst file at: " << indexFilePath << std::endl;
}
}

/*
* void API::Put(LL, LL)
*
* Store key-value pair in kv database
*/
void API::Put(long long key, long long value) {
// Put method
// check if db is open
Expand All @@ -115,6 +94,12 @@ namespace kvdb {

}

/*
* LL API::Get(LL)
*
* Return the value of a key, return -1 if the key
* doesn't exist in memtable or SSTs
*/
long long API::Get(long long key) {
// Get method
// check if db is open
Expand All @@ -135,6 +120,66 @@ namespace kvdb {
return -1;
}

// memory cleanup function
void API::cleanup() {
// Check if memtable is not null
if (memtable) {
delete memtable; // Delete the dynamically allocated memtable
memtable = nullptr; // Set pointer to nullptr to avoid dangling pointer
}
std::cout << "Cleanup completed." << std::endl;
}

/*
* unordered_map<LL, LL> API::Scan(LL, LL)
*
* Search the memtable and the SSTs.
* step 1:
* scan the SSTs from oldest to youngest
* step 2:
* scan the memtable
*/
unordered_map<long long, long long> Scan(long long, long long) {
unordered_map<long long, long long> result;

// step 1:
// scan the SSTs from oldest to youngest

// step 2:
// scan the memtable

return result;
}

API::~API() {
// Check if memtable is not null
if (memtable) {
delete memtable; // Delete the dynamically allocated memtable
memtable = nullptr; // Set pointer to nullptr to avoid dangling pointer
}
}

// helper function
void API::set_path(fs::path _path) {
path = _path;
memtable->set_path(_path);
index->set_path(_path);

// Construct the full path to the "Index.sst" file
fs::path indexFilePath = path / "Index.sst";

// Check if the "Index.sst" file exists
if (!fs::exists(indexFilePath)) {
// If the file does not exist, create an empty "Index.sst" file
std::ofstream outfile(indexFilePath);
if (!outfile.is_open()) {
throw std::runtime_error("Failed to create Index.sst file at: " + indexFilePath.string());
}
outfile.close(); // Close the file after creation
std::cout << "Created new Index.sst file at: " << indexFilePath << std::endl;
}
}

// Debug helper function
void API::IndexCheck() {
cout << "\n-->Inside API::IndexCheck()" << endl;
Expand All @@ -144,4 +189,6 @@ namespace kvdb {
cout << "\n-->Inside Index.sst" << endl;
index->printSSRsInFile();
}


}
2 changes: 2 additions & 0 deletions api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include "Memtable.h"
#include <filesystem> // C++17 lib
#include <unordered_map>
#include "SSTIndex.h"

namespace fs = std::filesystem;
Expand All @@ -20,6 +21,7 @@ namespace kvdb {
void Close();
void Put(long long key, long long value);
long long Get(long long key);
unordered_map<long long, long long> Scan(long long, long long);
Memtable* GetMemtable() const {return memtable;};
void IndexCheck();

Expand Down

0 comments on commit 716162d

Please sign in to comment.