Skip to content

Commit

Permalink
#1707: Support for local=true in the type discovery endpoints (GET /n…
Browse files Browse the repository at this point in the history
…gsi-ld/v1/types[/{typeName}]
  • Loading branch information
kzangeli committed Dec 13, 2024
2 parents b8d51a9 + 51f2004 commit 887aa7b
Show file tree
Hide file tree
Showing 14 changed files with 655 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Fixed Issues:
#XXXX: Complex @contexts of subscriptions weren't persisted in mongodb
#1707: Support for local=true in the type discovery endpoints (GET /ngsi-ld/v1/types[/{typeName}]
#1708: Reloaded hosted contexts were not persisted in mongo, only the context cache

## New Features:
* Support for ...
Expand Down
8 changes: 8 additions & 0 deletions src/lib/logMsg/traceLevels.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ typedef enum TraceLevels
LmtContextDownload, // Context Download
LmtCoreContext, // Core Context
LmtUserContext, // User Context
LmtContextItem, // Item in context cache
LmtExpand, // JSON-LD Expansions
LmtCompact, // JSON-LD Compactions

// GeoJSON
LmtGeoJSON = 110, // GeoJSON ... everything (for now)
Expand All @@ -131,6 +134,11 @@ typedef enum TraceLevels
//
LmtSubordinate = 140,

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

//
// Misc
//
Expand Down
6 changes: 4 additions & 2 deletions src/lib/orionld/context/orionldAttributeExpand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <string.h> // strcmp

#include "logMsg/logMsg.h" // LM_*
#include "logMsg/traceLevels.h" // Lmt*

#include "orionld/context/orionldContextItemAlreadyExpanded.h" // orionldContextItemAlreadyExpanded
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
Expand Down Expand Up @@ -67,7 +66,7 @@ char* orionldAttributeExpand
// The implementation needs modifications.
//
// Wait ... what if the @context says it must be a String, a DateTime?
// The @context doesn't distinguish between attributesd and sub-attributes ...
// The @context doesn't distinguish between attributes and sub-attributes ...
// It will need to be a String wherever it is.
//
// We should probably forbid an attribute to have the name 'observedAt'
Expand All @@ -80,7 +79,10 @@ char* orionldAttributeExpand
#endif

if (orionldContextItemAlreadyExpanded(sName) == true)
{
LM_T(LmtExpand, ("Already Expanded: '%s'", sName));
return sName;
}

return orionldContextItemExpand(contextP, sName, useDefaultUrlIfNotFound, contextItemPP);
}
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
3 changes: 3 additions & 0 deletions src/lib/orionld/context/orionldContextHashTablesFill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ bool orionldContextHashTablesFill(OrionldContext* contextP, KjNode* keyValueTree
return false;
}

LM_T(LmtContextItem, ("Adding '%s' -> '%s' to hash table for context '%s' (step 1)", hiP->name, hiP->id, contextP->url));
khashItemAdd(nameHashTableP, hiP->name, hiP);
}

Expand All @@ -123,6 +124,8 @@ bool orionldContextHashTablesFill(OrionldContext* contextP, KjNode* keyValueTree
hashItemP->id = kaStrdup(&kalloc, hashItemP->id);
khashItemAdd(valueHashTableP, hashItemP->id, hashItemP);

LM_T(LmtContextItem, ("Fixed '%s' -> '%s' in hash table for context '%s' (step 2)", hashItemP->name, hashItemP->id, contextP->url));

itemP = itemP->next;
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/lib/orionld/context/orionldContextItemExpand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <string.h> // strchr

#include "logMsg/logMsg.h" // LM_*
#include "logMsg/traceLevels.h" // Lmt*

#include "orionld/types/OrionldContextItem.h" // OrionldContextItem
#include "orionld/types/OrionldContext.h" // OrionldContext
Expand Down Expand Up @@ -72,7 +71,11 @@ char* orionldContextItemExpand
contextP = orionldCoreContextP;

if ((colonP = strchr((char*) shortName, ':')) != NULL)
return orionldContextPrefixExpand(contextP, shortName, colonP);
{
char* longName = orionldContextPrefixExpand(contextP, shortName, colonP);
LM_T(LmtExpand, ("Prefix-Expanded '%s' to '%s'", shortName, longName));
return longName;
}

// 1. Lookup in Core Context
if (orionldCoreContextP != NULL)
Expand All @@ -98,17 +101,21 @@ char* orionldContextItemExpand

orionldCoreContextP->expansions += 1; // Really, @vocab expansions of the core context

LM_T(LmtExpand, ("Vocab-Expanded '%s' to '%s'", shortName, longName));
return longName;
}

LM_T(LmtExpand, ("No Expansion found for '%s'", shortName));
return NULL;
}

// 4. Save the pointer to the context item
if (contextItemPP != NULL)
*contextItemPP = contextItemP;

contextP->expansions += 1; // Really, @vocab expansions of the core context
contextP->expansions += 1;

LM_T(LmtExpand, ("Expanded '%s' to '%s'", shortName, contextItemP->id));

return contextItemP->id;
}
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
Loading

0 comments on commit 887aa7b

Please sign in to comment.