Ferenc Szontágh
2024-06-27 3ac954922108b07fb3f7a7ce1c727bfcfad8b263
readme and some test
2 files modified
1 files added
164 ■■■■■ changed files
CMakeLists.txt 2 ●●● patch | view | raw | blame | history
README.md 102 ●●●●● patch | view | raw | blame | history
examples/example.cpp 60 ●●●●● patch | view | raw | blame | history
CMakeLists.txt
@@ -25,7 +25,7 @@
target_link_libraries(RocksDBWrapperExecutable RocksDBWrapper)
# Optionally add tests
option(BUILD_TESTS "Build tests" ON)
option(BUILD_TESTS "Build tests" OFF)
if (BUILD_TESTS)
    include(FetchContent)
README.md
@@ -1,4 +1,102 @@
## RocksDBWrapper
# RocksDBWrapper
A simple wrapper for rocksdb
RocksDBWrapper is a C++ library that provides a convenient interface for storing, retrieving, and indexing data using RocksDB. This library supports serialization of complex data structures and indexing for efficient queries.
## Features
* **Store and Retrieve Data:** Easily store and retrieve complex data structures.
* **Key Existence Check:** Check if a specific key exists in the database.
* **Member Indexing:** Index struct members for efficient search queries.
* **Text Search:** Perform text-based searches on indexed members.
* **Conditional Search:** Perform conditional searches on the data.
## Installation
### Dependencies
* [RocksDB](https://github.com/facebook/rocksdb)
* [tser](https://github.com/StefanoDacchille/tser)
* [GoogleTest](https://github.com/google/googletest) (for testing)
### Build
```sh
mkdir build
cd build
cmake .. -DBUILD_TESTS=ON  # Add -DBUILD_TESTS=OFF if you do not want to build tests
make
```
## Public Methods Documentation
### Constructor and Destructor
#### `RocksDBWrapper(const std::string &db_path, const std::string &index_path)`
* **Description:** Constructs a `RocksDBWrapper` instance and opens the specified database and index paths.
* **Parameters:**
  * `db_path`: The path to the main RocksDB database.
  * `index_path`: The path to the index RocksDB database.
#### `~RocksDBWrapper()`
* **Description:** Destructor that cleans up the database and index database resources.
### Data Storage and Retrieval
#### `template <typename T> void store(const std::string &key, const T &value)`
* **Description:** Stores a value associated with the specified key.
* **Parameters:**
  * `key`: The key to associate with the value.
  * `value`: The value to store.
#### `template <typename T> bool get(const std::string &key, T &value)`
* **Description:** Retrieves the value associated with the specified key.
* **Parameters:**
  * `key`: The key to retrieve the value for.
  * `value`: The variable to store the retrieved value.
* **Returns:** `true` if the key was found and the value was retrieved; `false` otherwise.
### Key Existence
#### `bool keyExists(const std::string &key)`
* **Description:** Checks if a key exists in the database.
* **Parameters:**
  * `key`: The key to check for existence.
* **Returns:** `true` if the key exists; `false` otherwise.
### Searching
#### `template <typename T> std::vector<std::string> search(const std::string &member_name, const std::string &member_value)`
* **Description:** Searches for keys by a member's value.
* **Parameters:**
  * `member_name`: The name of the member to search by.
  * `member_value`: The value of the member to search for.
* **Returns:** A vector of keys that match the search criteria.
#### `template <typename T> void search_text(const std::string &search_value, std::vector<T> &res)`
* **Description:** Performs a text-based search on indexed members.
* **Parameters:**
  * `search_value`: The text to search for.
  * `res`: A vector to store the results.
#### `template <typename T> void search_conditional(std::vector<T> &res, std::function<bool(const T &)> condition)`
* **Description:** Performs a conditional search on the data.
* **Parameters:**
  * `res`: A vector to store the results.
  * `condition`: A function to define the search condition.
This `README.md` provides a comprehensive overview of the `RocksDBWrapper` library, including installation instructions, usage examples, and documentation of public methods.
examples/example.cpp
New file
@@ -0,0 +1,60 @@
#include "RocksDBWrapper.h"
#include "tser/tser.hpp"
#include <iostream>
#include <string>
struct TestStruct {
    int id;
    std::string name;
    double value;
    DEFINE_SERIALIZABLE(id, name, value);
};
int main() {
    RocksDBWrapper db("/tmp/testdb", "/tmp/testindex");
    TestStruct ts1{1, "Alpha", 1.1};
    TestStruct ts2{2, "Beta", 2.2};
    TestStruct ts3{3, "Gamma", 3.3};
    // Store data
    db.store("key1", ts1);
    db.store("key2", ts2);
    db.store("key3", ts3);
    // Retrieve data
    TestStruct retrieved;
    if (db.get("key1", retrieved)) {
        std::cout << "Retrieved: " << retrieved.name << std::endl;
    }
    // Check key existence
    if (db.keyExists("key2")) {
        std::cout << "Key 'key2' exists in the database." << std::endl;
    }
    // Search by member
    auto keys = db.search<TestStruct>("name", "Beta");
    for (const auto& key : keys) {
        std::cout << "Found key for Beta: " << key << std::endl;
    }
    // Text search
    std::vector<TestStruct> results;
    db.search_text("Gam", results);
    for (const auto& result : results) {
        std::cout << "Text search found: " << result.name << std::endl;
    }
    // Conditional search
    std::vector<TestStruct> cond_results;
    db.search_conditional<TestStruct>(cond_results, [](const TestStruct& s) {
        return s.value > 2.0;
    });
    for (const auto& result : cond_results) {
        std::cout << "Conditional search found: " << result.name << std::endl;
    }
    return 0;
}