Skip to content

Commit 07a294a

Browse files
Added support for UUID (binary, subtype 3)
1 parent 2a56f18 commit 07a294a

6 files changed

Lines changed: 44 additions & 11 deletions

File tree

src/main/org/bson/BSON.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
package org.bson;
44

5+
import java.nio.charset.*;
56
import java.util.*;
67
import java.util.regex.*;
7-
import java.nio.charset.*;
88

9-
import org.bson.io.*;
109
import org.bson.util.*;
1110

1211
public class BSON {
@@ -45,6 +44,7 @@ public class BSON {
4544

4645
public static final byte B_FUNC = 1;
4746
public static final byte B_BINARY = 2;
47+
public static final byte B_UUID = 3;
4848

4949
// ---- regular expression handling ----
5050

src/main/org/bson/BSONCallback.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
package org.bson;
44

5-
import java.io.*;
6-
75
import org.bson.types.*;
86

97
public interface BSONCallback {
@@ -41,4 +39,8 @@ public interface BSONCallback {
4139
*/
4240
void gotBinaryArray( String name , byte[] b );
4341
void gotBinary( String name , byte type , byte[] data );
42+
/**
43+
* subtype 3
44+
*/
45+
void gotUUID( String name , long part1, long part2);
4446
}

src/main/org/bson/BSONDecoder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
package org.bson;
44

5+
import static org.bson.BSON.*;
6+
57
import java.io.*;
68

7-
import static org.bson.BSON.*;
89
import org.bson.io.*;
910
import org.bson.types.*;
1011

@@ -218,6 +219,14 @@ void _binary( String name )
218219
_in.fill( data );
219220
_callback.gotBinaryArray( name , data );
220221
return;
222+
case B_UUID:
223+
if ( totalLen != 132 )
224+
throw new IllegalArgumentException( "bad data size subtype 3 len: " + totalLen + " != 16");
225+
226+
long part1 = _in.readLong();
227+
long part2 = _in.readLong();
228+
_callback.gotUUID(name, part1, part2);
229+
return;
221230
}
222231

223232
byte[] data = new byte[totalLen];

src/main/org/bson/BSONEncoder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ else if ( val instanceof byte[] )
164164
putBinary( name , (byte[])val );
165165
else if ( val instanceof Binary )
166166
putBinary( name , (Binary)val );
167+
else if ( val instanceof UUID )
168+
putUUID( name , (UUID)val );
167169
else if ( val.getClass().isArray() )
168170
putIterable( name , Arrays.asList( (Object[])val ) );
169171

@@ -286,6 +288,13 @@ protected void putBinary( String name , Binary val ){
286288
_buf.write( val.getData() );
287289
}
288290

291+
protected void putUUID( String name , UUID val ){
292+
_put( BINARY , name );
293+
_buf.writeInt( 4 + 64*2);
294+
_buf.write( B_UUID );
295+
_buf.writeLong( val.getMostSignificantBits());
296+
_buf.writeLong( val.getLeastSignificantBits());
297+
}
289298

290299
protected void putSymbol( String name , Symbol s ){
291300
_putString(name, s.getSymbol(), SYMBOL);

src/main/org/bson/BasicBSONCallback.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package org.bson;
44

5-
import java.io.*;
65
import java.util.*;
76
import java.util.regex.*;
87

@@ -130,6 +129,10 @@ public void gotBinaryArray( String name , byte[] b ){
130129
public void gotBinary( String name , byte type , byte[] data ){
131130
_put( name , new Binary( type , data ) );
132131
}
132+
133+
public void gotUUID( String name , long part1, long part2){
134+
_put( name , new UUID(part1, part2) );
135+
}
133136

134137
protected void _put( String name , Object o ){
135138
cur().put( name , BSON.applyDecodingHooks( o ) );

src/test/com/mongodb/JavaClientTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
package com.mongodb;
1919

2020
import java.io.*;
21+
import java.nio.*;
2122
import java.util.*;
2223
import java.util.regex.*;
23-
import java.nio.*;
24-
25-
import org.testng.annotations.Test;
26-
27-
import com.mongodb.util.*;
2824

2925
import org.bson.*;
3026
import org.bson.types.*;
27+
import org.testng.annotations.*;
28+
29+
import com.mongodb.util.*;
3130

3231
public class JavaClientTest extends TestCase {
3332

@@ -184,6 +183,17 @@ public void testBinary()
184183
}
185184

186185
}
186+
@Test
187+
public void testUUID()
188+
throws MongoException {
189+
DBCollection c = _db.getCollection( "testUUID" );
190+
c.drop();
191+
c.save( BasicDBObjectBuilder.start().add( "a" , new UUID(1,2)).get() );
192+
193+
DBObject out = c.findOne();
194+
UUID b = (UUID)(out.get( "a" ) );
195+
assertEquals( new UUID(1,2), b);
196+
}
187197

188198
@Test
189199
public void testEval()

0 commit comments

Comments
 (0)