1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.allanbank.mongodb.client.callback;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicBoolean;
27
28 import com.allanbank.mongodb.Callback;
29 import com.allanbank.mongodb.MongoDbException;
30 import com.allanbank.mongodb.MongoIterator;
31 import com.allanbank.mongodb.bson.Document;
32 import com.allanbank.mongodb.client.Client;
33 import com.allanbank.mongodb.client.MongoIteratorImpl;
34 import com.allanbank.mongodb.client.message.CursorableMessage;
35 import com.allanbank.mongodb.client.message.Query;
36 import com.allanbank.mongodb.client.message.Reply;
37
38
39
40
41
42
43
44
45
46 public final class MultipleCursorCallback extends
47 AbstractReplyCallback<Collection<MongoIterator<Document>>> implements
48 AddressAware {
49
50
51 private volatile String myAddress;
52
53
54 private final Client myClient;
55
56
57 private final CursorableMessage myMessage;
58
59
60 private volatile Reply myReply;
61
62
63
64
65
66 private final AtomicBoolean mySetOther;
67
68
69
70
71
72
73
74
75
76
77
78
79 public MultipleCursorCallback(final Client client,
80 final CursorableMessage message,
81 final Callback<Collection<MongoIterator<Document>>> results) {
82
83 super(results);
84
85 myClient = client;
86 myMessage = message;
87
88 mySetOther = new AtomicBoolean(false);
89 }
90
91
92
93
94
95
96 public String getAddress() {
97 return myAddress;
98 }
99
100
101
102
103
104
105
106 @Override
107 public void setAddress(final String address) {
108 myAddress = address;
109 trigger();
110 }
111
112
113
114
115
116
117
118
119
120 @Override
121 protected MongoDbException asError(final Reply reply, final int okValue,
122 final int errorNumber, final String errorMessage) {
123 return super.asError(reply, okValue, errorNumber, false, errorMessage,
124 myMessage);
125 }
126
127
128
129
130
131
132
133
134
135 @Override
136 protected Collection<MongoIterator<Document>> convert(final Reply reply)
137 throws MongoDbException {
138 final List<Reply> results = CommandCursorTranslator.translateAll(reply);
139 final List<MongoIterator<Document>> iters = new ArrayList<MongoIterator<Document>>(
140 results.size());
141 for (final Reply r : results) {
142 iters.add(new MongoIteratorImpl(myMessage, myClient, myAddress, r));
143 }
144 return iters;
145 }
146
147
148
149
150
151
152
153
154
155 @Override
156 protected void handle(final Reply reply) {
157 myReply = reply;
158 trigger();
159 }
160
161
162
163
164 private void trigger() {
165 if (!mySetOther.compareAndSet(false, true)) {
166 super.handle(myReply);
167 }
168 }
169 }