Skip to content

Commit 07f1371

Browse files
author
mpv1989
committed
* added convenience methods (ArangoCollection.getIndex/ deleteIndex)
1 parent bfe49f9 commit 07f1371

File tree

7 files changed

+121
-23
lines changed

7 files changed

+121
-23
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
v4.1.11 (2017-03-xx)
22
---------------------------
33
* added convenience methods (ArangoDatabase.arango(), ArangoCollection.db(), ArangoGraph.db())
4+
* added convenience methods (ArangoCollection.getIndex(String), .deleteIndex(key))
45
* fixed exception handling in Connection (issue #110)
56

67
v4.1.10 (2017-02-22)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,32 @@ public Boolean documentExists(final String key, final DocumentExistsOptions opti
529529
}
530530
}
531531

532+
/**
533+
* Returns an index
534+
*
535+
* @see <a href="https://docs.arangodb.com/current/HTTP/Indexes/WorkingWith.html#read-index">API Documentation</a>
536+
* @param id
537+
* The index-handle
538+
* @return information about the index
539+
* @throws ArangoDBException
540+
*/
541+
public IndexEntity getIndex(final String id) throws ArangoDBException {
542+
return executor.execute(getIndexRequest(id), IndexEntity.class);
543+
}
544+
545+
/**
546+
* Deletes an index
547+
*
548+
* @see <a href="https://docs.arangodb.com/current/HTTP/Indexes/WorkingWith.html#delete-index">API Documentation</a>
549+
* @param id
550+
* The index-handle
551+
* @return the id of the index
552+
* @throws ArangoDBException
553+
*/
554+
public String deleteIndex(final String id) throws ArangoDBException {
555+
return executor.execute(deleteIndexRequest(id), deleteIndexResponseDeserializer());
556+
}
557+
532558
/**
533559
* Creates a hash index for the collection if it does not already exist.
534560
*

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ public Collection<CollectionEntity> getCollections(final CollectionsReadOptions
156156
* @throws ArangoDBException
157157
*/
158158
public IndexEntity getIndex(final String id) throws ArangoDBException {
159-
return executor.execute(getIndexRequest(id), IndexEntity.class);
159+
executor.validateIndexId(id);
160+
final String[] split = id.split("/");
161+
return collection(split[0]).getIndex(split[1]);
160162
}
161163

162164
/**
@@ -169,7 +171,9 @@ public IndexEntity getIndex(final String id) throws ArangoDBException {
169171
* @throws ArangoDBException
170172
*/
171173
public String deleteIndex(final String id) throws ArangoDBException {
172-
return executor.execute(deleteIndexRequest(id), deleteIndexResponseDeserializer());
174+
executor.validateIndexId(id);
175+
final String[] split = id.split("/");
176+
return collection(split[0]).deleteIndex(split[1]);
173177
}
174178

175179
/**

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static interface ResponseDeserializer<T> {
4848
T deserialize(Response response) throws VPackException;
4949
}
5050

51-
private static final String REGEX_DOCUMENT_KEY = "[^/]+";
52-
private static final String REGEX_DOCUMENT_ID = "[^/]+/[^/]+";
51+
protected static final String REGEX_KEY = "[^/]+";
52+
protected static final String REGEX_ID = "[^/]+/[^/]+";
5353

5454
private final Communication<R, C> communication;
5555
private final DocumentCache documentCache;
@@ -107,12 +107,16 @@ private String encode(final String value) throws UnsupportedEncodingException {
107107
.replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~");
108108
}
109109

110+
public void validateIndexId(final String id) {
111+
validateName("index id", REGEX_ID, id);
112+
}
113+
110114
public void validateDocumentKey(final String key) throws ArangoDBException {
111-
validateName("document key", REGEX_DOCUMENT_KEY, key);
115+
validateName("document key", REGEX_KEY, key);
112116
}
113117

114118
public void validateDocumentId(final String id) throws ArangoDBException {
115-
validateName("document id", REGEX_DOCUMENT_ID, id);
119+
validateName("document id", REGEX_ID, id);
116120
}
117121

118122
public String createDocumentHandle(final String collection, final String key) {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Iterator;
2727
import java.util.Map;
2828

29+
import com.arangodb.ArangoDBException;
2930
import com.arangodb.entity.DocumentCreateEntity;
3031
import com.arangodb.entity.DocumentDeleteEntity;
3132
import com.arangodb.entity.DocumentField;
@@ -462,6 +463,37 @@ protected Request documentExistsRequest(final String key, final DocumentExistsOp
462463
return request;
463464
}
464465

466+
protected Request getIndexRequest(final String id) {
467+
return new Request(db.name(), RequestType.GET,
468+
executor.createPath(ArangoDBConstants.PATH_API_INDEX, createIndexId(id)));
469+
}
470+
471+
protected Request deleteIndexRequest(final String id) {
472+
return new Request(db.name(), RequestType.DELETE,
473+
executor.createPath(ArangoDBConstants.PATH_API_INDEX, createIndexId(id)));
474+
}
475+
476+
protected ResponseDeserializer<String> deleteIndexResponseDeserializer() {
477+
return new ResponseDeserializer<String>() {
478+
@Override
479+
public String deserialize(final Response response) throws VPackException {
480+
return response.getBody().get(ArangoDBConstants.ID).getAsString();
481+
}
482+
};
483+
}
484+
485+
private String createIndexId(final String id) {
486+
final String index;
487+
if (id.matches(ArangoExecutor.REGEX_ID)) {
488+
index = id;
489+
} else if (id.matches(ArangoExecutor.REGEX_KEY)) {
490+
index = name + "/" + id;
491+
} else {
492+
throw new ArangoDBException(String.format("index id %s is not valid.", id));
493+
}
494+
return index;
495+
}
496+
465497
protected Request createHashIndexRequest(final Collection<String> fields, final HashIndexOptions options) {
466498
final Request request;
467499
request = new Request(db.name(), RequestType.POST, ArangoDBConstants.PATH_API_INDEX);

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,6 @@ public Collection<CollectionEntity> deserialize(final Response response) throws
103103
};
104104
}
105105

106-
protected Request getIndexRequest(final String id) {
107-
return new Request(name, RequestType.GET, executor.createPath(ArangoDBConstants.PATH_API_INDEX, id));
108-
}
109-
110-
protected Request deleteIndexRequest(final String id) {
111-
return new Request(name, RequestType.DELETE, executor.createPath(ArangoDBConstants.PATH_API_INDEX, id));
112-
}
113-
114-
protected ResponseDeserializer<String> deleteIndexResponseDeserializer() {
115-
return new ResponseDeserializer<String>() {
116-
@Override
117-
public String deserialize(final Response response) throws VPackException {
118-
return response.getBody().get(ArangoDBConstants.ID).getAsString();
119-
}
120-
};
121-
}
122-
123106
protected Request dropRequest() {
124107
return new Request(ArangoDBConstants.SYSTEM, RequestType.DELETE,
125108
executor.createPath(ArangoDBConstants.PATH_API_DATABASE, name));

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,54 @@ public void deleteDocumentIfMatchFail() {
675675
}
676676
}
677677

678+
@Test
679+
public void getIndex() {
680+
final Collection<String> fields = new ArrayList<String>();
681+
fields.add("a");
682+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null);
683+
final IndexEntity readResult = db.collection(COLLECTION_NAME).getIndex(createResult.getId());
684+
assertThat(readResult.getId(), is(createResult.getId()));
685+
assertThat(readResult.getType(), is(createResult.getType()));
686+
}
687+
688+
@Test
689+
public void getIndexByKey() {
690+
final Collection<String> fields = new ArrayList<String>();
691+
fields.add("a");
692+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null);
693+
final IndexEntity readResult = db.collection(COLLECTION_NAME).getIndex(createResult.getId().split("/")[1]);
694+
assertThat(readResult.getId(), is(createResult.getId()));
695+
assertThat(readResult.getType(), is(createResult.getType()));
696+
}
697+
698+
@Test
699+
public void deleteIndex() {
700+
final Collection<String> fields = new ArrayList<String>();
701+
fields.add("a");
702+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null);
703+
final String id = db.collection(COLLECTION_NAME).deleteIndex(createResult.getId());
704+
assertThat(id, is(createResult.getId()));
705+
try {
706+
db.getIndex(id);
707+
fail();
708+
} catch (final ArangoDBException e) {
709+
}
710+
}
711+
712+
@Test
713+
public void deleteIndexByKey() {
714+
final Collection<String> fields = new ArrayList<String>();
715+
fields.add("a");
716+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null);
717+
final String id = db.collection(COLLECTION_NAME).deleteIndex(createResult.getId().split("/")[1]);
718+
assertThat(id, is(createResult.getId()));
719+
try {
720+
db.getIndex(id);
721+
fail();
722+
} catch (final ArangoDBException e) {
723+
}
724+
}
725+
678726
@Test
679727
public void createHashIndex() {
680728
final Collection<String> fields = new ArrayList<String>();

0 commit comments

Comments
 (0)