diff --git a/src/app/orionld/orionld.cpp b/src/app/orionld/orionld.cpp index 5df77cdcfc..97d22ae42f 100644 --- a/src/app/orionld/orionld.cpp +++ b/src/app/orionld/orionld.cpp @@ -125,6 +125,7 @@ extern "C" #include "orionld/context/orionldContextFromUrl.h" // contextDownloadListInit, contextDownloadListRelease #include "orionld/contextCache/orionldContextCacheRelease.h" // orionldContextCacheRelease #include "orionld/rest/orionldServiceInit.h" // orionldServiceInit +#include "orionld/entityMaps/entityMapsRelease.h" // entityMapsRelease #include "orionld/db/dbInit.h" // dbInit #include "orionld/mqtt/mqttRelease.h" // mqttRelease #include "orionld/regCache/regCacheInit.h" // regCacheInit @@ -132,6 +133,7 @@ extern "C" #include "orionld/regCache/regCacheRelease.h" // regCacheRelease #include "orionld/pernot/pernotSubCacheInit.h" // pernotSubCacheInit #include "orionld/pernot/pernotLoop.h" // pernotLoopStart +#include "orionld/pernot/pernotRelease.h" // pernotRelease #include "orionld/version.h" #include "orionld/orionRestServices.h" @@ -630,6 +632,14 @@ void exitFunc(void) pgConnectionPoolsFree(); } + // Cleanup entity maps + if (entityMaps != NULL) + entityMapsRelease(); + + // Cleanup periodic notifications + if (pernot == true) + pernotRelease(); + kaBufferReset(&kalloc, KFALSE); } diff --git a/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp b/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp index f8f24200e3..f80f130050 100644 --- a/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp +++ b/src/lib/orionld/mongoc/mongocGeoIndexInit.cpp @@ -117,19 +117,15 @@ bool mongocGeoIndexInit(void) "}"); unwind = BCON_NEW("$unwind", BCON_UTF8("$attributeKeys")); - - group = BCON_NEW("$group", "{", - "_id", BCON_UTF8("$attributeKeys.k"), - "}"); + group = BCON_NEW("$group", "{", "_id", BCON_UTF8("$attributeKeys.k"), "}"); bson_append_document(objectArray, "0", 1, project); bson_append_document(objectArray, "1", 1, unwind); bson_append_document(objectArray, "2", 1, group); - // Append the array to the document + // Append the array to the pipeline bson_append_array(pipeline, "pipeline", 8, objectArray); - // Print the BSON document as a JSON string if (lmTraceIsSet(LmtMongoc)) { char* str = bson_as_relaxed_extended_json(pipeline, NULL); @@ -176,11 +172,21 @@ bool mongocGeoIndexInit(void) LM_T(LmtMongoc, ("Found geoProperty: '%s'", geoPropertyName)); if (dbGeoIndexLookup(tenantP->tenant, geoPropertyName) == NULL) + { mongocGeoIndexCreate(tenantP, geoPropertyName); + LM_T(LmtMongoc, ("Creating index for property '%s'", geoPropertyName)); + } } mongoc_cursor_destroy(mongoCursorP); mongoc_collection_destroy(mCollectionP); + + bson_destroy(objectArray); + bson_destroy(group); + bson_destroy(unwind); + bson_destroy(project); + bson_destroy(pipeline); + tenantP = tenantP->next; } diff --git a/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp b/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp index 6936bfb4fe..213a978baa 100644 --- a/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp +++ b/src/lib/orionld/mongoc/mongocRegistrationsIter.cpp @@ -86,6 +86,7 @@ int mongocRegistrationsIter(RegCache* rcP, RegCacheIterFunc callback) { char* json = bson_as_relaxed_extended_json(mongoDocP, NULL); LM_T(LmtMongoc, ("Found a registration in the DB: '%s'", json)); + bson_free(json); KjNode* dbRegP = mongocKjTreeFromBson(mongoDocP, &title, &details); if (dbRegP == NULL) diff --git a/src/lib/orionld/pernot/CMakeLists.txt b/src/lib/orionld/pernot/CMakeLists.txt index f27a44126a..91d51eb3ea 100644 --- a/src/lib/orionld/pernot/CMakeLists.txt +++ b/src/lib/orionld/pernot/CMakeLists.txt @@ -23,7 +23,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.5) SET (SOURCES pernotSubCacheInit.cpp pernotSubCacheAdd.cpp - pernotSubCacheRemove.cpp + pernotItemRelease.cpp + pernotRelease.cpp pernotSubCacheLookup.cpp pernotLoop.cpp pernotTreat.cpp diff --git a/src/lib/orionld/pernot/PernotSubCache.cpp b/src/lib/orionld/pernot/pernotItemRelease.cpp similarity index 70% rename from src/lib/orionld/pernot/PernotSubCache.cpp rename to src/lib/orionld/pernot/pernotItemRelease.cpp index 6305c02276..a8c224ca98 100644 --- a/src/lib/orionld/pernot/PernotSubCache.cpp +++ b/src/lib/orionld/pernot/pernotItemRelease.cpp @@ -22,19 +22,27 @@ * * Author: Ken Zangelin */ -#include "common/RenderFormat.h" // RenderFormat +extern "C" +{ +#include "kjson/kjFree.h" // kjFree +} -#include "orionld/q/QNode.h" // QNode -#include "orionld/context/OrionldContext.h" // OrionldContext -#include "orionld/pernot/PernotSubCache.h" // Own interface +#include "orionld/pernot/PernotSubscription.h" // PernotSubscription +#include "orionld/pernot/pernotItemRelease.h" // Own interface // ----------------------------------------------------------------------------- // -// pernotSubCacheRemove - +// pernotItemRelease - // -bool pernotSubCacheRemove(PernotSubscription* pSubP) +bool pernotItemRelease(PernotSubscription* pSubP) { + if (pSubP->subscriptionId != NULL) + free(pSubP->subscriptionId); + + if (pSubP->kjSubP != NULL) + kjFree(pSubP->kjSubP); + return true; } diff --git a/src/lib/orionld/pernot/pernotSubCacheRemove.h b/src/lib/orionld/pernot/pernotItemRelease.h similarity index 85% rename from src/lib/orionld/pernot/pernotSubCacheRemove.h rename to src/lib/orionld/pernot/pernotItemRelease.h index b40898124d..57813a4a3b 100644 --- a/src/lib/orionld/pernot/pernotSubCacheRemove.h +++ b/src/lib/orionld/pernot/pernotItemRelease.h @@ -1,5 +1,5 @@ -#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTSUBCACHEREMOVE_H_ -#define SRC_LIB_ORIONLD_PERNOT_PERNOTSUBCACHEREMOVE_H_ +#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTITEMRELEASE_H_ +#define SRC_LIB_ORIONLD_PERNOT_PERNOTITEMRELEASE_H_ /* * @@ -38,8 +38,8 @@ // ----------------------------------------------------------------------------- // -// pernotSubCacheRemove - +// pernotItemRelease - // -extern bool pernotSubCacheRemove(PernotSubscription* pSubP); +extern bool pernotItemRelease(PernotSubscription* pSubP); -#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTSUBCACHEREMOVE_H_ +#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTITEMRELEASE_H_ diff --git a/src/lib/orionld/pernot/pernotRelease.cpp b/src/lib/orionld/pernot/pernotRelease.cpp new file mode 100644 index 0000000000..7031fc9461 --- /dev/null +++ b/src/lib/orionld/pernot/pernotRelease.cpp @@ -0,0 +1,52 @@ +/* +* +* Copyright 2023 FIWARE Foundation e.V. +* +* This file is part of Orion-LD Context Broker. +* +* Orion-LD Context Broker is free software: you can redistribute it and/or +* modify it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* Orion-LD Context Broker is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +* General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/. +* +* For those usages not covered by this license please contact with +* orionld at fiware dot org +* +* Author: Ken Zangelin +*/ +extern "C" +{ +#include "kjson/kjFree.h" // kjFree +} + +#include "orionld/common/orionldState.h" // pernotSubCache +#include "orionld/pernot/PernotSubscription.h" // PernotSubscription +#include "orionld/pernot/pernotItemRelease.h" // pernotItemRelease +#include "orionld/pernot/pernotRelease.h" // Own interface + + + +// ----------------------------------------------------------------------------- +// +// pernotRelease - +// +void pernotRelease(void) +{ + LM_T(LmtPernot, ("Releasing all pernot subscriptions")); + + PernotSubscription* psP = pernotSubCache.head; + while (psP != NULL) + { + LM_T(LmtPernot, ("Releasing pernot subscription %s (at %p)", psP->subscriptionId, psP)); + pernotItemRelease(psP); + psP = psP->next; + } +} diff --git a/src/lib/orionld/pernot/pernotSubCacheRemove.cpp b/src/lib/orionld/pernot/pernotRelease.h similarity index 79% rename from src/lib/orionld/pernot/pernotSubCacheRemove.cpp rename to src/lib/orionld/pernot/pernotRelease.h index 357b970c71..1449cba0e9 100644 --- a/src/lib/orionld/pernot/pernotSubCacheRemove.cpp +++ b/src/lib/orionld/pernot/pernotRelease.h @@ -1,3 +1,6 @@ +#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTRELEASE_H_ +#define SRC_LIB_ORIONLD_PERNOT_PERNOTRELEASE_H_ + /* * * Copyright 2023 FIWARE Foundation e.V. @@ -22,16 +25,13 @@ * * Author: Ken Zangelin */ -#include "orionld/pernot/PernotSubscription.h" // PernotSubscription -#include "orionld/pernot/pernotSubCacheRemove.h" // Own interface // ----------------------------------------------------------------------------- // -// pernotSubCacheRemove - +// pernotRelease - // -bool pernotSubCacheRemove(PernotSubscription* pSubP) -{ - return true; -} +extern void pernotRelease(void); + +#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTRELEASE_H_ diff --git a/src/lib/orionld/pernot/pernotSubCacheAdd.cpp b/src/lib/orionld/pernot/pernotSubCacheAdd.cpp index f4d799396a..416f0a6b1f 100644 --- a/src/lib/orionld/pernot/pernotSubCacheAdd.cpp +++ b/src/lib/orionld/pernot/pernotSubCacheAdd.cpp @@ -149,6 +149,8 @@ PernotSubscription* pernotSubCacheAdd PernotSubscription* pSubP = (PernotSubscription*) malloc(sizeof(PernotSubscription)); bzero(pSubP, sizeof(PernotSubscription)); + LM_T(LmtPernot, ("Creating pernot subscription %s (at %p)", subscriptionId, pSubP)); + if (subscriptionId == NULL) { KjNode* idP = kjLookup(apiSubP, "id"); @@ -157,7 +159,7 @@ PernotSubscription* pernotSubCacheAdd subscriptionId = idP->value.s; } - LM_T(LmtPernot, ("Adding pernot subscription '%s' to cache", subscriptionId)); + LM_T(LmtPernot, ("Adding pernot subscription '%s' to cache (top level name: '%s')", subscriptionId, apiSubP->name)); pSubP->subscriptionId = strdup(subscriptionId); pSubP->timeInterval = timeInterval; diff --git a/src/lib/orionld/q/qLex.cpp b/src/lib/orionld/q/qLex.cpp index 1d8144c570..a3b739d962 100644 --- a/src/lib/orionld/q/qLex.cpp +++ b/src/lib/orionld/q/qLex.cpp @@ -94,10 +94,13 @@ static QNode* qTermPush(QNode* prev, char* term, bool* lastTermIsTimestampP, cha LM_T(LmtQ, ("term: '%s' (termLen: %d)", term, termLen)); - if (strcmp(&term[termLen - 10], "modifiedAt") == 0) *lastTermIsTimestampP = true; - else if (strcmp(&term[termLen - 9], "createdAt") == 0) *lastTermIsTimestampP = true; - else if (strcmp(&term[termLen - 10], "observedAt") == 0) *lastTermIsTimestampP = true; - else *lastTermIsTimestampP = false; + *lastTermIsTimestampP = false; + if (termLen >= 9) + { + if (strcmp(&term[termLen - 10], "modifiedAt") == 0) *lastTermIsTimestampP = true; + else if (strcmp(&term[termLen - 9], "createdAt") == 0) *lastTermIsTimestampP = true; + else if (strcmp(&term[termLen - 10], "observedAt") == 0) *lastTermIsTimestampP = true; + } if (*lastTermIsTimestampP == true) LM_T(LmtQ, ("Pushing a Timestamp term: '%s'", term)); diff --git a/src/lib/orionld/serviceRoutines/orionldPostSubscriptions.cpp b/src/lib/orionld/serviceRoutines/orionldPostSubscriptions.cpp index 86ceb104bc..e3a4da510b 100644 --- a/src/lib/orionld/serviceRoutines/orionldPostSubscriptions.cpp +++ b/src/lib/orionld/serviceRoutines/orionldPostSubscriptions.cpp @@ -49,7 +49,7 @@ extern "C" #include "orionld/pernot/PernotSubscription.h" // PernotSubscription #include "orionld/pernot/PernotSubCache.h" // PernotSubCache #include "orionld/pernot/pernotSubCacheAdd.h" // pernotSubCacheAdd -#include "orionld/pernot/pernotSubCacheRemove.h" // pernotSubCacheRemove +#include "orionld/pernot/pernotItemRelease.h" // pernotItemRelease #include "orionld/pernot/pernotSubCacheLookup.h" // pernotSubCacheLookup #include "orionld/mqtt/mqttParse.h" // mqttParse #include "orionld/mqtt/mqttConnectionEstablish.h" // mqttConnectionEstablish @@ -317,7 +317,7 @@ bool orionldPostSubscriptions(void) if (cSubP != NULL) subCacheItemRemove(cSubP); else - pernotSubCacheRemove(pSubP); + pernotItemRelease(pSubP); if (qTree != NULL) qRelease(qTree); diff --git a/src/lib/rest/rest.cpp b/src/lib/rest/rest.cpp index 2d48d32d82..5589f592a1 100644 --- a/src/lib/rest/rest.cpp +++ b/src/lib/rest/rest.cpp @@ -70,6 +70,7 @@ extern "C" #include "orionld/rest/orionldMhdConnectionInit.h" // orionldMhdConnectionInit #include "orionld/rest/orionldMhdConnectionPayloadRead.h" // orionldMhdConnectionPayloadRead #include "orionld/rest/orionldMhdConnectionTreat.h" // orionldMhdConnectionTreat +#include "orionld/forwarding/distOpListRelease.h" // distOpListRelease #include "rest/HttpHeaders.h" // HTTP_* defines #include "rest/Verb.h" @@ -427,15 +428,11 @@ static void requestCompleted free(orionldState.curlHeadersV); } - // - // FIXME: - // Would be nice to do this here instead of in every service routine - // Just, I don't have the pointer (distOpList) in orionldState. - // At least I can do the call to curl_multi_cleanup - // + if (orionldState.distOpList != NULL) + distOpListRelease(orionldState.distOpList); + if (orionldState.curlDoMultiP != NULL) { - // distOpListRelease(orionldState.distOpList); curl_multi_cleanup(orionldState.curlDoMultiP); orionldState.curlDoMultiP = NULL; }