From fa8d14018891d90e23fce0e510a01728970932f2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 14 May 2018 22:25:26 +0200 Subject: Implement node load/fetch order --- src/Database.cpp | 12 +++++++----- src/DatabaseStorage.cpp | 28 +++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp index ddef6d1..93dec99 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -170,7 +170,7 @@ namespace odhtdb u64 range; }; - void Database::seed(const DatabaseNode &nodeToSeed) + void Database::seed(const DatabaseNode &nodeToSeed, DatabaseFetchOrder fetchOrder) { if(seedInfoMap.find(*nodeToSeed.getRequestHash()) != seedInfoMap.end()) { @@ -208,7 +208,7 @@ namespace odhtdb newSeedInfo.reponseKeyInfoHash = responseKeyShared; // TODO: If this response key is spammed, generate a new one. - auto responseKeyFuture = node.listen(*newSeedInfo.reponseKeyInfoHash, [this, nodeToSeed](const shared_ptr &value) + auto responseKeyFuture = node.listen(*responseKeyShared, [this, nodeToSeed](const shared_ptr &value) { const Hash requestHash(value->data.data(), value->data.size()); if(requestHash == *nodeToSeed.getRequestHash()) @@ -233,6 +233,7 @@ namespace odhtdb return true; // We sent the request, ignore our own requests bool userWantsCreateNode = deserializer.extract() == 1; + DatabaseFetchOrder fetchOrder = deserializer.extract(); if(userWantsCreateNode) { @@ -287,7 +288,7 @@ namespace odhtdb if(!ok) Log::error("Failed to put response for old data for 'add' data"); }); - }); + }, fetchOrder); } catch (std::exception &e) { @@ -302,6 +303,7 @@ namespace odhtdb serializer.add(responseKey, OPENDHT_INFOHASH_LEN); bool iHaveCreateNode = databaseStorage.doesNodeExist(*nodeToSeed.getRequestHash()); serializer.add(iHaveCreateNode ? (u8)0 : (u8)1); + serializer.add(fetchOrder); DataViewMap userLatestActionCounter; databaseStorage.fetchNodeUserActionGaps(*nodeToSeed.getRequestHash(), [&serializer, &userLatestActionCounter](const DataView userPublicKey, u64 actionGapStart, u64 actionGapRange) @@ -349,9 +351,9 @@ namespace odhtdb } } - void Database::loadNode(const Hash &nodeHash) + void Database::loadNode(const Hash &nodeHash, DatabaseLoadOrder loadOrder) { - databaseStorage.loadNode(nodeHash); + databaseStorage.loadNode(nodeHash, loadOrder); } unique_ptr Database::create() diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index 6064ee5..8bef77e 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -215,11 +215,19 @@ namespace odhtdb cleanup(); } - void DatabaseStorage::loadNode(const Hash &nodeHash) + void DatabaseStorage::loadNode(const Hash &nodeHash, DatabaseLoadOrder loadOrder) { + string orderByString; + if(loadOrder == DatabaseLoadOrder::OLDEST_FIRST) + orderByString = " ORDER BY timestamp ASC"; + else if(loadOrder == DatabaseLoadOrder::NEWEST_FIRST) + orderByString = " ORDER BY timestamp DESC"; + if(database->onCreateNodeCallbackFunc) { - SqlQuery nodeQuery(sqliteDb, "SELECT timestamp, creatorPublicKey, adminGroupId From Node WHERE nodeHash = ?", { DataView(nodeHash.getData(), nodeHash.getSize()) }); + string queryStr = "SELECT timestamp, creatorPublicKey, adminGroupId From Node WHERE nodeHash = ?"; + queryStr += orderByString; + SqlQuery nodeQuery(sqliteDb, queryStr.c_str(), { DataView(nodeHash.getData(), nodeHash.getSize()) }); while(nodeQuery.next()) { u64 timestamp = nodeQuery.getInt64(0); @@ -234,7 +242,9 @@ namespace odhtdb } } - SqlQuery nodeQuery(sqliteDb, "SELECT id, requestHash, operation, timestamp, creatorPublicKey From NodeAddData WHERE node = ? AND decrypted = 1", { DataView(nodeHash.getData(), nodeHash.getSize()) }); + string queryStr = "SELECT id, requestHash, operation, timestamp, creatorPublicKey From NodeAddData WHERE node = ? AND decrypted = 1"; + queryStr += orderByString; + SqlQuery nodeQuery(sqliteDb, queryStr.c_str(), { DataView(nodeHash.getData(), nodeHash.getSize()) }); while(nodeQuery.next()) { i64 rowId = nodeQuery.getInt64(0); @@ -732,9 +742,17 @@ namespace odhtdb } } - void DatabaseStorage::fetchNodeAddDataRaw(const Hash &nodeHash, FetchNodeAddDataRawCallbackFunc callbackFunc) + void DatabaseStorage::fetchNodeAddDataRaw(const Hash &nodeHash, FetchNodeAddDataRawCallbackFunc callbackFunc, DatabaseFetchOrder fetchOrder) { - SqlQuery query(sqliteDb, "SELECT rawData.data, nodeAddData.creatorPublicKey, nodeAddData.userActionCounter From NodeAddData AS nodeAddData INNER JOIN NodeAddDataRaw AS rawData ON rawData.nodeAddDataId = nodeAddData.id WHERE nodeAddData.node = ?", { DataView(nodeHash.getData(), nodeHash.getSize()) }); + string orderByString; + if(fetchOrder == DatabaseFetchOrder::OLDEST_FIRST) + orderByString = " ORDER BY nodeAddData.timestamp ASC"; + else if(fetchOrder == DatabaseFetchOrder::NEWEST_FIRST) + orderByString = " ORDER BY nodeAddData.timestamp DESC"; + + string queryStr = "SELECT rawData.data, nodeAddData.creatorPublicKey, nodeAddData.userActionCounter From NodeAddData AS nodeAddData INNER JOIN NodeAddDataRaw AS rawData ON rawData.nodeAddDataId = nodeAddData.id WHERE nodeAddData.node = ?"; + queryStr += orderByString; + SqlQuery query(sqliteDb, queryStr.c_str(), { DataView(nodeHash.getData(), nodeHash.getSize()) }); while(query.next()) { const DataView rawData = query.getBlob(0); -- cgit v1.2.3