Skip to content

Commit cf484f1

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents fe72f3f + 161aba9 commit cf484f1

16 files changed

+192
-142
lines changed

src/main/com/mongodb/DB.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public CommandResult command( DBObject cmd, DBEncoder encoder ) throws MongoExce
150150

151151
public CommandResult command( DBObject cmd , int options, DBEncoder encoder )
152152
throws MongoException {
153-
return command(cmd, options, getReadPreference(), encoder);
153+
return command(cmd, options, null, encoder);
154154
}
155155

156156
public CommandResult command( DBObject cmd , int options, ReadPreference readPrefs )
@@ -295,8 +295,7 @@ public Set<String> getCollectionNames()
295295
if (namespaces == null)
296296
throw new RuntimeException("this is impossible");
297297

298-
// TODO - Is ReadPreference OK for collection Names?
299-
Iterator<DBObject> i = namespaces.__find(new BasicDBObject(), null, 0, 0, 0, getOptions(), null, null);
298+
Iterator<DBObject> i = namespaces.__find(new BasicDBObject(), null, 0, 0, 0, getOptions(), getReadPreference(), null);
300299
if (i == null)
301300
return new HashSet<String>();
302301

src/main/com/mongodb/DBCollection.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,43 @@ public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, bo
378378
throw new MongoException("FindAndModify: Remove cannot be mixed with the Update, or returnNew params!");
379379

380380
CommandResult res = this._db.command( cmd );
381-
if (res.ok() || res.getErrorMessage().equals( "No matching object found" ))
382-
return (DBObject) res.get( "value" );
381+
if (res.ok() || res.getErrorMessage().equals( "No matching object found" )) {
382+
return replaceWithObjectClass((DBObject) res.get( "value" ));
383+
}
383384
res.throwOnError();
384385
return null;
385386
}
386387

388+
/**
389+
* Doesn't yet handle internal classes properly, so this method only does something if object class is set but
390+
* no internal classes are set.
391+
*
392+
* @param oldObj the original value from the command result
393+
* @return replaced object if necessary, or oldObj
394+
*/
395+
private DBObject replaceWithObjectClass(DBObject oldObj) {
396+
if (oldObj == null || getObjectClass() == null & _internalClass.isEmpty()) {
397+
return oldObj;
398+
}
399+
400+
DBObject newObj = instantiateObjectClassInstance();
401+
402+
for (String key : oldObj.keySet()) {
403+
newObj.put(key, oldObj.get(key));
404+
}
405+
return newObj;
406+
}
407+
408+
private DBObject instantiateObjectClassInstance() {
409+
try {
410+
return (DBObject) getObjectClass().newInstance();
411+
} catch (InstantiationException e) {
412+
throw new MongoInternalException("can't create instance of type " + getObjectClass(), e);
413+
} catch (IllegalAccessException e) {
414+
throw new MongoInternalException("can't create instance of type " + getObjectClass(), e);
415+
}
416+
}
417+
387418

388419
/**
389420
* calls {@link DBCollection#findAndModify(com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject, boolean, com.mongodb.DBObject, boolean, boolean)}

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,12 @@ void _checkClosed(){
123123
throw new IllegalStateException( "this Mongo has been closed" );
124124
}
125125

126-
WriteResult _checkWriteError( DB db , MyPort mp , DBPort port , WriteConcern concern )
126+
WriteResult _checkWriteError( DB db, DBPort port , WriteConcern concern )
127127
throws MongoException, IOException {
128-
CommandResult e = null;
129-
e = port.runCommand( db , concern.getCommand() );
130-
131-
if ( ! e.hasErr() )
132-
return new WriteResult( e , concern );
128+
CommandResult e = port.runCommand( db , concern.getCommand() );
133129

134130
e.throwOnError();
135-
return null;
131+
return new WriteResult( e , concern );
136132
}
137133

138134
@Override
@@ -155,7 +151,7 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
155151
port.checkAuth( db );
156152
port.say( m );
157153
if ( concern.callGetLastError() ){
158-
return _checkWriteError( db , mp , port , concern );
154+
return _checkWriteError( db , port , concern );
159155
}
160156
else {
161157
return new WriteResult( db , port , concern );
@@ -503,7 +499,7 @@ public String debugString(){
503499
buf.append( "replica set : " ).append( _allHosts );
504500
} else {
505501
ServerAddress master = getAddress();
506-
buf.append( master ).append( " " ).append( master != null ? master._addr : null );
502+
buf.append( master ).append( " " ).append( master != null ? master.getSocketAddress() : null );
507503
}
508504

509505
return buf.toString();

src/main/com/mongodb/OutMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ byte[] toByteArray(){
120120
}
121121

122122
void doneWithMessage(){
123-
if ( _buffer != null && _mongo != null )
123+
if ( _buffer != null && _mongo != null ) {
124124
_buffer.reset();
125125
_mongo._bufferPool.done( _buffer );
126+
}
126127

127128
_buffer = null;
128129
_mongo = null;

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static class UpdatableNode {
490490

491491
private void updateAddr() {
492492
try {
493-
if (_addr.updateInetAddr()) {
493+
if (_addr.updateInetAddress()) {
494494
// address changed, need to use new ports
495495
_port = new DBPort(_addr, null, _mongoOptions);
496496
_mongo.getConnector().updatePortPool(_addr);
@@ -704,9 +704,8 @@ public long getNextResolveTime() {
704704
public synchronized void updateAll(){
705705
HashSet<UpdatableNode> seenNodes = new HashSet<UpdatableNode>();
706706

707-
// make a copy of _all, since UpdatableNode.update can add to it
708-
for (UpdatableNode node : new ArrayList<UpdatableNode>(_all)) {
709-
node.update(seenNodes);
707+
for (int i = 0; i < _all.size(); i++) {
708+
_all.get(i).update(seenNodes);
710709
}
711710

712711
if (seenNodes.size() > 0) {

src/main/com/mongodb/ServerAddress.java

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.net.InetAddress;
2222
import java.net.InetSocketAddress;
2323
import java.net.UnknownHostException;
24-
import java.util.ArrayList;
25-
import java.util.List;
2624

2725
/**
2826
* mongo server address
@@ -72,8 +70,7 @@ public ServerAddress( String host , int port )
7270

7371
_host = host;
7472
_port = port;
75-
_all = _getAddress( _host );
76-
_addr = new InetSocketAddress( _all[0] , _port );
73+
updateInetAddress();
7774
}
7875

7976
/**
@@ -98,41 +95,11 @@ public ServerAddress( InetAddress addr , int port ){
9895
* @param addr inet socket address containing hostname and port
9996
*/
10097
public ServerAddress( InetSocketAddress addr ){
101-
_addr = addr;
102-
_host = _addr.getHostName();
103-
_port = _addr.getPort();
104-
_all = null;
98+
_address = addr;
99+
_host = _address.getHostName();
100+
_port = _address.getPort();
105101
}
106102

107-
// --------
108-
// pairing
109-
// --------
110-
111-
/**
112-
* Determines if the database at this address is paired.
113-
* @return if this address connects to a set of paired databases
114-
*/
115-
boolean isPaired(){
116-
return _all != null && _all.length > 1;
117-
}
118-
119-
/**
120-
* If this is the address of a paired database, returns addresses for
121-
* all of the databases with which it is paired.
122-
* @return the addresses
123-
* @throws RuntimeException if this address is not one of a paired database
124-
*/
125-
List<ServerAddress> explode(){
126-
if ( _all == null || _all.length <= 1 )
127-
throw new RuntimeException( "not replica set mode. num addresses : " + ((_all == null) ? 0 : _all.length) );
128-
129-
List<ServerAddress> s = new ArrayList<ServerAddress>();
130-
for ( int i=0; i<_all.length; i++ ){
131-
s.add( new ServerAddress( _all[i] , _port ) );
132-
}
133-
return s;
134-
}
135-
136103
// --------
137104
// equality, etc...
138105
// --------
@@ -165,7 +132,7 @@ public boolean equals( Object other ){
165132
a._host.equals( _host );
166133
}
167134
if ( other instanceof InetSocketAddress ){
168-
return _addr.equals( other );
135+
return _address.equals( other );
169136
}
170137
return false;
171138
}
@@ -177,62 +144,51 @@ public int hashCode(){
177144

178145
/**
179146
* Gets the hostname
180-
* @return
147+
* @return hostname
181148
*/
182149
public String getHost(){
183150
return _host;
184151
}
185152

186153
/**
187154
* Gets the port number
188-
* @return
155+
* @return port
189156
*/
190157
public int getPort(){
191158
return _port;
192159
}
193160

194161
/**
195162
* Gets the underlying socket address
196-
* @return
163+
* @return socket address
197164
*/
198165
public InetSocketAddress getSocketAddress(){
199-
return _addr;
166+
return _address;
200167
}
201168

202169
@Override
203170
public String toString(){
204-
return _host + ":" + _port;
171+
return _address.toString();
205172
}
206173

207174
final String _host;
208175
final int _port;
209-
InetSocketAddress _addr;
210-
InetAddress[] _all;
176+
volatile InetSocketAddress _address;
211177

212178
// --------
213179
// static helpers
214180
// --------
215181

216-
private static InetAddress[] _getAddress( String host )
217-
throws UnknownHostException {
218-
219-
if ( host.toLowerCase().equals("localhost") ){
220-
return new InetAddress[] { InetAddress.getLocalHost()};
221-
}
222-
223-
return InetAddress.getAllByName( host );
224-
}
225-
226182
/**
227-
* Returns the default database host: db_ip environment variable, or "127.0.0.1"
228-
* @return
183+
* Returns the default database host: "127.0.0.1"
184+
* @return IP address of default host.
229185
*/
230186
public static String defaultHost(){
231187
return "127.0.0.1";
232188
}
233189

234-
/** Returns the default database port: db_port environment variable, or 27017 as a default
235-
* @return
190+
/** Returns the default database port: 27017
191+
* @return the default port
236192
*/
237193
public static int defaultPort(){
238194
return DBPort.PORT;
@@ -243,13 +199,9 @@ public static int defaultPort(){
243199
* @return true if host resolved to a new IP that is different from old one, false otherwise
244200
* @throws UnknownHostException
245201
*/
246-
boolean updateInetAddr() throws UnknownHostException {
247-
InetSocketAddress oldaddr = _addr;
248-
_all = _getAddress( _host );
249-
_addr = new InetSocketAddress( _all[0] , _port );
250-
if (!_addr.equals(oldaddr))
251-
return true;
252-
return false;
202+
boolean updateInetAddress() throws UnknownHostException {
203+
InetSocketAddress oldAddress = _address;
204+
_address = new InetSocketAddress( InetAddress.getByName(_host) , _port );
205+
return !_address.equals(oldAddress);
253206
}
254-
255207
}

src/main/com/mongodb/gridfs/GridFSDBFile.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public long writeTo( String filename ) throws IOException {
6464
* @throws IOException
6565
*/
6666
public long writeTo( File f ) throws IOException {
67-
67+
6868
FileOutputStream out = null;
6969
try{
7070
out = new FileOutputStream( f );
@@ -138,6 +138,7 @@ public int read(){
138138
public int read(byte[] b){
139139
return read( b , 0 , b.length );
140140
}
141+
141142
public int read(byte[] b, int off, int len){
142143

143144
if ( _data == null || _offset >= _data.length ){
@@ -166,35 +167,29 @@ public long skip(long numBytesToSkip) throws IOException {
166167
//Don't count those extra bytes to skip in with the return value
167168
return 0;
168169

169-
if (_offset + numBytesToSkip <= _chunkSize) {
170-
//We're skipping over bytes in the current chunk, adjust the offset accordingly
171-
_offset += numBytesToSkip;
172-
if (_data == null && _currentChunkIdx < _numChunks)
173-
_data = getChunk(_currentChunkIdx);
174-
175-
return numBytesToSkip;
170+
// offset in the whole file
171+
long offsetInFile = 0;
172+
if (_currentChunkIdx >= 0)
173+
offsetInFile = _currentChunkIdx * _chunkSize + _offset;
174+
if (numBytesToSkip + offsetInFile >= _length) {
175+
_currentChunkIdx = _numChunks;
176+
_data = null;
177+
return _length - offsetInFile;
176178
}
177179

178-
//We skipping over the remainder of this chunk, could do this less recursively...
179-
++_currentChunkIdx;
180-
long skippedBytes = 0;
181-
if (_currentChunkIdx < _numChunks)
182-
skippedBytes = _chunkSize - _offset;
183-
else
184-
skippedBytes = _lastChunkSize;
185-
186-
_offset = 0;
187-
_data = null;
180+
int temp = _currentChunkIdx;
181+
_currentChunkIdx = (int)((numBytesToSkip + offsetInFile) / _chunkSize);
182+
if (temp != _currentChunkIdx)
183+
_data = getChunk(_currentChunkIdx);
184+
_offset = (int)((numBytesToSkip + offsetInFile) % _chunkSize);
188185

189-
return skippedBytes + skip(numBytesToSkip - skippedBytes);
186+
return numBytesToSkip;
190187
}
191188

192189
final int _numChunks;
193-
//Math trick to ensure the _lastChunkSize is between 1 and _chunkSize
194-
final long _lastChunkSize = ((_length - 1) % _chunkSize) + 1;
195190

196191
int _currentChunkIdx = -1;
197-
int _offset;
192+
int _offset = 0;
198193
byte[] _data = null;
199194
}
200195

src/main/com/mongodb/gridfs/GridFSInputFile.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,21 @@ public void setContentType( String ct ) {
138138

139139
/**
140140
* Set the chunk size. This must be called before saving any data.
141-
* @param _chunkSize
141+
* @param chunkSize The size in bytes.
142142
*/
143-
public void setChunkSize(long _chunkSize) {
143+
public void setChunkSize(long chunkSize) {
144144
if (_outputStream != null || _savedChunks)
145145
return;
146-
this._chunkSize = _chunkSize;
146+
_chunkSize = chunkSize;
147+
_buffer = new byte[(int) _chunkSize];
147148
}
148149

149150
/**
150151
* calls {@link GridFSInputFile#save(long)} with the existing chunk size
151152
*/
152153
@Override
153154
public void save() {
154-
save( _chunkSize);
155+
save( _chunkSize );
155156
}
156157

157158
/**

0 commit comments

Comments
 (0)