FAQ
Hi !


I managed to get through this by modifying the encoder and decoder in the
java driver's source but I'd preffer relying on the official version rather
than hacking my own.

What I did was modify *BasicBSONEncoder *:

protected void _putObjectField( String name , Object val ){
...

}else if (val instanceof DBRefBase) {
DBRefBase valc = (DBRefBase)val;
BSONObject temp = new BasicBSONObject();
temp.put("$ref", valc.getRef());
temp.put("$id", valc.getId());
if(valc.getDB()!=null)temp.put("$db", valc.getDB().getName());
putObject( name, temp );
}
....
}


and *BasicBSONDecoder *as follow :


boolean decodeElement()
throws IOException {

....
case REF:

_in.readInt();

_callback.objectStart( name );
while ( decodeElement() );
_callback.objectDone();

break;

....
}



I also modified *DefaultDBEncoder *as follow

protected void putDBRef( String name, DBRefBase ref ){

_put( OBJECT , name );
final int sizePos = _buf.getPosition();
_buf.writeInt( 0 );

_putObjectField( "$ref" , ref.getRef() );
_putObjectField( "$id" , ref.getId() );


_putObjectField( "$db" , ref.getDB().getName() ); //added by AZ


_buf.write( EOO );
_buf.writeInt( sizePos , _buf.getPosition() - sizePos );
}




then *DefaultDBCallback*
:


@Override
public Object objectDone(){
BSONObject o = (BSONObject)super.objectDone();
String lastName = null;
if ( _nameStack.size() > 0 ){
lastName = _nameStack.removeLast();
}
if ( ! ( o instanceof List ) && lastName != null &&
o.containsField( "$ref" ) &&
o.containsField( "$id" ) ){

//special cross db rf case (added by AZ)
if(o.containsField("$db") && !o.get("$db").equals(_db.getName())){
DB sis = _db.getSisterDB((String) o.get("$db"));
return cur().put(lastName, new DBRef( sis, o ) );
}

return cur().put(lastName, new DBRef( _db, o ) );
}

return o;
}




modified
*JSONSerializers.DBRefBaseSerializer* as follow :


private static class DBRefBaseSerializer extends CompoundObjectSerializer
{

DBRefBaseSerializer(ObjectSerializer serializer) {
super(serializer);
}

@Override
public void serialize(Object obj, StringBuilder buf) {
DBRefBase ref = (DBRefBase) obj;
BasicDBObject temp = new BasicDBObject();
temp.put("$ref", ref.getRef());
temp.put("$id", ref.getId());
if(ref.getDB()!=null)temp.put("$db", ref.getDB().getName());
serializer.serialize(temp, buf);
}

}



and finally *DBRefBase*


@Override
public String toString(){

//added by AZ for cross DB references
String dbPart = "";
if(_db!=null){
dbPart = ", \"$db\" : \"" + _db.getName() + "\"";
}

return "{ \"$ref\" : \"" + _ns + "\", \"$id\" : \"" + _id +
"\""+dbPart+" }";
}



I think thats all.
My DBRefs now have a propper $db field in the database, and also when
fetched back.
I can do cross db references serializing and deserializing with my modified
version of Morphia.

However I believe that DBRefs are now treated as regular DBObjects when it
comes do deserialization ... don't know if that's a big problem.


I'm however not sure how these modifications could affect other parts of
the framework ...

Could anyone tell me if there was other thing to consider and if there is
any chance to see the $db field being supported by the official driver ?


thank you for your feedback !

Cheers,

Alex





--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user+unsubscribe@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 2 | next ›
Discussion Overview
groupmongodb-user @
categoriesmongodb
postedMar 3, '13 at 7:56a
activeMar 9, '13 at 12:00a
posts2
users1
websitemongodb.org
irc#mongodb

1 user in discussion

Azlist1: 2 posts

People

Translate

site design / logo © 2021 Grokbase