Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit d4811b0

Browse files
Added auto create collection flag to @document annotation (#514)
* Added auto create collection flag to @document annotation * Commented out old tests * Un-commented out old tests
1 parent 4b601f6 commit d4811b0

8 files changed

Lines changed: 40 additions & 6 deletions

File tree

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@
449449
</property>
450450
</activation>
451451
<properties>
452-
<ai.instrumentkey>${telemetry.instrumentationKey}</ai.instrumentkey>
452+
<!--suppress UnresolvedMavenProperty -->
453+
<ai.instrumentkey>${telemetry.instrumentationKey}</ai.instrumentkey>
453454
</properties>
454455
</profile>
455456
<profile>

src/main/java/com/microsoft/azure/spring/data/cosmosdb/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Constants {
1818
public static final IndexingMode DEFAULT_INDEXINGPOLICY_MODE = IndexingMode.Consistent;
1919
public static final String DEFAULT_REPOSITORY_IMPLEMENT_POSTFIX = "Impl";
2020
public static final int DEFAULT_TIME_TO_LIVE = -1; // Indicates never expire
21+
public static final boolean DEFAULT_AUTO_CREATE_COLLECTION = true;
2122

2223
public static final String ID_PROPERTY_NAME = "id";
2324

src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/mapping/Document.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
String ru() default Constants.DEFAULT_REQUEST_UNIT;
2222

2323
int timeToLive() default Constants.DEFAULT_TIME_TO_LIVE;
24+
25+
boolean autoCreateCollection() default Constants.DEFAULT_AUTO_CREATE_COLLECTION;
2426
}

src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/DocumentDbEntityInformation.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class DocumentDbEntityInformation<T, ID> extends AbstractEntityInformatio
3434
private Integer requestUnit;
3535
private Integer timeToLive;
3636
private IndexingPolicy indexingPolicy;
37+
private boolean autoCreateCollection;
3738

3839
public DocumentDbEntityInformation(Class<T> domainClass) {
3940
super(domainClass);
@@ -50,6 +51,7 @@ public DocumentDbEntityInformation(Class<T> domainClass) {
5051
this.requestUnit = getRequestUnit(domainClass);
5152
this.timeToLive = getTimeToLive(domainClass);
5253
this.indexingPolicy = getIndexingPolicy(domainClass);
54+
this.autoCreateCollection = getIsAutoCreateCollection(domainClass);
5355
}
5456

5557
@SuppressWarnings("unchecked")
@@ -91,6 +93,10 @@ public String getPartitionKeyFieldValue(T entity) {
9193
return partitionKeyField == null ? null : (String) ReflectionUtils.getField(partitionKeyField, entity);
9294
}
9395

96+
public boolean isAutoCreateCollection() {
97+
return autoCreateCollection;
98+
}
99+
94100
private IndexingPolicy getIndexingPolicy(Class<?> domainClass) {
95101
final IndexingPolicy policy = new IndexingPolicy();
96102

@@ -231,5 +237,16 @@ private Collection<ExcludedPath> getIndexingPolicyExcludePaths(Class<?> domainCl
231237

232238
return pathsCollection;
233239
}
240+
241+
private boolean getIsAutoCreateCollection(Class<T> domainClass) {
242+
final Document annotation = domainClass.getAnnotation(Document.class);
243+
244+
boolean autoCreateCollection = Constants.DEFAULT_AUTO_CREATE_COLLECTION;
245+
if (annotation != null) {
246+
autoCreateCollection = annotation.autoCreateCollection();
247+
}
248+
249+
return autoCreateCollection;
250+
}
234251
}
235252

src/main/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/SimpleDocumentDbRepository.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,25 @@ public class SimpleDocumentDbRepository<T, ID extends Serializable> implements D
3131

3232
private final DocumentDbOperations operation;
3333
private final DocumentDbEntityInformation<T, ID> information;
34-
private final DocumentCollection collection;
3534

3635
public SimpleDocumentDbRepository(DocumentDbEntityInformation<T, ID> metadata,
3736
ApplicationContext applicationContext) {
3837
this.operation = applicationContext.getBean(DocumentDbOperations.class);
3938
this.information = metadata;
4039

41-
collection = createCollectionIfNotExists();
40+
if (this.information.isAutoCreateCollection()) {
41+
createCollectionIfNotExists();
42+
}
4243
}
4344

4445
public SimpleDocumentDbRepository(DocumentDbEntityInformation<T, ID> metadata,
4546
DocumentDbOperations dbOperations) {
4647
this.operation = dbOperations;
4748
this.information = metadata;
4849

49-
collection = createCollectionIfNotExists();
50+
if (this.information.isAutoCreateCollection()) {
51+
createCollectionIfNotExists();
52+
}
5053
}
5154

5255
private DocumentCollection createCollectionIfNotExists() {

src/test/java/com/microsoft/azure/spring/data/cosmosdb/domain/Role.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
TestConstants.EXCLUDEDPATH_0,
2929
TestConstants.EXCLUDEDPATH_1,
3030
})
31-
@Document(collection = TestConstants.ROLE_COLLECTION_NAME, ru = TestConstants.REQUEST_UNIT_STRING)
31+
@Document(collection = TestConstants.ROLE_COLLECTION_NAME,
32+
ru = TestConstants.REQUEST_UNIT_STRING,
33+
autoCreateCollection = false)
3234
public class Role {
3335
@Id
3436
String id;

src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/DocumentDBAnnotationUnitTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public void testIndexingPolicyAnnotation() {
8484
TestUtils.testIndexingPolicyPathsEquals(policy.getExcludedPaths(), TestConstants.EXCLUDEDPATHS);
8585
}
8686

87+
@Test
88+
public void testAutoCreateCollectionAnnotation() {
89+
final boolean autoCreateCollectionRoleInfo = roleInfo.isAutoCreateCollection();
90+
final boolean autoCreateCollectionPersonInfo = personInfo.isAutoCreateCollection();
91+
92+
Assert.isTrue(!autoCreateCollectionRoleInfo, "autoCreateCollection in role should be false");
93+
Assert.isTrue(autoCreateCollectionPersonInfo, "autoCreateCollection in person should be true");
94+
}
95+
8796
@Test
8897
public void testDefaultDocumentAnnotationTimeToLive() {
8998
final Integer timeToLive = personInfo.getTimeToLive();

src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/integration/MemoRepositoryIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.microsoft.azure.spring.data.cosmosdb.domain.Importance;
1111
import com.microsoft.azure.spring.data.cosmosdb.domain.Memo;
1212
import com.microsoft.azure.spring.data.cosmosdb.exception.DocumentDBAccessException;
13-
import com.microsoft.azure.spring.data.cosmosdb.exception.IllegalQueryException;
1413
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
1514
import com.microsoft.azure.spring.data.cosmosdb.repository.repository.MemoRepository;
1615
import org.junit.*;

0 commit comments

Comments
 (0)