[JAVA-215] Java driver's ensureIndex() does not preserve order Created: 12/Nov/10 Updated: 22/Dec/10 Resolved: 13/Nov/10 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | API |
| Affects Version/s: | None |
| Fix Version/s: | 2.4 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Vijay Pandurangan | Assignee: | Eliot Horowitz (Inactive) |
| Resolution: | Cannot Reproduce | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Java |
||
| Backwards Compatibility: | Major Change |
| Description |
|
From what I understand, the order of the keys in an index is important, since mongo can use any index that has additional keys after the keys required for a query. Thus index(A,B,C) is different from index(C,B,A) 1. ensureIndex() takes a BasicDBObject as an argument. public static String genIndexName( DBObject keys ){ return name; 3. Generates a name for a DBObject. When there are multiple keys, "key1": 1, "key2":2, keyset() will return the data in arbitrary order. The name will then be the same for each SET of keys. final String name = options.get( "name" ).toString(); if ( _createIndexes.contains( name ) ) Will fail, if any other index with the same keys exist, regardless of order. 5. Even if that does not fail, and a name is manually supplied, createIndex's comment implies that it uses a set of fields, which will not preserve order /** Forces creation of an index on a set of fields, if one does not already exist.
6. Mongo behaves correctly when adding indices from the console. |
| Comments |
| Comment by Vijay Pandurangan [ 26/Nov/10 ] |
|
Hi Eliot, I have attempted to recreate this, but it turns out the indexes were Vijay |
| Comment by Eliot Horowitz (Inactive) [ 26/Nov/10 ] |
|
@miguel Not sure I understand what the issue is? |
| Comment by Miguel Abraham Manzanilla [ 26/Nov/10 ] |
|
I think the problem with this way of index is that the map with keys and numbers (ie "key1": 1, "key2":2, ...) is wrong. If it is like the one on console, the number after the key means ascending order when 1 and descending order when -1. So i think any non negative would be used as ascending order. Just an opinion BTW. |
| Comment by auto [ 13/Nov/10 ] |
|
Author: {'login': 'erh', 'name': 'Eliot Horowitz', 'email': 'eliot@10gen.com'}Message: more tests for |
| Comment by Eliot Horowitz (Inactive) [ 13/Nov/10 ] |
|
added more tests - but the code seems correct by all accounts |
| Comment by Scott Hernandez (Inactive) [ 13/Nov/10 ] |
|
BasicDBObject extends BasicBSONObject which extends LinkedHahMap which preserves the order of the inserts. There are many tests which depend on this, and prove it. The name is generated correctly based on the order. Do you have a test to show a problem? Take a look at DBCollectionTest.testGenIndexName() for an example of the tests being done. |
| Comment by Vijay Pandurangan [ 12/Nov/10 ] |
|
Note that fixing this could have really bad implications for old code that is performant because the keys were generated in a specific order (!!) |