Datastore: no priority queue anymore for republish

Now handled only by last republish time.
This commit is contained in:
Théophile Bastian 2016-11-28 18:39:34 +01:00
parent 50975cd0e0
commit fa51b5c170
2 changed files with 20 additions and 33 deletions

View file

@ -10,28 +10,25 @@
#include <cstdlib> #include <cstdlib>
using namespace std; using namespace std;
DataStore::DataStore(Protocol* proto) : proto(proto) DataStore::DataStore(Protocol* proto) :
proto(proto), myData(0), hasOwnData(false)
{} {}
DataStore::~DataStore() { DataStore::~DataStore() {
} }
void DataStore::update() { void DataStore::update() {
while(!timeEvents.empty()) { while(!expireEvents.empty()) {
TimeEvent evt = timeEvents.top(); TimeEvent evt = expireEvents.front();
if(evt.time > time(NULL)) // We're done for now. if(evt.time > time(NULL)) // We're done for now.
break; break;
timeEvents.pop(); expireEvents.pop();
switch(evt.type) { handleExpire(evt.id, evt.seqno);
case EV_REPUBLISH: }
handleRepublish(evt.id); if(hasOwnData) {
break; if(time(NULL) - recvTime[myData] > csts::TIME_REPUBLISH_DATA)
handleRepublish(myData);
case EV_EXPIRES:
handleExpire(evt.id, evt.seqno);
break;
}
} }
} }
@ -43,14 +40,13 @@ void DataStore::addData(Bytes pck, u32 seqno, u64 id, bool mine) {
curSeqno[id] = seqno; curSeqno[id] = seqno;
if(mine) { if(mine) {
timeEvents.push(TimeEvent( hasOwnData = true;
time(NULL) + csts::TIME_REPUBLISH_DATA, myData = id;
seqno, id, EV_REPUBLISH));
} }
else { else {
timeEvents.push(TimeEvent( expireEvents.push(TimeEvent(
time(NULL) + csts::TIMEOUT_DATA, time(NULL) + csts::TIMEOUT_DATA,
seqno, id, EV_EXPIRES)); seqno, id));
// If it is not renewed, it will expire. // If it is not renewed, it will expire.
} }
@ -128,10 +124,7 @@ void DataStore::handleRepublish(u64 datId) {
} }
curSeqno[datId] = time(NULL); curSeqno[datId] = time(NULL);
recvTime[datId] = time(NULL);
timeEvents.push(TimeEvent(
time(NULL) + csts::TIME_REPUBLISH_DATA,
curSeqno[datId], datId, EV_REPUBLISH));
handleFlood(datId); handleFlood(datId);
} }

View file

@ -66,20 +66,12 @@ class DataStore {
void cleanData(u64 id); void cleanData(u64 id);
private: private:
enum EvType {
EV_REPUBLISH, EV_EXPIRES
};
struct TimeEvent { struct TimeEvent {
TimeEvent(time_t time, u32 seqno, u64 id, EvType type) : TimeEvent(time_t time, u32 seqno, u64 id) :
time(time), seqno(seqno), id(id), type(type) {} time(time), seqno(seqno), id(id) {}
time_t time; time_t time;
u32 seqno; u32 seqno;
u64 id; u64 id;
EvType type;
bool operator<(const TimeEvent& e) const {
return time > e.time; // Max-priority queue
}
}; };
struct Data { struct Data {
@ -90,8 +82,10 @@ class DataStore {
bool isMine; bool isMine;
}; };
std::priority_queue<TimeEvent> timeEvents; std::queue<TimeEvent> expireEvents;
Protocol* proto; Protocol* proto;
u64 myData;
bool hasOwnData;
std::unordered_map<u64, Data> data; std::unordered_map<u64, Data> data;
std::unordered_map<u64, u32> curSeqno; std::unordered_map<u64, u32> curSeqno;
std::unordered_map<u64, time_t> recvTime; std::unordered_map<u64, time_t> recvTime;