Skip to content

Commit

Permalink
Merge pull request #1658 from FIWARE/orderBy/id+type
Browse files Browse the repository at this point in the history
More features for orderBy
  • Loading branch information
kzangeli authored Aug 26, 2024
2 parents f5762db + 12409f9 commit 41204d9
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Fixed Issues:

## New Features:
* Distributed subscriptions: subordinate subscriptions are DELETED when their "father" is deleted
* Distributed subscriptions: subordinate subscriptions are deleted when their "father" is deleted
* Support for the URL parameter 'csf' in GET /ngsi-ld/v1/csourceRegistrations
* Support for the URL parameter 'orderBy' (must be an attribute) in GET //ngsi-ld/v1/entities, but only if 'local' is set. (This is not NGSI-LD standard. Yet ...)
- Also supporting sorting on entity id, type, and modifiedAt
- One more URL parameter '?reverse=true' to reverse the sorting order

## Notes
1 change: 1 addition & 0 deletions src/lib/orionld/common/orionldState.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ typedef struct OrionldUriParams
char* notExists;
char* metadata;
char* orderBy;
bool reverse;
bool collapse;
bool reset;
char* attributeFormat;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/orionld/mhd/mhdConnectionInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,16 @@ MHD_Result orionldUriArgumentGet(void* cbDataP, MHD_ValueKind kind, const char*
{
orionldState.uriParams.orderBy = (char*) value;
}
else if (strcmp(key, "reverse") == 0)
{
if (strcmp(value, "true") == 0)
orionldState.uriParams.reverse = true;
else if (strcmp(key, "false") != 0)
{
orionldError(OrionldBadRequestData, "Invalid value for uri parameter /reverse/", value, 400);
return MHD_YES;
}
}
else if (strcmp(key, "collapse") == 0)
{
if (strcmp(value, "true") == 0)
Expand Down
40 changes: 28 additions & 12 deletions src/lib/orionld/mongoc/mongocEntitiesQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,30 +695,46 @@ KjNode* mongocEntitiesQuery
bson_t sortDoc;
int limit = orionldState.uriParams.limit;
int offset = orionldState.uriParams.offset;
int sortOrder = 1;

if (orionldState.uriParams.reverse == true)
sortOrder = -1;

bson_init(&sortDoc);

if (orderBy == NULL)
bson_append_int32(&sortDoc, "creDate", 7, 1);
if ((orderBy == NULL) || (strcmp(orderBy, "createdAt") == 0))
bson_append_int32(&sortDoc, "creDate", 7, sortOrder);
else
{
char* longName = orionldAttributeExpand(orionldState.contextP, orderBy, true, NULL);
int len = strlen(longName) + 14;
char* path = kaAlloc(&orionldState.kalloc, len);
char pathV[32];
char* path = pathV;
int len = sizeof(pathV);

if ((strcmp(orderBy, "id") == 0) || (strcmp(orderBy, "type") == 0))
len = snprintf(path, len, "_id.%s", orderBy);
else if (strcmp(orderBy, "modifiedAt") == 0)
len = snprintf(path, len, "%s", "modDate");
else
{
char* longName = orionldAttributeExpand(orionldState.contextP, orderBy, true, NULL);

len = snprintf(path, len - 1, "attrs.%s.value", longName);
len = strlen(longName) + 14;
path = kaAlloc(&orionldState.kalloc, len);

// Replacing dots for '=' - including the last one (should not be replaced)
dotForEq(&path[6]);
len = snprintf(path, len - 1, "attrs.%s.value", longName);

// Reversing the '=' of ".value" to a dot
path[len - 6] = '.';
// Replacing dots for '=' - including the last one (should not be replaced)
dotForEq(&path[6]);

// Reversing the '=' of ".value" to a dot
path[len - 6] = '.';
}

LM_T(LmtMongoc, ("Ordering by '%s' (%d letters)", path, len));
bson_append_int32(&sortDoc, path, len, 1);
bson_append_int32(&sortDoc, path, len, sortOrder);
}

bson_append_int32(&sortDoc, "_id.id", 6, 1);
bson_append_int32(&sortDoc, "_id.id", 6, sortOrder); // Secondary sort on Entity ID
bson_append_document(&options, "sort", 4, &sortDoc);
bson_destroy(&sortDoc);

Expand Down
1 change: 1 addition & 0 deletions src/lib/orionld/service/orionldServiceInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
serviceP->uriParams |= ORIONLD_URIPARAM_ONLYIDS;
serviceP->uriParams |= ORIONLD_URIPARAM_ENTITYMAP;
serviceP->uriParams |= ORIONLD_URIPARAM_ORDERBY;
serviceP->uriParams |= ORIONLD_URIPARAM_REVERSE;
}
else if (serviceP->serviceRoutine == orionldGetEntity)
{
Expand Down
1 change: 1 addition & 0 deletions src/lib/orionld/types/OrionLdRestService.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ typedef struct OrionLdRestServiceSimplifiedVector
#define ORIONLD_URIPARAM_EXPAND_VALUES (UINT64_C(1) << 41)
#define ORIONLD_URIPARAM_KIND (UINT64_C(1) << 42)
#define ORIONLD_URIPARAM_ORDERBY (UINT64_C(1) << 43)
#define ORIONLD_URIPARAM_REVERSE (UINT64_C(1) << 44)



Expand Down
Loading

0 comments on commit 41204d9

Please sign in to comment.