Skip to content

Commit

Permalink
FIX correctly identify changes in JSON metadata values in subscriptio…
Browse files Browse the repository at this point in the history
…n triggering logic
  • Loading branch information
fgalan committed Nov 8, 2023
1 parent 1520c7e commit 484babf
Show file tree
Hide file tree
Showing 8 changed files with 641 additions and 56 deletions.
69 changes: 15 additions & 54 deletions src/lib/mongoBackend/MongoCommonUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static bool hasMetadata(std::string name, std::string type, ContextAttribute* ca
*
* equalMetadataValues -
*/
static bool equalMetadataValues(const orion::BSONObj& md1, const orion::BSONObj& md2)
static bool equalMetadataItems(const orion::BSONObj& md1, const orion::BSONObj& md2)
{
bool md1TypeExist = md1.hasField(ENT_ATTRS_MD_TYPE);
bool md2TypeExist = md2.hasField(ENT_ATTRS_MD_TYPE);
Expand All @@ -147,58 +147,23 @@ static bool equalMetadataValues(const orion::BSONObj& md1, const orion::BSONObj&
return false;
}

// If type exists in both metadata elments, check if they are the same
// If type exists in both metadata elements, check if they are the same
if (md1TypeExist && md2TypeExist)
{
if (getFieldF(md1, ENT_ATTRS_MD_TYPE).type() != getFieldF(md2, ENT_ATTRS_MD_TYPE).type())
if ((getFieldF(md1, ENT_ATTRS_MD_TYPE).type() != orion::String))
{
LM_E(("Runtime Error (unallowed JSON type for metadata NGSI type: %d)", getFieldF(md1, ENT_ATTRS_MD_TYPE).type()));
return false;
}
switch (getFieldF(md1, ENT_ATTRS_MD_TYPE).type())
if ((getFieldF(md2, ENT_ATTRS_MD_TYPE).type() != orion::String))
{
/* FIXME #643 P6: metadata array/object are now supported, but we haven't
implemented yet the logic to compare compounds between them
case Object:
...
break;
case Array:
...
break;
*/

case orion::NumberDouble:
if (getNumberFieldF(md1, ENT_ATTRS_MD_TYPE) != getNumberFieldF(md2, ENT_ATTRS_MD_TYPE))
{
return false;
}
break;

case orion::Bool:
if (getBoolFieldF(md1, ENT_ATTRS_MD_TYPE) != getBoolFieldF(md2, ENT_ATTRS_MD_TYPE))
{
return false;
}
break;

case orion::String:
if (getStringFieldF(md1, ENT_ATTRS_MD_TYPE) != getStringFieldF(md2, ENT_ATTRS_MD_TYPE))
{
return false;
}
break;

case orion::jstNULL:
if (!getFieldF(md2, ENT_ATTRS_MD_TYPE).isNull())
{
return false;
}
break;
LM_E(("Runtime Error (unallowed JSON type for metadata NGSI type: %d)", getFieldF(md2, ENT_ATTRS_MD_TYPE).type()));
return false;
}

default:
LM_E(("Runtime Error (unknown JSON type for metadata NGSI type: %d)", getFieldF(md1, ENT_ATTRS_MD_TYPE).type()));
if (getStringFieldF(md1, ENT_ATTRS_MD_TYPE) != getStringFieldF(md2, ENT_ATTRS_MD_TYPE))
{
return false;
break;
}
}

Expand All @@ -210,15 +175,11 @@ static bool equalMetadataValues(const orion::BSONObj& md1, const orion::BSONObj&

switch (getFieldF(md1, ENT_ATTRS_MD_VALUE).type())
{
/* FIXME not yet
case orion::Object:
...
break;
return getObjectFieldF(md1, ENT_ATTRS_MD_VALUE).equal(getObjectFieldF(md2, ENT_ATTRS_MD_VALUE));

case orion::Array:
...
break;
*/
return getArrayFieldF(md1, ENT_ATTRS_MD_VALUE).equal(getArrayFieldF(md2, ENT_ATTRS_MD_VALUE));

case orion::NumberDouble:
return getNumberFieldF(md1, ENT_ATTRS_MD_VALUE) == getNumberFieldF(md2, ENT_ATTRS_MD_VALUE);
Expand Down Expand Up @@ -266,7 +227,7 @@ static bool equalMetadata(const orion::BSONObj& md1, const orion::BSONObj& md2)
orion::BSONObj md1Item = getObjectFieldF(md1, currentMd);
orion::BSONObj md2Item = getObjectFieldF(md2, currentMd);

if (!equalMetadataValues(md1Item, md2Item))
if (!equalMetadataItems(md1Item, md2Item))
{
return false;
}
Expand All @@ -281,7 +242,7 @@ static bool equalMetadata(const orion::BSONObj& md1, const orion::BSONObj& md2)
*
* changedAttr -
*/
static bool attrValueChanges(const orion::BSONObj& attr, ContextAttribute* caP, const bool& forcedUpdate, ApiVersion apiVersion)
static bool attrValueChanges(const orion::BSONObj& attr, ContextAttribute* caP, const bool& forcedUpdate)
{
/* Not finding the attribute field at MongoDB is considered as an implicit "" */
if (!attr.hasField(ENT_ATTRS_VALUE))
Expand Down Expand Up @@ -545,7 +506,7 @@ static ChangeType mergeAttrInfo
bool mdChanged;
if (caP->compoundValueP == NULL)
{
valueChanged = attrValueChanges(attr, caP, forcedUpdate, apiVersion);
valueChanged = attrValueChanges(attr, caP, forcedUpdate);
}
else
{
Expand Down
11 changes: 11 additions & 0 deletions src/lib/mongoDriver/BSONArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ std::string BSONArray::toString(void) const



/* ****************************************************************************
*
* BSONArray::equal -
*/
bool BSONArray::equal(const BSONArray& ba)
{
return (bson_compare(this->b, ba.b) == 0);
}



/* ****************************************************************************
*
* BSONArray::operator= -
Expand Down
1 change: 1 addition & 0 deletions src/lib/mongoDriver/BSONArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BSONArray
BSONArray(const BSONArray& _ba);
int nFields(void) const;
std::string toString(void) const;
bool equal(const BSONArray& ba);
BSONArray& operator= (const BSONArray& rhs);

// methods to be used only by mongoDriver/ code (with references to low-level driver code)
Expand Down
11 changes: 11 additions & 0 deletions src/lib/mongoDriver/BSONObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ void BSONObj::toElementsVector(std::vector<BSONElement>* v)



/* ****************************************************************************
*
* BSONObj::equal -
*/
bool BSONObj::equal(const BSONObj& bo)
{
return (bson_compare(this->b, bo.b) == 0);
}



/* ****************************************************************************
*
* BSONObj::operator= -
Expand Down
1 change: 1 addition & 0 deletions src/lib/mongoDriver/BSONObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class BSONObj
bool isEmpty(void) const;
void toStringMap(std::map<std::string, std::string>* m);
void toElementsVector(std::vector<BSONElement>* v);
bool equal(const BSONObj& bo);
BSONObj& operator= (const BSONObj& rhs);

// methods to be used only by mongoDriver/ code (with references to low-level driver code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh

--NAME--
Notify upon actual change in geo:json value
Right JSON change evaluation in subs (GeoJson case)

--SHELL-INIT--
dbInit CB
Expand Down
Loading

0 comments on commit 484babf

Please sign in to comment.