Document API select query FETCHPLAN ignored?
I have a problem using FETCHPLAN to preload document links as embedded documents.
Here is the test code:
ODatabaseDocumentTx db = new ODatabaseDocumentTx( "..." ).open( "admin", "admin" );
db.getMetadata().getSchema().dropClass( "A" );
db.getMetadata().getSchema().dropClass( "B" );
db.getMetadata().getSchema().createClass( "A" );
db.getMetadata().getSchema().createClass( "B" );
ODocument docA = new ODocument( "A" );
ODocument docB = new ODocument( "B" );
// A[ref] --link--> B
docB.field( "name", "value" );
docA.field( "ref", docB );
docA.save();
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>( "SELECT FROM A" );
query.setFetchPlan( "*:-1" ); // SELECT FROM A FETCHPLAN *:-1 works in Studio as intended
List<ODocument> queryResult =
//db.query( query );
db.command( query ).execute();
queryResult = queryResult.stream()
// pre-loading with load or reload and fetchplan does not work
//.map( doc -> doc.reload( "*:-1", true, true ) )
//.map( doc -> doc.load( "*:-1", true ) )
//.map( ODocument.class::cast )
// detaching before closing db does not work
//.peek( doc -> System.out.println( "detached " + doc.detach() ) )
.collect( Collectors.toList() );
// Result is: {"ref":"#17:0"}
// Expected result is: {"ref":{"name":"value"}}
db.close();
// THROWS: com.orientechnologies.orient.core.exception.ODatabaseException: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db);
queryResult.stream().map( doc -> doc.toJSON( "fetchPlan:*:-1" ) )
.forEach( System.out::println );
With the studio it works as intended
SELECT FROM A FETCHPLAN *:-1 yields
"result": [
{
"@type": "d",
"@rid": "#16:0",
"@version": 1,
"@class": "A",
"ref": {
"@type": "d",
"@rid": "#17:0",
"@version": 1,
"@class": "B",
"name": "value"
}
}
]
as opposed to
SELECT FROM A yields
"result": [
{
"@type": "d",
"@rid": "#16:0",
"@version": 1,
"@class": "A",
"ref": "#17:0",
"@fieldTypes": "ref=x"
}
My current workaround is a "detaching"-function wich replaces ORID fields with ORID#getRecord()
private static void detach( ODocument doc ) {
for( String name : doc.fieldNames() ) {
Object field = doc.field( name );
if( field instanceof ORID ) {
ODocument embDoc = ( (ORID) field ).getRecord();
detach( embDoc );
doc.field( name, embDoc );
}
}
}
AFAIK the fetch plan does not "hydrate" the objects directly. It puts the fetched records into local cache instead. Then, when you're iterating over the structures holding referenced records, such as LinkList, LinkBag etc, the iterator is implemented in a way that it asks the storage for the record based on RID (basically the same way you do in your detach helper) and the storage looks into local cache before actually fetching the record. (There is also rawIterator() which does not do this conversion) Also there is shortcut to do this RID to ODocument (or vice versa) conversion, methods: convertLinks2Records() convertRecords2Links()
As for the detaching, I don't know about Document API, but Graph API documentation states this:
Does detach go recursively to detach all connected elements? No, it works only at the current element level.
(I quess the the situation will be the same, because Graph API is just a wrapper over Document API)
You can also observe that ODocument.toJSON() never returns linked records, but RIDs (even though they might be fetched with fetch plan)
However, I believe that fetch plan doesn not work in remote mode whatsoever, check this: https://groups.google.com/forum/#!searchin/orient-database/fetch$20plan$20actually/orient-database/cfvYY853JMA/mesNeXOiBQAJ
Hi urugator, thanks for your input.
convertLinks2Records belongs to the ORecordLazyMultiValue interface wich is not implemented by ODocument (it uses the java.lang.Interable interface).
But if pre-fetching is not like I assumed it to work then those methods surely will come in handy in the future.
You can also observe that ODocument.toJSON() never returns linked records, but RIDs (even though they might be fetched with fetch plan)
That's because the default ODocument#toJSON() uses the format option fetchPlan:*:0 wich fetches with zero depth and I'm using fetchPlan:*:-1 in my test.
Yea, I was refering to structures referencing multiple records. In your case of single record reference, the conversion is straithforward via getRecord(). However convert/expand all fields on Document is not possible, it wouldn't even make much sence I quess...
Are you sure you can pass a fetch plan to ODocument#toJSON()? I know you can do that in query, but it's not documented in case of this method:
iFormat - Format settings separated by comma. Available settings are:
rid: exports the record's id as property "@rid"
version: exports the record's version as property "@version"
class: exports the record's class as property "@class"
attribSameRow: exports all the record attributes in the same row
indent:
I'm pretty sure (you can test it by removing the argument). It's just not documented.
But take a look at the last element from the string in the default default wrapper for ODocumen#toJSON
https://github.com/orientechnologies/orientdb/blob/master/core/src/main/java/com/orientechnologies/orient/core/record/ORecordAbstract.java#L188
wich is handled by the ORecordSerializerJSON at https://github.com/orientechnologies/orientdb/blob/master/core/src/main/java/com/orientechnologies/orient/core/serialization/serializer/record/string/ORecordSerializerJSON.java#L134
Cool, good to know
hi @Vannav,
The issue here i think is that using streaming you are jumping to another thread that does not have the associated the database, if you do a normal for with a toJSON everything will work, we are going to solve some of this thread problem in orientdb 3.0