{"id":26681177,"url":"https://github.com/najmulislamnajim/cpp_api_server_with_rocksdb","last_synced_at":"2025-03-26T07:14:34.927Z","repository":{"id":284295422,"uuid":"954462968","full_name":"najmulislamnajim/cpp_api_server_with_rocksdb","owner":"najmulislamnajim","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-25T06:23:17.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T07:23:33.904Z","etag":null,"topics":["cpp17","rocksdb","sql"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/najmulislamnajim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-25T05:54:43.000Z","updated_at":"2025-03-25T06:23:21.000Z","dependencies_parsed_at":"2025-03-25T07:23:36.834Z","dependency_job_id":"badd0de3-6c00-4766-a8f8-abd2ada7835d","html_url":"https://github.com/najmulislamnajim/cpp_api_server_with_rocksdb","commit_stats":null,"previous_names":["najmulislamnajim/cpp_api_server_with_rocksdb"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/najmulislamnajim%2Fcpp_api_server_with_rocksdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/najmulislamnajim%2Fcpp_api_server_with_rocksdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/najmulislamnajim%2Fcpp_api_server_with_rocksdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/najmulislamnajim%2Fcpp_api_server_with_rocksdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/najmulislamnajim","download_url":"https://codeload.github.com/najmulislamnajim/cpp_api_server_with_rocksdb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245605719,"owners_count":20643030,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp17","rocksdb","sql"],"created_at":"2025-03-26T07:14:34.338Z","updated_at":"2025-03-26T07:14:34.911Z","avatar_url":"https://github.com/najmulislamnajim.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"```markdown\n# RocksDB and MariaDB Server\n\nThis project implements a simple HTTP server using `cpp-httplib`, with a key-value store powered by RocksDB and a relational database backend using MariaDB.\nThe server accepts POST requests to store JSON data in both RocksDB and MariaDB.\n\n**Author**: Najmul Islam  \n**Email**: najmulislamru@gmail.com  \n**Date**: 2025-03-25  \n**Last Modified**: 2025-03-25  \n\n## Files\n- **`database.h`**: Header file defining the `Database` class for RocksDB operations.\n- **`database.cpp`**: Implementation of the `Database` class, handling RocksDB CRUD operations (create, read, update, delete) for JSON and array data.\n- **`server.cpp`**: Main application file, setting up an HTTP server with a POST endpoint (`/api/bill`) to store data in both RocksDB and MariaDB.\n\n## Features\n- Stores full JSON data in RocksDB using a unique key.\n- Extracts specific fields (`billing_doc_no`, `billing_date`, `da_code`) from JSON and saves them to a MariaDB table (`rdl_test`).\n- Supports multi-process access to RocksDB (one writer, multiple readers).\n\n## Prerequisites\nTo run this project on a Linux system (e.g., Ubuntu), install the following dependencies:\n\n### 1. GCC/G++ (C++17 support)\n```bash\nsudo apt update\nsudo apt install g++-7\n# Verify: g++ --version (must support C++17).\n```\n\n### 2. RocksDB\n```bash\nsudo apt install librocksdb-dev\n```\n\n### 3. MariaDB/MySQL Connector/C++\n```bash\nsudo apt install libmysqlcppconn-dev\n```\n\n### 4. cpp-httplib\nDownload `httplib.h` from cpp-httplib GitHub:\n```bash\nmkdir cpp-httplib\nwget -O cpp-httplib/httplib.h https://github.com/yhirose/cpp-httplib/raw/master/httplib.h\n```\n\n### 5. nlohmann/json\nDownload `json.hpp` from nlohmann/json GitHub:\n```bash\nmkdir nlohmann\nwget -O nlohmann/json.hpp https://github.com/nlohmann/json/raw/develop/single_include/nlohmann/json.hpp\n```\n\n### 6. MariaDB Server\nInstall and configure MariaDB:\n```bash\nsudo apt install mariadb-server\nsudo systemctl start mariadb\nsudo mysql_secure_installation\n```\n\n## Directory Structure\n```text\nrocksdb/\n├── cpp-httplib/\n│   └── httplib.h\n├── nlohmann/\n│   └── json.hpp\n├── database.h\n├── database.cpp\n└── server.cpp\n```\n\n## Compilation\nCompile the project from the project root directory:\n\n```bash\ng++ -o server server.cpp database.cpp -lrocksdb -lmysqlcppconn -std=c++17 -Icpp-httplib -Inlohmann\n# -lrocksdb: Links the RocksDB library.\n# -lmysqlcppconn: Links the MySQL Connector/C++ library for MariaDB.\n# -std=c++17: Enables C++17 features.\n# -Icpp-httplib: Includes the cpp-httplib directory for httplib.h.\n# -Inlohmann: Includes the nlohmann directory for json.hpp.\n```\n\n\n## Running the Server\nEnsure the RocksDB database directory exists and is writable:\n```bash\nmkdir -p /mnt/test/Desktop/Impala/learning/rocksdb/learning\nchmod -R u+rwx /mnt/test/Desktop/Impala/learning/rocksdb/learning\n# Note: The default RocksDB path in server.cpp is /mnt/test/Desktop/Impala/learning/rocksdb/learning. For portability, consider changing it to ./db in the code.\n```\n\nStart the server:\n```bash\n./server\n# The server runs on 0.0.0.0:8080.\n```\n\n## Testing\nSend a POST request to the `/api/bill` endpoint:\n\n```bash\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"key\":\"123456\",\"billing_doc_no\":3,\"billing_date\":\"2025-03-25\",\"da_code\":5001}' http://localhost:8080/api/bill\n# Expected Response: {\"status\": \"success\", \"key\": \"123456\"}.\n```\n\n## Verification\n- **RocksDB**: The full JSON is stored in `/mnt/test/Desktop/Impala/learning/rocksdb/learning` under the key `123456`.\n\n\n## Troubleshooting\n### RocksDB Lock Errors:\nIf you see \"Resource temporarily unavailable,\" remove the stale LOCK file:\n```bash\nrm /mnt/test/Desktop/Impala/learning/rocksdb/learning/LOCK\n```\n\n### Compilation Errors:\n- Ensure all libraries are installed (`librocksdb-dev`, `libmysqlcppconn-dev`).\n- Verify `httplib.h` and `json.hpp` are in the correct directories.\n\n### MariaDB Connection Issues:\n- Confirm the host (`test:3306`), username (`root`), and password (`xyz`) in `server.cpp` match your MariaDB setup.\n- Test connectivity:\n  ```bash\n  mysql -u root -p -h test\n  ```\n\n\n\n## Notes\n- **Path Portability**: The RocksDB path is hardcoded. Update `server.cpp` to use a relative path (e.g., `./db`) for easier deployment.\n- **Error Handling**: The server assumes JSON fields exist and match expected types. Add validation if needed.\n- **Multi-Process**: The `Database` class supports read-only access from other processes (`Database db(\"./db\", true)`).\n\nFor further assistance, contact **Najmul Islam** at [najmulislamru@gmail.com](mailto:najmulislamru@gmail.com).\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnajmulislamnajim%2Fcpp_api_server_with_rocksdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnajmulislamnajim%2Fcpp_api_server_with_rocksdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnajmulislamnajim%2Fcpp_api_server_with_rocksdb/lists"}