diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-10-21 15:57:36 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-10-21 15:57:36 +0200 |
commit | 109108c896f1ada76121330ab01602072f32dd8c (patch) | |
tree | 64546db690e8edb814053ebcfb3603ff792a0a54 | |
parent | 22abf1aa83b2668f918556491a55947be798f89f (diff) |
Add database object to callback funcs
-rw-r--r-- | include/odhtdb/Database.hpp | 20 | ||||
-rw-r--r-- | src/DatabaseStorage.cpp | 15 | ||||
-rw-r--r-- | src/Log.cpp | 12 |
3 files changed, 29 insertions, 18 deletions
diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 61365a0..ca713bd 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -46,6 +46,8 @@ namespace odhtdb public: DatabaseAddException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; + + class Database; struct DatabaseCreateNodeRequest { @@ -56,13 +58,15 @@ namespace odhtdb const Signature::PublicKey *creatorPublicKey; const DataView groupId; bool loadedFromCache; + const Database *database; - DatabaseCreateNodeRequest(const Hash *_nodeHash, u64 _timestamp, const Signature::PublicKey *_creatorPublicKey, const DataView &_groupId) : + DatabaseCreateNodeRequest(const Hash *_nodeHash, u64 _timestamp, const Signature::PublicKey *_creatorPublicKey, const DataView &_groupId, const Database *_database) : nodeHash(_nodeHash), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey), groupId(_groupId), - loadedFromCache(false) + loadedFromCache(false), + database(_database) { } @@ -78,14 +82,16 @@ namespace odhtdb const Signature::PublicKey *creatorPublicKey; const DataView decryptedData; bool loadedFromCache; + const Database *database; - DatabaseAddNodeRequest(const Hash *_nodeHash, const Hash *_requestHash, u64 _timestamp, const Signature::PublicKey *_creatorPublicKey, const DataView &_decryptedData) : + DatabaseAddNodeRequest(const Hash *_nodeHash, const Hash *_requestHash, u64 _timestamp, const Signature::PublicKey *_creatorPublicKey, const DataView &_decryptedData, const Database *_database) : nodeHash(_nodeHash), requestHash(_requestHash), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey), decryptedData(_decryptedData), - loadedFromCache(false) + loadedFromCache(false), + database(_database) { } @@ -102,15 +108,17 @@ namespace odhtdb const Signature::PublicKey *userToAddPublicKey; const DataView groupToAddUserTo; bool loadedFromCache; + const Database *database; - DatabaseAddUserRequest(const Hash *_nodeHash, const Hash *_requestHash, u64 _timestamp, const Signature::PublicKey *_creatorPublicKey, const Signature::PublicKey *_userToAddPublicKey, const DataView &_groupToAddUserTo) : + DatabaseAddUserRequest(const Hash *_nodeHash, const Hash *_requestHash, u64 _timestamp, const Signature::PublicKey *_creatorPublicKey, const Signature::PublicKey *_userToAddPublicKey, const DataView &_groupToAddUserTo, const Database *_database) : nodeHash(_nodeHash), requestHash(_requestHash), timestamp(_timestamp), creatorPublicKey(_creatorPublicKey), userToAddPublicKey(_userToAddPublicKey), groupToAddUserTo(_groupToAddUserTo), - loadedFromCache(false) + loadedFromCache(false), + database(_database) { } diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index de9d0df..34dd09a 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -14,6 +14,9 @@ using namespace std; +// TODO: The latest user data will be used when processing old nodes. When processing a node the user state should be the same as it was at the time the node was created. +// This can be done by having history of changes in user data so we can retrieve the user data state at a specific time in history. + namespace odhtdb { enum StorageType : u8 @@ -230,7 +233,7 @@ namespace odhtdb const DataView adminGroupRaw = nodeQuery.getBlob(2); memcpy(adminGroupId, adminGroupRaw.data, GROUP_ID_LENGTH); - DatabaseCreateNodeRequest createNodeRequest(&nodeHash, timestamp, &creatorPublicKey, DataView(adminGroupId, GROUP_ID_LENGTH)); + DatabaseCreateNodeRequest createNodeRequest(&nodeHash, timestamp, &creatorPublicKey, DataView(adminGroupId, GROUP_ID_LENGTH), database); createNodeRequest.loadedFromCache = true; database->onCreateNodeCallbackFunc(createNodeRequest); } @@ -278,7 +281,7 @@ namespace odhtdb OwnedByteArray decryptedData(new u8[decryptedDataSize], decryptedDataSize); memcpy(decryptedData.data, decryptedDataRaw, decryptedDataSize); - DatabaseAddNodeRequest addNodeRequest(&nodeHash, &requestHash, timestamp, &creatorPublicKey, DataView(decryptedData.data, decryptedData.size)); + DatabaseAddNodeRequest addNodeRequest(&nodeHash, &requestHash, timestamp, &creatorPublicKey, DataView(decryptedData.data, decryptedData.size), database); addNodeRequest.loadedFromCache = true; database->onAddNodeCallbackFunc(addNodeRequest); } @@ -307,7 +310,7 @@ namespace odhtdb u8 groupToAddUserTo[GROUP_ID_LENGTH]; memcpy(groupToAddUserTo, groupToAddUserToRaw, GROUP_ID_LENGTH); - DatabaseAddUserRequest addUserRequest(&nodeHash, &requestHash, timestamp, &creatorPublicKey, &userToAddPublicKey, DataView(groupToAddUserTo, GROUP_ID_LENGTH)); + DatabaseAddUserRequest addUserRequest(&nodeHash, &requestHash, timestamp, &creatorPublicKey, &userToAddPublicKey, DataView(groupToAddUserTo, GROUP_ID_LENGTH), database); addUserRequest.loadedFromCache = true; database->onAddUserCallbackFunc(addUserRequest); } @@ -1048,7 +1051,7 @@ namespace odhtdb bool DatabaseStorage::decryptNodeData(const Hash &nodeHash, const shared_ptr<OwnedByteArray> decryptionKey, const Signature::PublicKey *creatorPublicKey, const DataView &adminGroupId, u64 timestamp) { - const DatabaseCreateNodeRequest createNodeRequest(&nodeHash, timestamp, creatorPublicKey, adminGroupId); + const DatabaseCreateNodeRequest createNodeRequest(&nodeHash, timestamp, creatorPublicKey, adminGroupId, database); if(database->onCreateNodeCallbackFunc) database->onCreateNodeCallbackFunc(createNodeRequest); @@ -1206,7 +1209,7 @@ namespace odhtdb setNodeAddDataDecryptedData(rowId, decryptedBody.getDecryptedText()); Log::debug("Got add object, timestamp: %zu, data: %.*s", timestamp, decryptedBody.getDecryptedText().size, decryptedBody.getDecryptedText().data); - const DatabaseAddNodeRequest addNodeRequest(&nodeHash, &dataHash, timestamp, creatorPublicKey, decryptedBody.getDecryptedText()); + const DatabaseAddNodeRequest addNodeRequest(&nodeHash, &dataHash, timestamp, creatorPublicKey, decryptedBody.getDecryptedText(), database); if(database->onAddNodeCallbackFunc) database->onAddNodeCallbackFunc(addNodeRequest); } @@ -1239,7 +1242,7 @@ namespace odhtdb setNodeAddDataDecrypted(rowId); Log::debug("Got add user object, timestamp: %zu, user added: %s", timestamp, userToAddPublicKey->toString().c_str()); - DatabaseAddUserRequest addUserRequest(&nodeHash, &dataHash, timestamp, creatorPublicKey, userToAddPublicKey, groupToAddUserTo); + DatabaseAddUserRequest addUserRequest(&nodeHash, &dataHash, timestamp, creatorPublicKey, userToAddPublicKey, groupToAddUserTo, database); if(database->onAddUserCallbackFunc) database->onAddUserCallbackFunc(addUserRequest); return true; diff --git a/src/Log.cpp b/src/Log.cpp index b8f75c8..0feb3ac 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -13,9 +13,9 @@ namespace odhtdb std::lock_guard<std::mutex> lock(mutexLog); va_list args; va_start(args, fmt); - fputs("\033[1;32mDebug:\033[0m ", stdout); - vfprintf(stdout, fmt, args); - fputs("\n", stdout); + fputs("\033[1;32mDebug:\033[0m ", stderr); + vfprintf(stderr, fmt, args); + fputs("\n", stderr); va_end(args); } @@ -24,9 +24,9 @@ namespace odhtdb std::lock_guard<std::mutex> lock(mutexLog); va_list args; va_start(args, fmt); - fputs("\033[1;33mWarning:\033[0m ", stdout); - vfprintf(stdout, fmt, args); - fputs("\n", stdout); + fputs("\033[1;33mWarning:\033[0m ", stderr); + vfprintf(stderr, fmt, args); + fputs("\n", stderr); va_end(args); } |