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 import java.util.Arrays;
24
25 import com.allanbank.mongodb.ReadPreference;
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.client.Message;
30 import com.allanbank.mongodb.client.Operation;
31 import com.allanbank.mongodb.error.DocumentToLargeException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class KillCursors extends AbstractMessage {
54
55
56 private final long[] myCursorIds;
57
58
59
60
61
62
63
64
65
66 public KillCursors(final BsonInputStream in) throws IOException {
67 init(".");
68
69 in.readInt();
70 final int numberOfCursors = in.readInt();
71 myCursorIds = new long[numberOfCursors];
72 for (int i = 0; i < numberOfCursors; ++i) {
73 myCursorIds[i] = in.readLong();
74 }
75 }
76
77
78
79
80
81
82
83
84
85 public KillCursors(final long[] cursorIds,
86 final ReadPreference readPreference) {
87 super("", "", readPreference);
88 myCursorIds = Arrays.copyOf(cursorIds, cursorIds.length);
89 }
90
91
92
93
94
95
96
97
98
99
100 @Override
101 public boolean equals(final Object object) {
102 boolean result = false;
103 if (this == object) {
104 result = true;
105 }
106 else if ((object != null) && (getClass() == object.getClass())) {
107 final KillCursors other = (KillCursors) object;
108
109
110 result = Arrays.equals(myCursorIds, other.myCursorIds);
111 }
112 return result;
113 }
114
115
116
117
118
119
120 public long[] getCursorIds() {
121 return Arrays.copyOf(myCursorIds, myCursorIds.length);
122 }
123
124
125
126
127
128
129
130 @Override
131 public String getOperationName() {
132 return Operation.KILL_CURSORS.name();
133 }
134
135
136
137
138
139
140 @Override
141 public int hashCode() {
142 int result = 1;
143 result = (31 * result) + super.hashCode();
144 result = (31 * result) + Arrays.hashCode(myCursorIds);
145 return result;
146 }
147
148
149
150
151
152
153
154 @Override
155 public int size() {
156
157 int size = HEADER_SIZE + 8;
158
159
160 size += (8 * myCursorIds.length);
161
162 return size;
163 }
164
165
166
167
168
169
170
171 @Override
172 public void validateSize(final int maxDocumentSize)
173 throws DocumentToLargeException {
174 if (maxDocumentSize < (myCursorIds.length * 8)) {
175 throw new DocumentToLargeException((myCursorIds.length * 8),
176 maxDocumentSize, null);
177 }
178 }
179
180
181
182
183
184
185
186
187
188 @Override
189 public void write(final int messageId, final BsonOutputStream out)
190 throws IOException {
191 int size = HEADER_SIZE;
192 size += 4;
193 size += 4;
194 size += (8 * myCursorIds.length);
195
196 writeHeader(out, messageId, 0, Operation.KILL_CURSORS, size);
197 out.writeInt(0);
198 out.writeInt(myCursorIds.length);
199 for (final long myCursorId : myCursorIds) {
200 out.writeLong(myCursorId);
201 }
202 }
203
204
205
206
207
208
209
210
211
212 @Override
213 public void write(final int messageId, final BufferingBsonOutputStream out)
214 throws IOException {
215 final long start = writeHeader(out, messageId, 0,
216 Operation.KILL_CURSORS);
217 out.writeInt(0);
218 out.writeInt(myCursorIds.length);
219 for (final long myCursorId : myCursorIds) {
220 out.writeLong(myCursorId);
221 }
222 finishHeader(out, start);
223
224 out.flushBuffer();
225 }
226 }