Line data Source code
1 : #include <rocksdb/utilities/transaction.h>
2 : #include "DatabaseManager.h"
3 :
4 : #define DB_NAME "users"
5 : #define DB_CHAT "chats"
6 : using namespace rocksdb;
7 : using std::string;
8 :
9 676 : DatabaseManager::DatabaseManager(DB *database){
10 676 : db = database;
11 676 : iter = NULL;
12 676 : }
13 :
14 121 : DatabaseManager::~DatabaseManager() {
15 121 : db = NULL;
16 : /*Nose porque pero ahora dejo de fallar
17 : solo comente esta "eliminacion"
18 : podria ser que estoy llamando a un metodo
19 : de la clase a eliminar; */
20 : //deleteIterator();
21 121 : }
22 :
23 24 : bool DatabaseManager::correctEntry(string key, string value) {
24 24 : string aux;
25 24 : LOGG(DEBUG) << "Looking for existance of " << key << " - " << value;
26 48 : Status status = db->Get(ReadOptions(), key, &aux);
27 24 : if (! status.ok()) {
28 6 : LOGG(DEBUG) << key << " not found in DB";
29 6 : return false;
30 18 : } else if (value.compare(aux) != 0) {
31 0 : LOGG(DEBUG) << value << " does not mathces value in DB";
32 0 : return false;
33 : }
34 18 : LOGG(DEBUG) << "Correct key-value";
35 42 : return true;
36 : }
37 :
38 1286 : bool DatabaseManager::addEntry(std::string key, std::string value) {
39 1286 : LOGG(DEBUG) << "Add Entry: "<< key << "," << value;
40 1286 : return db->Put(WriteOptions(), key, value).ok();
41 : }
42 :
43 155 : bool DatabaseManager::deleteEntry(string key) {
44 155 : return db->Delete(WriteOptions(), key).ok();
45 : }
46 :
47 2258 : bool DatabaseManager::getEntry(string key, string &found){
48 2258 : string aux;
49 4516 : Status status = db->Get(ReadOptions(), key, &aux);
50 2258 : if (! status.ok()) {
51 544 : return false;
52 : }
53 1714 : found = aux;
54 3972 : return true;
55 : }
56 :
57 :
58 74 : void DatabaseManager::replaceEntry(std::string key, std::string value) {
59 74 : string aux;
60 74 : if (getEntry(key, aux)) {
61 63 : deleteEntry(key);
62 : }
63 74 : addEntry(key, value);
64 74 : }
65 :
66 12 : void DatabaseManager::createIterator() {
67 12 : iter = db->NewIterator(rocksdb::ReadOptions());
68 12 : iter->SeekToFirst();
69 12 : LOGG(DEBUG) << "Iterator created";
70 12 : }
71 :
72 47 : bool DatabaseManager::advanceIterator() {
73 47 : if (! iter->Valid()) {
74 0 : return false;
75 : }
76 47 : iter->Next();
77 47 : return true;
78 : }
79 53 : bool DatabaseManager::getActualPair(std::string& key, std::string &value) {
80 53 : if (iter == NULL) {
81 0 : return false;
82 : }
83 53 : key = iter->key().ToString();
84 53 : value = iter->value().ToString();
85 53 : return true;
86 : }
87 :
88 12 : void DatabaseManager::deleteIterator() {
89 12 : if (iter == NULL) {
90 12 : return;
91 : }
92 12 : LOGG(DEBUG) << "About to delete iterator";
93 12 : delete iter;
94 12 : iter = NULL;
95 12 : LOGG(DEBUG) << "Deleted";
96 : }
97 :
98 6 : bool DatabaseManager::validIterator() {
99 6 : if (iter == NULL)
100 0 : return false;
101 6 : return iter->Valid();
102 66 : }
|