Skip to content

Commit a31e2e0

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-java-driver
2 parents b2c7d8b + ac5769e commit a31e2e0

File tree

4 files changed

+338
-29
lines changed

4 files changed

+338
-29
lines changed

src/main/com/mongodb/MongoOptions.java

Lines changed: 278 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import javax.net.SocketFactory;
2222

2323
/**
24-
* Various settings for the driver
24+
* Various settings for the driver.
25+
* Not thread safe.
2526
*/
2627
public class MongoOptions {
2728

@@ -186,8 +187,8 @@ else if (safe)
186187
*/
187188
public boolean safe;
188189

189-
/**
190-
* The "w" value of the global WriteConcern.
190+
/**
191+
* The "w" value, (number of writes), of the global WriteConcern.
191192
* Default is 0.
192193
*/
193194
public int w;
@@ -200,12 +201,14 @@ else if (safe)
200201

201202
/**
202203
* The "fsync" value of the global WriteConcern.
204+
* true indicates writes should wait for data to be written to server data file
203205
* Default is false.
204206
*/
205207
public boolean fsync;
206208

207209
/**
208210
* The "j" value of the global WriteConcern.
211+
* true indicates writes should wait for a journaling group commit
209212
* Default is false.
210213
*/
211214
public boolean j;
@@ -237,4 +240,276 @@ public String toString(){
237240
return buf.toString();
238241
}
239242

243+
/**
244+
* @return The description for <code>Mongo</code> instances created with these options
245+
*/
246+
public synchronized String getDescription() {
247+
return description;
248+
}
249+
250+
/**
251+
*
252+
* @param desc The description for <code>Mongo</code> instances created with these options
253+
*/
254+
public synchronized void setDescription(String desc) {
255+
description = desc;
256+
}
257+
258+
/**
259+
*
260+
* @return the maximum number of connections allowed per host for this Mongo instance
261+
*/
262+
public synchronized int getConnectionsPerHost() {
263+
return connectionsPerHost;
264+
}
265+
266+
/**
267+
*
268+
* @param connections sets the maximum number of connections allowed per host for this Mongo instance
269+
*/
270+
public synchronized void setConnectionsPerHost(int connections) {
271+
connectionsPerHost = connections;
272+
}
273+
274+
/**
275+
*
276+
* @return the maximum number of threads that
277+
* may be waiting for a connection
278+
*/
279+
public synchronized int getThreadsAllowedToBlockForConnectionMultiplier() {
280+
return threadsAllowedToBlockForConnectionMultiplier;
281+
}
282+
283+
/**
284+
*
285+
* @param this multiplied with connectionsPerHost, sets the maximum number of threads that
286+
* may be waiting for a connection
287+
*/
288+
public synchronized void setThreadsAllowedToBlockForConnectionMultiplier(int threads) {
289+
threadsAllowedToBlockForConnectionMultiplier = threads;
290+
}
291+
292+
/**
293+
*
294+
* @return The maximum time in milliseconds that threads wait for a connection
295+
*/
296+
public synchronized int getMaxWaitTime() {
297+
return maxWaitTime;
298+
}
299+
300+
/**
301+
*
302+
* @param timeMS set the maximum time in milliseconds that threads wait for a connection
303+
*/
304+
public synchronized void setMaxWaitTime(int timeMS) {
305+
maxWaitTime = timeMS;
306+
}
307+
308+
/**
309+
*
310+
* @return the connection timeout in milliseconds.
311+
*/
312+
public synchronized int getConnectTimeout() {
313+
return connectTimeout;
314+
}
315+
316+
/**
317+
*
318+
* @param timeoutMS set the connection timeout in milliseconds.
319+
*/
320+
public synchronized void setConnectTimeout(int timeoutMS) {
321+
connectTimeout = timeoutMS;
322+
}
323+
324+
/**
325+
*
326+
* @return The socket timeout in milliseconds
327+
*/
328+
public synchronized int getSocketTimeout() {
329+
return socketTimeout;
330+
}
331+
332+
/**
333+
*
334+
* @param timeoutMS set the socket timeout in milliseconds
335+
*/
336+
public synchronized void setSocketTimeout(int timeoutMS) {
337+
socketTimeout = timeoutMS;
338+
}
339+
340+
/**
341+
*
342+
* @return connection keep-alive flag
343+
*/
344+
public synchronized boolean isSocketKeepAlive() {
345+
return socketKeepAlive;
346+
}
347+
348+
/**
349+
*
350+
* @param keepAlive set connection keep-alive flag
351+
*/
352+
public synchronized void setSocketKeepAlive(boolean keepAlive) {
353+
socketKeepAlive = keepAlive;
354+
}
355+
356+
/**
357+
*
358+
* @return keep trying connection flag
359+
*/
360+
public synchronized boolean isAutoConnectRetry() {
361+
return autoConnectRetry;
362+
}
363+
364+
/**
365+
*
366+
* @param retry sets keep trying connection flag
367+
*/
368+
public synchronized void setAutoConnectRetry(boolean retry) {
369+
autoConnectRetry = retry;
370+
}
371+
372+
/**
373+
*
374+
* @return max time in MS to retrying open connection
375+
*/
376+
public synchronized long getMaxAutoConnectRetryTime() {
377+
return maxAutoConnectRetryTime;
378+
}
379+
380+
/**
381+
*
382+
* @param retryTimeMS set max time in MS to retrying open connection
383+
*/
384+
public synchronized void setMaxAutoConnectRetryTime(long retryTimeMS) {
385+
maxAutoConnectRetryTime = retryTimeMS;
386+
}
387+
388+
/**
389+
*
390+
* @return the DBCallback decoding factory
391+
*/
392+
public synchronized DBDecoderFactory getDbDecoderFactory() {
393+
return dbDecoderFactory;
394+
}
395+
396+
/**
397+
*
398+
* @param factory sets the DBCallback decoding factory
399+
*/
400+
public synchronized void setDbDecoderFactory(DBDecoderFactory factory) {
401+
dbDecoderFactory = factory;
402+
}
403+
404+
/**
405+
*
406+
* @return the encoding factory
407+
*/
408+
public synchronized DBEncoderFactory getDbEncoderFactory() {
409+
return dbEncoderFactory;
410+
}
411+
412+
/**
413+
*
414+
* @param factory sets the encoding factory
415+
*/
416+
public synchronized void setDbEncoderFactory(DBEncoderFactory factory) {
417+
dbEncoderFactory = factory;
418+
}
419+
420+
/**
421+
*
422+
* @return true if driver uses WriteConcern.SAFE for all operations.
423+
*/
424+
public synchronized boolean isSafe() {
425+
return safe;
426+
}
427+
428+
/**
429+
*
430+
* @param isSafe true if driver uses WriteConcern.SAFE for all operations.
431+
*/
432+
public synchronized void setSafe(boolean isSafe) {
433+
safe = isSafe;
434+
}
435+
436+
/**
437+
*
438+
* @return value returns the number of writes of the global WriteConcern.
439+
*/
440+
public synchronized int getW() {
441+
return w;
442+
}
443+
444+
/**
445+
*
446+
* @param val set the number of writes of the global WriteConcern.
447+
*/
448+
public synchronized void setW(int val) {
449+
w = val;
450+
}
451+
452+
/**
453+
*
454+
* @return timeout for write operation
455+
*/
456+
public synchronized int getWtimeout() {
457+
return wtimeout;
458+
}
459+
460+
/**
461+
*
462+
* @param timeoutMS sets timeout for write operation
463+
*/
464+
public synchronized void setWtimeout(int timeoutMS) {
465+
wtimeout = timeoutMS;
466+
}
467+
468+
/**
469+
*
470+
* @return true if global write concern is set to fsync
471+
*/
472+
public synchronized boolean isFsync() {
473+
return fsync;
474+
}
475+
476+
/**
477+
*
478+
* @param sync sets global write concern's fsync safe value
479+
*/
480+
public synchronized void setFsync(boolean sync) {
481+
fsync = sync;
482+
}
483+
484+
/**
485+
*
486+
* @return true if global write concern is set to journal safe
487+
*/
488+
public synchronized boolean isJ() {
489+
return j;
490+
}
491+
492+
/**
493+
*
494+
* @param safe sets global write concern's journal safe value
495+
*/
496+
public synchronized void setJ(boolean safe) {
497+
j = safe;
498+
}
499+
500+
/**
501+
*
502+
* @return the socket factory for creating sockets to mongod
503+
*/
504+
public synchronized SocketFactory getSocketFactory() {
505+
return socketFactory;
506+
}
507+
508+
/**
509+
*
510+
* @param factory sets the socket factory for creating sockets to mongod
511+
*/
512+
public synchronized void setSocketFactory(SocketFactory factory) {
513+
socketFactory = factory;
514+
}
240515
}

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

0 commit comments

Comments
 (0)