Skip to content

Commit

Permalink
Reloaded hosted contexts were not persisted in mongo, only the contex…
Browse files Browse the repository at this point in the history
…t cache
  • Loading branch information
kzangeli committed Dec 12, 2024
1 parent dbdc38a commit 8e9a1a3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 24 deletions.
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
36 changes: 20 additions & 16 deletions src/lib/orionld/mongoc/mongocContextCachePersist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,44 @@ extern "C"
//
// mongocContextCachePersist -
//
void mongocContextCachePersist(KjNode* contextObject)
void mongocContextCachePersist(KjNode* contextObject, bool reload)
{
bson_error_t error;

mongocConnectionGet(NULL, DbContexts);
sem_wait(&mongocContextsSem);

//
// If the context already exists, it will be REMOVED
// If the context is to be reloaded, it is REMOVED before inserted
//
KjNode* urlNodeP = kjLookup(contextObject, "url");
char* url = (urlNodeP != NULL)? urlNodeP->value.s : NULL;

if (url != NULL)
if (reload == true)
{
bson_t mongoFilter;
bson_error_t error;
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);
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));
// 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);

bson_error_t mcError;
bool r = mongoc_collection_insert_one(orionldState.mongoc.contextsP, &bson, NULL, NULL, &mcError);
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_
2 changes: 1 addition & 1 deletion src/lib/orionld/serviceRoutines/orionldDeleteContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bool orionldDeleteContext(void)
kjFree(oldContextP->tree);

// Update mongo with the new context
orionldContextCachePersist(contextP);
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

0 comments on commit 8e9a1a3

Please sign in to comment.