Skip to content

Commit 731840a

Browse files
committed
Add getters/setters to MongoOptions class
1 parent 1497db1 commit 731840a

File tree

2 files changed

+320
-3
lines changed

2 files changed

+320
-3
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/test/com/mongodb/MongoOptionsTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,47 @@ public void testCopy() throws Exception {
7070
assertEquals(options.socketFactory, copy.socketFactory);
7171
assertEquals(options.description, copy.description);
7272
}
73+
74+
@Test
75+
public void testGetterSetters() throws Exception {
76+
77+
final MongoOptions options = new MongoOptions();
78+
79+
options.setConnectionsPerHost(100);
80+
options.setThreadsAllowedToBlockForConnectionMultiplier(101);
81+
options.setMaxWaitTime(102);
82+
options.setConnectTimeout(103);
83+
options.setSocketTimeout(104);
84+
options.setSocketKeepAlive(true);
85+
options.setAutoConnectRetry(true);
86+
options.setMaxAutoConnectRetryTime(105);
87+
options.setSafe(true);
88+
options.setW(106);
89+
options.setWtimeout(107);
90+
options.setFsync(true);
91+
options.setJ(false);
92+
options.setDbDecoderFactory(null);
93+
options.setDbEncoderFactory(null);
94+
options.setSocketFactory(null);
95+
options.setDescription("very cool");
96+
97+
assertEquals(options.getConnectionsPerHost(), 100);
98+
assertEquals(options.getThreadsAllowedToBlockForConnectionMultiplier(), 101);
99+
assertEquals(options.getMaxWaitTime(), 102);
100+
assertEquals(options.getConnectTimeout(), 103);
101+
assertEquals(options.getSocketTimeout(), 104);
102+
assertEquals(options.isSocketKeepAlive(), true);
103+
assertEquals(options.isAutoConnectRetry(), true);
104+
assertEquals(options.getMaxAutoConnectRetryTime(), 105);
105+
assertEquals(options.isSafe(), true);
106+
assertEquals(options.getW(), 106);
107+
assertEquals(options.getWtimeout(), 107);
108+
assertEquals(options.isFsync(), true);
109+
assertEquals(options.isJ(), false);
110+
assertEquals(options.getDbDecoderFactory(), null);
111+
assertEquals(options.getDbEncoderFactory(), null);
112+
assertEquals(options.getSocketFactory(), null);
113+
assertEquals(options.getDescription(), "very cool");
114+
}
73115
}
74116

0 commit comments

Comments
 (0)