Skip to content

Commit ad28a7e

Browse files
ehennumehennum
ehennum
authored andcommitted
#1296 refactoring for handle provider
1 parent 69d600a commit ad28a7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+584
-162
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.marklogic.client.dataservices;
1717

1818
import com.marklogic.client.DatabaseClient;
19+
import com.marklogic.client.dataservices.impl.HandleProvider;
1920
import com.marklogic.client.dataservices.impl.InputEndpointImpl;
2021
import com.marklogic.client.io.marker.BufferableContentHandle;
2122
import com.marklogic.client.io.marker.BufferableHandle;
@@ -37,7 +38,7 @@ public interface InputCaller<I> extends IOEndpoint {
3738
* @return the InputCaller instance for calling the endpoint.
3839
*/
3940
static <I> InputCaller<I> on(DatabaseClient client, JSONWriteHandle apiDecl, BufferableContentHandle<I,?> inputHandle) {
40-
return new InputEndpointImpl(client, apiDecl, false, inputHandle);
41+
return new InputEndpointImpl(client, apiDecl, new HandleProvider.ContentHandleProvider<>(inputHandle,null));
4142
}
4243

4344
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.marklogic.client.DatabaseClient;
2121
import com.marklogic.client.SessionState;
22+
import com.marklogic.client.dataservices.impl.HandleProvider;
2223
import com.marklogic.client.dataservices.impl.InputEndpointImpl;
2324
import com.marklogic.client.io.InputStreamHandle;
2425
import com.marklogic.client.io.marker.JSONWriteHandle;
@@ -39,7 +40,7 @@ static InputEndpoint on(DatabaseClient client, JSONWriteHandle apiDecl) {
3940
final class EndpointLocal<O> extends InputEndpointImpl<InputStream,O>
4041
implements InputEndpoint {
4142
private EndpointLocal(DatabaseClient client, JSONWriteHandle apiDecl) {
42-
super(client, apiDecl, false, new InputStreamHandle());
43+
super(client, apiDecl, new HandleProvider.ContentHandleProvider<>(new InputStreamHandle(), null));
4344
}
4445
public InputEndpoint.BulkInputCaller bulkCaller() {
4546
return new BulkLocal(this);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.marklogic.client.dataservices;
1717

1818
import com.marklogic.client.DatabaseClient;
19+
import com.marklogic.client.dataservices.impl.HandleProvider;
1920
import com.marklogic.client.dataservices.impl.InputOutputEndpointImpl;
2021
import com.marklogic.client.io.marker.BufferableContentHandle;
2122
import com.marklogic.client.io.marker.BufferableHandle;
@@ -46,15 +47,17 @@ static <I,O> InputOutputCaller<I,O> on(
4647
DatabaseClient client, JSONWriteHandle apiDecl,
4748
BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<O,?> outputHandle
4849
) {
49-
return new InputOutputEndpointImpl(client, apiDecl, false, inputHandle, outputHandle);
50+
return new InputOutputEndpointImpl<>(client, apiDecl, new HandleProvider.ContentHandleProvider<>(inputHandle, outputHandle));
5051
}
5152

52-
static <I extends BufferableContentHandle<?,?>,O extends BufferableContentHandle<?,?>> InputOutputCaller<I,O> onHandles(
53+
/* TODO: ADD TO ISSUE -- ALSO FOR InputCaller AND OutputCaller -- AND DELETE
54+
static <IC,IR,OC,OR> InputOutputCaller<BufferableContentHandle<IC,IR>,BufferableContentHandle<OC,OR>> onHandles(
5355
DatabaseClient client, JSONWriteHandle apiDecl,
54-
I inputHandle, O outputHandle
56+
BufferableContentHandle<IC,IR> inputHandle, BufferableContentHandle<OC,OR> outputHandle
5557
) {
56-
return new InputOutputEndpointImpl(client, apiDecl, true, inputHandle, outputHandle);
58+
return new InputOutputEndpointImpl<>(client, apiDecl, new HandleProvider.DirectHandleProvider<>(inputHandle, outputHandle));
5759
}
60+
*/
5861

5962
/**
6063
* Makes one call to an endpoint that doesn't take endpoint constants, endpoint state, or a session.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.marklogic.client.DatabaseClient;
1919
import com.marklogic.client.SessionState;
20+
import com.marklogic.client.dataservices.impl.HandleProvider;
2021
import com.marklogic.client.dataservices.impl.InputOutputEndpointImpl;
2122
import com.marklogic.client.io.InputStreamHandle;
2223
import com.marklogic.client.io.marker.JSONWriteHandle;
@@ -40,7 +41,7 @@ static InputOutputEndpoint on(DatabaseClient client, JSONWriteHandle apiDecl) {
4041
final class EndpointLocal extends InputOutputEndpointImpl<InputStream,InputStream>
4142
implements InputOutputEndpoint {
4243
private EndpointLocal(DatabaseClient client, JSONWriteHandle apiDecl) {
43-
super(client, apiDecl, false, new InputStreamHandle(), new InputStreamHandle());
44+
super(client, apiDecl, new HandleProvider.ContentHandleProvider<>(new InputStreamHandle(), new InputStreamHandle()));
4445
}
4546
public InputOutputEndpoint.BulkInputOutputCaller bulkCaller() {
4647
return new BulkLocal(this);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.marklogic.client.dataservices;
1717

1818
import com.marklogic.client.DatabaseClient;
19+
import com.marklogic.client.dataservices.impl.HandleProvider;
1920
import com.marklogic.client.dataservices.impl.OutputEndpointImpl;
2021
import com.marklogic.client.io.marker.BufferableContentHandle;
2122
import com.marklogic.client.io.marker.JSONWriteHandle;
@@ -40,7 +41,7 @@ public interface OutputCaller<O> extends IOEndpoint {
4041
static <O> OutputCaller<O> on(
4142
DatabaseClient client, JSONWriteHandle apiDecl, BufferableContentHandle<O,?> outputHandle
4243
) {
43-
return new OutputEndpointImpl(client, apiDecl, false, outputHandle);
44+
return new OutputEndpointImpl(client, apiDecl, new HandleProvider.ContentHandleProvider<>(null, outputHandle));
4445
}
4546

4647
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.marklogic.client.DatabaseClient;
1919
import com.marklogic.client.SessionState;
20+
import com.marklogic.client.dataservices.impl.HandleProvider;
2021
import com.marklogic.client.dataservices.impl.OutputEndpointImpl;
2122
import com.marklogic.client.io.InputStreamHandle;
2223
import com.marklogic.client.io.marker.JSONWriteHandle;
@@ -39,7 +40,7 @@ static OutputEndpoint on(DatabaseClient client, JSONWriteHandle apiDecl) {
3940
final class EndpointLocal<I> extends OutputEndpointImpl<I,InputStream>
4041
implements OutputEndpoint {
4142
private EndpointLocal(DatabaseClient client, JSONWriteHandle apiDecl) {
42-
super(client, apiDecl, false, new InputStreamHandle());
43+
super(client, apiDecl, new HandleProvider.ContentHandleProvider<>(null, new InputStreamHandle()));
4344
}
4445
public OutputEndpoint.BulkOutputCaller bulkCaller() {
4546
return new BulkLocal(this);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
final public class ExecCallerImpl<I,O> extends IOCallerImpl<I,O> {
2222
public ExecCallerImpl(JSONWriteHandle apiDeclaration) {
23-
super(apiDeclaration, false, null, null);
23+
super(apiDeclaration, new HandleProvider.ContentHandleProvider<>(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/ExecEndpointImpl.java

Lines changed: 6 additions & 7 deletions
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.
@@ -127,7 +127,7 @@ public void awaitCompletion() {
127127
else if(getCallContextQueue() != null && !getCallContextQueue().isEmpty()){
128128
try {
129129
for (int i = 0; i < getThreadCount(); i++) {
130-
BulkCallableImpl bulkCallableImpl = new BulkCallableImpl(this);
130+
BulkCallableImpl<I,O> bulkCallableImpl = new BulkCallableImpl(this);
131131
submitTask(bulkCallableImpl);
132132
}
133133
if(getCallerThreadPoolExecutor() != null)
@@ -235,8 +235,7 @@ private boolean processOutput(CallContextImpl<I,O> callContext){
235235
}
236236
}
237237

238-
// TODO: static private class BulkCallableImpl<I,O>
239-
private class BulkCallableImpl implements Callable<Boolean> {
238+
static private class BulkCallableImpl<I,O> implements Callable<Boolean> {
240239
private final BulkExecCallerImpl<I,O> bulkExecCallerImpl;
241240

242241
BulkCallableImpl(BulkExecCallerImpl<I,O> bulkExecCallerImpl) {
@@ -251,11 +250,11 @@ public Boolean call() throws InterruptedException{
251250

252251
if(continueCalling) {
253252
bulkExecCallerImpl.getCallContextQueue().put(callContext);
254-
submitTask(this);
253+
bulkExecCallerImpl.submitTask(this);
255254
}
256255
else {
257-
if (aliveCallContextCount.decrementAndGet() == 0 && getCallerThreadPoolExecutor() != null) {
258-
getCallerThreadPoolExecutor().shutdown();
256+
if (bulkExecCallerImpl.aliveCallContextCount.decrementAndGet() == 0 && bulkExecCallerImpl.getCallerThreadPoolExecutor() != null) {
257+
bulkExecCallerImpl.getCallerThreadPoolExecutor().shutdown();
259258
}
260259
}
261260

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2021 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.dataservices.impl;
17+
18+
import com.marklogic.client.impl.RESTServices;
19+
import com.marklogic.client.io.marker.BufferableContentHandle;
20+
21+
public interface HandleProvider<I, O> {
22+
BufferableContentHandle<?, ?> getInputHandle();
23+
BufferableContentHandle<?, ?> getOutputHandle();
24+
I[] newInputArray(int length);
25+
O[] newOutputArray(int length);
26+
BufferableContentHandle<?, ?>[] bufferableInputHandleOn(I[] input);
27+
O[] outputAsArray(CallContextImpl<I, O> callCtxt, RESTServices.MultipleCallResponse response);
28+
29+
class ContentHandleProvider<I,O> implements HandleProvider<I,O> {
30+
private final BufferableContentHandle<I,?> inputHandle;
31+
private final BufferableContentHandle<O,?> outputHandle;
32+
public ContentHandleProvider(BufferableContentHandle<I,?> inputHandle, BufferableContentHandle<O,?> outputHandle) {
33+
this.inputHandle = inputHandle;
34+
this.outputHandle = outputHandle;
35+
}
36+
@Override
37+
public BufferableContentHandle<I,?> getInputHandle() {
38+
return inputHandle;
39+
}
40+
@Override
41+
public BufferableContentHandle<O,?> getOutputHandle() {
42+
return outputHandle;
43+
}
44+
@Override
45+
public I[] newInputArray(int length) {
46+
if (inputHandle == null) {
47+
throw new IllegalStateException("No input handle provided");
48+
}
49+
return inputHandle.newArray(length);
50+
}
51+
@Override
52+
public O[] newOutputArray(int length) {
53+
if (outputHandle == null) {
54+
throw new IllegalStateException("No output handle provided");
55+
}
56+
return outputHandle.newArray(length);
57+
}
58+
@Override
59+
public BufferableContentHandle<?,?>[] bufferableInputHandleOn(I[] input) {
60+
if (inputHandle == null) {
61+
throw new IllegalStateException("No input handle provided");
62+
}
63+
return inputHandle.resendableHandleFor(input);
64+
}
65+
@Override
66+
public O[] outputAsArray(CallContextImpl<I,O> callCtxt, RESTServices.MultipleCallResponse response) {
67+
if (outputHandle == null) {
68+
throw new IllegalStateException("No output handle provided");
69+
}
70+
return response.asArrayOfContent(
71+
callCtxt.isLegacyContext() ? null : callCtxt.getEndpointState(), outputHandle
72+
);
73+
}
74+
}
75+
76+
class DirectHandleProvider<IC,IR,OC,OR>
77+
implements HandleProvider<BufferableContentHandle<IC,IR>, BufferableContentHandle<OC,OR>> {
78+
private final BufferableContentHandle<IC,IR> inputHandle;
79+
private final BufferableContentHandle<OC,OR> outputHandle;
80+
public DirectHandleProvider(BufferableContentHandle<IC,IR> inputHandle, BufferableContentHandle<OC,OR> outputHandle) {
81+
this.inputHandle = inputHandle;
82+
this.outputHandle = outputHandle;
83+
}
84+
@Override
85+
public BufferableContentHandle<IC,IR> getInputHandle() {
86+
return inputHandle;
87+
}
88+
@Override
89+
public BufferableContentHandle<OC,OR> getOutputHandle() {
90+
return outputHandle;
91+
}
92+
@Override
93+
public BufferableContentHandle<IC,IR>[] newInputArray(int length) {
94+
if (inputHandle == null) {
95+
throw new IllegalStateException("No input handle provided");
96+
}
97+
return inputHandle.newHandleArray(length);
98+
}
99+
@Override
100+
public BufferableContentHandle<OC,OR>[] newOutputArray(int length) {
101+
if (outputHandle == null) {
102+
throw new IllegalStateException("No output handle provided");
103+
}
104+
return outputHandle.newHandleArray(length);
105+
}
106+
@Override
107+
public BufferableContentHandle<IC,IR>[] bufferableInputHandleOn(BufferableContentHandle<IC,IR>[] input) {
108+
return input;
109+
}
110+
@Override
111+
public BufferableContentHandle<OC,OR>[] outputAsArray(
112+
CallContextImpl<BufferableContentHandle<IC,IR>, BufferableContentHandle<OC,OR>> callCtxt,
113+
RESTServices.MultipleCallResponse response) {
114+
if (outputHandle == null) {
115+
throw new IllegalStateException("No output handle provided");
116+
}
117+
return response.asArrayOfHandles(callCtxt.getEndpointState(), outputHandle);
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)