Extend the fetchplan format parameter of the ODocument.toJSON()
Hi, how discussed in the forum ( http://bit.ly/14dyUHG ), it would useful to have a new value to set in the fetchplan to avoid a field creation in the JSON string. It would also be useful if this value can be propagated to all the linked documents traversed by the toJSON() method
Hi, this issue is still open, the -2 value to exclude the unwanted fields does not work as expected.
Here is my analysis: The patch proposed, i.e. to set -2 as the value to skip a field, works only if the field is on the first level.
My case: I have a schema like this:
user orole rules
Profile --------> OUser ---------> ORole --------> Rules
What I would to achieve is a json of a Profile's record which has:
- All the Profile's fields
- Just the OUser name field
- Just the ORole name for each role that the user belongs to.
- NO Rules.
So I wrote a sql statement like this:
select @this.tojson("fetchPlan:user.name:1 user.password:-2 user.roles:1 user.roles.rules:-2") from Profile
The result is:
{
"user":{
"name":"user2d569e6f-d5b3-4477-ace6-d2e18f789c26",
"status":"ACTIVE",
"roles":[{
"name":"registereduser",
"inheritedRole":null,
"mode":0,
"rules":{"database":2,"database.schema":7,"database.cluster.internal":2,"database.cluster.orole":2,"database.cluster.ouser":2,"database.class.*":15,"database.cluster
*":15,"database.command":15,"database.hook.record":15},
"@fieldTypes":"mode=b"
}]
}
}
The OUser.password field is skipped as expected, the user.roles.rules on the contrary are extracted. Inspecting the code I think the problem is in the OFetchHelper class. Here there are three methods:
- getDepthLevel()
- processRecordRidMap()
- processRecord()
I consider only processRecord() for the moment. In this method there is the instructions
if (iFieldDepthLevel > -1)
depthLevel = iFieldDepthLevel;
else
depthLevel = getDepthLevel(iFetchPlan, fieldPath);
if (depthLevel == -2)
continue;
But depthLevel is evaluated only if iFieldDepthLevel is equals to -1, i.e. only at the first level. So for nested objects depthLevel is never inferred from the fetchPlan. I thought to rewrite this code like the following:
int depthLevel;
depthLevel = getDepthLevel(iFetchPlan, fieldPath);
if (depthLevel == -2)
continue;
if (iFieldDepthLevel > -1)
depthLevel = iFieldDepthLevel;
But I have two more problems:
- possible performance degradation since in this way getDepthLevel() is called more time than in the first version
- the getDepthLevel() has another issue inside it. Let's me explain. In getDepthLevel() the code is:
for (String fieldFetchDefinition : iFetchPlan.keySet()) {
if (iFieldPath.startsWith(fieldFetchDefinition)) {
// GET THE FETCH PLAN FOR THE GENERIC FIELD IF SPECIFIED
depthLevel = iFetchPlan.get(fieldFetchDefinition);
break;
} else if (fieldFetchDefinition.startsWith(iFieldPath)) {
// SETS THE FETCH LEVEL TO 2 (LOADS ALL DOCUMENT FIELDS)
depthLevel = 1;
break;
}
}
Please note the first "if" statement: if (iFieldPath.startsWith(fieldFetchDefinition)) this means that if the fetchPlan contains both rules: user.roles:1 user.roles.rules:-2 when the second one is evaluated, since it starts with the first one, the wrong depthLevel is returned. I rearranged the test like:
for (String fieldFetchDefinition : iFetchPlan.keySet()) {
if (iFieldPath.equals(fieldFetchDefinition)) {
// GET THE FETCH PLAN FOR THE GENERIC FIELD IF SPECIFIED
depthLevel = iFetchPlan.get(fieldFetchDefinition);
break;
} else if (fieldFetchDefinition.startsWith(iFieldPath)) {
// SETS THE FETCH LEVEL TO 2 (LOADS ALL DOCUMENT FIELDS)
depthLevel = 1;
break;
}
}
changing the startsWith() with the equalsTo() method. But I don't know if there are aftermath. Furthermore I don't understand the second "if" statement.
Not relevant anymore, in v3.0 fetchplan are deprecated in favor of new SELECT extended projections