aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-14 22:25:26 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commitfa8d14018891d90e23fce0e510a01728970932f2 (patch)
tree723d8d126c87f974598fd865c22c516ea14bd699 /src
parentc820b10adddc7a229cf2de99937d05a5715d2ab0 (diff)
Implement node load/fetch order
Diffstat (limited to 'src')
-rw-r--r--src/Database.cpp12
-rw-r--r--src/DatabaseStorage.cpp28
2 files changed, 30 insertions, 10 deletions
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> &value)
+ auto responseKeyFuture = node.listen(*responseKeyShared, [this, nodeToSeed](const shared_ptr<Value> &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<u8>() == 1;
+ DatabaseFetchOrder fetchOrder = deserializer.extract<DatabaseFetchOrder>();
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<u64> 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<DatabaseCreateResponse> 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);