aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <0xdec05eba@gmail.com>2018-05-14 22:25:26 +0200
committerdec05eba <0xdec05eba@gmail.com>2018-05-14 22:25:29 +0200
commit2260790118358449a18527fcd9e6ff46d2caccf7 (patch)
tree9bc70e9bedc290ad4f147f5bf8f23e29b40794a0 /src
parent7d675a45023e44cfa03d7fa95e4ec20e3a1c2fb9 (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);