Skip to content

DATAKV-112, DATAKV-117 - Make caching repository queries more explicit and pass on type information. #18

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAKV-112-SNAPSHOT</version>

<name>Spring Data KeyValue</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,33 @@ protected AbstractKeyValueAdapter(QueryEngine<? extends KeyValueAdapter, ?, ?> e
return engine;
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable, java.lang.Class)
*/
@Override
public <T> T get(Serializable id, Serializable keyspace, Class<T> type) {
return (T) get(id, keyspace);
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable, java.lang.Class)
*/
@Override
public <T> T delete(Serializable id, Serializable keyspace, Class<T> type) {
return (T) delete(id, keyspace);
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable, java.lang.Class)
*/
@Override
public <T> Iterable<T> find(KeyValueQuery<?> query, Serializable keyspace, Class<T> type) {
return (Iterable<T>) engine.execute(query, keyspace, type);
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#find(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.io.Serializable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,6 +58,15 @@ public interface KeyValueAdapter extends DisposableBean {
*/
Object get(Serializable id, Serializable keyspace);

/**
* @param id
* @param keyspace
* @param type
* @return
* @since 1.1
*/
<T> T get(Serializable id, Serializable keyspace, Class<T> type);

/**
* Delete and return the obect with given type and id.
*
Expand All @@ -67,6 +76,15 @@ public interface KeyValueAdapter extends DisposableBean {
*/
Object delete(Serializable id, Serializable keyspace);

/**
* @param id
* @param keyspace
* @param type
* @return
* @since 1.1
*/
<T> T delete(Serializable id, Serializable keyspace, Class<T> type);

/**
* Get all elements for given keyspace.
*
Expand All @@ -76,7 +94,7 @@ public interface KeyValueAdapter extends DisposableBean {
Iterable<?> getAllOf(Serializable keyspace);

/**
* Returns a {@link KeyValueIterator} that iterates over all entries.
* Returns a {@link CloseableIterator} that iterates over all entries.
*
* @param keyspace
* @return
Expand Down Expand Up @@ -104,6 +122,15 @@ public interface KeyValueAdapter extends DisposableBean {
*/
Iterable<?> find(KeyValueQuery<?> query, Serializable keyspace);

/**
* @param query
* @param keyspace
* @param type
* @return
* @since 1.1
*/
<T> Iterable<T> find(KeyValueQuery<?> query, Serializable keyspace, Class<T> type);

/**
* Count number of objects within {@literal keyspace}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public <T> T findById(final Serializable id, final Class<T> type) {
@Override
public T doInKeyValue(KeyValueAdapter adapter) {

Object result = adapter.get(id, keyspace);
Object result = adapter.get(id, keyspace, type);

if (result == null || typeCheck(type, result)) {
return (T) result;
Expand Down Expand Up @@ -342,10 +342,9 @@ public <T> T delete(final Serializable id, final Class<T> type) {

T result = execute(new KeyValueCallback<T>() {

@SuppressWarnings("unchecked")
@Override
public T doInKeyValue(KeyValueAdapter adapter) {
return (T) adapter.delete(id, keyspace);
return (T) adapter.delete(id, keyspace, type);
}
});

Expand Down Expand Up @@ -394,7 +393,7 @@ public <T> Iterable<T> find(final KeyValueQuery<?> query, final Class<T> type) {
@Override
public Iterable<T> doInKeyValue(KeyValueAdapter adapter) {

Iterable<?> result = adapter.find(query, resolveKeySpace(type));
Iterable<?> result = adapter.find(query, resolveKeySpace(type), type);
if (result == null) {
return Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,21 @@ public Collection<?> execute(KeyValueQuery<?> query, Serializable keyspace) {
return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace);
}

/**
* Extract query attributes and delegate to concrete execution.
*
* @param query
* @param keyspace
* @return
*/
public <T> Collection<T> execute(KeyValueQuery<?> query, Serializable keyspace, Class<T> type) {

CRITERIA criteria = this.criteriaAccessor != null ? this.criteriaAccessor.resolve(query) : null;
SORT sort = this.sortAccessor != null ? this.sortAccessor.resolve(query) : null;

return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace, type);
}

/**
* Extract query attributes and delegate to concrete execution.
*
Expand All @@ -79,6 +94,21 @@ public long count(KeyValueQuery<?> query, Serializable keyspace) {
*/
public abstract Collection<?> execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace);

/**
* @param criteria
* @param sort
* @param offset
* @param rows
* @param keyspace
* @param type
* @return
* @since 1.1
*/
public <T> Collection<T> execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace,
Class<T> type) {
return (Collection<T>) execute(criteria, sort, offset, rows, keyspace);
}

/**
* @param criteria
* @param keyspace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery;
import org.springframework.data.keyvalue.repository.query.SpelQueryCreator;
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
Expand Down Expand Up @@ -92,6 +93,7 @@ public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfi

builder.addPropertyReference("keyValueOperations", attributes.getString(KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE));
builder.addPropertyValue("queryCreator", getQueryCreatorType(config));
builder.addPropertyValue("queryType", getQueryType(config));
builder.addPropertyReference("mappingContext", MAPPING_CONTEXT_BEAN_NAME);
}

Expand All @@ -106,16 +108,37 @@ private static Class<?> getQueryCreatorType(AnnotationRepositoryConfigurationSou

AnnotationMetadata metadata = config.getEnableAnnotationMetadata();

Map<String, Object> queryCreatorFoo = metadata.getAnnotationAttributes(QueryCreatorType.class.getName());
Map<String, Object> queryCreatorAnnotationAttributes = metadata.getAnnotationAttributes(QueryCreatorType.class.getName());

if (queryCreatorFoo == null) {
if (queryCreatorAnnotationAttributes == null) {
return SpelQueryCreator.class;
}

AnnotationAttributes queryCreatorAttributes = new AnnotationAttributes(queryCreatorFoo);
AnnotationAttributes queryCreatorAttributes = new AnnotationAttributes(queryCreatorAnnotationAttributes);
return queryCreatorAttributes.getClass("value");
}

/**
* Detects the query creator type to be used for the factory to set. Will lookup a {@link QueryCreatorType} annotation
* on the {@code @Enable}-annotation or use {@link SpelQueryCreator} if not found.
*
* @param config
* @return
*/
private static Class<?> getQueryType(AnnotationRepositoryConfigurationSource config) {

AnnotationMetadata metadata = config.getEnableAnnotationMetadata();

Map<String, Object> queryCreatorAnnotationAttributes = metadata.getAnnotationAttributes(QueryCreatorType.class.getName());

if (queryCreatorAnnotationAttributes == null) {
return KeyValuePartTreeQuery.class;
}

AnnotationAttributes queryCreatorAttributes = new AnnotationAttributes(queryCreatorAnnotationAttributes);
return queryCreatorAttributes.getClass("repositoryQueryType");
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#registerBeansForRoot(org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.data.repository.config.RepositoryConfigurationSource)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,17 +21,28 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.parser.AbstractQueryCreator;

/**
* Annotation to customize the query creator type to be used for a specific store.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface QueryCreatorType {

Class<? extends AbstractQueryCreator<?, ?>> value();

/**
* The {@link RepositoryQuery} type to be created by the {@link QueryCreatorType#value()}.
*
* @return
* @since 1.1
*/
Class<? extends RepositoryQuery> repositoryQueryType() default KeyValuePartTreeQuery.class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.repository.query;

import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
import org.springframework.data.repository.query.parser.PartTree;

/**
* {@link KeyValuePartTreeQuery} implementation deriving queries from {@link PartTree} using a predefined
* {@link AbstractQueryCreator} that caches the once created query.
*
* @author Christoph Strobl
* @since 1.1
*/
public class CachingKeyValuePartTreeQuery extends KeyValuePartTreeQuery {

private KeyValueQuery<?> cachedQuery;

public CachingKeyValuePartTreeQuery(QueryMethod queryMethod, EvaluationContextProvider evaluationContextProvider,
KeyValueOperations keyValueOperations, Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
super(queryMethod, evaluationContextProvider, keyValueOperations, queryCreator);
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery#prepareQuery(java.lang.Object[])
*/
protected KeyValueQuery<?> prepareQuery(Object[] parameters) {

if (cachedQuery == null) {
cachedQuery = super.prepareQuery(parameters);
}

return prepareQuery(cachedQuery, parameters);
}
}
Loading