[JAVA-75] Generated ObjectID not unique Created: 06/Jan/10  Updated: 13/Jan/10  Resolved: 06/Jan/10

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

Type: Bug Priority: Major - P3
Reporter: Max Dessì Assignee: Eliot Horowitz (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MacOSX 10.6.2 JDK 1.6



 Description   

In a test class the following code

List<DBObject> users = new ArrayList<DBObject>();
users.add(new BasicDBObject(userOne.toMap()));
users.add(new BasicDBObject(userTwo.toMap()));
users.add(new BasicDBObject(userThree.toMap()));
repo.getCollection().insert(users);

produce the same ObjectID for each userXXX inserted

I see in the driver the "algorithm":
private static int _nextInc = (new java.util.Random()).nextInt();
private static int _gentime = _flip( (int)(System.currentTimeMillis()/1000) );

Suggestion:
With an "algorithm" like this :

public static String calculateUniqueId() {
long number = System.currentTimeMillis() * 1000 + (long) (Math.random() * 1000);
return Long.toString(number, 25);
}

I have ids different for each userXXX in the same test where ObjectID fails



 Comments   
Comment by Max Dessì [ 07/Jan/10 ]

I don't have sharp eye like you
Thanks a lot.
Sorry for the noise.

Comment by Kristina Chodorow (Inactive) [ 07/Jan/10 ]

Here are your ids (spaces added for emphasis):

4b4599fe45d2ba538 9 58a21c
4b4599fe45d2ba538 a 58a21c
4b4599fe45d2ba538 b 58a21c

Comment by Max Dessì [ 07/Jan/10 ]

No, the second example is more explicit but the logic it's the same:

mapOne => userOne.toMap()
mapTwo => userTwo.toMap()
mapThree => userThree.toMap()

repo.getCollection().insert(users) => datastore.getDB().getCollection("usersTest").insert(users)

I have the same problem if replace the list insert with three DBObject insert:

datastore.getDB().getCollection("usersTest").insert(new BasicDBObject(one));
datastore.getDB().getCollection("usersTest").insert(new BasicDBObject(two));
datastore.getDB().getCollection("usersTest").insert(new BasicDBObject(three));

How can obtain dirfferent ids ?
What's wrong ?
Thanks

Comment by Eliot Horowitz (Inactive) [ 07/Jan/10 ]

Those are differnt.

Comment by Max Dessì [ 07/Jan/10 ]

Eliot, this is example code:

@Test
public void getInsertCollection() {
Map mapOne = new HashMap();
mapOne.put("ZipCode","05125");
mapOne.put("City","Modena");
mapOne.put("Country","Italy");
mapOne.put("Province","MO");
mapOne.put("Region","Emilia Romagna");
mapOne.put("Street","V.le Sidney Sonnino 133");
mapOne.put("DateOfBirth",new Date());
mapOne.put("Email","jack.folla@yahoo.it");
mapOne.put("Name","Jack");
mapOne.put("Surname","Folla");
mapOne.put("SensibleData","no sensible data");
mapOne.put("AcceptSms",true);
mapOne.put("Mobile","33912345563");
mapOne.put("TelephoneWork","0709876543");
mapOne.put("CenterId","centerThree");
mapOne.put("Description","descrThree");
mapOne.put("GroupId","groupThree");
BasicDBObject one = new BasicDBObject(mapOne);

Map mapTwo = new HashMap();
mapTwo.put("ZipCode","09129");
mapTwo.put("City","Cagliari");
mapTwo.put("Country","Italy");
mapTwo.put("Province","OR");
mapTwo.put("Region","Sardegna");
mapTwo.put("Street","V.le Sidney Sonnino 4");
mapTwo.put("DateOfBirth",new Date());
mapTwo.put("Email","jack.folla@gmail.com");
mapTwo.put("Name","Jim");
mapTwo.put("Surname","Finn");
mapTwo.put("SensibleData","no sensible data");
mapTwo.put("AcceptSms",true);
mapTwo.put("Mobile","3391234556");
mapTwo.put("TelephoneWork","070987654");
mapTwo.put("CenterId","centerFour");
mapTwo.put("Description","descr");
mapTwo.put("GroupId","groupFour");
BasicDBObject two = new BasicDBObject(mapTwo);

Map mapThree = new HashMap();
mapThree.put("ZipCode","09100");
mapThree.put("City","Cagliari");
mapThree.put("Country","Campidano");
mapThree.put("Province","CA");
mapThree.put("Region","Sardegna");
mapThree.put("Street","V.le Europe 4");
mapThree.put("DateOfBirth",new Date());
mapThree.put("Email","max@gmail.com");
mapThree.put("Name","Paperino");
mapThree.put("Surname","Paolino");
mapThree.put("SensibleData","no sensible data");
mapThree.put("AcceptSms",true);
mapThree.put("Mobile","3391234557");
mapThree.put("TelephoneWork","070987654");
mapThree.put("CenterId","centerFive");
mapThree.put("Description","descr");
mapThree.put("GroupId","groupFive");
BasicDBObject three = new BasicDBObject(mapThree);

List<DBObject> users = new ArrayList<DBObject>();
users.add(new BasicDBObject(one));
users.add(new BasicDBObject(two));
users.add(new BasicDBObject(three));

datastore.getDB().getCollection("usersTest").insert(users);

DBCursor cursor = getDB().getCollection("usersTest").find();
while(cursor.hasNext())

{ DBObject current = cursor.next(); StringBuilder sb = new StringBuilder(); sb.append("id:").append(current.get("_id")).append(" name:").append(current.get("Name")).append(" Surname:").append(current.get("Surname")); System.out.println(sb.toString()); }

getDB().getCollection("usersTest").drop();
}

and produce the output:

id:4b4599fe45d2ba538958a21c name:Jack Surname:Folla
id:4b4599fe45d2ba538a58a21c name:Jim Surname:Finn
id:4b4599fe45d2ba538b58a21c name:Paperino Surname:Paolino

Comment by Eliot Horowitz (Inactive) [ 06/Jan/10 ]

i tried to find any issue with the code and tried your snippet but can't find anything wrong.
Certainly can't reproduce.
The current algorithm is very fast, and is guaranteed not to generate dups.

If you can send a full program that displays the error, can take a look.

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