Skip to content

Commit

Permalink
Merge pull request #1720 from FIWARE/bug/context-not-persisted-to-mon…
Browse files Browse the repository at this point in the history
…go-on-reload

Reloaded hosted contexts were not persisted in mongo
  • Loading branch information
kzangeli authored Dec 12, 2024
2 parents 98ab745 + 8e9a1a3 commit 51f2004
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Fixed Issues:
#XXXX: Complex @contexts of subscriptions weren't persisted in mongodb
#1708: Reloaded hosted contexts were not persisted in mongo, only the context cache

## New Features:
* Support for ...
Expand Down
5 changes: 5 additions & 0 deletions src/lib/logMsg/traceLevels.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ typedef enum TraceLevels
//
LmtSubordinate = 140,

//
// Context Cache
//
LmtContextCachePersist = 150, // Persisting contexts in DB

//
// Misc
//
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/context/orionldContextFromUrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ OrionldContext* orionldContextFromUrl(char* url, char* id)
contextP->origin = OrionldContextDownloaded;
contextP->usedAt = orionldState.requestTime;

orionldContextCachePersist(contextP);
orionldContextCachePersist(contextP, false);
}

// Remove the 'url' from the contextDownloadList and persist it to DB
Expand Down
4 changes: 2 additions & 2 deletions src/lib/orionld/contextCache/orionldContextCachePersist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern "C"
// "value": JSON Array|Object representation of the VALUE of the @context (i.e. NOT containing the @context member)
// }
//
void orionldContextCachePersist(OrionldContext* contextP)
void orionldContextCachePersist(OrionldContext* contextP, bool reload)
{
KjNode* contextObjP = kjObject(orionldState.kjsonP, NULL);
KjNode* idP;
Expand Down Expand Up @@ -98,5 +98,5 @@ void orionldContextCachePersist(OrionldContext* contextP)
valueP->name = (char*) "value";
kjChildAdd(contextObjP, valueP);

mongocContextCachePersist(contextObjP);
mongocContextCachePersist(contextObjP, reload);
}
2 changes: 1 addition & 1 deletion src/lib/orionld/contextCache/orionldContextCachePersist.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
//
// orionldContextCachePersist -
//
extern void orionldContextCachePersist(OrionldContext* contextP);
extern void orionldContextCachePersist(OrionldContext* contextP, bool reload);

#endif // SRC_LIB_ORIONLD_CONTEXTCACHE_ORIONLDCONTEXTCACHEPERSIST_H_
2 changes: 1 addition & 1 deletion src/lib/orionld/mhd/mhdConnectionTreat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ MHD_Result mhdConnectionTreat(void)
{
orionldState.contextP->origin = OrionldContextFromInline;
orionldState.contextP->kind = OrionldContextHosted;
orionldContextCachePersist(orionldState.contextP);
orionldContextCachePersist(orionldState.contextP, false);
}
}

Expand Down
38 changes: 30 additions & 8 deletions src/lib/orionld/mongoc/mongocContextCachePersist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
extern "C"
{
#include "kjson/KjNode.h" // KjNode
#include "kjson/kjLookup.h" // kjLookup
}

#include "logMsg/logMsg.h" // LM_*
Expand All @@ -43,23 +44,44 @@ extern "C"
//
// mongocContextCachePersist -
//
void mongocContextCachePersist(KjNode* contextObject)
void mongocContextCachePersist(KjNode* contextObject, bool reload)
{
bson_t bson;

mongocKjTreeToBson(contextObject, &bson);
bson_error_t error;

mongocConnectionGet(NULL, DbContexts);

sem_wait(&mongocContextsSem);

bson_error_t mcError;
bool r = mongoc_collection_insert_one(orionldState.mongoc.contextsP, &bson, NULL, NULL, &mcError);
//
// If the context is to be reloaded, it is REMOVED before inserted
//
if (reload == true)
{
KjNode* urlNodeP = kjLookup(contextObject, "url");
char* url = (urlNodeP != NULL)? urlNodeP->value.s : NULL;

if (url != NULL)
{
bson_t mongoFilter;

bson_init(&mongoFilter);
bson_append_utf8(&mongoFilter, "url", 3, url, -1);

// Remove the context
if (mongoc_collection_remove(orionldState.mongoc.contextsP, MONGOC_REMOVE_SINGLE_REMOVE, &mongoFilter, NULL, &error) == false)
LM_E(("Database Error (mongoc_collection_remove returned %d.%d:%s)", error.domain, error.code, error.message));
bson_destroy(&mongoFilter);
}
}

bson_t bson;
mongocKjTreeToBson(contextObject, &bson);

bool r = mongoc_collection_insert_one(orionldState.mongoc.contextsP, &bson, NULL, NULL, &error);

sem_post(&mongocContextsSem);

if (r == false)
LM_E(("Database Error (persisting context: %s)", mcError.message));
LM_E(("Database Error (persisting context: %s)", error.message));

bson_destroy(&bson);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/mongoc/mongocContextCachePersist.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ extern "C"
//
// mongocContextCachePersist -
//
extern void mongocContextCachePersist(KjNode* contextObject);
extern void mongocContextCachePersist(KjNode* contextObject, bool reload);

#endif // SRC_LIB_ORIONLD_MONGOC_MONGOCCONTEXTCACHEPERSIST_H_
4 changes: 4 additions & 0 deletions src/lib/orionld/serviceRoutines/orionldDeleteContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern "C"
#include "orionld/contextCache/orionldContextCacheLookup.h" // orionldContextCacheLookup
#include "orionld/contextCache/orionldContextCacheDelete.h" // orionldContextCacheDelete
#include "orionld/contextCache/orionldContextCacheInsert.h" // orionldContextCacheInsert
#include "orionld/contextCache/orionldContextCachePersist.h" // orionldContextCachePersist
#include "orionld/serviceRoutines/orionldDeleteContext.h" // Own Interface


Expand Down Expand Up @@ -111,6 +112,9 @@ bool orionldDeleteContext(void)

// Free the kj tree of the now obsolete old context
kjFree(oldContextP->tree);

// Update mongo with the new context
orionldContextCachePersist(contextP, true);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/serviceRoutines/orionldPostContexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool orionldPostContexts(void)

httpHeaderLocationAdd(contextP->url, NULL, NULL);

orionldContextCachePersist(contextP);
orionldContextCachePersist(contextP, false);
orionldState.httpStatusCode = 201;

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ MongoDB server version: REGEX(.*)
"unit" : "%",
"en" : "Humidity",
"el" : "Υγρασία"
},
"pressure" : {
"@id" : "myAttributes:pressure",
"@type" : "xsd:integer",
"unit" : "bar",
"en" : "Pressure",
"el" : "Υγρασία"
}
}
}
Expand Down

0 comments on commit 51f2004

Please sign in to comment.