Skip to content

Feature/named indices #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Added

- added support for named indices

## [5.0.7] - 2019-07-19

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/arangodb/entity/IndexEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class IndexEntity implements Entity {

private String id;
private String name;
private IndexType type;
private Collection<String> fields;
private Double selectivityEstimate;
Expand All @@ -48,6 +49,10 @@ public String getId() {
return id;
}

public String getName() {
return name;
}

public IndexType getType() {
return type;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/arangodb/model/FulltextIndexOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class FulltextIndexOptions {
private Iterable<String> fields;
private final IndexType type = IndexType.fulltext;
private Integer minLength;
private String name;

public FulltextIndexOptions() {
super();
Expand Down Expand Up @@ -60,6 +61,20 @@ public Integer getMinLength() {
return minLength;
}

/**
* @param name
* the name of the index
* @return options
*/
public FulltextIndexOptions name(final String name) {
this.name = name;
return this;
}

protected String getName() {
return name;
}

/**
* @param minLength
* Minimum character length of words to index. Will default to a server-defined value if unspecified. It
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/arangodb/model/GeoIndexOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class GeoIndexOptions {
private Iterable<String> fields;
private final IndexType type = IndexType.geo;
private Boolean geoJson;
private String name;

public GeoIndexOptions() {
super();
Expand All @@ -55,6 +56,20 @@ protected IndexType getType() {
return type;
}

/**
* @param name
* the name of the index
* @return options
*/
public GeoIndexOptions name(final String name) {
this.name = name;
return this;
}

protected String getName() {
return name;
}

public Boolean getGeoJson() {
return geoJson;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/arangodb/model/HashIndexOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class HashIndexOptions {
private Boolean unique;
private Boolean sparse;
private Boolean deduplicate;
private String name;

public HashIndexOptions() {
super();
Expand Down Expand Up @@ -99,4 +100,18 @@ public HashIndexOptions deduplicate(final Boolean deduplicate) {
return this;
}

/**
* @param name
* the name of the index
* @return options
*/
public HashIndexOptions name(final String name) {
this.name = name;
return this;
}

protected String getName() {
return name;
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/arangodb/model/PersistentIndexOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class PersistentIndexOptions {

private Iterable<String> fields;
protected IndexType type = IndexType.persistent;
private String name;
private Boolean unique;
private Boolean sparse;

Expand Down Expand Up @@ -71,6 +72,20 @@ public PersistentIndexOptions unique(final Boolean unique) {
return this;
}

/**
* @param name
* the name of the index
* @return options
*/
public PersistentIndexOptions name(final String name) {
this.name = name;
return this;
}

protected String getName() {
return name;
}

public Boolean getSparse() {
return sparse;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/arangodb/model/SkiplistIndexOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class SkiplistIndexOptions {
private Boolean unique;
private Boolean sparse;
private Boolean deduplicate;
private String name;

public SkiplistIndexOptions() {
super();
Expand Down Expand Up @@ -99,4 +100,18 @@ public SkiplistIndexOptions deduplicate(final Boolean deduplicate) {
return this;
}

/**
* @param name
* the name of the index
* @return options
*/
public SkiplistIndexOptions name(final String name) {
this.name = name;
return this;
}

protected String getName() {
return name;
}

}
170 changes: 161 additions & 9 deletions src/test/java/com/arangodb/ArangoCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.HashMap;
import java.util.Map;

import com.arangodb.model.*;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -63,16 +64,7 @@
import com.arangodb.entity.MultiDocumentEntity;
import com.arangodb.entity.Permissions;
import com.arangodb.entity.ServerRole;
import com.arangodb.model.CollectionCreateOptions;
import com.arangodb.model.CollectionPropertiesOptions;
import com.arangodb.model.DocumentCreateOptions;
import com.arangodb.model.DocumentDeleteOptions;
import com.arangodb.model.DocumentExistsOptions;
import com.arangodb.model.DocumentImportOptions;
import com.arangodb.model.DocumentImportOptions.OnDuplicate;
import com.arangodb.model.DocumentReadOptions;
import com.arangodb.model.DocumentReplaceOptions;
import com.arangodb.model.DocumentUpdateOptions;
import com.arangodb.velocypack.VPackSlice;

/**
Expand Down Expand Up @@ -1044,6 +1036,35 @@ public void createHashIndex() {
assertThat(indexResult.getUnique(), is(false));
}

@Test
public void createHashIndexWithOptions() {
if (!requireVersion(3, 5)) {
return;
}

final HashIndexOptions options = new HashIndexOptions();
options.name("myHashIndex");

final Collection<String> fields = new ArrayList<String>();
fields.add("a");
fields.add("b");
final IndexEntity indexResult = db.collection(COLLECTION_NAME).ensureHashIndex(fields, options);
assertThat(indexResult, is(notNullValue()));
assertThat(indexResult.getConstraint(), is(nullValue()));
assertThat(indexResult.getFields(), hasItem("a"));
assertThat(indexResult.getFields(), hasItem("b"));
assertThat(indexResult.getId(), startsWith(COLLECTION_NAME));
assertThat(indexResult.getIsNewlyCreated(), is(true));
assertThat(indexResult.getMinLength(), is(nullValue()));
if (arangoDB.getRole() == ServerRole.SINGLE) {
assertThat(indexResult.getSelectivityEstimate(), is(1.));
}
assertThat(indexResult.getSparse(), is(false));
assertThat(indexResult.getType(), is(IndexType.hash));
assertThat(indexResult.getUnique(), is(false));
assertThat(indexResult.getName(), is("myHashIndex"));
}

@Test
public void createGeoIndex() {
final Collection<String> fields = new ArrayList<String>();
Expand All @@ -1063,6 +1084,33 @@ public void createGeoIndex() {
}
}

@Test
public void createGeoIndexWithOptions() {
if (!requireVersion(3, 5)) {
return;
}

final GeoIndexOptions options = new GeoIndexOptions();
options.name("myGeoIndex1");

final Collection<String> fields = new ArrayList<String>();
fields.add("a");
final IndexEntity indexResult = db.collection(COLLECTION_NAME).ensureGeoIndex(fields, options);
assertThat(indexResult, is(notNullValue()));
assertThat(indexResult.getFields(), hasItem("a"));
assertThat(indexResult.getId(), startsWith(COLLECTION_NAME));
assertThat(indexResult.getIsNewlyCreated(), is(true));
assertThat(indexResult.getMinLength(), is(nullValue()));
assertThat(indexResult.getSparse(), is(true));
assertThat(indexResult.getUnique(), is(false));
if (requireVersion(3, 4)) {
assertThat(indexResult.getType(), is(IndexType.geo));
} else {
assertThat(indexResult.getType(), is(IndexType.geo1));
}
assertThat(indexResult.getName(), is("myGeoIndex1"));
}

@Test
public void createGeo2Index() {
final Collection<String> fields = new ArrayList<String>();
Expand All @@ -1084,6 +1132,35 @@ public void createGeo2Index() {
}
}

@Test
public void createGeo2IndexWithOptions() {
if (!requireVersion(3, 5)) {
return;
}

final GeoIndexOptions options = new GeoIndexOptions();
options.name("myGeoIndex2");

final Collection<String> fields = new ArrayList<String>();
fields.add("a");
fields.add("b");
final IndexEntity indexResult = db.collection(COLLECTION_NAME).ensureGeoIndex(fields, options);
assertThat(indexResult, is(notNullValue()));
assertThat(indexResult.getFields(), hasItem("a"));
assertThat(indexResult.getFields(), hasItem("b"));
assertThat(indexResult.getId(), startsWith(COLLECTION_NAME));
assertThat(indexResult.getIsNewlyCreated(), is(true));
assertThat(indexResult.getMinLength(), is(nullValue()));
assertThat(indexResult.getSparse(), is(true));
assertThat(indexResult.getUnique(), is(false));
if (requireVersion(3, 4)) {
assertThat(indexResult.getType(), is(IndexType.geo));
} else {
assertThat(indexResult.getType(), is(IndexType.geo2));
}
assertThat(indexResult.getName(), is("myGeoIndex2"));
}

@Test
public void createSkiplistIndex() {
final Collection<String> fields = new ArrayList<String>();
Expand All @@ -1102,6 +1179,32 @@ public void createSkiplistIndex() {
assertThat(indexResult.getUnique(), is(false));
}

@Test
public void createSkiplistIndexWithOptions() {
if (!requireVersion(3, 5)) {
return;
}

final SkiplistIndexOptions options = new SkiplistIndexOptions();
options.name("mySkiplistIndex");

final Collection<String> fields = new ArrayList<String>();
fields.add("a");
fields.add("b");
final IndexEntity indexResult = db.collection(COLLECTION_NAME).ensureSkiplistIndex(fields, options);
assertThat(indexResult, is(notNullValue()));
assertThat(indexResult.getConstraint(), is(nullValue()));
assertThat(indexResult.getFields(), hasItem("a"));
assertThat(indexResult.getFields(), hasItem("b"));
assertThat(indexResult.getId(), startsWith(COLLECTION_NAME));
assertThat(indexResult.getIsNewlyCreated(), is(true));
assertThat(indexResult.getMinLength(), is(nullValue()));
assertThat(indexResult.getSparse(), is(false));
assertThat(indexResult.getType(), is(IndexType.skiplist));
assertThat(indexResult.getUnique(), is(false));
assertThat(indexResult.getName(), is("mySkiplistIndex"));
}

@Test
public void createPersistentIndex() {
final Collection<String> fields = new ArrayList<String>();
Expand All @@ -1120,6 +1223,32 @@ public void createPersistentIndex() {
assertThat(indexResult.getUnique(), is(false));
}

@Test
public void createPersistentIndexWithOptions() {
if (!requireVersion(3, 5)) {
return;
}

final PersistentIndexOptions options = new PersistentIndexOptions();
options.name("myPersistentIndex");

final Collection<String> fields = new ArrayList<String>();
fields.add("a");
fields.add("b");
final IndexEntity indexResult = db.collection(COLLECTION_NAME).ensurePersistentIndex(fields, options);
assertThat(indexResult, is(notNullValue()));
assertThat(indexResult.getConstraint(), is(nullValue()));
assertThat(indexResult.getFields(), hasItem("a"));
assertThat(indexResult.getFields(), hasItem("b"));
assertThat(indexResult.getId(), startsWith(COLLECTION_NAME));
assertThat(indexResult.getIsNewlyCreated(), is(true));
assertThat(indexResult.getMinLength(), is(nullValue()));
assertThat(indexResult.getSparse(), is(false));
assertThat(indexResult.getType(), is(IndexType.persistent));
assertThat(indexResult.getUnique(), is(false));
assertThat(indexResult.getName(), is("myPersistentIndex"));
}

@Test
public void createFulltextIndex() {
final Collection<String> fields = new ArrayList<String>();
Expand All @@ -1135,6 +1264,29 @@ public void createFulltextIndex() {
assertThat(indexResult.getUnique(), is(false));
}

@Test
public void createFulltextIndexWithOptions() {
if (!requireVersion(3, 5)) {
return;
}

final FulltextIndexOptions options = new FulltextIndexOptions();
options.name("myFulltextIndex");

final Collection<String> fields = new ArrayList<String>();
fields.add("a");
final IndexEntity indexResult = db.collection(COLLECTION_NAME).ensureFulltextIndex(fields, options);
assertThat(indexResult, is(notNullValue()));
assertThat(indexResult.getConstraint(), is(nullValue()));
assertThat(indexResult.getFields(), hasItem("a"));
assertThat(indexResult.getId(), startsWith(COLLECTION_NAME));
assertThat(indexResult.getIsNewlyCreated(), is(true));
assertThat(indexResult.getSparse(), is(true));
assertThat(indexResult.getType(), is(IndexType.fulltext));
assertThat(indexResult.getUnique(), is(false));
assertThat(indexResult.getName(), is("myFulltextIndex"));
}

@Test
public void getIndexes() {
final Collection<String> fields = new ArrayList<String>();
Expand Down