Re: [casbah] Bson Binary

267 views
Skip to first unread message

Brendan W. McAdams

unread,
Mar 16, 2011, 5:14:43 PM3/16/11
to mongodb-ca...@googlegroups.com
***NOTE*** - This feature needs to be documented well, and I haven't had time.  Anyone want to volunteer to writeup docs?

Casbah itself currently has no code in place to convert anything to Binary.
However, the Java driver likely does.

It is possible to register Conversion helpers with the Driver, and Casbah provides even more Scalaey syntax using pattern matching and mixin traits.

There are two types of Conversion Helpers - Encoding Hooks and Decoding Hooks.

In your case you'd want to write an Encoding Hook which specifically controls the behavior of Array[Byte] as it is written to MongoDB.

KEEP IN MIND - The Encoding Hook *MUST* Return a type the Java Driver understands.  E.G. the Joda Time helpers encoding hook returns a JDK Date as we know how to turn that into BSON.

Look at:




For details on how I currently do it.

Writing your own Helper, you might do this:

import org.bson.{ BSON, Transformer }
import org.bson.types.Binary

trait CustomBinaryDataSerializer extends MongoConversionHelper {

private val transformer = new Transformer {
 // Invoked by the Java driver whenever it encounters a type that has a registered encoding hook
def transform(o: AnyRef): AnyRef = o match {
// I think there's a potential problem here WRT type erasure...
case binary: Array[Byte] => new Binary(0, binary)
case _ => o
}
}

// Loads your hook into MongoDB
override def register() = {
BSON.addEncodingHook(classOf[Array[Byte]], transformer)
super.register() // <-- this is important if you want to setup a chain of transformers in one declaration
}

override def unregister() = {
BSON.removeEncodingHooks(classOf[[Array[Byte]])
super.unregister()
}  
}

object CustomBinarySupport extends CustomBinaryDataSerializer {
def apply() = {
super.register()
}
}

Then just run CustomBinarySupport() somewhere in your code to register the new encoder.

You might also venture so far as to add a custom decoder using the Decoding hooks, which would try to be clever in the same way; I'm not sure if you can hook into get the binary type from the driver though (i'll look into it, that'd be a useful feature with the hooks).

On Wednesday, March 16, 2011 at 3:10 PM, Benjamin Darfler wrote:

We are storing some byte arrays in Mongo using Casbah. We are very
data size conscious so we are using Bson Binary type 0 instead of the
default type 2 that Casbah uses. I'm currently doing a map over our
DbObject and converting Array[Byte] to new Binary(0, Array[Byte]) but
I have to assume there is an Implicit somewhere that is doing the
conversion to new Binary(2, Array[Byte]). I wonder if there is an easy
way to override this or re implement it such that I get all the lovely
Implicit magic and don't need to iterate over all my objects before
saving them.

Benjamin Darfler

unread,
Mar 16, 2011, 3:10:49 PM3/16/11
to mongodb-casbah-users

Benjamin Darfler

unread,
Mar 16, 2011, 9:47:08 PM3/16/11
to mongodb-casbah-users
Cool but I'm a bit overwhlemed by it all. For mow I'm just doing a map
on the db object and replacing Array[Byte] with new Binary(0,
Array[Byte]). Its more readily understandable.

On Mar 16, 5:14 pm, "Brendan W. McAdams" <bren...@10gen.com> wrote:
> ***NOTE*** - This feature needs to be documented well, and I haven't had time. Anyone want to volunteer to writeup docs?
>
> Casbah itself currently has no code in place to convert anything to Binary.
> However, the Java driver likely does.
>
> It is possible to register Conversion helpers with the Driver, and Casbah provides even more Scalaey syntax using pattern matching and mixin traits.
>
> There are two types of Conversion Helpers - Encoding Hooks and Decoding Hooks.
>
> In your case you'd want to write an Encoding Hook which specifically controls the behavior of Array[Byte] as it is written to MongoDB.
>
> KEEP IN MIND - The Encoding Hook *MUST* Return a type the Java Driver understands. E.G. the Joda Time helpers encoding hook returns a JDK Date as we know how to turn that into BSON.
>
> Look at:
>
> https://github.com/mongodb/casbah/tree/master/casbah-commons/src/main...
>
> https://github.com/mongodb/casbah/blob/master/casbah-commons/src/main...(Base Traits / Objects for build your own)
>
> https://github.com/mongodb/casbah/blob/master/casbah-commons/src/main...(Current shipping Implementations)
Reply all
Reply to author
Forward
0 new messages