Skip to content

Commit 2b81f0b

Browse files
committed
Added the ability to use DBRefs with the async driver
Moved DBRef, DBRefCodec, DBRefCodecProvider and DocumentToDBRefTransformer into driver-core Updated the DEFAULT_CODEC_REGISTRY for the async driver JAVA-2426
1 parent 19aac82 commit 2b81f0b

File tree

10 files changed

+99
-67
lines changed

10 files changed

+99
-67
lines changed

driver-async/src/main/com/mongodb/async/client/MongoClients.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2017 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package com.mongodb.async.client;
1818

1919
import com.mongodb.ConnectionString;
20+
import com.mongodb.DBRefCodecProvider;
21+
import com.mongodb.DocumentToDBRefTransformer;
2022
import com.mongodb.client.MongoDriverInformation;
2123
import com.mongodb.client.gridfs.codecs.GridFSFileCodecProvider;
2224
import com.mongodb.client.model.geojson.codecs.GeoJsonCodecProvider;
@@ -214,10 +216,11 @@ public static CodecRegistry getDefaultCodecRegistry() {
214216

215217
private static final CodecRegistry DEFAULT_CODEC_REGISTRY =
216218
fromProviders(asList(new ValueCodecProvider(),
217-
new DocumentCodecProvider(),
218219
new BsonValueCodecProvider(),
219-
new IterableCodecProvider(),
220-
new MapCodecProvider(),
220+
new DBRefCodecProvider(),
221+
new DocumentCodecProvider(new DocumentToDBRefTransformer()),
222+
new IterableCodecProvider(new DocumentToDBRefTransformer()),
223+
new MapCodecProvider(new DocumentToDBRefTransformer()),
221224
new GeoJsonCodecProvider(),
222225
new GridFSFileCodecProvider()));
223226

driver/src/main/com/mongodb/DocumentToDBRefTransformer.java renamed to driver-core/src/main/com/mongodb/DocumentToDBRefTransformer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2017 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,14 @@
1919
import org.bson.Document;
2020
import org.bson.Transformer;
2121

22-
final class DocumentToDBRefTransformer implements Transformer {
22+
/**
23+
* A Document to DBRef Transformer.
24+
*
25+
* Can be used with any {@link org.bson.codecs.Codec} that takes a {@link Transformer}.
26+
*
27+
* @since 3.5
28+
*/
29+
public final class DocumentToDBRefTransformer implements Transformer {
2330
@Override
2431
public Object transform(final Object value) {
2532
if (value instanceof Document) {

driver/src/test/unit/com/mongodb/DBRefSpecification.groovy renamed to driver-core/src/test/unit/com/mongodb/DBRefSpecification.groovy

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2017 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,6 @@
1616

1717
package com.mongodb
1818

19-
import org.bson.BSONDecoder
20-
import org.bson.BasicBSONDecoder
21-
import org.bson.io.BasicOutputBuffer
22-
import org.bson.io.OutputBuffer
2319
import spock.lang.Specification
2420

2521
class DBRefSpecification extends Specification {
@@ -60,10 +56,10 @@ class DBRefSpecification extends Specification {
6056

6157
def 'equivalent instances should be equal and have the same hash code'() {
6258
given:
63-
DBRef referenceA = new DBRef('foo.bar', 4);
64-
DBRef referenceB = new DBRef('foo.bar', 4);
65-
DBRef referenceC = new DBRef('mydb', 'foo.bar', 4);
66-
DBRef referenceD = new DBRef('mydb', 'foo.bar', 4);
59+
DBRef referenceA = new DBRef('foo.bar', 4)
60+
DBRef referenceB = new DBRef('foo.bar', 4)
61+
DBRef referenceC = new DBRef('mydb', 'foo.bar', 4)
62+
DBRef referenceD = new DBRef('mydb', 'foo.bar', 4)
6763

6864
expect:
6965
referenceA.equals(referenceA)
@@ -75,11 +71,11 @@ class DBRefSpecification extends Specification {
7571

7672
def 'non-equivalent instances should not be equal and have different hash codes'() {
7773
given:
78-
DBRef referenceA = new DBRef('foo.bar', 4);
79-
DBRef referenceB = new DBRef('foo.baz', 4);
80-
DBRef referenceC = new DBRef('foo.bar', 5);
81-
DBRef referenceD = new DBRef('mydb', 'foo.bar', 4);
82-
DBRef referenceE = new DBRef('yourdb', 'foo.bar', 4);
74+
DBRef referenceA = new DBRef('foo.bar', 4)
75+
DBRef referenceB = new DBRef('foo.baz', 4)
76+
DBRef referenceC = new DBRef('foo.bar', 5)
77+
DBRef referenceD = new DBRef('mydb', 'foo.bar', 4)
78+
DBRef referenceE = new DBRef('yourdb', 'foo.bar', 4)
8379

8480
expect:
8581
!referenceA.equals(null)
@@ -100,55 +96,17 @@ class DBRefSpecification extends Specification {
10096
new DBRef('mydb', 'foo.bar', 4).toString() == '{ "$ref" : "foo.bar", "$id" : "4, "$db" : "mydb" }'
10197
}
10298

103-
def 'should encode and decode DBRefs'() {
104-
given:
105-
DBRef reference = new DBRef('coll', 'hello world');
106-
DBObject document = new BasicDBObject('!', reference);
107-
OutputBuffer buffer = new BasicOutputBuffer();
108-
109-
when:
110-
DefaultDBEncoder.FACTORY.create().writeObject(buffer, document);
111-
DefaultDBCallback callback = new DefaultDBCallback(null);
112-
BSONDecoder decoder = new BasicBSONDecoder();
113-
decoder.decode(buffer.toByteArray(), callback);
114-
DBRef decoded = ((DBObject) callback.get()).get('!');
115-
116-
then:
117-
decoded.databaseName == null
118-
decoded.collectionName == 'coll'
119-
decoded.id == 'hello world'
120-
}
121-
122-
def 'should encode and decode DBRefs with a database name'() {
123-
given:
124-
DBRef reference = new DBRef('db', 'coll', 'hello world');
125-
DBObject document = new BasicDBObject('!', reference);
126-
OutputBuffer buffer = new BasicOutputBuffer();
127-
128-
when:
129-
DefaultDBEncoder.FACTORY.create().writeObject(buffer, document);
130-
DefaultDBCallback callback = new DefaultDBCallback(null);
131-
BSONDecoder decoder = new BasicBSONDecoder();
132-
decoder.decode(buffer.toByteArray(), callback);
133-
DBRef decoded = ((DBObject) callback.get()).get('!');
134-
135-
then:
136-
decoded.databaseName == 'db'
137-
decoded.collectionName == 'coll'
138-
decoded.id == 'hello world'
139-
}
140-
14199
def 'testSerialization'() throws Exception {
142100
given:
143-
DBRef originalDBRef = new DBRef('col', 42);
101+
DBRef originalDBRef = new DBRef('col', 42)
144102

145-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
146-
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
103+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
104+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)
147105

148106
when:
149-
objectOutputStream.writeObject(originalDBRef);
150-
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
151-
DBRef deserializedDBRef = (DBRef) objectInputStream.readObject();
107+
objectOutputStream.writeObject(originalDBRef)
108+
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()))
109+
DBRef deserializedDBRef = (DBRef) objectInputStream.readObject()
152110

153111
then:
154112
originalDBRef == deserializedDBRef

driver/src/test/unit/com/mongodb/DocumentToDBRefTransformerSpecification.groovy renamed to driver-core/src/test/unit/com/mongodb/DocumentToDBRefTransformerSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2017 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

driver/src/main/com/mongodb/MongoClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public class MongoClient extends Mongo implements Closeable {
8585

8686
private static final CodecRegistry DEFAULT_CODEC_REGISTRY =
8787
fromProviders(asList(new ValueCodecProvider(),
88+
new BsonValueCodecProvider(),
8889
new DBRefCodecProvider(),
89-
new DocumentCodecProvider(new DocumentToDBRefTransformer()),
9090
new DBObjectCodecProvider(),
91-
new BsonValueCodecProvider(),
91+
new DocumentCodecProvider(new DocumentToDBRefTransformer()),
9292
new IterableCodecProvider(new DocumentToDBRefTransformer()),
9393
new MapCodecProvider(new DocumentToDBRefTransformer()),
9494
new GeoJsonCodecProvider(),
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2017 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb
18+
19+
import org.bson.BSONDecoder
20+
import org.bson.BasicBSONDecoder
21+
import org.bson.io.BasicOutputBuffer
22+
import org.bson.io.OutputBuffer
23+
import spock.lang.Specification
24+
25+
class DBEncoderDecoderDBRefSpecification extends Specification {
26+
27+
def 'should encode and decode DBRefs'() {
28+
given:
29+
DBRef reference = new DBRef('coll', 'hello world');
30+
DBObject document = new BasicDBObject('!', reference);
31+
OutputBuffer buffer = new BasicOutputBuffer();
32+
33+
when:
34+
DefaultDBEncoder.FACTORY.create().writeObject(buffer, document);
35+
DefaultDBCallback callback = new DefaultDBCallback(null);
36+
BSONDecoder decoder = new BasicBSONDecoder();
37+
decoder.decode(buffer.toByteArray(), callback);
38+
DBRef decoded = ((DBObject) callback.get()).get('!');
39+
40+
then:
41+
decoded.databaseName == null
42+
decoded.collectionName == 'coll'
43+
decoded.id == 'hello world'
44+
}
45+
46+
def 'should encode and decode DBRefs with a database name'() {
47+
given:
48+
DBRef reference = new DBRef('db', 'coll', 'hello world');
49+
DBObject document = new BasicDBObject('!', reference);
50+
OutputBuffer buffer = new BasicOutputBuffer();
51+
52+
when:
53+
DefaultDBEncoder.FACTORY.create().writeObject(buffer, document);
54+
DefaultDBCallback callback = new DefaultDBCallback(null);
55+
BSONDecoder decoder = new BasicBSONDecoder();
56+
decoder.decode(buffer.toByteArray(), callback);
57+
DBRef decoded = ((DBObject) callback.get()).get('!');
58+
59+
then:
60+
decoded.databaseName == 'db'
61+
decoded.collectionName == 'coll'
62+
decoded.id == 'hello world'
63+
}
64+
}

0 commit comments

Comments
 (0)