Skip to content

Commit 757ae85

Browse files
author
Mark
committed
added bulk import API
1 parent d39b291 commit 757ae85

9 files changed

+747
-3
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
v4.1.3 (2016-11-xx)
22
---------------------------
33
* fixed error while serializing long values with VPackBuilder
4+
* added bulk import API
45

56
v4.1.2 (2016-11-10)
67
---------------------------

src/main/java/com/arangodb/ArangoCollection.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@
3030
import com.arangodb.entity.CollectionRevisionEntity;
3131
import com.arangodb.entity.DocumentCreateEntity;
3232
import com.arangodb.entity.DocumentDeleteEntity;
33+
import com.arangodb.entity.DocumentImportEntity;
3334
import com.arangodb.entity.DocumentUpdateEntity;
3435
import com.arangodb.entity.IndexEntity;
3536
import com.arangodb.entity.MultiDocumentEntity;
36-
import com.arangodb.internal.InternalArangoCollection;
3737
import com.arangodb.internal.ArangoExecutorSync;
38+
import com.arangodb.internal.InternalArangoCollection;
3839
import com.arangodb.internal.velocystream.ConnectionSync;
3940
import com.arangodb.model.CollectionPropertiesOptions;
4041
import com.arangodb.model.DocumentCreateOptions;
4142
import com.arangodb.model.DocumentDeleteOptions;
4243
import com.arangodb.model.DocumentExistsOptions;
44+
import com.arangodb.model.DocumentImportOptions;
4345
import com.arangodb.model.DocumentReadOptions;
4446
import com.arangodb.model.DocumentReplaceOptions;
4547
import com.arangodb.model.DocumentUpdateOptions;
@@ -135,6 +137,61 @@ public <T> MultiDocumentEntity<DocumentCreateEntity<T>> insertDocuments(
135137
insertDocumentsResponseDeserializer(values, params));
136138
}
137139

140+
/**
141+
* Imports documents
142+
*
143+
* @param values
144+
* a list of Objects that will be stored as documents
145+
* @return information about the import
146+
* @throws ArangoDBException
147+
*/
148+
public DocumentImportEntity importDocuments(final Collection<?> values) throws ArangoDBException {
149+
return importDocuments(values, new DocumentImportOptions());
150+
}
151+
152+
/**
153+
* Imports documents
154+
*
155+
* @param values
156+
* a list of Objects that will be stored as documents
157+
* @param options
158+
* Additional options, can be null
159+
* @return information about the import
160+
* @throws ArangoDBException
161+
*/
162+
public DocumentImportEntity importDocuments(final Collection<?> values, final DocumentImportOptions options)
163+
throws ArangoDBException {
164+
return executor.execute(importDocumentsRequest(values, options), DocumentImportEntity.class);
165+
}
166+
167+
/**
168+
* Imports documents
169+
*
170+
* @param values
171+
* JSON-encoded array of objects that will be stored as documents
172+
* @return information about the import
173+
* @throws ArangoDBException
174+
*/
175+
public DocumentImportEntity importDocuments(final String values) throws ArangoDBException {
176+
return executor.execute(importDocumentsRequest(values, new DocumentImportOptions()),
177+
DocumentImportEntity.class);
178+
}
179+
180+
/**
181+
* Imports documents
182+
*
183+
* @param values
184+
* JSON-encoded array of objects that will be stored as documents
185+
* @param options
186+
* Additional options, can be null
187+
* @return information about the import
188+
* @throws ArangoDBException
189+
*/
190+
public DocumentImportEntity importDocuments(final String values, final DocumentImportOptions options)
191+
throws ArangoDBException {
192+
return executor.execute(importDocumentsRequest(values, options), DocumentImportEntity.class);
193+
}
194+
138195
/**
139196
* Reads a single document
140197
*
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
import java.util.ArrayList;
24+
import java.util.Collection;
25+
26+
/**
27+
* @author Mark - mark at arangodb.com
28+
*
29+
*/
30+
public class DocumentImportEntity {
31+
32+
private Integer created;
33+
private Integer errors;
34+
private Integer empty;
35+
private Integer updated;
36+
private Integer ignored;
37+
private Collection<String> details;
38+
39+
public DocumentImportEntity() {
40+
super();
41+
details = new ArrayList<String>();
42+
}
43+
44+
/**
45+
* @return number of documents imported.
46+
*/
47+
public Integer getCreated() {
48+
return created;
49+
}
50+
51+
public void setCreated(final Integer created) {
52+
this.created = created;
53+
}
54+
55+
/**
56+
* @return number of documents that were not imported due to an error.
57+
*/
58+
public Integer getErrors() {
59+
return errors;
60+
}
61+
62+
public void setErrors(final Integer errors) {
63+
this.errors = errors;
64+
}
65+
66+
/**
67+
* @return number of empty lines found in the input (will only contain a value greater zero for types documents or
68+
* auto).
69+
*/
70+
public Integer getEmpty() {
71+
return empty;
72+
}
73+
74+
public void setEmpty(final Integer empty) {
75+
this.empty = empty;
76+
}
77+
78+
/**
79+
* @return number of updated/replaced documents (in case onDuplicate was set to either update or replace).
80+
*/
81+
public Integer getUpdated() {
82+
return updated;
83+
}
84+
85+
public void setUpdated(final Integer updated) {
86+
this.updated = updated;
87+
}
88+
89+
/**
90+
* @return number of failed but ignored insert operations (in case onDuplicate was set to ignore).
91+
*/
92+
public Integer getIgnored() {
93+
return ignored;
94+
}
95+
96+
public void setIgnored(final Integer ignored) {
97+
this.ignored = ignored;
98+
}
99+
100+
/**
101+
* @return if query parameter details is set to true, the result contain details with more detailed information
102+
* about which documents could not be inserted.
103+
*/
104+
public Collection<String> getDetails() {
105+
return details;
106+
}
107+
108+
public void setDetails(final Collection<String> details) {
109+
this.details = details;
110+
}
111+
112+
}

src/main/java/com/arangodb/internal/ArangoDBConstants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ArangoDBConstants {
5858
public static final String PATH_API_ADMIN_LOG = "/_admin/log";
5959
public static final String PATH_API_ADMIN_LOG_LEVEL = "/_admin/log/level";
6060
public static final String PATH_API_ADMIN_ROUTING_RELOAD = "/_admin/routing/reload";
61+
public static final String PATH_API_IMPORT = "/_api/import";
6162

6263
public static final String ENCRYPTION_PLAIN = "plain";
6364

@@ -101,4 +102,12 @@ public class ArangoDBConstants {
101102
public static final String VERTEX = "vertex";
102103
public static final String EDGE = "edge";
103104
public static final String ERROR = "error";
105+
public static final String FROM_PREFIX = "fromPrefix";
106+
public static final String TO_PREFIX = "toPrefix";
107+
public static final String OVERWRITE = "overwrite";
108+
public static final String ON_DUPLICATE = "onDuplicate";
109+
public static final String COMPLETE = "complete";
110+
public static final String DETAILS = "details";
111+
public static final String TYPE = "type";
112+
104113
}

src/main/java/com/arangodb/internal/InternalArangoCollection.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
import com.arangodb.model.DocumentCreateOptions;
4141
import com.arangodb.model.DocumentDeleteOptions;
4242
import com.arangodb.model.DocumentExistsOptions;
43+
import com.arangodb.model.DocumentImportOptions;
4344
import com.arangodb.model.DocumentReadOptions;
4445
import com.arangodb.model.DocumentReplaceOptions;
4546
import com.arangodb.model.DocumentUpdateOptions;
4647
import com.arangodb.model.FulltextIndexOptions;
4748
import com.arangodb.model.GeoIndexOptions;
4849
import com.arangodb.model.HashIndexOptions;
50+
import com.arangodb.model.ImportType;
4951
import com.arangodb.model.OptionsBuilder;
5052
import com.arangodb.model.PersistentIndexOptions;
5153
import com.arangodb.model.SkiplistIndexOptions;
@@ -159,6 +161,29 @@ public MultiDocumentEntity<DocumentCreateEntity<T>> deserialize(final Response r
159161
};
160162
}
161163

164+
protected Request importDocumentsRequest(final String values, final DocumentImportOptions options) {
165+
return importDocumentsRequest(options).putQueryParam(ArangoDBConstants.TYPE, ImportType.auto)
166+
.setBody(executor.serialize(values));
167+
}
168+
169+
protected Request importDocumentsRequest(final Collection<?> values, final DocumentImportOptions options) {
170+
return importDocumentsRequest(options).putQueryParam(ArangoDBConstants.TYPE, ImportType.list)
171+
.setBody(executor.serialize(values));
172+
}
173+
174+
protected Request importDocumentsRequest(final DocumentImportOptions options) {
175+
final DocumentImportOptions params = options != null ? options : new DocumentImportOptions();
176+
return new Request(db, RequestType.POST, ArangoDBConstants.PATH_API_IMPORT)
177+
.putQueryParam(ArangoDBConstants.COLLECTION, name)
178+
.putQueryParam(ArangoDBConstants.FROM_PREFIX, params.getFromPrefix())
179+
.putQueryParam(ArangoDBConstants.TO_PREFIX, params.getToPrefix())
180+
.putQueryParam(ArangoDBConstants.OVERWRITE, params.getOverwrite())
181+
.putQueryParam(ArangoDBConstants.WAIT_FOR_SYNC, params.getWaitForSync())
182+
.putQueryParam(ArangoDBConstants.ON_DUPLICATE, params.getOnDuplicate())
183+
.putQueryParam(ArangoDBConstants.COMPLETE, params.getComplete())
184+
.putQueryParam(ArangoDBConstants.DETAILS, params.getDetails());
185+
}
186+
162187
protected Request getDocumentRequest(final String key, final DocumentReadOptions options) {
163188
final Request request = new Request(db, RequestType.GET,
164189
executor.createPath(ArangoDBConstants.PATH_API_DOCUMENT, createDocumentHandle(key)));

0 commit comments

Comments
 (0)