[JAVA-65] BasicDBObject.entrySet().iterator does not produce values in the same order as BasicDBObject.keySet() Created: 18/Dec/09  Updated: 13/Jan/10  Resolved: 18/Dec/09

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 1.1
Fix Version/s: 1.2

Type: Bug Priority: Minor - P4
Reporter: Keith Branton Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Attachments: Text File BasicDBObject.java     Text File DBObjectTest.java    

 Description   

BasicDBObject preserves insertion order of keys - and really needs to - so keySet() is returned in insertion order, BUT it does not provide this functionality for entrySet() - which I generally use a lot more often than keySet() when iterating through maps.

Digging into the code I see BasicDBObject is a HashMap combined with an OrderedSet was used to maintain insertion key order, but entrySet is not overridden.

I'm guessing the original author was unaware of java.util.LinkedHashMap. I changed BasicDBObject to extend LinkedHashMap, and removed _keys. All unit tests pass. I'm seeing pretty significant speed improvements in some benchmarks I put together to compare implementations. And the code is a little simpler too.

I have attached a modified copy of BasicDBObject for consideration, as well as a copy of DBObjectTest that includes a new test - testEntrySetOrder that asserts that the entrySet and keySet are both maintained in insertion order.



 Comments   
Comment by Eliot Horowitz (Inactive) [ 18/Dec/09 ]

Thanks - merged this in.
Only generally used LinkedHashMap for caches, so didn't think of it for this use case.

Comment by auto [ 18/Dec/09 ]

Author:

{'name': 'Eliot Horowitz', 'email': 'eliot@10gen.com'}

Message: use LinkedHashMap for BasicDBObject since it maintains insertion order JAVA-65
http://github.com/mongodb/mongo-java-driver/commit/729fc494e5305d362eea95b01c381f2a7daf2652

Generated at Thu Feb 08 08:51:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.