1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb.client.message;
21
22 import java.io.IOException;
23
24 import com.allanbank.mongodb.ReadPreference;
25 import com.allanbank.mongodb.bson.Document;
26 import com.allanbank.mongodb.bson.io.BsonInputStream;
27 import com.allanbank.mongodb.bson.io.BsonOutputStream;
28 import com.allanbank.mongodb.bson.io.BufferingBsonOutputStream;
29 import com.allanbank.mongodb.bson.io.StringEncoder;
30 import com.allanbank.mongodb.client.Message;
31 import com.allanbank.mongodb.client.Operation;
32 import com.allanbank.mongodb.error.DocumentToLargeException;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class Delete extends AbstractMessage {
56
57
58 public static final int SINGLE_DELETE_BIT = 1;
59
60
61 private final Document myQuery;
62
63
64
65
66
67 private final boolean mySingleDelete;
68
69
70
71
72
73
74
75
76
77 public Delete(final BsonInputStream in) throws IOException {
78 in.readInt();
79 init(in.readCString());
80 final int flags = in.readInt();
81 myQuery = in.readDocument();
82 mySingleDelete = (flags & SINGLE_DELETE_BIT) == SINGLE_DELETE_BIT;
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public Delete(final String databaseName, final String collectionName,
99 final Document query, final boolean singleDelete) {
100 super(databaseName, collectionName, ReadPreference.PRIMARY);
101 myQuery = query;
102 mySingleDelete = singleDelete;
103 }
104
105
106
107
108
109
110
111
112
113
114 @Override
115 public boolean equals(final Object object) {
116 boolean result = false;
117 if (this == object) {
118 result = true;
119 }
120 else if ((object != null) && (getClass() == object.getClass())) {
121 final Delete other = (Delete) object;
122
123 result = super.equals(object)
124 && (mySingleDelete == other.mySingleDelete)
125 && myQuery.equals(other.myQuery);
126 }
127 return result;
128 }
129
130
131
132
133
134
135
136 @Override
137 public String getOperationName() {
138 return Operation.DELETE.name();
139 }
140
141
142
143
144
145
146 public Document getQuery() {
147 return myQuery;
148 }
149
150
151
152
153
154
155 @Override
156 public int hashCode() {
157 int result = 1;
158 result = (31 * result) + super.hashCode();
159 result = (31 * result) + (mySingleDelete ? 1 : 3);
160 result = (31 * result) + myQuery.hashCode();
161 return result;
162 }
163
164
165
166
167
168
169
170 public boolean isSingleDelete() {
171 return mySingleDelete;
172 }
173
174
175
176
177
178
179
180 @Override
181 public int size() {
182 int size = HEADER_SIZE + 10;
183
184 size += StringEncoder.utf8Size(myDatabaseName);
185
186 size += StringEncoder.utf8Size(myCollectionName);
187
188
189 size += myQuery.size();
190
191 return size;
192 }
193
194
195
196
197
198
199
200 @Override
201 public void validateSize(final int maxDocumentSize)
202 throws DocumentToLargeException {
203 final long size = myQuery.size();
204 if (maxDocumentSize < size) {
205 throw new DocumentToLargeException((int) size, maxDocumentSize,
206 myQuery);
207 }
208 }
209
210
211
212
213
214
215
216
217
218 @Override
219 public void write(final int messageId, final BsonOutputStream out)
220 throws IOException {
221 final int flags = computeFlags();
222
223 int size = HEADER_SIZE;
224 size += 4;
225 size += out.sizeOfCString(myDatabaseName, ".", myCollectionName);
226 size += 4;
227 size += myQuery.size();
228
229 writeHeader(out, messageId, 0, Operation.DELETE, size);
230 out.writeInt(0);
231 out.writeCString(myDatabaseName, ".", myCollectionName);
232 out.writeInt(flags);
233 out.writeDocument(myQuery);
234 }
235
236
237
238
239
240
241
242
243
244 @Override
245 public void write(final int messageId, final BufferingBsonOutputStream out)
246 throws IOException {
247 final int flags = computeFlags();
248
249 final long start = writeHeader(out, messageId, 0, Operation.DELETE);
250 out.writeInt(0);
251 out.writeCString(myDatabaseName, ".", myCollectionName);
252 out.writeInt(flags);
253 out.writeDocument(myQuery);
254 finishHeader(out, start);
255
256 out.flushBuffer();
257 }
258
259
260
261
262
263
264 private int computeFlags() {
265 int flags = 0;
266 if (mySingleDelete) {
267 flags += SINGLE_DELETE_BIT;
268 }
269 return flags;
270 }
271 }