and now it's a list that cleans up after itself

This commit is contained in:
Howard Stearns 2019-03-27 14:58:58 -07:00
parent f6ee77a6ae
commit 489fadda6e

View file

@ -1398,20 +1398,21 @@ bool EntityTree::isScriptInWhitelist(const QString& scriptProperty) {
void EntityTree::addCertifiedEntityOnServer(EntityItemPointer entity) { void EntityTree::addCertifiedEntityOnServer(EntityItemPointer entity) {
QString certID(entity->getCertificateID()); QString certID(entity->getCertificateID());
QList<EntityItemID> entityList; EntityItemID existingEntityItemID;
if (!certID.isEmpty()) { if (!certID.isEmpty()) {
EntityItemID entityItemID = entity->getEntityItemID(); EntityItemID entityItemID = entity->getEntityItemID();
QWriteLocker locker(&_entityCertificateIDMapLock); QWriteLocker locker(&_entityCertificateIDMapLock);
entityList = _entityCertificateIDMap.value(certID); QList<EntityItemID>& entityList = _entityCertificateIDMap[certID]; // inserts it if needed.
QList<EntityItemID> newList; if (!entityList.isEmpty() && !entity->getCertificateType().contains(DOMAIN_UNLIMITED)) {
newList << entityItemID; existingEntityItemID = entityList.first(); // we will only care about the first, if any, below.
_entityCertificateIDMap.insert(certID, newList); entityList.removeOne(existingEntityItemID);
qCDebug(entities) << "Certificate ID" << certID << "belongs to" << entityItemID; }
entityList << entityItemID; // adds to list within hash because entityList is a reference.
qCDebug(entities) << "Certificate ID" << certID << "belongs to" << entityItemID << "total" << entityList.size() << "entities.";
} }
// Delete an already-existing entity from the tree if it has the same // Delete an already-existing entity from the tree if it has the same
// CertificateID as the entity we're trying to add. // CertificateID as the entity we're trying to add.
if (!entityList.isEmpty() && !entity->getCertificateType().contains(DOMAIN_UNLIMITED)) { if (!existingEntityItemID.isNull()) {
EntityItemID existingEntityItemID = entityList.first();
qCDebug(entities) << "Certificate ID" << certID << "already exists on entity with ID" qCDebug(entities) << "Certificate ID" << certID << "already exists on entity with ID"
<< existingEntityItemID << ". Deleting existing entity."; << existingEntityItemID << ". Deleting existing entity.";
withWriteLock([&] { withWriteLock([&] {
@ -1424,8 +1425,10 @@ void EntityTree::removeCertifiedEntityOnServer(EntityItemPointer entity) {
QString certID = entity->getCertificateID(); QString certID = entity->getCertificateID();
if (!certID.isEmpty()) { if (!certID.isEmpty()) {
QWriteLocker entityCertificateIDMapLocker(&_entityCertificateIDMapLock); QWriteLocker entityCertificateIDMapLocker(&_entityCertificateIDMapLock);
QList<EntityItemID> entityList = _entityCertificateIDMap.value(certID); QList<EntityItemID>& entityList = _entityCertificateIDMap[certID];
if (!entityList.isEmpty() && (entity->getEntityItemID() == entityList.first())) { entityList.removeOne(entity->getEntityItemID());
if (entityList.isEmpty()) {
// hmmm, do we to make it be a hash instead of a list, so that this is faster if you stamp out 1000 of a domainUnlimited?
_entityCertificateIDMap.remove(certID); _entityCertificateIDMap.remove(certID);
} }
} }
@ -1439,7 +1442,6 @@ void EntityTree::startDynamicDomainVerificationOnServer(float minimumAgeToRemove
i.next(); i.next();
const auto& certificateID = i.key(); const auto& certificateID = i.key();
const auto& entityIDs = i.value(); const auto& entityIDs = i.value();
if (entityIDs.isEmpty()) { if (entityIDs.isEmpty()) {
continue; continue;
} }
@ -1475,16 +1477,18 @@ void EntityTree::startDynamicDomainVerificationOnServer(float minimumAgeToRemove
return; return;
} }
// Entity does not belong here: // Entity does not belong here:
{ QList<EntityItemID> retained;
EntityItemID entityID = entityIDs.first(); for (int i = 0; i < entityIDs.size(); i++) {
EntityItemID entityID = entityIDs.at(i);
EntityItemPointer entity = findEntityByEntityItemID(entityID); EntityItemPointer entity = findEntityByEntityItemID(entityID);
if (!entity) { if (!entity) {
qCDebug(entities) << "Entity undergoing dynamic domain verification is no longer available:" << entityID; qCDebug(entities) << "Entity undergoing dynamic domain verification is no longer available:" << entityID;
return; continue;
} }
if (entity->getAge() <= minimumAgeToRemove) { if (entity->getAge() <= minimumAgeToRemove) {
qCDebug(entities) << "Entity failed dynamic domain verification, but was created too recently to necessitate deletion:" << entityID; qCDebug(entities) << "Entity failed dynamic domain verification, but was created too recently to necessitate deletion:" << entityID;
return; retained << entityID;
continue;
} }
qCDebug(entities) << "Entity's cert's domain ID" << jsonObject["domain_id"].toString() qCDebug(entities) << "Entity's cert's domain ID" << jsonObject["domain_id"].toString()
<< "doesn't match the current Domain ID" << thisDomainID << "; deleting entity" << entityID; << "doesn't match the current Domain ID" << thisDomainID << "; deleting entity" << entityID;
@ -1494,7 +1498,13 @@ void EntityTree::startDynamicDomainVerificationOnServer(float minimumAgeToRemove
} }
{ {
QWriteLocker entityCertificateIDMapLocker(&_entityCertificateIDMapLock); QWriteLocker entityCertificateIDMapLocker(&_entityCertificateIDMapLock);
if (retained.isEmpty()) {
qCDebug(entities) << "Removed" << certificateID;
_entityCertificateIDMap.remove(certificateID); _entityCertificateIDMap.remove(certificateID);
} else {
qCDebug(entities) << "Retained" << retained.size() << "young entities for" << certificateID;
_entityCertificateIDMap[certificateID] = retained;
}
} }
}); });
} }