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 <nlohmann/json.hpp>
4
5#include "rocksdb/db.h"
6#include "rocksdb/options.h"
7#include "rocksdb/slice.h"
8#include "rocksdb/db.h"
9#include "rocksdb/options.h"
10#include "rocksdb/utilities/transaction_db.h"
11
12//using ROCKSDB_NAMESPACE::DB;
13//using ROCKSDB_NAMESPACE::Options;
14using ROCKSDB_NAMESPACE::PinnableSlice;
15using ROCKSDB_NAMESPACE::ReadOptions;
16using ROCKSDB_NAMESPACE::Status;
17using ROCKSDB_NAMESPACE::WriteBatch;
18using ROCKSDB_NAMESPACE::WriteOptions;
19
20rocksdb::DB* m_database = NULL;
21rocksdb::Options m_options;
22
24 Status s = rocksdb::DB::Open(m_options, PANOPTES_DATABASE_PATH, &m_database);
25 if (s.ok()) {
26 return PANO_SUCCESS;
27 }
28 else {
29 return DB_INITIALIZATION;
30 }
31}
32
33BOOL PanoptesDatabase::AddEntry(std::string key, std::string entry) {
34 Status s = m_database->Put(WriteOptions(), key, entry);
35 if (s.ok()) {
36 return true;
37 }
38 else {
39 return false;
40 }
41}
42
43std::string PanoptesDatabase::GetEntry(std::string hash) {
44 std::string value;
45 Status s = m_database->Get(ReadOptions(), hash, &value);
46 return value;
47}
48
49std::string PanoptesDatabase::UpdateEntry(std::string key, std::string entry) {
50 std::string dbEntryStr = GetEntry(key);
51 nlohmann::json dbEntry = nlohmann::json::parse(dbEntryStr);
52 nlohmann::json entryToMerge = nlohmann::json::parse(entry);
53 dbEntry.merge_patch(entryToMerge);
54 std::string combinedEntry = dbEntry.dump();
55 WriteBatch batch;
56
57 batch.Delete(key);
58 batch.Put(key, combinedEntry);
59 Status s = m_database->Write(WriteOptions(), &batch);
60 if (s.ok()) {
61 return combinedEntry;
62 }
63 else {
64 return "";
65 }
66}
67
69 m_options.IncreaseParallelism();
70 m_options.OptimizeLevelStyleCompaction();
71 m_options.create_if_missing = true;
72}
#define PANO_SUCCESS
#define DB_INITIALIZATION
ERRORCODE InitializeDatabase()
Definition database.cpp:23
std::string UpdateEntry(std::string key, std::string entry)
Definition database.cpp:49
std::string GetEntry(std::string hash)
Definition database.cpp:43
BOOL AddEntry(std::string key, std::string entry)
Definition database.cpp:33
rocksdb::Options m_options
Definition database.cpp:21
rocksdb::DB * m_database
Definition database.cpp:20
int BOOL
Definition inject.h:3
#define ERRORCODE
#define PANOPTES_DATABASE_PATH