Skip to content

Commit 852e5dd

Browse files
Merge branch 'master' of github.com:mongodb/mongo-java-driver
2 parents 66118a6 + 65281dc commit 852e5dd

File tree

8 files changed

+246
-183
lines changed

8 files changed

+246
-183
lines changed

src/main/com/mongodb/DBCallback.java

Lines changed: 3 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,128 +2,13 @@
22

33
package com.mongodb;
44

5-
import java.util.*;
6-
import java.util.logging.*;
75

86
import org.bson.*;
9-
import org.bson.types.*;
107

118
/**
12-
* This class overrides BasicBSONCallback to implement some extra features specific to the Database.
13-
* For example DBRef type.
14-
* @author antoine
9+
* The DB callback interface.
1510
*/
16-
public class DBCallback extends BasicBSONCallback {
11+
public interface DBCallback extends BSONCallback {
1712

18-
public static interface Factory {
19-
public DBCallback create( DBCollection collection );
20-
}
21-
22-
static class DefaultFactory implements Factory {
23-
@Override
24-
public DBCallback create( DBCollection collection ){
25-
return new DBCallback( collection );
26-
}
27-
}
28-
29-
public static Factory FACTORY = new DefaultFactory();
30-
31-
public DBCallback( DBCollection coll ){
32-
_collection = coll;
33-
_db = _collection == null ? null : _collection.getDB();
34-
}
35-
36-
@Override
37-
@SuppressWarnings("deprecation")
38-
public void gotDBRef( String name , String ns , ObjectId id ){
39-
if ( id.equals( Bytes.COLLECTION_REF_ID ) )
40-
cur().put( name , _collection );
41-
else
42-
cur().put( name , new DBPointer( (DBObject)cur() , name , _db , ns , id ) );
43-
}
44-
45-
@Override
46-
public void objectStart(boolean array, String name){
47-
_lastName = name;
48-
super.objectStart( array , name );
49-
}
50-
51-
@Override
52-
public Object objectDone(){
53-
BSONObject o = (BSONObject)super.objectDone();
54-
if ( ! ( o instanceof List ) &&
55-
o.containsField( "$ref" ) &&
56-
o.containsField( "$id" ) ){
57-
return cur().put( _lastName , new DBRef( _db, o ) );
58-
}
59-
60-
return o;
61-
}
62-
63-
@Override
64-
public BSONObject create(){
65-
return _create( null );
66-
}
67-
68-
@Override
69-
public BSONObject create( boolean array , List<String> path ){
70-
if ( array )
71-
return new BasicDBList();
72-
return _create( path );
73-
}
74-
75-
private DBObject _create( List<String> path ){
76-
77-
Class c = null;
78-
79-
if ( _collection != null && _collection._objectClass != null){
80-
if ( path == null || path.size() == 0 ){
81-
c = _collection._objectClass;
82-
}
83-
else {
84-
StringBuilder buf = new StringBuilder();
85-
for ( int i=0; i<path.size(); i++ ){
86-
if ( i > 0 )
87-
buf.append(".");
88-
buf.append( path.get(i) );
89-
}
90-
c = _collection.getInternalClass( buf.toString() );
91-
}
92-
93-
}
94-
95-
if ( c != null ){
96-
try {
97-
return (DBObject)c.newInstance();
98-
}
99-
catch ( InstantiationException ie ){
100-
LOGGER.log( Level.FINE , "can't create a: " + c , ie );
101-
throw new MongoInternalException( "can't instantiate a : " + c , ie );
102-
}
103-
catch ( IllegalAccessException iae ){
104-
LOGGER.log( Level.FINE , "can't create a: " + c , iae );
105-
throw new MongoInternalException( "can't instantiate a : " + c , iae );
106-
}
107-
}
108-
109-
if ( _collection != null && _collection._name.equals( "$cmd" ) )
110-
return new CommandResult();
111-
return new BasicDBObject();
112-
}
113-
114-
DBObject dbget(){
115-
DBObject o = (DBObject)get();
116-
return o;
117-
}
118-
119-
@Override
120-
public void reset(){
121-
_lastName = null;
122-
super.reset();
123-
}
124-
125-
private String _lastName;
126-
final DBCollection _collection;
127-
final DB _db;
128-
static final Logger LOGGER = Logger.getLogger( "com.mongo.DECODING" );
12913
}
14+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2011 10gen 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+
/**
20+
* The DBCallback factory interface.
21+
*/
22+
public interface DBCallbackFactory {
23+
24+
public DBCallback create( DBCollection collection );
25+
26+
}
27+

src/main/com/mongodb/DBPort.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ boolean _open()
196196
_socket.connect( _addr , _options.connectTimeout );
197197

198198
_socket.setTcpNoDelay( ! USE_NAGLE );
199+
_socket.setKeepAlive( _options.socketKeepAlive );
199200
_socket.setSoTimeout( _options.socketTimeout );
200201
_in = new BufferedInputStream( _socket.getInputStream() );
201202
_out = _socket.getOutputStream();
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// DBCallback.java
2+
3+
package com.mongodb;
4+
5+
// Bson
6+
import org.bson.*;
7+
import org.bson.types.*;
8+
9+
// Java
10+
import java.util.*;
11+
import java.util.logging.*;
12+
13+
/**
14+
* This class overrides BasicBSONCallback to implement some extra features specific to the Database.
15+
* For example DBRef type.
16+
* @author antoine
17+
*/
18+
public class DefaultDBCallback extends BasicBSONCallback implements DBCallback {
19+
20+
static class DefaultFactory implements DBCallbackFactory {
21+
@Override
22+
public DBCallback create( DBCollection collection ){
23+
return new DefaultDBCallback( collection );
24+
}
25+
}
26+
27+
public static DBCallbackFactory FACTORY = new DefaultFactory();
28+
29+
public DefaultDBCallback( DBCollection coll ){
30+
_collection = coll;
31+
_db = _collection == null ? null : _collection.getDB();
32+
}
33+
34+
@Override
35+
@SuppressWarnings("deprecation")
36+
public void gotDBRef( String name , String ns , ObjectId id ){
37+
if ( id.equals( Bytes.COLLECTION_REF_ID ) )
38+
cur().put( name , _collection );
39+
else
40+
cur().put( name , new DBPointer( (DBObject)cur() , name , _db , ns , id ) );
41+
}
42+
43+
@Override
44+
public void objectStart(boolean array, String name){
45+
_lastName = name;
46+
super.objectStart( array , name );
47+
}
48+
49+
@Override
50+
public Object objectDone(){
51+
BSONObject o = (BSONObject)super.objectDone();
52+
if ( ! ( o instanceof List ) &&
53+
o.containsField( "$ref" ) &&
54+
o.containsField( "$id" ) ){
55+
return cur().put( _lastName , new DBRef( _db, o ) );
56+
}
57+
58+
return o;
59+
}
60+
61+
@Override
62+
public BSONObject create(){
63+
return _create( null );
64+
}
65+
66+
@Override
67+
public BSONObject create( boolean array , List<String> path ){
68+
if ( array )
69+
return new BasicDBList();
70+
return _create( path );
71+
}
72+
73+
private DBObject _create( List<String> path ){
74+
75+
Class c = null;
76+
77+
if ( _collection != null && _collection._objectClass != null){
78+
if ( path == null || path.size() == 0 ){
79+
c = _collection._objectClass;
80+
}
81+
else {
82+
StringBuilder buf = new StringBuilder();
83+
for ( int i=0; i<path.size(); i++ ){
84+
if ( i > 0 )
85+
buf.append(".");
86+
buf.append( path.get(i) );
87+
}
88+
c = _collection.getInternalClass( buf.toString() );
89+
}
90+
91+
}
92+
93+
if ( c != null ){
94+
try {
95+
return (DBObject)c.newInstance();
96+
}
97+
catch ( InstantiationException ie ){
98+
LOGGER.log( Level.FINE , "can't create a: " + c , ie );
99+
throw new MongoInternalException( "can't instantiate a : " + c , ie );
100+
}
101+
catch ( IllegalAccessException iae ){
102+
LOGGER.log( Level.FINE , "can't create a: " + c , iae );
103+
throw new MongoInternalException( "can't instantiate a : " + c , iae );
104+
}
105+
}
106+
107+
if ( _collection != null && _collection._name.equals( "$cmd" ) )
108+
return new CommandResult();
109+
return new BasicDBObject();
110+
}
111+
112+
DBObject dbget(){
113+
return (DBObject)get();
114+
}
115+
116+
@Override
117+
public void reset(){
118+
_lastName = null;
119+
super.reset();
120+
}
121+
122+
private String _lastName;
123+
final DBCollection _collection;
124+
final DB _db;
125+
static final Logger LOGGER = Logger.getLogger( "com.mongo.DECODING" );
126+
}

src/main/com/mongodb/Mongo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,13 @@ void _applyMongoOptions() {
512512
setWriteConcern( _options.getWriteConcern() );
513513
}
514514

515+
/**
516+
* Returns the mongo options.
517+
*/
518+
public MongoOptions getMongoOptions() {
519+
return _options;
520+
}
521+
515522
/**
516523
* Gets the maximum size for a BSON object supported by the current master server.
517524
* Note that this value may change over time depending on which server is master.

0 commit comments

Comments
 (0)