Skip to content

Commit f4f1fbc

Browse files
author
mpv1989
committed
added convenience methods (ArangoCollection.getIndex/ .deleteIndex)
1 parent 859b447 commit f4f1fbc

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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))
5+
* fixed exception handling in Connection (issue #110)
46

57
v4.1.10 (2017-02-22)
68
---------------------------

src/main/java/com/arangodb/ArangoCollectionAsync.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,30 @@ public CompletableFuture<Boolean> documentExists(final String key, final Documen
517517
};
518518
}
519519

520+
/**
521+
* Returns an index
522+
*
523+
* @see <a href="https://docs.arangodb.com/current/HTTP/Indexes/WorkingWith.html#read-index">API Documentation</a>
524+
* @param id
525+
* The index-handle
526+
* @return information about the index
527+
*/
528+
public CompletableFuture<IndexEntity> getIndex(final String id) {
529+
return executor.execute(getIndexRequest(id), IndexEntity.class);
530+
}
531+
532+
/**
533+
* Deletes an index
534+
*
535+
* @see <a href="https://docs.arangodb.com/current/HTTP/Indexes/WorkingWith.html#delete-index">API Documentation</a>
536+
* @param id
537+
* The index-handle
538+
* @return the id of the index
539+
*/
540+
public CompletableFuture<String> deleteIndex(final String id) {
541+
return executor.execute(deleteIndexRequest(id), deleteIndexResponseDeserializer());
542+
}
543+
520544
/**
521545
* Creates a hash index for the collection, if it does not already exist.
522546
*

src/main/java/com/arangodb/ArangoDatabaseAsync.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ public CompletableFuture<Collection<CollectionEntity>> getCollections(final Coll
157157
* @return information about the index
158158
*/
159159
public CompletableFuture<IndexEntity> getIndex(final String id) {
160-
return executor.execute(getIndexRequest(id), IndexEntity.class);
160+
executor.validateIndexId(id);
161+
final String[] split = id.split("/");
162+
return collection(split[0]).getIndex(split[1]);
161163
}
162164

163165
/**
@@ -169,7 +171,9 @@ public CompletableFuture<IndexEntity> getIndex(final String id) {
169171
* @return the id of the index
170172
*/
171173
public CompletableFuture<String> deleteIndex(final String id) {
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/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
package com.arangodb;
2222

23+
import static org.hamcrest.CoreMatchers.notNullValue;
2324
import static org.hamcrest.Matchers.anyOf;
2425
import static org.hamcrest.Matchers.containsString;
2526
import static org.hamcrest.Matchers.empty;
2627
import static org.hamcrest.Matchers.hasItem;
2728
import static org.hamcrest.Matchers.instanceOf;
2829
import static org.hamcrest.Matchers.is;
2930
import static org.hamcrest.Matchers.not;
30-
import static org.hamcrest.Matchers.notNullValue;
3131
import static org.hamcrest.Matchers.nullValue;
3232
import static org.hamcrest.Matchers.startsWith;
3333
import static org.junit.Assert.assertThat;
@@ -733,6 +733,72 @@ public void deleteDocumentIfMatchFail() throws InterruptedException, ExecutionEx
733733
}
734734
}
735735

736+
@Test
737+
public void getIndex() throws InterruptedException, ExecutionException {
738+
final Collection<String> fields = new ArrayList<>();
739+
fields.add("a");
740+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null).get();
741+
final CompletableFuture<IndexEntity> f = db.collection(COLLECTION_NAME).getIndex(createResult.getId());
742+
assertThat(f, is(notNullValue()));
743+
f.whenComplete((readResult, ex) -> {
744+
assertThat(readResult.getId(), is(createResult.getId()));
745+
assertThat(readResult.getType(), is(createResult.getType()));
746+
});
747+
f.get();
748+
}
749+
750+
@Test
751+
public void getIndexByKey() throws InterruptedException, ExecutionException {
752+
final Collection<String> fields = new ArrayList<>();
753+
fields.add("a");
754+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null).get();
755+
final CompletableFuture<IndexEntity> f = db.collection(COLLECTION_NAME)
756+
.getIndex(createResult.getId().split("/")[1]);
757+
assertThat(f, is(notNullValue()));
758+
f.whenComplete((readResult, ex) -> {
759+
assertThat(readResult.getId(), is(createResult.getId()));
760+
assertThat(readResult.getType(), is(createResult.getType()));
761+
});
762+
f.get();
763+
}
764+
765+
@Test
766+
public void deleteIndex() throws InterruptedException, ExecutionException {
767+
final Collection<String> fields = new ArrayList<>();
768+
fields.add("a");
769+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null).get();
770+
final CompletableFuture<String> f = db.collection(COLLECTION_NAME).deleteIndex(createResult.getId());
771+
assertThat(f, is(notNullValue()));
772+
f.whenComplete((id, ex) -> {
773+
assertThat(id, is(createResult.getId()));
774+
try {
775+
db.getIndex(id);
776+
fail();
777+
} catch (final ArangoDBException e) {
778+
}
779+
});
780+
f.get();
781+
}
782+
783+
@Test
784+
public void deleteIndexByKey() throws InterruptedException, ExecutionException {
785+
final Collection<String> fields = new ArrayList<>();
786+
fields.add("a");
787+
final IndexEntity createResult = db.collection(COLLECTION_NAME).createHashIndex(fields, null).get();
788+
final CompletableFuture<String> f = db.collection(COLLECTION_NAME)
789+
.deleteIndex(createResult.getId().split("/")[1]);
790+
assertThat(f, is(notNullValue()));
791+
f.whenComplete((id, ex) -> {
792+
assertThat(id, is(createResult.getId()));
793+
try {
794+
db.getIndex(id);
795+
fail();
796+
} catch (final ArangoDBException e) {
797+
}
798+
});
799+
f.get();
800+
}
801+
736802
@Test
737803
public void createHashIndex() throws InterruptedException, ExecutionException {
738804
final Collection<String> fields = new ArrayList<>();

0 commit comments

Comments
 (0)