|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2020 MarkLogic Corporation |
| 2 | + * Copyright (c) 2021 MarkLogic Corporation |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 |
|
18 | 18 | import com.fasterxml.jackson.databind.JsonNode;
|
19 | 19 | import com.marklogic.client.DatabaseClient;
|
| 20 | +import com.marklogic.client.MarkLogicInternalException; |
20 | 21 | import com.marklogic.client.SessionState;
|
21 | 22 | import com.marklogic.client.impl.BaseProxy;
|
22 | 23 | import com.marklogic.client.impl.NodeConverter;
|
23 | 24 | import com.marklogic.client.impl.RESTServices;
|
24 | 25 | import com.marklogic.client.io.BaseHandle;
|
25 | 26 | import com.marklogic.client.io.BytesHandle;
|
26 | 27 | import com.marklogic.client.io.marker.BufferableContentHandle;
|
27 |
| -import com.marklogic.client.io.marker.BufferableHandle; |
28 | 28 | import com.marklogic.client.io.marker.JSONWriteHandle;
|
29 | 29 |
|
30 |
| -import java.util.stream.Stream; |
31 |
| - |
32 | 30 | abstract class IOCallerImpl<I,O> extends BaseCallerImpl {
|
33 | 31 | private final JsonNode apiDeclaration;
|
34 | 32 | private final String endpointPath;
|
35 | 33 | private final BaseProxy.DBFunctionRequest requester;
|
36 | 34 |
|
37 |
| - private BufferableContentHandle<I,?> inputHandle; |
38 |
| - private BufferableContentHandle<O,?> outputHandle; |
| 35 | + private BufferableContentHandle<?,?> inputHandle; |
| 36 | + private BufferableContentHandle<?,?> outputHandle; |
39 | 37 |
|
40 | 38 | private ParamdefImpl endpointStateParamdef;
|
41 | 39 | private ParamdefImpl sessionParamdef;
|
42 | 40 | private ParamdefImpl endpointConstantsParamdef;
|
43 | 41 | private ParamdefImpl inputParamdef;
|
44 | 42 | private ReturndefImpl returndef;
|
45 | 43 |
|
| 44 | + private boolean isHandleIO = false; |
| 45 | + |
46 | 46 | IOCallerImpl(
|
47 |
| - JSONWriteHandle apiDeclaration, BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<O,?> outputHandle |
| 47 | + JSONWriteHandle apiDeclaration, boolean isHandleIO, |
| 48 | + BufferableContentHandle<?,?> inputHandle, BufferableContentHandle<?,?> outputHandle |
48 | 49 | ) {
|
49 | 50 | super();
|
50 | 51 |
|
| 52 | + this.isHandleIO = isHandleIO; |
| 53 | + |
51 | 54 | if (apiDeclaration== null) {
|
52 | 55 | throw new IllegalArgumentException("null endpoint declaration");
|
53 | 56 | }
|
@@ -178,18 +181,40 @@ abstract class IOCallerImpl<I,O> extends BaseCallerImpl {
|
178 | 181 | );
|
179 | 182 | }
|
180 | 183 |
|
181 |
| - BufferableContentHandle<I, ?> getInputHandle() { |
182 |
| - return inputHandle; |
183 |
| - } |
184 |
| - BufferableContentHandle<O, ?> getOutputHandle() { |
| 184 | + BufferableContentHandle<?, ?> getOutputHandle() { |
185 | 185 | return outputHandle;
|
186 | 186 | }
|
| 187 | + BufferableContentHandle<?, ?> castInputAsHandle(I input) { |
| 188 | + if (!isHandleIO) { |
| 189 | + throw new MarkLogicInternalException("Cannot cast input to handle unless using handles for IO"); |
| 190 | + } |
| 191 | + return (BufferableContentHandle<?, ?>) input; |
| 192 | + } |
| 193 | + O castHandleAsOutput(BufferableContentHandle<?, ?> handle) { |
| 194 | + if (!isHandleIO) { |
| 195 | + throw new MarkLogicInternalException("Cannot cast handle to output unless using handles for IO"); |
| 196 | + } |
| 197 | + return (O) handle; |
| 198 | + } |
| 199 | + |
| 200 | + BufferableContentHandle<I, ?> getContentInputHandle() { |
| 201 | + if (isHandleIO) { |
| 202 | + throw new MarkLogicInternalException("Cannot get handle for input when using handles for IO"); |
| 203 | + } |
| 204 | + return (BufferableContentHandle<I, ?>) inputHandle; |
| 205 | + } |
| 206 | + BufferableContentHandle<O, ?> getContentOutputHandle() { |
| 207 | + if (isHandleIO) { |
| 208 | + throw new MarkLogicInternalException("Cannot get handle for output when using handles for IO"); |
| 209 | + } |
| 210 | + return (BufferableContentHandle<O, ?>) outputHandle; |
| 211 | + } |
187 | 212 |
|
188 | 213 | BaseProxy.DBFunctionRequest makeRequest(DatabaseClient db, CallContextImpl<I,O> callCtxt) {
|
189 | 214 | return makeRequest(db, callCtxt, (RESTServices.CallField) null);
|
190 | 215 | }
|
191 | 216 | BaseProxy.DBFunctionRequest makeRequest(
|
192 |
| - DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableHandle[] input |
| 217 | + DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableContentHandle[] input |
193 | 218 | ) {
|
194 | 219 | RESTServices.CallField inputField = null;
|
195 | 220 |
|
@@ -286,10 +311,14 @@ O responseSingle(BaseProxy.DBFunctionRequest request) {
|
286 | 311 | throw new UnsupportedOperationException("multiple return from endpoint: "+getEndpointPath());
|
287 | 312 | }
|
288 | 313 |
|
289 |
| - return request.responseSingle(getReturndef().isNullable(), getReturndef().getFormat()).asContent(outputHandle); |
| 314 | + return request.responseSingle( |
| 315 | + getReturndef().isNullable(), getReturndef().getFormat()).asContent(getContentOutputHandle() |
| 316 | + ); |
290 | 317 | }
|
291 | 318 | O[] responseMultipleAsArray(BaseProxy.DBFunctionRequest request, CallContextImpl<I,O> callCtxt) {
|
292 |
| - return responseMultiple(request).asArrayOfContent(callCtxt.isLegacyContext() ? null : callCtxt.getEndpointState(), outputHandle); |
| 319 | + return responseMultiple(request).asArrayOfContent( |
| 320 | + callCtxt.isLegacyContext() ? null : callCtxt.getEndpointState(), getContentOutputHandle() |
| 321 | + ); |
293 | 322 | }
|
294 | 323 | private RESTServices.MultipleCallResponse responseMultiple(BaseProxy.DBFunctionRequest request) {
|
295 | 324 | if (getReturndef() == null) {
|
|
0 commit comments