Skip to content

Datacouch 550 autoindex not working in spring boot app #295

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
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ private void prepareIndexCreator(final ApplicationContext context) {

if (context instanceof ConfigurableApplicationContext && indexCreator != null) {
((ConfigurableApplicationContext) context).addApplicationListener(indexCreator);
if (mappingContext instanceof CouchbaseMappingContext) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it not make sense to take this opportunity to move the glueing of beans out of the main code path and into a @configuration class?

CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
cmc.setIndexCreator(indexCreator);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@ public boolean isIndexCreatorFor(final MappingContext<?, ?> context) {
return this.mappingContext.equals(context);
}

public boolean hasSeen(CouchbasePersistentEntity<?> entity) {
return classesSeen.containsKey(entity.getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

package org.springframework.data.couchbase.core.mapping;

import java.util.Optional;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.couchbase.core.index.CouchbasePersistentEntityIndexCreator;
import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.data.mapping.context.MappingContextEvent;
import org.springframework.data.mapping.model.FieldNamingStrategy;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
Expand All @@ -31,6 +36,7 @@
* {@link BasicCouchbasePersistentEntity} and {@link BasicCouchbasePersistentProperty} as primary abstractions.
*
* @author Michael Nitschinger
* @author Michael Reiche
*/
public class CouchbaseMappingContext
extends AbstractMappingContext<BasicCouchbasePersistentEntity<?>, CouchbasePersistentProperty>
Expand All @@ -50,6 +56,8 @@ public class CouchbaseMappingContext
private FieldNamingStrategy fieldNamingStrategy = DEFAULT_NAMING_STRATEGY;

private boolean autoIndexCreation = true;
private ApplicationEventPublisher eventPublisher;
private CouchbasePersistentEntityIndexCreator indexCreator = null;

/**
* Configures the {@link FieldNamingStrategy} to be used to determine the field name if no manual mapping is applied.
Expand Down Expand Up @@ -101,6 +109,15 @@ protected CouchbasePersistentProperty createPersistentProperty(Property property
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
super.setApplicationContext(applicationContext);
}

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
eventPublisher = applicationEventPublisher;
if (this.eventPublisher == null) {
this.eventPublisher = context;
}
}

public boolean isAutoIndexCreation() {
Expand All @@ -111,4 +128,45 @@ public void setAutoIndexCreation(boolean autoCreateIndexes) {
this.autoIndexCreation = autoCreateIndexes;
}

/**
* override method from AbstractMappingContext as that method will not publishEvent() if it finds the entity has
* already been cached
*
* @param typeInformation - entity type
*/
@Override
protected Optional<BasicCouchbasePersistentEntity<?>> addPersistentEntity(TypeInformation<?> typeInformation) {
Optional<BasicCouchbasePersistentEntity<?>> entity = super.addPersistentEntity(typeInformation);

if (this.eventPublisher != null && entity.isPresent()) {
if (this.indexCreator != null) {
if (!indexCreator.hasSeen(entity.get())) {
this.eventPublisher.publishEvent(new MappingContextEvent(this, entity.get()));
}
}
}
return entity;
}

/**
* override method from AbstractMappingContext as that method will not publishEvent() if it finds the entity has
* already been cached. Instead, user our own addPersistEntity that will.
*
* @param typeInformation - entity type
*/
@Override
public BasicCouchbasePersistentEntity<?> getPersistentEntity(TypeInformation<?> typeInformation) {
Optional<BasicCouchbasePersistentEntity<?>> entity = addPersistentEntity(typeInformation);
return entity.isPresent() ? entity.get() : null;
}

/**
* capture the indexCreator when it has been added as a listener. only publishEvent() if the indexCreator hasn't
* already seen the class.
*
* @param indexCreator
*/
public void setIndexCreator(CouchbasePersistentEntityIndexCreator indexCreator) {
this.indexCreator = indexCreator;
}
}