Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
database.cpp
Go to the documentation of this file.
1#include "database.hpp"
2#include "pano_log.h"
3#include "utils.h"
4#include <nlohmann/json.hpp>
5
6#include "rocksdb/db.h"
7#include "rocksdb/options.h"
8#include "rocksdb/slice.h"
9#include "rocksdb/db.h"
10#include "rocksdb/options.h"
11#include "rocksdb/utilities/transaction_db.h"
12
13//using ROCKSDB_NAMESPACE::DB;
14//using ROCKSDB_NAMESPACE::Options;
15using ROCKSDB_NAMESPACE::PinnableSlice;
16using ROCKSDB_NAMESPACE::ReadOptions;
17using ROCKSDB_NAMESPACE::Status;
18using ROCKSDB_NAMESPACE::WriteBatch;
19using ROCKSDB_NAMESPACE::WriteOptions;
20
21rocksdb::DB* m_database = NULL;
22rocksdb::Options m_options;
23
25#ifdef _DEBUG
26 std::string databasePath = GetExecutableDirectory() + "\\database";
27#else
28 std::string databasePath = PANOPTES_DATABASE_PATH;
29#endif // _DEBUG
30
31 Status s = rocksdb::DB::Open(m_options, databasePath, &m_database);
32 if (s.ok()) {
33 return PANO_SUCCESS;
34 }
35 else {
36 return DB_INITIALIZATION;
37 }
38}
39
41 if (m_database == NULL) {
42 return;
43 }
44
45 m_database->Close();
46 delete m_database;
47 m_database = NULL;
48}
49
50BOOL PanoptesDatabase::AddEntry(std::string key, std::string entry) {
51 Status s = m_database->Put(WriteOptions(), key, entry);
52 if (s.ok()) {
53 return true;
54 }
55 else {
56 return false;
57 }
58}
59
60std::string PanoptesDatabase::GetEntry(std::string hash) {
61 std::string value;
62 Status s = m_database->Get(ReadOptions(), hash, &value);
63 return value;
64}
65
66std::string PanoptesDatabase::UpdateEntry(std::string key, std::string entry) {
67 std::string dbEntryStr = GetEntry(key);
68 nlohmann::json dbEntry = nlohmann::json::parse(dbEntryStr);
69 nlohmann::json entryToMerge = nlohmann::json::parse(entry);
70 dbEntry.merge_patch(entryToMerge);
71 std::string combinedEntry = dbEntry.dump();
72 WriteBatch batch;
73
74 batch.Delete(key);
75 batch.Put(key, combinedEntry);
76 Status s = m_database->Write(WriteOptions(), &batch);
77 if (s.ok()) {
78 return combinedEntry;
79 }
80 else {
81 return "";
82 }
83}
84
86 m_options.IncreaseParallelism();
87 m_options.OptimizeLevelStyleCompaction();
88 m_options.create_if_missing = true;
89}
#define PANO_SUCCESS
#define DB_INITIALIZATION
ERRORCODE InitializeDatabase()
Definition database.cpp:24
std::string UpdateEntry(std::string key, std::string entry)
Definition database.cpp:66
static void CloseDatabase()
Flushes and closes the database. Safe to call if it was never opened.
Definition database.cpp:40
std::string GetEntry(std::string hash)
Definition database.cpp:60
BOOL AddEntry(std::string key, std::string entry)
Definition database.cpp:50
rocksdb::Options m_options
Definition database.cpp:22
rocksdb::DB * m_database
Definition database.cpp:21
#define ERRORCODE
#define PANOPTES_DATABASE_PATH
std::string GetExecutableDirectory()
Definition utils.cpp:71