1f10dd97ee288ae85fc4216745eab5ef5f11b5a6
[aai/esr-gui.git] /
1 "use strict";
2
3 var needSlaveOk = ['primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
4
5 /**
6  * @fileOverview The **ReadPreference** class is a class that represents a MongoDB ReadPreference and is
7  * used to construct connections.
8  *
9  * @example
10  * var ReplSet = require('mongodb-core').ReplSet
11  *   , ReadPreference = require('mongodb-core').ReadPreference
12  *   , assert = require('assert');
13  *
14  * var server = new ReplSet([{host: 'localhost', port: 30000}], {setName: 'rs'});
15  * // Wait for the connection event
16  * server.on('connect', function(server) {
17  *   var cursor = server.cursor('db.test'
18  *     , {find: 'db.test', query: {}}
19  *     , {readPreference: new ReadPreference('secondary')});
20  *   cursor.next(function(err, doc) {
21  *     server.destroy();
22  *   });
23  * });
24  *
25  * // Start connecting
26  * server.connect();
27  */
28
29 /**
30  * Creates a new Pool instance
31  * @class
32  * @param {string} preference A string describing the preference (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
33  * @param {array} tags The tags object
34  * @param {object} [options] Additional read preference options
35  * @param {number} [options.maxStalenessSeconds] Max Secondary Read Stalleness in Seconds, Minimum value is 90 seconds.
36  * @property {string} preference The preference string (primary|primaryPreferred|secondary|secondaryPreferred|nearest)
37  * @property {array} tags The tags object
38  * @property {object} options Additional read preference options
39  * @property {number} maxStalenessSeconds MaxStalenessSeconds value for the read preference
40  * @return {ReadPreference}
41  */
42 var ReadPreference = function(preference, tags, options) {
43   this.preference = preference;
44   this.tags = tags;
45   this.options = options;
46
47   // Add the maxStalenessSeconds value to the read Preference
48   if(this.options && this.options.maxStalenessSeconds != null) {
49     this.options = options;
50     this.maxStalenessSeconds = this.options.maxStalenessSeconds >= 0
51       ? this.options.maxStalenessSeconds : null;
52   } else if(tags && typeof tags == 'object') {
53     this.options = tags, tags = null;
54   }
55 }
56
57 /**
58  * This needs slaveOk bit set
59  * @method
60  * @return {boolean}
61  */
62 ReadPreference.prototype.slaveOk = function() {
63   return needSlaveOk.indexOf(this.preference) != -1;
64 }
65
66 /**
67  * Are the two read preference equal
68  * @method
69  * @return {boolean}
70  */
71 ReadPreference.prototype.equals = function(readPreference) {
72   return readPreference.preference == this.preference;
73 }
74
75 /**
76  * Return JSON representation
77  * @method
78  * @return {Object}
79  */
80 ReadPreference.prototype.toJSON = function() {
81   var readPreference = {mode: this.preference};
82   if(Array.isArray(this.tags)) readPreference.tags = this.tags;
83   if(this.maxStalenessSeconds) readPreference.maxStalenessSeconds = this.maxStalenessSeconds;
84   return readPreference;
85 }
86
87 /**
88  * Primary read preference
89  * @method
90  * @return {ReadPreference}
91  */
92 ReadPreference.primary = new ReadPreference('primary');
93 /**
94  * Primary Preferred read preference
95  * @method
96  * @return {ReadPreference}
97  */
98 ReadPreference.primaryPreferred = new ReadPreference('primaryPreferred');
99 /**
100  * Secondary read preference
101  * @method
102  * @return {ReadPreference}
103  */
104 ReadPreference.secondary = new ReadPreference('secondary');
105 /**
106  * Secondary Preferred read preference
107  * @method
108  * @return {ReadPreference}
109  */
110 ReadPreference.secondaryPreferred = new ReadPreference('secondaryPreferred');
111 /**
112  * Nearest read preference
113  * @method
114  * @return {ReadPreference}
115  */
116 ReadPreference.nearest = new ReadPreference('nearest');
117
118 module.exports = ReadPreference;