@@ -100,24 +100,20 @@ public static ObjectId massageToObjectId( Object o ){
100100 }
101101
102102 public ObjectId ( Date time ){
103- _time = _flip ( (int )(time .getTime () / 1000 ) );
104- _machine = _genmachine ;
105- _inc = _nextInc .getAndIncrement ();
106- _new = false ;
103+ this (time , _genmachine , _nextInc .getAndIncrement ());
107104 }
108105
109106 public ObjectId ( Date time , int inc ){
110107 this ( time , _genmachine , inc );
111108 }
112109
113110 public ObjectId ( Date time , int machine , int inc ){
114- _time = _flip ( ( int )(time .getTime () / 1000 ) );
111+ _time = ( int )(time .getTime () / 1000 );
115112 _machine = machine ;
116113 _inc = inc ;
117114 _new = false ;
118115 }
119116
120-
121117 /** Creates a new instance from a string.
122118 * @param s the string to convert
123119 * @throws IllegalArgumentException if the string is not a valid id
@@ -133,44 +129,45 @@ public ObjectId( String s , boolean babble ){
133129
134130 if ( babble )
135131 s = babbleToMongod ( s );
136-
132+
137133 byte b [] = new byte [12 ];
138134 for ( int i =0 ; i <b .length ; i ++ ){
139- b [b . length -( i + 1 ) ] = (byte )Integer .parseInt ( s .substring ( i *2 , i *2 + 2 ) , 16 );
135+ b [i ] = (byte )Integer .parseInt ( s .substring ( i *2 , i *2 + 2 ) , 16 );
140136 }
141137 ByteBuffer bb = ByteBuffer .wrap ( b );
142-
143- _inc = bb .getInt ();
144- _machine = bb .getInt ();
145138 _time = bb .getInt ();
146-
139+ _machine = bb .getInt ();
140+ _inc = bb .getInt ();
147141 _new = false ;
148142 }
149143
150144 public ObjectId ( byte [] b ){
151145 if ( b .length != 12 )
152146 throw new IllegalArgumentException ( "need 12 bytes" );
153- reverse ( b );
154147 ByteBuffer bb = ByteBuffer .wrap ( b );
155-
156- _inc = bb .getInt ();
157- _machine = bb .getInt ();
158148 _time = bb .getInt ();
149+ _machine = bb .getInt ();
150+ _inc = bb .getInt ();
151+ _new = false ;
159152 }
160153
161-
154+ /**
155+ * Creates an ObjectId
156+ * @param time time in seconds
157+ * @param machine machine ID
158+ * @param inc incremental value
159+ */
162160 public ObjectId ( int time , int machine , int inc ){
163161 _time = time ;
164162 _machine = machine ;
165163 _inc = inc ;
166-
167164 _new = false ;
168165 }
169166
170167 /** Create a new object id.
171168 */
172169 public ObjectId (){
173- _time = _curtime ( );
170+ _time = ( int ) ( System . currentTimeMillis () / 1000 );
174171 _machine = _genmachine ;
175172 _inc = _nextInc .getAndIncrement ();
176173 _new = true ;
@@ -221,20 +218,12 @@ public String toStringMongod(){
221218 public byte [] toByteArray (){
222219 byte b [] = new byte [12 ];
223220 ByteBuffer bb = ByteBuffer .wrap ( b );
224- bb .putInt ( _inc );
225- bb .putInt ( _machine );
221+ // by default BB is big endian like we need
226222 bb .putInt ( _time );
227- reverse ( b );
223+ bb .putInt ( _machine );
224+ bb .putInt ( _inc );
228225 return b ;
229226 }
230-
231- static void reverse ( byte [] b ){
232- for ( int i =0 ; i <b .length /2 ; i ++ ){
233- byte t = b [i ];
234- b [i ] = b [ b .length -(i +1 ) ];
235- b [b .length -(i +1 )] = t ;
236- }
237- }
238227
239228 static String _pos ( String s , int p ){
240229 return s .substring ( p * 2 , ( p * 2 ) + 2 );
@@ -257,41 +246,61 @@ public String toString(){
257246 return toStringMongod ();
258247 }
259248
260- int _compare ( int i , int j ){
261- i = _flip ( i ) ;
262- j = _flip ( j ) ;
263-
264- final int diff = j - i ;
265-
266- if ( i >= 0 ){
267- return j >= 0 ? - diff : - 1 ;
268- }
269-
270- return j < 0 ? - diff : 1 ;
249+ int _compareUnsigned ( int i , int j ){
250+ long li = 0xFFFFFFFFL ;
251+ li = i & li ;
252+ long lj = 0xFFFFFFFFL ;
253+ lj = j & lj ;
254+ long diff = li - lj ;
255+ if (diff < Integer . MIN_VALUE )
256+ return Integer . MIN_VALUE ;
257+ if ( diff > Integer . MAX_VALUE )
258+ return Integer . MAX_VALUE ;
259+ return ( int ) diff ;
271260 }
272261
273262 public int compareTo ( ObjectId id ){
274263 if ( id == null )
275264 return -1 ;
276265
277- int x = _compare ( _time , id ._time );
266+ int x = _compareUnsigned ( _time , id ._time );
278267 if ( x != 0 )
279268 return x ;
280269
281- x = _compare ( _machine , id ._machine );
270+ x = _compareUnsigned ( _machine , id ._machine );
282271 if ( x != 0 )
283272 return x ;
284273
285- return _compare ( _inc , id ._inc );
274+ x = _compareUnsigned ( _inc , id ._inc );
275+ if (Math .abs (x ) > Integer .MAX_VALUE / 2 ) {
276+ // this means that for same second and process more than (max int)/2 were generated
277+ // highly unlikely, most likely the counter wrapped
278+ if (x < 0 )
279+ return 1 ;
280+ else
281+ return -1 ;
282+ }
283+ return x ;
286284 }
287285
288286 public int getMachine (){
289287 return _machine ;
290288 }
291-
289+
290+ /**
291+ * Gets the time of this ID, in milliseconds
292+ * @return
293+ */
292294 public long getTime (){
293- long z = _flip ( _time );
294- return z * 1000 ;
295+ return _time * 1000L ;
296+ }
297+
298+ /**
299+ * Gets the time of this ID, in seconds
300+ * @return
301+ */
302+ public int getTimeSecond (){
303+ return _time ;
295304 }
296305
297306 public int getInc (){
@@ -316,6 +325,22 @@ public void notNew(){
316325 _new = false ;
317326 }
318327
328+ /**
329+ * Gets the generated machine ID, identifying the machine / process / class loader
330+ * @return
331+ */
332+ public static int getGenMachineId () {
333+ return _genmachine ;
334+ }
335+
336+ /**
337+ * Gets the current value of the auto increment
338+ * @return
339+ */
340+ public static int getCurrentInc () {
341+ return _nextInc .get ();
342+ }
343+
319344 final int _time ;
320345 final int _machine ;
321346 final int _inc ;
@@ -331,12 +356,7 @@ public static int _flip( int x ){
331356 return z ;
332357 }
333358
334- private static int _curtime (){
335- return _flip ( (int )(System .currentTimeMillis ()/1000 ) );
336- }
337-
338359 private static AtomicInteger _nextInc = new AtomicInteger ( (new java .util .Random ()).nextInt () );
339- private static final String _incLock = new String ( "ObjectId._incLock" );
340360
341361 private static final int _genmachine ;
342362 static {
0 commit comments