0

The MongoDB Java driver documentation of the packet org.bson mentions various "Lazy" versions of other classes. Unfortunately the Javadocs of these classes can barely be called documentation.

What is their purpose and how does their behavior differ from the normal versions?

5
  • 1
    To me, the term "Java API" has specific meaning, as in: the classes included with Java Standard Edition. The classes you are talking about are not part of the Java API. Commented Feb 1, 2013 at 12:03
  • 1
    @MarkRotteveel Thank you for your feedback. I added MongoDB to the question title to make it more obvious that I am talking about the MongoDB API, not the standard Java classes Commented Feb 1, 2013 at 12:05
  • @fork I know what lazy loading means. I want to know how to use these undocumented classes from the MongoDB API. Commented Feb 1, 2013 at 12:31
  • @fork: It seems you were wrong after all. They don't have anything to do with lazy loading. See the answer by jyemin. Commented Feb 2, 2013 at 16:45
  • @Philipp im glad somebody proved me wrong :) Commented Feb 2, 2013 at 18:23

1 Answer 1

2

Under normal operation, the driver creates and consumes documents using the DBObject Map-like interface. When inserting documents, it iterates over the map to convert it the corresponding BSON representation. When querying, it creates new documents by putting key-value pairs into the map.

But there are times when you want to work with raw BSON and not pay the cost of all this serialization and deserialization. That's what the lazy DBObject implementations are for. Instead of treating them as a map, a custom encoder instead writes the bytes directly to the BSON stream. Similarly, a custom decoder writes the raw bytes directly into the lazy DBObject.

In this context, the meaning of the term lazy is that, since the lazy equivalents still have to implement the DBObject interface, they do so by "lazily" interpreting the raw BSON byte array that they contain.

One last note: the lazy DBObject classes are very likely not going to be included in the upcoming 3.0 release of the driver, as the entire serialization is changing in a way that is not compatibile with lazy DBObjects. There will be equivalent functionality, but not API compatible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.