Skip to content

Commit daabfec

Browse files
authored
[DE-789] MD-Index support (#320)
* MDIndex * MDPrefixedIndex
1 parent e032eaa commit daabfec

File tree

12 files changed

+400
-16
lines changed

12 files changed

+400
-16
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 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.springframework.annotation;
22+
23+
import com.arangodb.model.MDIFieldValueTypes;
24+
25+
import java.lang.annotation.*;
26+
27+
/**
28+
* Annotation to define given fields to be indexed using ArangoDB's Multi-Dimensional index.
29+
*/
30+
@Repeatable(MDIndexes.class)
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ElementType.TYPE})
33+
public @interface MDIndex {
34+
35+
/**
36+
* A list of attribute paths
37+
*/
38+
String[] fields();
39+
40+
/**
41+
* If {@literal true}, then create a unique index
42+
*/
43+
boolean unique() default false;
44+
45+
/**
46+
* must be {@link MDIFieldValueTypes#DOUBLE}, currently only doubles are supported as values
47+
*/
48+
MDIFieldValueTypes fieldValueTypes() default MDIFieldValueTypes.DOUBLE;
49+
50+
/**
51+
* If {@literal true}, then create a sparse index
52+
*/
53+
boolean sparse() default false;
54+
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 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.springframework.annotation;
22+
23+
import java.lang.annotation.ElementType;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Target({ElementType.TYPE})
30+
public @interface MDIndexes {
31+
32+
MDIndex[] value();
33+
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 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.springframework.annotation;
22+
23+
import com.arangodb.model.MDIFieldValueTypes;
24+
25+
import java.lang.annotation.*;
26+
27+
/**
28+
* Annotation to define given fields to be indexed using ArangoDB's Multi-Dimensional Prefixed index.
29+
*/
30+
@Repeatable(MDPrefixedIndexes.class)
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ElementType.TYPE})
33+
public @interface MDPrefixedIndex {
34+
35+
/**
36+
* A list of attribute paths
37+
*/
38+
String[] fields();
39+
40+
/**
41+
* Attribute names used as search prefix
42+
*/
43+
String[] prefixFields();
44+
45+
/**
46+
* If {@literal true}, then create a unique index
47+
*/
48+
boolean unique() default false;
49+
50+
/**
51+
* must be {@link MDIFieldValueTypes#DOUBLE}, currently only doubles are supported as values
52+
*/
53+
MDIFieldValueTypes fieldValueTypes() default MDIFieldValueTypes.DOUBLE;
54+
55+
/**
56+
* If {@literal true}, then create a sparse index
57+
*/
58+
boolean sparse() default false;
59+
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 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.springframework.annotation;
22+
23+
import java.lang.annotation.ElementType;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Target({ElementType.TYPE})
30+
public @interface MDPrefixedIndexes {
31+
32+
MDPrefixedIndex[] value();
33+
34+
}

src/main/java/com/arangodb/springframework/core/CollectionOperations.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@
2222

2323
import java.util.Collection;
2424

25+
import com.arangodb.model.*;
2526
import org.springframework.dao.DataAccessException;
2627

2728
import com.arangodb.entity.CollectionPropertiesEntity;
2829
import com.arangodb.entity.IndexEntity;
2930
import com.arangodb.entity.Permissions;
30-
import com.arangodb.model.FulltextIndexOptions;
31-
import com.arangodb.model.GeoIndexOptions;
32-
import com.arangodb.model.PersistentIndexOptions;
33-
import com.arangodb.model.TtlIndexOptions;
3431

3532
/**
3633
* Interface that specifies a basic set of ArangoDB operations on collection
@@ -137,6 +134,32 @@ IndexEntity ensurePersistentIndex(Iterable<String> fields, PersistentIndexOption
137134
*/
138135
IndexEntity ensureTtlIndex(Iterable<String> fields, TtlIndexOptions options) throws DataAccessException;
139136

137+
/**
138+
* Creates a MD-index for the collection, if it does not already exist.
139+
*
140+
* @param fields
141+
* A list of attribute paths
142+
* @param options
143+
* Additional options, can be null
144+
* @return information about the index
145+
* @throws DataAccessException
146+
*/
147+
IndexEntity ensureMDIndex(Iterable<String> fields, MDIndexOptions options)
148+
throws DataAccessException;
149+
150+
/**
151+
* Creates a MD-Prefixed-index for the collection, if it does not already exist.
152+
*
153+
* @param fields
154+
* A list of attribute paths
155+
* @param options
156+
* Additional options, can be null
157+
* @return information about the index
158+
* @throws DataAccessException
159+
*/
160+
IndexEntity ensureMDPrefixedIndex(Iterable<String> fields, MDPrefixedIndexOptions options)
161+
throws DataAccessException;
162+
140163
/**
141164
* Deletes the index with the given {@code id} from the collection.
142165
*

src/main/java/com/arangodb/springframework/core/mapping/ArangoPersistentEntity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@
2424
import java.util.Map;
2525
import java.util.Optional;
2626

27-
import com.arangodb.springframework.annotation.TtlIndex;
27+
import com.arangodb.springframework.annotation.*;
2828
import org.springframework.context.ApplicationContextAware;
2929
import org.springframework.data.mapping.IdentifierAccessor;
3030
import org.springframework.data.mapping.PersistentEntity;
3131

3232
import com.arangodb.model.CollectionCreateOptions;
33-
import com.arangodb.springframework.annotation.FulltextIndex;
34-
import com.arangodb.springframework.annotation.GeoIndex;
35-
import com.arangodb.springframework.annotation.PersistentIndex;
3633

3734
/**
3835
* @author Mark Vollmary
@@ -59,6 +56,10 @@ public interface ArangoPersistentEntity<T>
5956

6057
Optional<TtlIndex> getTtlIndex();
6158

59+
Collection<MDIndex> getMDIndexes();
60+
61+
Collection<MDPrefixedIndex> getMDPrefixedIndexes();
62+
6263
Map<String, ArangoPersistentProperty> getComputedValuesProperties();
6364

6465
Collection<ArangoPersistentProperty> getPersistentIndexedProperties();

src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ public Optional<TtlIndex> getTtlIndex() {
272272
return getIndex(TtlIndex.class);
273273
}
274274

275+
@Override
276+
public Collection<MDIndex> getMDIndexes() {
277+
final Collection<MDIndex> indexes = getIndexes(MDIndex.class);
278+
Optional.ofNullable(findAnnotation(MDIndexes.class))
279+
.ifPresent(i -> indexes.addAll(Arrays.asList(i.value())));
280+
return indexes;
281+
}
282+
283+
@Override
284+
public Collection<MDPrefixedIndex> getMDPrefixedIndexes() {
285+
final Collection<MDPrefixedIndex> indexes = getIndexes(MDPrefixedIndex.class);
286+
Optional.ofNullable(findAnnotation(MDPrefixedIndexes.class))
287+
.ifPresent(i -> indexes.addAll(Arrays.asList(i.value())));
288+
return indexes;
289+
}
290+
275291
private <A extends Annotation> Optional<A> getIndex(final Class<A> annotation) {
276292
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(getType(), annotation));
277293
}

src/main/java/com/arangodb/springframework/core/template/ArangoTemplate.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import com.arangodb.*;
2424
import com.arangodb.entity.*;
2525
import com.arangodb.model.*;
26-
import com.arangodb.springframework.annotation.FulltextIndex;
27-
import com.arangodb.springframework.annotation.GeoIndex;
28-
import com.arangodb.springframework.annotation.PersistentIndex;
29-
import com.arangodb.springframework.annotation.TtlIndex;
26+
import com.arangodb.springframework.annotation.*;
3027
import com.arangodb.springframework.core.ArangoOperations;
3128
import com.arangodb.springframework.core.CollectionOperations;
3229
import com.arangodb.springframework.core.UserOperations;
@@ -174,6 +171,8 @@ private static void ensureCollectionIndexes(final CollectionOperations collectio
174171
persistentEntity.getFulltextIndexedProperties().forEach(p -> ensureFulltextIndex(collection, p));
175172
persistentEntity.getTtlIndex().ifPresent(index -> ensureTtlIndex(collection, index));
176173
persistentEntity.getTtlIndexedProperty().ifPresent(p -> ensureTtlIndex(collection, p));
174+
persistentEntity.getMDIndexes().forEach(index -> ensureMDIndex(collection, index));
175+
persistentEntity.getMDPrefixedIndexes().forEach(index -> ensureMDPrefixedIndex(collection, index));
177176
}
178177

179178
private static void ensurePersistentIndex(final CollectionOperations collection, final PersistentIndex annotation) {
@@ -230,6 +229,25 @@ private static void ensureTtlIndex(final CollectionOperations collection, final
230229
collection.ensureTtlIndex(Collections.singleton(value.getFieldName()), options);
231230
}
232231

232+
private static void ensureMDIndex(final CollectionOperations collection, final MDIndex annotation) {
233+
collection.ensureMDIndex(Arrays.asList(annotation.fields()),
234+
new MDIndexOptions()
235+
.unique(annotation.unique())
236+
.fieldValueTypes(annotation.fieldValueTypes())
237+
.sparse(annotation.sparse())
238+
);
239+
}
240+
241+
private static void ensureMDPrefixedIndex(final CollectionOperations collection, final MDPrefixedIndex annotation) {
242+
collection.ensureMDPrefixedIndex(Arrays.asList(annotation.fields()),
243+
new MDPrefixedIndexOptions()
244+
.prefixFields(Arrays.asList(annotation.prefixFields()))
245+
.unique(annotation.unique())
246+
.fieldValueTypes(annotation.fieldValueTypes())
247+
.sparse(annotation.sparse())
248+
);
249+
}
250+
233251
private Optional<String> determineCollectionFromId(final Object id) {
234252
return id != null ? Optional.ofNullable(MetadataUtils.determineCollectionFromId(converter.convertId(id)))
235253
: Optional.empty();

src/main/java/com/arangodb/springframework/core/template/DefaultCollectionOperations.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Collection;
2424
import java.util.Map;
2525

26+
import com.arangodb.model.*;
2627
import org.springframework.dao.DataAccessException;
2728
import org.springframework.dao.support.DataAccessUtils;
2829
import org.springframework.dao.support.PersistenceExceptionTranslator;
@@ -32,10 +33,6 @@
3233
import com.arangodb.entity.CollectionPropertiesEntity;
3334
import com.arangodb.entity.IndexEntity;
3435
import com.arangodb.entity.Permissions;
35-
import com.arangodb.model.FulltextIndexOptions;
36-
import com.arangodb.model.GeoIndexOptions;
37-
import com.arangodb.model.PersistentIndexOptions;
38-
import com.arangodb.model.TtlIndexOptions;
3936
import com.arangodb.springframework.core.CollectionOperations;
4037

4138
/**
@@ -148,6 +145,26 @@ public IndexEntity ensureTtlIndex(Iterable<String> fields, TtlIndexOptions optio
148145
}
149146
}
150147

148+
@Override
149+
public IndexEntity ensureMDIndex(final Iterable<String> fields, final MDIndexOptions options)
150+
throws DataAccessException {
151+
try {
152+
return collection.ensureMDIndex(fields, options);
153+
} catch (final ArangoDBException e) {
154+
throw translateException(e);
155+
}
156+
}
157+
158+
@Override
159+
public IndexEntity ensureMDPrefixedIndex(final Iterable<String> fields, final MDPrefixedIndexOptions options)
160+
throws DataAccessException {
161+
try {
162+
return collection.ensureMDPrefixedIndex(fields, options);
163+
} catch (final ArangoDBException e) {
164+
throw translateException(e);
165+
}
166+
}
167+
151168
@Override
152169
public void dropIndex(final String id) throws DataAccessException {
153170
try {

0 commit comments

Comments
 (0)