1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb;
21
22 import java.beans.PropertyEditorSupport;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class DurabilityEditor extends PropertyEditorSupport {
63
64
65 public static final int DEFAULT_WAIT_TIME_MS = 30000;
66
67
68 public static final Set<String> MONGODB_URI_FIELDS;
69
70 static {
71 final Set<String> fields = new HashSet<String>();
72 fields.add("safe");
73 fields.add("w");
74 fields.add("wtimeout");
75 fields.add("wtimeoutms");
76 fields.add("fsync");
77 fields.add("journal");
78
79 MONGODB_URI_FIELDS = Collections.unmodifiableSet(fields);
80 }
81
82
83
84
85 public DurabilityEditor() {
86 super();
87 }
88
89
90
91
92
93
94
95
96
97
98 @Override
99 public void setAsText(final String durabilityString)
100 throws IllegalArgumentException {
101
102 if ("NONE".equalsIgnoreCase(durabilityString)) {
103 setValue(Durability.NONE);
104 }
105 else if ("ACK".equalsIgnoreCase(durabilityString)) {
106 setValue(Durability.ACK);
107 }
108 else if ("FSYNC".equalsIgnoreCase(durabilityString)) {
109 setValue(Durability.fsyncDurable(DEFAULT_WAIT_TIME_MS));
110 }
111 else if ("JOURNAL".equalsIgnoreCase(durabilityString)) {
112 setValue(Durability.journalDurable(DEFAULT_WAIT_TIME_MS));
113 }
114 else if ("MAJORITY".equalsIgnoreCase(durabilityString)) {
115 setValue(Durability.replicaDurable(Durability.MAJORITY_MODE,
116 DEFAULT_WAIT_TIME_MS));
117 }
118 else if (MongoDbUri.isUri(durabilityString)) {
119 final MongoDbUri uri = new MongoDbUri(durabilityString);
120 final Durability parsed = fromUriParameters(uri.getParsedOptions());
121 if (parsed != null) {
122 setValue(parsed);
123 }
124 }
125 else {
126 final Durability durability = Durability.valueOf(durabilityString);
127 if (durability != null) {
128 setValue(durability);
129 }
130 else {
131 throw new IllegalArgumentException(
132 "Could not determine the durability for '"
133 + durabilityString + "'.");
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145
146 private Durability fromUriParameters(final Map<String, String> parameters) {
147 boolean safe = false;
148 int w = 1;
149 String wTxt = null;
150 boolean fsync = false;
151 boolean journal = false;
152 int wtimeout = 0;
153
154
155 for (final Map.Entry<String, String> entry : parameters.entrySet()) {
156 final String parameter = entry.getKey();
157 final String value = entry.getValue();
158
159 if ("safe".equalsIgnoreCase(parameter)) {
160 safe = Boolean.parseBoolean(value);
161 }
162 else if ("w".equalsIgnoreCase(parameter)) {
163 safe = true;
164 try {
165 w = Integer.parseInt(value);
166 wTxt = null;
167 }
168 catch (final NumberFormatException nfe) {
169 w = 2;
170 wTxt = value;
171 }
172 }
173 else if ("wtimeout".equalsIgnoreCase(parameter)
174 || "wtimeoutms".equalsIgnoreCase(parameter)) {
175 safe = true;
176 wtimeout = Integer.parseInt(value);
177 if (w <= 1) {
178 w = 2;
179 }
180 }
181 else if ("fsync".equalsIgnoreCase(parameter)) {
182 fsync = Boolean.parseBoolean(value);
183 if (fsync) {
184 journal = false;
185 safe = true;
186 }
187 }
188 else if ("journal".equalsIgnoreCase(parameter)) {
189 journal = Boolean.parseBoolean(value);
190 if (journal) {
191 fsync = false;
192 safe = true;
193 }
194 }
195 }
196
197
198 Durability result = null;
199 if (safe) {
200 if (fsync) {
201 result = Durability.fsyncDurable(wtimeout);
202 }
203 else if (journal) {
204 result = Durability.journalDurable(wtimeout);
205 }
206 else if (w == 1) {
207 result = Durability.ACK;
208 }
209 else if (w > 1) {
210 if (wTxt != null) {
211 result = Durability.replicaDurable(wTxt, wtimeout);
212 }
213 else {
214 result = Durability.replicaDurable(w, wtimeout);
215 }
216 }
217 else if (w <= 0) {
218 result = Durability.NONE;
219 }
220 }
221 return result;
222
223 }
224 }