Skip to content

Commit 071c67d

Browse files
DATAMONGO-1855 - Polishing
Introduce base class to share code between imperative and reactive GridFs.
1 parent 53a354d commit 071c67d

File tree

7 files changed

+422
-257
lines changed

7 files changed

+422
-257
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/AsyncInputStreamAdapter.java

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* {@link #close()} is propagated as cancellation signal to the binary {@link Publisher}.
5050
*
5151
* @author Mark Paluch
52+
* @author Christoph Strobl
5253
* @since 2.2
5354
*/
5455
@RequiredArgsConstructor
@@ -94,20 +95,23 @@ public Publisher<Integer> read(ByteBuffer dst) {
9495
try {
9596

9697
if (error != null) {
98+
9799
sink.error(error);
98100
return;
99101
}
100102

101103
if (bytecount == -1) {
104+
102105
sink.success(-1);
103106
return;
104107
}
105108

106109
ByteBuffer byteBuffer = db.asByteBuffer();
107110
int toWrite = byteBuffer.remaining();
108-
dst.put(byteBuffer);
109111

112+
dst.put(byteBuffer);
110113
sink.success(toWrite);
114+
111115
} catch (Exception e) {
112116
sink.error(e);
113117
} finally {
@@ -127,6 +131,7 @@ public Publisher<Integer> read(ByteBuffer dst) {
127131
public Publisher<Success> close() {
128132

129133
return Mono.create(sink -> {
134+
130135
cancelled = true;
131136

132137
if (error != null) {
@@ -141,6 +146,7 @@ public Publisher<Success> close() {
141146
protected void request(int n) {
142147

143148
if (complete) {
149+
144150
terminatePendingReads();
145151
return;
146152
}
@@ -150,67 +156,9 @@ protected void request(int n) {
150156
if (SUBSCRIBED.get(this) == SUBSCRIPTION_NOT_SUBSCRIBED) {
151157

152158
if (SUBSCRIBED.compareAndSet(this, SUBSCRIPTION_NOT_SUBSCRIBED, SUBSCRIPTION_SUBSCRIBED)) {
153-
154-
buffers.subscribe(new CoreSubscriber<DataBuffer>() {
155-
156-
@Override
157-
public Context currentContext() {
158-
return subscriberContext;
159-
}
160-
161-
@Override
162-
public void onSubscribe(Subscription s) {
163-
subscription = s;
164-
165-
Operators.addCap(DEMAND, AsyncInputStreamAdapter.this, -1);
166-
s.request(1);
167-
}
168-
169-
@Override
170-
public void onNext(DataBuffer dataBuffer) {
171-
172-
if (cancelled || complete) {
173-
DataBufferUtils.release(dataBuffer);
174-
Operators.onNextDropped(dataBuffer, subscriberContext);
175-
return;
176-
}
177-
178-
BiConsumer<DataBuffer, Integer> poll = readRequests.poll();
179-
180-
if (poll == null) {
181-
182-
DataBufferUtils.release(dataBuffer);
183-
Operators.onNextDropped(dataBuffer, subscriberContext);
184-
subscription.cancel();
185-
return;
186-
}
187-
188-
poll.accept(dataBuffer, dataBuffer.readableByteCount());
189-
190-
requestFromSubscription(subscription);
191-
}
192-
193-
@Override
194-
public void onError(Throwable t) {
195-
196-
if (cancelled || complete) {
197-
Operators.onErrorDropped(t, subscriberContext);
198-
return;
199-
}
200-
201-
error = t;
202-
complete = true;
203-
terminatePendingReads();
204-
}
205-
206-
@Override
207-
public void onComplete() {
208-
209-
complete = true;
210-
terminatePendingReads();
211-
}
212-
});
159+
buffers.subscribe(new DataBufferCoreSubscriber());
213160
}
161+
214162
} else {
215163

216164
Subscription subscription = this.subscription;
@@ -245,4 +193,65 @@ void terminatePendingReads() {
245193
readers.accept(factory.wrap(new byte[0]), -1);
246194
}
247195
}
196+
197+
private class DataBufferCoreSubscriber implements CoreSubscriber<DataBuffer> {
198+
199+
@Override
200+
public Context currentContext() {
201+
return AsyncInputStreamAdapter.this.subscriberContext;
202+
}
203+
204+
@Override
205+
public void onSubscribe(Subscription s) {
206+
207+
AsyncInputStreamAdapter.this.subscription = s;
208+
209+
Operators.addCap(DEMAND, AsyncInputStreamAdapter.this, -1);
210+
s.request(1);
211+
}
212+
213+
@Override
214+
public void onNext(DataBuffer dataBuffer) {
215+
216+
if (cancelled || complete) {
217+
DataBufferUtils.release(dataBuffer);
218+
Operators.onNextDropped(dataBuffer, AsyncInputStreamAdapter.this.subscriberContext);
219+
return;
220+
}
221+
222+
BiConsumer<DataBuffer, Integer> poll = AsyncInputStreamAdapter.this.readRequests.poll();
223+
224+
if (poll == null) {
225+
226+
DataBufferUtils.release(dataBuffer);
227+
Operators.onNextDropped(dataBuffer, AsyncInputStreamAdapter.this.subscriberContext);
228+
subscription.cancel();
229+
return;
230+
}
231+
232+
poll.accept(dataBuffer, dataBuffer.readableByteCount());
233+
234+
requestFromSubscription(subscription);
235+
}
236+
237+
@Override
238+
public void onError(Throwable t) {
239+
240+
if (AsyncInputStreamAdapter.this.cancelled || AsyncInputStreamAdapter.this.complete) {
241+
Operators.onErrorDropped(t, AsyncInputStreamAdapter.this.subscriberContext);
242+
return;
243+
}
244+
245+
AsyncInputStreamAdapter.this.error = t;
246+
AsyncInputStreamAdapter.this.complete = true;
247+
terminatePendingReads();
248+
}
249+
250+
@Override
251+
public void onComplete() {
252+
253+
AsyncInputStreamAdapter.this.complete = true;
254+
terminatePendingReads();
255+
}
256+
}
248257
}

0 commit comments

Comments
 (0)