Skip to content

Commit bd5053b

Browse files
committed
feat: supported /batch api from RemoteDatabase
Fixed issue #3817
1 parent a763aee commit bd5053b

4 files changed

Lines changed: 828 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com)
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+
* SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com)
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
package com.arcadedb.remote;
20+
21+
/**
22+
* Result of a remote batch import operation via the /batch HTTP endpoint.
23+
*
24+
* @author Luca Garulli (l.garulli@arcadedata.com)
25+
*/
26+
public class RemoteBatchResult {
27+
private final long verticesCreated;
28+
private final long edgesCreated;
29+
private final long elapsedMs;
30+
31+
RemoteBatchResult(final long verticesCreated, final long edgesCreated, final long elapsedMs) {
32+
this.verticesCreated = verticesCreated;
33+
this.edgesCreated = edgesCreated;
34+
this.elapsedMs = elapsedMs;
35+
}
36+
37+
public long getVerticesCreated() {
38+
return verticesCreated;
39+
}
40+
41+
public long getEdgesCreated() {
42+
return edgesCreated;
43+
}
44+
45+
public long getElapsedMs() {
46+
return elapsedMs;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "RemoteBatchResult{verticesCreated=" + verticesCreated + ", edgesCreated=" + edgesCreated + ", elapsedMs=" + elapsedMs + "}";
52+
}
53+
}

network/src/main/java/com/arcadedb/remote/RemoteDatabase.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ public RemoteTransactionExplicitLock acquireLock() {
262262
return explicitLock;
263263
}
264264

265+
/**
266+
* Returns a builder for configuring a remote batch graph import.
267+
* The builder mirrors the server-side GraphBatch.Builder parameters.
268+
*
269+
* @return a new {@link RemoteGraphBatch.Builder}
270+
*/
271+
public RemoteGraphBatch.Builder batch() {
272+
checkDatabaseIsOpen();
273+
return new RemoteGraphBatch.Builder(this);
274+
}
275+
265276
@Override
266277
public void begin() {
267278
begin(transactionIsolationLevel);
@@ -572,6 +583,42 @@ private String getUrl(final String command, final String databaseName) {
572583
return getUrl(command) + "/" + databaseName;
573584
}
574585

586+
JSONObject sendBatch(final String content, final Map<String, String> queryParams) {
587+
checkDatabaseIsOpen();
588+
589+
final StringBuilder urlBuilder = new StringBuilder(getUrl("batch", databaseName));
590+
if (queryParams != null && !queryParams.isEmpty()) {
591+
urlBuilder.append('?');
592+
boolean first = true;
593+
for (final Map.Entry<String, String> entry : queryParams.entrySet()) {
594+
if (!first)
595+
urlBuilder.append('&');
596+
urlBuilder.append(entry.getKey()).append('=').append(entry.getValue());
597+
first = false;
598+
}
599+
}
600+
601+
try {
602+
final HttpRequest request = createRequestBuilder("POST", urlBuilder.toString())
603+
.POST(HttpRequest.BodyPublishers.ofString(content))
604+
.header("Content-Type", "application/x-ndjson")
605+
.build();
606+
607+
final HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
608+
609+
if (response.statusCode() != 200) {
610+
final Exception detail = manageException(response, "batch import");
611+
throw new DatabaseOperationException("Error on batch import", detail);
612+
}
613+
614+
return new JSONObject(response.body());
615+
} catch (final DatabaseOperationException e) {
616+
throw e;
617+
} catch (final Exception e) {
618+
throw new DatabaseOperationException("Error on batch import", e);
619+
}
620+
}
621+
575622
protected ResultSet createResultSet(final JSONObject response) {
576623
final ResultSet resultSet = new InternalResultSet();
577624

0 commit comments

Comments
 (0)