Skip to content

Commit 69d600a

Browse files
ehennumehennum
ehennum
authored andcommitted
#1296 base changes for Data Services using handles
1 parent a840007 commit 69d600a

19 files changed

+115
-78
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/InputCaller.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ public interface InputCaller<I> extends IOEndpoint {
3737
* @return the InputCaller instance for calling the endpoint.
3838
*/
3939
static <I> InputCaller<I> on(DatabaseClient client, JSONWriteHandle apiDecl, BufferableContentHandle<I,?> inputHandle) {
40-
return new InputEndpointImpl(client, apiDecl, inputHandle);
40+
return new InputEndpointImpl(client, apiDecl, false, inputHandle);
4141
}
4242

4343
/**

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/InputEndpoint.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ static InputEndpoint on(DatabaseClient client, JSONWriteHandle apiDecl) {
3939
final class EndpointLocal<O> extends InputEndpointImpl<InputStream,O>
4040
implements InputEndpoint {
4141
private EndpointLocal(DatabaseClient client, JSONWriteHandle apiDecl) {
42-
super(client, apiDecl, new InputStreamHandle());
42+
super(client, apiDecl, false, new InputStreamHandle());
4343
}
4444
public InputEndpoint.BulkInputCaller bulkCaller() {
4545
return new BulkLocal(this);

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/InputOutputCaller.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ public interface InputOutputCaller<I,O> extends IOEndpoint {
4444
*/
4545
static <I,O> InputOutputCaller<I,O> on(
4646
DatabaseClient client, JSONWriteHandle apiDecl,
47-
BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<I,?> outputHandle
47+
BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<O,?> outputHandle
4848
) {
49-
return new InputOutputEndpointImpl(client, apiDecl, inputHandle, outputHandle);
49+
return new InputOutputEndpointImpl(client, apiDecl, false, inputHandle, outputHandle);
50+
}
51+
52+
static <I extends BufferableContentHandle<?,?>,O extends BufferableContentHandle<?,?>> InputOutputCaller<I,O> onHandles(
53+
DatabaseClient client, JSONWriteHandle apiDecl,
54+
I inputHandle, O outputHandle
55+
) {
56+
return new InputOutputEndpointImpl(client, apiDecl, true, inputHandle, outputHandle);
5057
}
5158

5259
/**

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/InputOutputEndpoint.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ static InputOutputEndpoint on(DatabaseClient client, JSONWriteHandle apiDecl) {
4040
final class EndpointLocal extends InputOutputEndpointImpl<InputStream,InputStream>
4141
implements InputOutputEndpoint {
4242
private EndpointLocal(DatabaseClient client, JSONWriteHandle apiDecl) {
43-
super(client, apiDecl, new InputStreamHandle(), new InputStreamHandle());
43+
super(client, apiDecl, false, new InputStreamHandle(), new InputStreamHandle());
4444
}
4545
public InputOutputEndpoint.BulkInputOutputCaller bulkCaller() {
4646
return new BulkLocal(this);

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/OutputCaller.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ public interface OutputCaller<O> extends IOEndpoint {
4040
static <O> OutputCaller<O> on(
4141
DatabaseClient client, JSONWriteHandle apiDecl, BufferableContentHandle<O,?> outputHandle
4242
) {
43-
return new OutputEndpointImpl(client, apiDecl, outputHandle);
43+
return new OutputEndpointImpl(client, apiDecl, false, outputHandle);
4444
}
4545

4646
/**

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/OutputEndpoint.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ static OutputEndpoint on(DatabaseClient client, JSONWriteHandle apiDecl) {
3939
final class EndpointLocal<I> extends OutputEndpointImpl<I,InputStream>
4040
implements OutputEndpoint {
4141
private EndpointLocal(DatabaseClient client, JSONWriteHandle apiDecl) {
42-
super(client, apiDecl, new InputStreamHandle());
42+
super(client, apiDecl, false, new InputStreamHandle());
4343
}
4444
public OutputEndpoint.BulkOutputCaller bulkCaller() {
4545
return new BulkLocal(this);

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/impl/ExecCallerImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020

2121
final public class ExecCallerImpl<I,O> extends IOCallerImpl<I,O> {
2222
public ExecCallerImpl(JSONWriteHandle apiDeclaration) {
23-
super(apiDeclaration, null, null);
23+
super(apiDeclaration, false, null, null);
2424

2525
if (getInputParamdef() != null) {
2626
throw new IllegalArgumentException("input parameter not supported in endpoint: "+ getEndpointPath());

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/impl/IOCallerImpl.java

+43-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,37 +17,40 @@
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
1919
import com.marklogic.client.DatabaseClient;
20+
import com.marklogic.client.MarkLogicInternalException;
2021
import com.marklogic.client.SessionState;
2122
import com.marklogic.client.impl.BaseProxy;
2223
import com.marklogic.client.impl.NodeConverter;
2324
import com.marklogic.client.impl.RESTServices;
2425
import com.marklogic.client.io.BaseHandle;
2526
import com.marklogic.client.io.BytesHandle;
2627
import com.marklogic.client.io.marker.BufferableContentHandle;
27-
import com.marklogic.client.io.marker.BufferableHandle;
2828
import com.marklogic.client.io.marker.JSONWriteHandle;
2929

30-
import java.util.stream.Stream;
31-
3230
abstract class IOCallerImpl<I,O> extends BaseCallerImpl {
3331
private final JsonNode apiDeclaration;
3432
private final String endpointPath;
3533
private final BaseProxy.DBFunctionRequest requester;
3634

37-
private BufferableContentHandle<I,?> inputHandle;
38-
private BufferableContentHandle<O,?> outputHandle;
35+
private BufferableContentHandle<?,?> inputHandle;
36+
private BufferableContentHandle<?,?> outputHandle;
3937

4038
private ParamdefImpl endpointStateParamdef;
4139
private ParamdefImpl sessionParamdef;
4240
private ParamdefImpl endpointConstantsParamdef;
4341
private ParamdefImpl inputParamdef;
4442
private ReturndefImpl returndef;
4543

44+
private boolean isHandleIO = false;
45+
4646
IOCallerImpl(
47-
JSONWriteHandle apiDeclaration, BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<O,?> outputHandle
47+
JSONWriteHandle apiDeclaration, boolean isHandleIO,
48+
BufferableContentHandle<?,?> inputHandle, BufferableContentHandle<?,?> outputHandle
4849
) {
4950
super();
5051

52+
this.isHandleIO = isHandleIO;
53+
5154
if (apiDeclaration== null) {
5255
throw new IllegalArgumentException("null endpoint declaration");
5356
}
@@ -178,18 +181,40 @@ abstract class IOCallerImpl<I,O> extends BaseCallerImpl {
178181
);
179182
}
180183

181-
BufferableContentHandle<I, ?> getInputHandle() {
182-
return inputHandle;
183-
}
184-
BufferableContentHandle<O, ?> getOutputHandle() {
184+
BufferableContentHandle<?, ?> getOutputHandle() {
185185
return outputHandle;
186186
}
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+
}
187212

188213
BaseProxy.DBFunctionRequest makeRequest(DatabaseClient db, CallContextImpl<I,O> callCtxt) {
189214
return makeRequest(db, callCtxt, (RESTServices.CallField) null);
190215
}
191216
BaseProxy.DBFunctionRequest makeRequest(
192-
DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableHandle[] input
217+
DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableContentHandle[] input
193218
) {
194219
RESTServices.CallField inputField = null;
195220

@@ -286,10 +311,14 @@ O responseSingle(BaseProxy.DBFunctionRequest request) {
286311
throw new UnsupportedOperationException("multiple return from endpoint: "+getEndpointPath());
287312
}
288313

289-
return request.responseSingle(getReturndef().isNullable(), getReturndef().getFormat()).asContent(outputHandle);
314+
return request.responseSingle(
315+
getReturndef().isNullable(), getReturndef().getFormat()).asContent(getContentOutputHandle()
316+
);
290317
}
291318
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+
);
293322
}
294323
private RESTServices.MultipleCallResponse responseMultiple(BaseProxy.DBFunctionRequest request) {
295324
if (getReturndef() == null) {

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/impl/IOEndpointImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ boolean checkQueue(BlockingQueue<I> queue, int batchSize) {
323323
I[] getInputBatch(BlockingQueue<I> queue, int batchSize) {
324324
List<I> inputStreamList = new ArrayList<>();
325325
queue.drainTo(inputStreamList, batchSize);
326-
return inputStreamList.toArray(endpoint.getCaller().getInputHandle().newArray(inputStreamList.size()));
326+
return inputStreamList.toArray(endpoint.getCaller().getContentInputHandle().newArray(inputStreamList.size()));
327327
}
328328
void processOutputBatch(O[] output, Consumer<O> outputListener) {
329329
if (output == null || output.length == 0) return;

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/impl/InputCallerImpl.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,16 +15,15 @@
1515
*/
1616
package com.marklogic.client.dataservices.impl;
1717

18-
import java.util.stream.Stream;
19-
2018
import com.marklogic.client.DatabaseClient;
2119
import com.marklogic.client.io.marker.BufferableContentHandle;
22-
import com.marklogic.client.io.marker.BufferableHandle;
2320
import com.marklogic.client.io.marker.JSONWriteHandle;
2421

2522
final public class InputCallerImpl<I,O> extends IOCallerImpl<I,O> {
26-
public InputCallerImpl(JSONWriteHandle apiDeclaration, BufferableContentHandle<I,?> inputHandle) {
27-
super(apiDeclaration, inputHandle, null);
23+
public InputCallerImpl(
24+
JSONWriteHandle apiDeclaration, boolean isHandleIO, BufferableContentHandle<?,?> inputHandle
25+
) {
26+
super(apiDeclaration, isHandleIO, inputHandle, null);
2827

2928
if (getInputParamdef() == null) {
3029
throw new IllegalArgumentException("input parameter missing in endpoint: "+ getEndpointPath());
@@ -42,7 +41,7 @@ public InputCallerImpl(JSONWriteHandle apiDeclaration, BufferableContentHandle<I
4241
}
4342
}
4443

45-
public void arrayCall(DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableHandle[] input) {
44+
public void arrayCall(DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableContentHandle<?,?>[] input) {
4645
responseWithState(makeRequest(db, callCtxt, input), callCtxt);
4746
}
4847
}

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/impl/InputEndpointImpl.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121

2222
import com.marklogic.client.dataservices.InputCaller;
2323
import com.marklogic.client.io.marker.BufferableContentHandle;
24-
import com.marklogic.client.io.marker.BufferableHandle;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726

@@ -35,8 +34,10 @@ public class InputEndpointImpl<I,O> extends IOEndpointImpl<I,O> implements Input
3534
private final InputCallerImpl<I,O> caller;
3635
private final int batchSize;
3736

38-
public InputEndpointImpl(DatabaseClient client, JSONWriteHandle apiDecl, BufferableContentHandle<I,?> inputHandle) {
39-
this(client, new InputCallerImpl<>(apiDecl, inputHandle));
37+
public InputEndpointImpl(
38+
DatabaseClient client, JSONWriteHandle apiDecl, boolean isHandleIO, BufferableContentHandle<?,?> inputHandle
39+
) {
40+
this(client, new InputCallerImpl<>(apiDecl, isHandleIO, inputHandle));
4041
}
4142
private InputEndpointImpl(DatabaseClient client, InputCallerImpl<I,O> caller) {
4243
super(client, caller);
@@ -58,7 +59,7 @@ public void call(I[] input) {
5859
@Override
5960
public void call(CallContext callContext, I[] input) {
6061
InputCallerImpl<I,O> callerImpl = getCaller();
61-
callerImpl.arrayCall(getClient(), checkAllowedArgs(callContext), callerImpl.getInputHandle().resendableHandleFor(input));
62+
callerImpl.arrayCall(getClient(), checkAllowedArgs(callContext), callerImpl.getContentInputHandle().resendableHandleFor(input));
6263
}
6364

6465
@Deprecated
@@ -196,7 +197,7 @@ private void processInput(CallContextImpl<I,O> callContext, I[] inputBatch) {
196197

197198
ErrorDisposition error = ErrorDisposition.RETRY;
198199

199-
BufferableHandle[] inputHandles = callerImpl.getInputHandle().resendableHandleFor(inputBatch);
200+
BufferableContentHandle<?,?>[] inputHandles = callerImpl.getContentInputHandle().resendableHandleFor(inputBatch);
200201
for (int retryCount = 0; retryCount < DEFAULT_MAX_RETRIES && error == ErrorDisposition.RETRY; retryCount++) {
201202
Throwable throwable = null;
202203
try {

marklogic-client-api/src/main/java/com/marklogic/client/dataservices/impl/InputOutputCallerImpl.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 MarkLogic Corporation
2+
* Copyright (c) 2021 MarkLogic Corporation
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,15 @@
1515
*/
1616
package com.marklogic.client.dataservices.impl;
1717

18-
import java.util.stream.Stream;
19-
2018
import com.marklogic.client.DatabaseClient;
2119
import com.marklogic.client.io.marker.BufferableContentHandle;
22-
import com.marklogic.client.io.marker.BufferableHandle;
2320
import com.marklogic.client.io.marker.JSONWriteHandle;
2421

2522
final public class InputOutputCallerImpl<I,O> extends IOCallerImpl<I,O> {
2623
public InputOutputCallerImpl(
27-
JSONWriteHandle apiDeclaration, BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<O,?> outputHandle
24+
JSONWriteHandle apiDeclaration, boolean isHandleIO, BufferableContentHandle<?,?> inputHandle, BufferableContentHandle<?,?> outputHandle
2825
) {
29-
super(apiDeclaration, inputHandle, outputHandle);
26+
super(apiDeclaration, isHandleIO, inputHandle, outputHandle);
3027

3128
if (getInputParamdef() == null) {
3229
throw new IllegalArgumentException("input parameter missing in endpoint: "+ getEndpointPath());
@@ -40,7 +37,7 @@ public InputOutputCallerImpl(
4037
}
4138
}
4239

43-
public O[] arrayCall(DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableHandle[] input) {
40+
public O[] arrayCall(DatabaseClient db, CallContextImpl<I,O> callCtxt, BufferableContentHandle<?,?>[] input) {
4441
return responseMultipleAsArray(makeRequest(db, callCtxt, input), callCtxt);
4542
}
4643
}

0 commit comments

Comments
 (0)