Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GetLastError |
|
| 2.0;2 |
1 | /* | |
2 | * #%L | |
3 | * GetLastError.java - mongodb-async-driver - Allanbank Consulting, Inc. | |
4 | * %% | |
5 | * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. | |
6 | * %% | |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
8 | * you may not use this file except in compliance with the License. | |
9 | * You may obtain a copy of the License at | |
10 | * | |
11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
12 | * | |
13 | * Unless required by applicable law or agreed to in writing, software | |
14 | * distributed under the License is distributed on an "AS IS" BASIS, | |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
16 | * See the License for the specific language governing permissions and | |
17 | * limitations under the License. | |
18 | * #L% | |
19 | */ | |
20 | package com.allanbank.mongodb.client.message; | |
21 | ||
22 | import java.io.StringWriter; | |
23 | ||
24 | import com.allanbank.mongodb.Durability; | |
25 | import com.allanbank.mongodb.ReadPreference; | |
26 | import com.allanbank.mongodb.bson.Document; | |
27 | import com.allanbank.mongodb.bson.builder.BuilderFactory; | |
28 | import com.allanbank.mongodb.bson.builder.DocumentBuilder; | |
29 | import com.allanbank.mongodb.bson.element.JsonSerializationVisitor; | |
30 | import com.allanbank.mongodb.client.connection.Connection; | |
31 | ||
32 | /** | |
33 | * Provides a convenient mechanism for creating a <a href= | |
34 | * "http://www.mongodb.org/display/DOCS/getLastError+Command" >getlasterror</a> | |
35 | * command. | |
36 | * <p> | |
37 | * This is a helper class for retrieving the results of {@link Delete}, | |
38 | * {@link Insert}, and {@link Update} commands. Get last error is not a part of | |
39 | * the standard wire protocol but is provided here due to the frequency of | |
40 | * usage. | |
41 | * </p> | |
42 | * | |
43 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
44 | * mutated in incompatible ways between any two releases of the driver. | |
45 | * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved | |
46 | */ | |
47 | public class GetLastError extends Query { | |
48 | ||
49 | /** | |
50 | * Creates the query document for the getlasterror command. | |
51 | * | |
52 | * @param fsync | |
53 | * If true the command waits for an fsync of the data to have | |
54 | * completed. | |
55 | * @param waitForJournal | |
56 | * If true the command waits for the preceding command to have | |
57 | * been written to the journal. | |
58 | * @param w | |
59 | * The replication factor to wait for. | |
60 | * @param wtimeout | |
61 | * The amount of time (in milliseconds) to wait for the write to | |
62 | * finish. | |
63 | * @return The command's query document. | |
64 | */ | |
65 | private static Document createQuery(final boolean fsync, | |
66 | final boolean waitForJournal, final int w, final int wtimeout) { | |
67 | 93 | final DocumentBuilder builder = BuilderFactory.start(); |
68 | 93 | builder.addInteger("getlasterror", 1); |
69 | 93 | if (waitForJournal) { |
70 | 2 | builder.addBoolean("j", waitForJournal); |
71 | } | |
72 | 93 | if (fsync) { |
73 | 2 | builder.addBoolean("fsync", fsync); |
74 | } | |
75 | 93 | if (w >= 1) { |
76 | 64 | builder.addInteger("w", w); |
77 | } | |
78 | 93 | if (wtimeout > 0) { |
79 | 2 | builder.addInteger("wtimeout", wtimeout); |
80 | } | |
81 | 93 | return builder.build(); |
82 | } | |
83 | ||
84 | /** | |
85 | * Create a new GetLastError. | |
86 | * | |
87 | * @param dbName | |
88 | * The name of the database. | |
89 | * @param fsync | |
90 | * If true the command waits for an fsync of the data to have | |
91 | * completed. | |
92 | * @param waitForJournal | |
93 | * If true the command waits for the preceding command to have | |
94 | * been written to the journal. | |
95 | * @param w | |
96 | * The replication factor to wait for. | |
97 | * @param wtimeout | |
98 | * The amount of time (in milliseconds) to wait for the write to | |
99 | * finish. | |
100 | */ | |
101 | public GetLastError(final String dbName, final boolean fsync, | |
102 | final boolean waitForJournal, final int w, final int wtimeout) { | |
103 | 93 | super(dbName, Connection.COMMAND_COLLECTION, createQuery(fsync, |
104 | waitForJournal, w, wtimeout), /* fields= */null, | |
105 | /* batchSize= */1, /* limit= */1, /* numberToSkip= */0, | |
106 | /* tailable= */false, ReadPreference.PRIMARY, | |
107 | /* noCursorTimeout= */false, /* awaitData= */false, | |
108 | /* exhaust= */false, /* partial= */false); | |
109 | 93 | } |
110 | ||
111 | /** | |
112 | * Create a new GetLastError. | |
113 | * | |
114 | * @param dbName | |
115 | * The name of the database. | |
116 | * @param durability | |
117 | * The Durability requested. | |
118 | */ | |
119 | public GetLastError(final String dbName, final Durability durability) { | |
120 | 102 | super(dbName, Connection.COMMAND_COLLECTION, durability.asDocument(), |
121 | /* fields= */null, /* batchSize= */1, /* limit= */1, | |
122 | /* numberToSkip= */0, /* tailable= */false, ReadPreference.PRIMARY, | |
123 | /* noCursorTimeout= */false, /* awaitData= */false, | |
124 | /* exhaust= */false, /* partial= */false); | |
125 | 102 | } |
126 | ||
127 | /** | |
128 | * {@inheritDoc} | |
129 | * <p> | |
130 | * Overridden to returns the getLastError on a single line. | |
131 | * </p> | |
132 | */ | |
133 | @Override | |
134 | public String toString() { | |
135 | 0 | final StringWriter sink = new StringWriter(); |
136 | ||
137 | 0 | final JsonSerializationVisitor visitor = new JsonSerializationVisitor( |
138 | sink, true); | |
139 | 0 | getQuery().accept(visitor); |
140 | ||
141 | 0 | return sink.toString(); |
142 | } | |
143 | } |