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

Added auto create collection flag to @Document annotation #514

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Changes from 1 commit
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
Next Next commit
Added auto create collection flag to @document annotation
kushagraThapar committed Mar 16, 2020
commit 43595857b02331a3892dd26f451efdae4c276eb8
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -449,7 +449,8 @@
</property>
</activation>
<properties>
<ai.instrumentkey>${telemetry.instrumentationKey}</ai.instrumentkey>
<!--suppress UnresolvedMavenProperty -->
<ai.instrumentkey>${telemetry.instrumentationKey}</ai.instrumentkey>
</properties>
</profile>
<profile>
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ public class Constants {
public static final IndexingMode DEFAULT_INDEXINGPOLICY_MODE = IndexingMode.Consistent;
public static final String DEFAULT_REPOSITORY_IMPLEMENT_POSTFIX = "Impl";
public static final int DEFAULT_TIME_TO_LIVE = -1; // Indicates never expire
public static final boolean DEFAULT_AUTO_CREATE_COLLECTION = true;

public static final String ID_PROPERTY_NAME = "id";

Original file line number Diff line number Diff line change
@@ -21,4 +21,6 @@
String ru() default Constants.DEFAULT_REQUEST_UNIT;

int timeToLive() default Constants.DEFAULT_TIME_TO_LIVE;

boolean autoCreateCollection() default Constants.DEFAULT_AUTO_CREATE_COLLECTION;
}
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ public class DocumentDbEntityInformation<T, ID> extends AbstractEntityInformatio
private Integer requestUnit;
private Integer timeToLive;
private IndexingPolicy indexingPolicy;
private boolean autoCreateCollection;

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

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

public boolean isAutoCreateCollection() {
return autoCreateCollection;
}

private IndexingPolicy getIndexingPolicy(Class<?> domainClass) {
final IndexingPolicy policy = new IndexingPolicy();

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

return pathsCollection;
}

private boolean getIsAutoCreateCollection(Class<T> domainClass) {
final Document annotation = domainClass.getAnnotation(Document.class);

boolean autoCreateCollection = Constants.DEFAULT_AUTO_CREATE_COLLECTION;
if (annotation != null) {
autoCreateCollection = annotation.autoCreateCollection();
}

return autoCreateCollection;
}
}

Original file line number Diff line number Diff line change
@@ -31,22 +31,25 @@ public class SimpleDocumentDbRepository<T, ID extends Serializable> implements D

private final DocumentDbOperations operation;
private final DocumentDbEntityInformation<T, ID> information;
private final DocumentCollection collection;

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

collection = createCollectionIfNotExists();
if (this.information.isAutoCreateCollection()) {
createCollectionIfNotExists();
}
}

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

collection = createCollectionIfNotExists();
if (this.information.isAutoCreateCollection()) {
createCollectionIfNotExists();
}
}

private DocumentCollection createCollectionIfNotExists() {
Original file line number Diff line number Diff line change
@@ -28,7 +28,9 @@
TestConstants.EXCLUDEDPATH_0,
TestConstants.EXCLUDEDPATH_1,
})
@Document(collection = TestConstants.ROLE_COLLECTION_NAME, ru = TestConstants.REQUEST_UNIT_STRING)
@Document(collection = TestConstants.ROLE_COLLECTION_NAME,
ru = TestConstants.REQUEST_UNIT_STRING,
autoCreateCollection = false)
public class Role {
@Id
String id;
Original file line number Diff line number Diff line change
@@ -84,6 +84,15 @@ public void testIndexingPolicyAnnotation() {
TestUtils.testIndexingPolicyPathsEquals(policy.getExcludedPaths(), TestConstants.EXCLUDEDPATHS);
}

@Test
public void testAutoCreateCollectionAnnotation() {
final boolean autoCreateCollectionRoleInfo = roleInfo.isAutoCreateCollection();
final boolean autoCreateCollectionPersonInfo = personInfo.isAutoCreateCollection();

Assert.isTrue(!autoCreateCollectionRoleInfo, "autoCreateCollection in role should be false");
Assert.isTrue(autoCreateCollectionPersonInfo, "autoCreateCollection in person should be true");
}

@Test
public void testDefaultDocumentAnnotationTimeToLive() {
final Integer timeToLive = personInfo.getTimeToLive();