diff --git a/pom.xml b/pom.xml
index faa2c50..5bea76e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -29,6 +29,30 @@
${springdata.commons}
+
+ org.springframework
+ spring-context
+
+
+
+ org.springframework
+ spring-tx
+
+
+
+ com.mysema.querydsl
+ querydsl-collections
+ ${querydsl}
+ true
+
+
+
+ joda-time
+ joda-time
+ 2.5
+ test
+
+
diff --git a/src/main/asciidoc/key-value-repositories.adoc b/src/main/asciidoc/key-value-repositories.adoc
new file mode 100644
index 0000000..e10fa86
--- /dev/null
+++ b/src/main/asciidoc/key-value-repositories.adoc
@@ -0,0 +1,147 @@
+:spring-framework-docs: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html
+
+[[key-value]]
+= Key Value Repositories
+
+This chapter explains concepts and usage patterns when working with the key value abstraction and the `java.util.Map` based implementation provided by Spring Data Commons.
+
+[[key-value.core-concepts]]
+== Core Concepts
+
+The Key/Value abstraction within Spring Data Commons requires an `Adapter` shielding the native store implementation freeing up `KeyValueTemplate` to work on top of any key/value pair like structure. Keys are distributed across <>. Unless otherwise specified the class name is used as the default keyspace for an entity.
+
+[source, java]
+----
+interface KeyValueOperations {
+
+ T insert(T objectToInsert); <1>
+
+ void update(Object objectToUpdate); <2>
+
+ void delete(Class> type); <3>
+
+ T findById(Serializable id, Class type); <4>
+
+ List findAllOf(Class type); <5>
+
+ List find(KeyValueQuery> query, Class type); <6>
+
+ //... more functionality omitted.
+
+}
+----
+<1> Inserts the given entity and assigns id if required.
+<2> Updates the given entity.
+<3> Removes all entities of matching type.
+<4> Returns the entity of given type with matching id.
+<5> Returns all entities of matching type.
+<6> Returns a List of all entities of given type matching the criteria of the query.
+
+[[key-value.template-configuration]]
+== Configuring The KeyValueTemplate
+
+In its very basic shape the `KeyValueTemplate` uses a `MapAdaper` wrapping a `ConcurrentHashMap` using link:{spring-framework-docs}/expressions.html[Spring Expression Language] to perform queries and sorting.
+
+NOTE: The used `KeyValueAdapter` does the heavy lifting when it comes to storing and retrieving data. The data structure used will influence performance and/or multi threading behavior.
+
+One may choose to use a different type or preinitialize the adapter with some values, and can do so using `MapKeyValueAdapterFactory`.
+
+[source, java]
+----
+@Bean
+public KeyValueOperations keyValueTemplate() {
+ return new KeyValueTemplate(keyValueAdapter());
+}
+
+@Bean
+public KeyValueAdapter keyValueAdapter() {
+
+ MapKeyValueAdapterFactory factory = new MapKeyValueAdapterFactory();
+ factory.setMapType(ConcurrentSkipListMap.class);
+ factory.setInitialValuesForKeyspace("lennister", singletonMap("1", "tyrion"));
+ factory.setInitialValuesForKeyspace("stark", singletonMap("1", "sansa"));
+
+ return factory.getAdapter();
+}
+----
+
+[[key-value.keyspaces]]
+== Keyspaces
+
+Keyspaces define in which part of the data structure the entity should be kept. So this is a rather similar concept as collections in MongoDB and Elasticsearch, Cores in Solr, Tables in JPA.
+By default the keyspace of an entity is extracted form its type, but one can also choose to store entities of different types within one keyspace. In that case any find operation will type check results.
+
+[source, java]
+----
+@KeySpace("persons")
+class Person {
+
+ @Id String id;
+ String firstname;
+ String lastname;
+}
+
+class User extends Person {
+ String username;
+}
+
+template.findAllOf(Person.class); <1>
+template.findAllOf(User.class); <2>
+----
+<1> Returns all entities for keyspace "persons".
+<2> Returns only elements of type `User` stored in keyspace "persons".
+
+[[key-value.template-query]]
+== Querying
+
+Query execution is managed by the `QueryEngine`. As mentioned before it is possible to instruct the `KeyValueAdapter` to use an implementation specific `QueryEngine` that allows access to native functionality.
+When used without further customization queries are be executed using a `SpELQueryEngine`.
+
+NOTE: For performance reasons, we highly recommend to have at least Spring 4.1.2 or better to make use of link:{spring-framework-docs}/expressions.html#expressions-spel-compilation[compiled SpEL Expressions].
+
+[source, java]
+----
+KeyValueQuery query = new KeyValueQuery("lastname == 'targaryen'");
+List targaryens = template.find(query, Person.class);
+----
+
+WARNING: Please note that you need to have getters/setters present to query properties using SpEL.
+
+[[key-value.template-sort]]
+== Sorting
+
+Depending on the store implementation provided by the adapter entities might already be stored in some sorted way but do not necessarily have to be. Again the underlying `QueryEngine` is capable of performing sort operations.
+When used without further customization sorting is done using a `SpelPropertyComperator` extracted from the `Sort` clause provided
+
+[source, java]
+----
+KeyValueQuery query = new KeyValueQuery("lastname == 'baratheon'");
+query.setSort(new Sort(DESC, "age"));
+List targaryens = template.find(query, Person.class);
+----
+
+WARNING: Please note that you need to have getters/setters present to sort using SpEL.
+
+[[key-value.repositories]]
+== Key Value Repositories
+
+KeyValue repositories reside on top of the `KeyValaueTemplate`. Using the default `SpelQueryCreator` allows deriving query and sort expressions from the given methodname.
+
+[source, java]
+----
+@Configuration
+@EnableKeyValueRepositories
+class KeyValueConfig {
+
+ @Bean
+ public KeyValueOperations keyValueTemplate() {
+ return new KeyValueTemplate(new MapKeyValueAdapter());
+ }
+
+}
+
+interface PersonRepository implements CrudRepository {
+ List findByLastname(String lastname);
+}
+----
+
diff --git a/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java b/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java
new file mode 100644
index 0000000..a4078f0
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2014 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.annotation;
+
+import static java.lang.annotation.ElementType.*;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.springframework.data.annotation.Persistent;
+
+/**
+ * Marker interface for methods with {@link Persistent} annotations indicating the presence of a dedicated keyspace the
+ * entity should reside in. If present the value will be picked up for resolving the keyspace.
+ *
+ *
+ *
+ * Can also be directly used on types to indicate the keyspace.
+ *
+ *
+ *
+ * @KeySpace("persons")
+ * public class Foo {
+ *
+ * }
+ *
+ *
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(value = { METHOD, TYPE })
+public @interface KeySpace {
+
+ String value() default "";
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java b/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java
new file mode 100644
index 0000000..748c31e
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+
+/**
+ * Base implementation of {@link KeyValueAdapter} holds {@link QueryEngine} to delegate {@literal find} and
+ * {@literal count} execution to.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public abstract class AbstractKeyValueAdapter implements KeyValueAdapter {
+
+ private final QueryEngine extends KeyValueAdapter, ?, ?> engine;
+
+ /**
+ * Creates new {@link AbstractKeyValueAdapter} with using the default query engine.
+ */
+ protected AbstractKeyValueAdapter() {
+ this(null);
+ }
+
+ /**
+ * Creates new {@link AbstractKeyValueAdapter} with using the default query engine.
+ *
+ * @param engine will be defaulted to {@link SpelQueryEngine} if {@literal null}.
+ */
+ protected AbstractKeyValueAdapter(QueryEngine extends KeyValueAdapter, ?, ?> engine) {
+
+ this.engine = engine != null ? engine : new SpelQueryEngine();
+ this.engine.registerAdapter(this);
+ }
+
+ /**
+ * Get the {@link QueryEngine} used.
+ *
+ * @return
+ */
+ protected QueryEngine extends KeyValueAdapter, ?, ?> getQueryEngine() {
+ return engine;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueAdapter#find(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.io.Serializable)
+ */
+ @Override
+ public Collection> find(KeyValueQuery> query, Serializable keyspace) {
+ return engine.execute(query, keyspace);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueAdapter#count(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.io.Serializable)
+ */
+ @Override
+ public long count(KeyValueQuery> query, Serializable keyspace) {
+ return engine.count(query, keyspace);
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/CriteriaAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/CriteriaAccessor.java
new file mode 100644
index 0000000..d1804ce
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/CriteriaAccessor.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2014 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.core;
+
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+
+/**
+ * Resolves the criteria object from given {@link KeyValueQuery}.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param
+ */
+public interface CriteriaAccessor {
+
+ /**
+ * Checks and reads {@link KeyValueQuery#getCritieria()} of given {@link KeyValueQuery}. Might also apply additional
+ * transformation to match the desired type.
+ *
+ * @param query can be {@literal null}.
+ * @return the criteria extracted from the query.
+ * @throws IllegalArgumentException in case the criteria is not valid for usage with specific {@link CriteriaAccessor}
+ * .
+ */
+ T resolve(KeyValueQuery> query);
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java b/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java
new file mode 100644
index 0000000..95dc054
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.UUID;
+
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.data.util.TypeInformation;
+import org.springframework.util.ClassUtils;
+
+/**
+ * Default implementation of {@link IdentifierGenerator} to generate identifiers of types {@link UUID}, String,
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ */
+enum DefaultIdentifierGenerator implements IdentifierGenerator {
+
+ INSTANCE;
+
+ private static final String ALGORITHM = "NativePRNGBlocking";
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.IdentifierGenerator#newIdForType(org.springframework.data.util.TypeInformation)
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ public T generateIdentifierOfType(TypeInformation identifierType) {
+
+ Class> type = identifierType.getType();
+
+ if (ClassUtils.isAssignable(UUID.class, type)) {
+ return (T) UUID.randomUUID();
+ } else if (ClassUtils.isAssignable(String.class, type)) {
+ return (T) UUID.randomUUID().toString();
+ } else if (ClassUtils.isAssignable(Integer.class, type)) {
+
+ try {
+ return (T) SecureRandom.getInstance(ALGORITHM);
+ } catch (NoSuchAlgorithmException e) {
+ throw new InvalidDataAccessApiUsageException("Could not create SecureRandom instance.", e);
+ }
+
+ } else if (ClassUtils.isAssignable(Long.class, type)) {
+
+ try {
+ return (T) Long.valueOf(SecureRandom.getInstance(ALGORITHM).nextLong());
+ } catch (NoSuchAlgorithmException e) {
+ throw new InvalidDataAccessApiUsageException("Could not create SecureRandom instance.", e);
+ }
+ }
+
+ throw new InvalidDataAccessApiUsageException("Non gereratable id type....");
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java
new file mode 100644
index 0000000..ab00989
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.io.Serializable;
+
+import org.springframework.data.mapping.IdentifierAccessor;
+import org.springframework.data.mapping.PersistentProperty;
+import org.springframework.data.mapping.PersistentPropertyAccessor;
+import org.springframework.util.Assert;
+
+/**
+ * {@link IdentifierAccessor} adding a {@link #getOrGenerateIdentifier()} to automatically generate an identifier and
+ * set it on the underling bean instance.
+ *
+ * @author Oliver Gierke
+ * @see #getOrGenerateIdentifier()
+ */
+class GeneratingIdAccessor implements IdentifierAccessor {
+
+ private final PersistentPropertyAccessor accessor;
+ private final PersistentProperty> identifierProperty;
+ private final IdentifierGenerator generator;
+
+ /**
+ * Creates a new {@link GeneratingIdAccessor} using the given {@link PersistentPropertyAccessor}, identifier property
+ * and {@link IdentifierGenerator}.
+ *
+ * @param accessor must not be {@literal null}.
+ * @param identifierProperty must not be {@literal null}.
+ * @param generator must not be {@literal null}.
+ */
+ public GeneratingIdAccessor(PersistentPropertyAccessor accessor, PersistentProperty> identifierProperty,
+ IdentifierGenerator generator) {
+
+ Assert.notNull(accessor, "PersistentPropertyAccessor must not be null!");
+ Assert.notNull(identifierProperty, "Identifier property must not be null!");
+ Assert.notNull(generator, "IdentifierGenerator must not be null!");
+
+ this.accessor = accessor;
+ this.identifierProperty = identifierProperty;
+ this.generator = generator;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.IdentifierAccessor#getIdentifier()
+ */
+ @Override
+ public Object getIdentifier() {
+ return accessor.getProperty(identifierProperty);
+ }
+
+ /**
+ * Returns the identifier value of the backing bean or generates a new one using the configured
+ * {@link IdentifierGenerator}.
+ *
+ * @return
+ */
+ public Object getOrGenerateIdentifier() {
+
+ Serializable existingIdentifier = (Serializable) getIdentifier();
+
+ if (existingIdentifier != null) {
+ return existingIdentifier;
+ }
+
+ Object generatedIdentifier = generator.generateIdentifierOfType(identifierProperty.getTypeInformation());
+ accessor.setProperty(identifierProperty, generatedIdentifier);
+
+ return generatedIdentifier;
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/IdentifierGenerator.java b/src/main/java/org/springframework/data/keyvalue/core/IdentifierGenerator.java
new file mode 100644
index 0000000..25361d2
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/IdentifierGenerator.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2014 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.core;
+
+import org.springframework.data.util.TypeInformation;
+
+/**
+ * API for components generating identifiers.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ */
+public interface IdentifierGenerator {
+
+ /**
+ * Creates an identifier of the given type.
+ *
+ * @param type must not be {@literal null}.
+ * @return an identifier of the given type.
+ */
+ T generateIdentifierOfType(TypeInformation type);
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeySpaceUtils.java b/src/main/java/org/springframework/data/keyvalue/core/KeySpaceUtils.java
new file mode 100644
index 0000000..640c2c6
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/KeySpaceUtils.java
@@ -0,0 +1,398 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.springframework.core.annotation.AnnotatedElementUtils;
+import org.springframework.core.annotation.AnnotationAttributes;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.core.style.ToStringCreator;
+import org.springframework.data.annotation.Persistent;
+import org.springframework.data.keyvalue.annotation.KeySpace;
+import org.springframework.data.keyvalue.core.KeySpaceUtils.MetaAnnotationUtils.AnnotationDescriptor;
+import org.springframework.util.Assert;
+import org.springframework.util.ObjectUtils;
+
+/**
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ */
+abstract class KeySpaceUtils {
+
+ private KeySpaceUtils() {}
+
+ /**
+ * Looks up {@link Persistent} when used as meta annotation to find the {@link KeySpace} attribute.
+ *
+ * @return
+ * @since 1.10
+ */
+ public static Object getKeySpace(Class> type) {
+
+ KeySpace keyspace = AnnotationUtils.findAnnotation(type, KeySpace.class);
+
+ if (keyspace != null) {
+ return AnnotationUtils.getValue(keyspace);
+ }
+
+ AnnotationDescriptor descriptor = KeySpaceUtils.MetaAnnotationUtils.findAnnotationDescriptor(type,
+ Persistent.class);
+
+ if (descriptor != null && descriptor.getComposedAnnotation() != null) {
+
+ Annotation composed = descriptor.getComposedAnnotation();
+
+ for (Method method : descriptor.getComposedAnnotationType().getDeclaredMethods()) {
+
+ keyspace = AnnotationUtils.findAnnotation(method, KeySpace.class);
+
+ if (keyspace != null) {
+ return AnnotationUtils.getValue(composed, method.getName());
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * {@code MetaAnnotationUtils} is a collection of utility methods that complements the standard support already
+ * available in {@link AnnotationUtils}.
+ *
+ * Whereas {@code AnnotationUtils} provides utilities for getting or finding an annotation,
+ * {@code MetaAnnotationUtils} goes a step further by providing support for determining the root class on
+ * which an annotation is declared, either directly or indirectly via a composed
+ * annotation. This additional information is encapsulated in an {@link AnnotationDescriptor}.
+ *
+ * The additional information provided by an {@code AnnotationDescriptor} is required by the
+ * Spring TestContext Framework in order to be able to support class hierarchy traversals for annotations
+ * such as {@link org.springframework.test.context.ContextConfiguration @ContextConfiguration},
+ * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}, and
+ * {@link org.springframework.test.context.ActiveProfiles @ActiveProfiles} which offer support for merging and
+ * overriding various inherited annotation attributes (e.g.,
+ * {@link org.springframework.test.context.ContextConfiguration#inheritLocations}).
+ *
+ * @author Sam Brannen
+ * @since 4.0
+ * @see AnnotationUtils
+ * @see AnnotationDescriptor
+ */
+ static abstract class MetaAnnotationUtils {
+
+ private MetaAnnotationUtils() {
+ /* no-op */
+ }
+
+ /**
+ * Find the {@link AnnotationDescriptor} for the supplied {@code annotationType} on the supplied {@link Class},
+ * traversing its annotations and superclasses if no annotation can be found on the given class itself.
+ *
+ * This method explicitly handles class-level annotations which are not declared as
+ * {@linkplain java.lang.annotation.Inherited inherited} as
+ * well as meta-annotations.
+ *
+ * The algorithm operates as follows:
+ *
+ *
Search for the annotation on the given class and return a corresponding {@code AnnotationDescriptor} if
+ * found.
+ *
Recursively search through all annotations that the given class declares.
+ *
Recursively search through the superclass hierarchy of the given class.
+ *
+ *
+ * In this context, the term recursively means that the search process continues by returning to step #1
+ * with the current annotation or superclass as the class to look for annotations on.
+ *
+ * If the supplied {@code clazz} is an interface, only the interface itself will be checked; the inheritance
+ * hierarchy for interfaces will not be traversed.
+ *
+ * @param clazz the class to look for annotations on
+ * @param annotationType the type of annotation to look for
+ * @return the corresponding annotation descriptor if the annotation was found; otherwise {@code null}
+ * @see AnnotationUtils#findAnnotationDeclaringClass(Class, Class)
+ * @see #findAnnotationDescriptorForTypes(Class, Class...)
+ */
+ public static AnnotationDescriptor findAnnotationDescriptor(Class> clazz,
+ Class annotationType) {
+ return findAnnotationDescriptor(clazz, new HashSet(), annotationType);
+ }
+
+ /**
+ * Perform the search algorithm for {@link #findAnnotationDescriptor(Class, Class)}, avoiding endless recursion by
+ * tracking which annotations have already been visited.
+ *
+ * @param clazz the class to look for annotations on
+ * @param visited the set of annotations that have already been visited
+ * @param annotationType the type of annotation to look for
+ * @return the corresponding annotation descriptor if the annotation was found; otherwise {@code null}
+ */
+ private static AnnotationDescriptor findAnnotationDescriptor(Class> clazz,
+ Set visited, Class annotationType) {
+
+ Assert.notNull(annotationType, "Annotation type must not be null");
+
+ if (clazz == null || clazz.equals(Object.class)) {
+ return null;
+ }
+
+ // Declared locally?
+ if (AnnotationUtils.isAnnotationDeclaredLocally(annotationType, clazz)) {
+ return new AnnotationDescriptor(clazz, clazz.getAnnotation(annotationType));
+ }
+
+ // Declared on a composed annotation (i.e., as a meta-annotation)?
+ for (Annotation composedAnnotation : clazz.getDeclaredAnnotations()) {
+ if (!AnnotationUtils.isInJavaLangAnnotationPackage(composedAnnotation) && visited.add(composedAnnotation)) {
+ AnnotationDescriptor descriptor = findAnnotationDescriptor(composedAnnotation.annotationType(), visited,
+ annotationType);
+ if (descriptor != null) {
+ return new AnnotationDescriptor(clazz, descriptor.getDeclaringClass(), composedAnnotation,
+ descriptor.getAnnotation());
+ }
+ }
+ }
+
+ // Declared on a superclass?
+ return findAnnotationDescriptor(clazz.getSuperclass(), visited, annotationType);
+ }
+
+ /**
+ * Find the {@link UntypedAnnotationDescriptor} for the first {@link Class} in the inheritance hierarchy of the
+ * specified {@code clazz} (including the specified {@code clazz} itself) which declares at least one of the
+ * specified {@code annotationTypes}.
+ *
+ * This method traverses the annotations and superclasses of the specified {@code clazz} if no annotation can be
+ * found on the given class itself.
+ *
+ * This method explicitly handles class-level annotations which are not declared as
+ * {@linkplain java.lang.annotation.Inherited inherited} as
+ * well as meta-annotations.
+ *
+ * The algorithm operates as follows:
+ *
+ *
Search for a local declaration of one of the annotation types on the given class and return a corresponding
+ * {@code UntypedAnnotationDescriptor} if found.
+ *
Recursively search through all annotations that the given class declares.
+ *
Recursively search through the superclass hierarchy of the given class.
+ *
+ *
+ * In this context, the term recursively means that the search process continues by returning to step #1
+ * with the current annotation or superclass as the class to look for annotations on.
+ *
+ * If the supplied {@code clazz} is an interface, only the interface itself will be checked; the inheritance
+ * hierarchy for interfaces will not be traversed.
+ *
+ * @param clazz the class to look for annotations on
+ * @param annotationTypes the types of annotations to look for
+ * @return the corresponding annotation descriptor if one of the annotations was found; otherwise {@code null}
+ * @see AnnotationUtils#findAnnotationDeclaringClassForTypes(java.util.List, Class)
+ * @see #findAnnotationDescriptor(Class, Class)
+ */
+ public static UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(Class> clazz,
+ Class extends Annotation>... annotationTypes) {
+ return findAnnotationDescriptorForTypes(clazz, new HashSet(), annotationTypes);
+ }
+
+ /**
+ * Perform the search algorithm for {@link #findAnnotationDescriptorForTypes(Class, Class...)}, avoiding endless
+ * recursion by tracking which annotations have already been visited.
+ *
+ * @param clazz the class to look for annotations on
+ * @param visited the set of annotations that have already been visited
+ * @param annotationTypes the types of annotations to look for
+ * @return the corresponding annotation descriptor if one of the annotations was found; otherwise {@code null}
+ */
+ private static UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(Class> clazz,
+ Set visited, Class extends Annotation>... annotationTypes) {
+
+ assertNonEmptyAnnotationTypeArray(annotationTypes, "The list of annotation types must not be empty");
+
+ if (clazz == null || clazz.equals(Object.class)) {
+ return null;
+ }
+
+ // Declared locally?
+ for (Class extends Annotation> annotationType : annotationTypes) {
+ if (AnnotationUtils.isAnnotationDeclaredLocally(annotationType, clazz)) {
+ return new UntypedAnnotationDescriptor(clazz, clazz.getAnnotation(annotationType));
+ }
+ }
+
+ // Declared on a composed annotation (i.e., as a meta-annotation)?
+ for (Annotation composedAnnotation : clazz.getDeclaredAnnotations()) {
+ if (!AnnotationUtils.isInJavaLangAnnotationPackage(composedAnnotation) && visited.add(composedAnnotation)) {
+ UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(
+ composedAnnotation.annotationType(), visited, annotationTypes);
+ if (descriptor != null) {
+ return new UntypedAnnotationDescriptor(clazz, descriptor.getDeclaringClass(), composedAnnotation,
+ descriptor.getAnnotation());
+ }
+ }
+ }
+
+ // Declared on a superclass?
+ return findAnnotationDescriptorForTypes(clazz.getSuperclass(), visited, annotationTypes);
+ }
+
+ /**
+ * Descriptor for an {@link Annotation}, including the {@linkplain #getDeclaringClass() class} on which the
+ * annotation is declared as well as the actual {@linkplain #getAnnotation() annotation} instance.
+ *
+ * If the annotation is used as a meta-annotation, the descriptor also includes the
+ * {@linkplain #getComposedAnnotation() composed annotation} on which the annotation is present. In such cases, the
+ * root declaring class is not directly annotated with the annotation but rather indirectly via the
+ * composed annotation.
+ *
+ * Given the following example, if we are searching for the {@code @Transactional} annotation on the
+ * {@code TransactionalTests} class, then the properties of the {@code AnnotationDescriptor} would be as follows.
+ *
+ *
rootDeclaringClass: {@code TransactionalTests} class object
+ *
declaringClass: {@code TransactionalTests} class object
+ *
composedAnnotation: {@code null}
+ *
annotation: instance of the {@code Transactional} annotation
+ *
+ *
+ *
+ * @Transactional
+ * @ContextConfiguration({ "/test-datasource.xml", "/repository-config.xml" })
+ * public class TransactionalTests {}
+ *
+ *
+ * Given the following example, if we are searching for the {@code @Transactional} annotation on the
+ * {@code UserRepositoryTests} class, then the properties of the {@code AnnotationDescriptor} would be as follows.
+ *
+ *
rootDeclaringClass: {@code UserRepositoryTests} class object
+ *
declaringClass: {@code RepositoryTests} class object
+ *
composedAnnotation: instance of the {@code RepositoryTests} annotation
+ *
annotation: instance of the {@code Transactional} annotation
+ *
+ * @author Sam Brannen
+ * @since 4.0
+ */
+ public static class AnnotationDescriptor {
+
+ private final Class> rootDeclaringClass;
+ private final Class> declaringClass;
+ private final Annotation composedAnnotation;
+ private final T annotation;
+ private final AnnotationAttributes annotationAttributes;
+
+ public AnnotationDescriptor(Class> rootDeclaringClass, T annotation) {
+ this(rootDeclaringClass, rootDeclaringClass, null, annotation);
+ }
+
+ public AnnotationDescriptor(Class> rootDeclaringClass, Class> declaringClass, Annotation composedAnnotation,
+ T annotation) {
+ Assert.notNull(rootDeclaringClass, "rootDeclaringClass must not be null");
+ Assert.notNull(annotation, "annotation must not be null");
+
+ this.rootDeclaringClass = rootDeclaringClass;
+ this.declaringClass = declaringClass;
+ this.composedAnnotation = composedAnnotation;
+ this.annotation = annotation;
+ this.annotationAttributes = AnnotatedElementUtils.getAnnotationAttributes(rootDeclaringClass, annotation
+ .annotationType().getName());
+ }
+
+ public Class> getRootDeclaringClass() {
+ return this.rootDeclaringClass;
+ }
+
+ public Class> getDeclaringClass() {
+ return this.declaringClass;
+ }
+
+ public T getAnnotation() {
+ return this.annotation;
+ }
+
+ public Class extends Annotation> getAnnotationType() {
+ return this.annotation.annotationType();
+ }
+
+ public AnnotationAttributes getAnnotationAttributes() {
+ return this.annotationAttributes;
+ }
+
+ public Annotation getComposedAnnotation() {
+ return this.composedAnnotation;
+ }
+
+ public Class extends Annotation> getComposedAnnotationType() {
+ return this.composedAnnotation == null ? null : this.composedAnnotation.annotationType();
+ }
+
+ /**
+ * Provide a textual representation of this {@code AnnotationDescriptor}.
+ */
+ @Override
+ public String toString() {
+ return new ToStringCreator(this)//
+ .append("rootDeclaringClass", rootDeclaringClass)//
+ .append("declaringClass", declaringClass)//
+ .append("composedAnnotation", composedAnnotation)//
+ .append("annotation", annotation)//
+ .toString();
+ }
+ }
+
+ /**
+ * Untyped extension of {@code AnnotationDescriptor} that is used to describe the declaration of one of
+ * several candidate annotation types where the actual annotation type cannot be predetermined.
+ *
+ * @author Sam Brannen
+ * @since 4.0
+ */
+ public static class UntypedAnnotationDescriptor extends AnnotationDescriptor {
+
+ public UntypedAnnotationDescriptor(Class> rootDeclaringClass, Annotation annotation) {
+ this(rootDeclaringClass, rootDeclaringClass, null, annotation);
+ }
+
+ public UntypedAnnotationDescriptor(Class> rootDeclaringClass, Class> declaringClass,
+ Annotation composedAnnotation, Annotation annotation) {
+ super(rootDeclaringClass, declaringClass, composedAnnotation, annotation);
+ }
+ }
+
+ private static void assertNonEmptyAnnotationTypeArray(Class>[] annotationTypes, String message) {
+ if (ObjectUtils.isEmpty(annotationTypes)) {
+ throw new IllegalArgumentException(message);
+ }
+
+ for (Class> clazz : annotationTypes) {
+ if (!Annotation.class.isAssignableFrom(clazz)) {
+ throw new IllegalArgumentException("Array elements must be of type Annotation");
+ }
+ }
+ }
+
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java
new file mode 100644
index 0000000..42be9c4
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+
+/**
+ * {@link KeyValueAdapter} unifies access and shields the underlying key/value specific implementation.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public interface KeyValueAdapter extends DisposableBean {
+
+ /**
+ * Add object with given id to keyspace.
+ *
+ * @param id must not be {@literal null}.
+ * @param keyspace must not be {@literal null}.
+ * @return the item previously associated with the id.
+ */
+ Object put(Serializable id, Object item, Serializable keyspace);
+
+ /**
+ * Check if a object with given id exists in keyspace.
+ *
+ * @param id must not be {@literal null}.
+ * @param keyspace must not be {@literal null}.
+ * @return true if item of type with id exists.
+ */
+ boolean contains(Serializable id, Serializable keyspace);
+
+ /**
+ * Get the object with given id from keyspace.
+ *
+ * @param id must not be {@literal null}.
+ * @param keyspace must not be {@literal null}.
+ * @return {@literal null} in case no matching item exists.
+ */
+ Object get(Serializable id, Serializable keyspace);
+
+ /**
+ * Delete and return the obect with given type and id.
+ *
+ * @param id must not be {@literal null}.
+ * @param keyspace must not be {@literal null}.
+ * @return {@literal null} if object could not be found
+ */
+ Object delete(Serializable id, Serializable keyspace);
+
+ /**
+ * Get all elements for given keyspace.
+ *
+ * @param keyspace must not be {@literal null}.
+ * @return empty {@link Collection} if nothing found.
+ */
+ Collection> getAllOf(Serializable keyspace);
+
+ /**
+ * Remove all objects of given type.
+ *
+ * @param keyspace must not be {@literal null}.
+ */
+ void deleteAllOf(Serializable keyspace);
+
+ /**
+ * Removes all objects.
+ */
+ void clear();
+
+ /**
+ * Find all matching objects within {@literal keyspace}.
+ *
+ * @param query
+ * @param keyspace must not be {@literal null}.
+ * @return empty {@link Collection} if no match found.
+ */
+ Collection> find(KeyValueQuery> query, Serializable keyspace);
+
+ /**
+ * Count all matching objects within {@literal keyspace}.
+ *
+ * @param query
+ * @param keyspace must not be {@literal null}.
+ * @return
+ */
+ long count(KeyValueQuery> query, Serializable keyspace);
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueCallback.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueCallback.java
new file mode 100644
index 0000000..b30f49d
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueCallback.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2014 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.core;
+
+/**
+ * Generic callback interface for code that operates on a {@link KeyValueAdapter}. This is particularly useful for
+ * delegating code that needs to work closely on the underlying key/value store implementation.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param
+ */
+public interface KeyValueCallback {
+
+ /**
+ * Gets called by {@code KeyValueTemplate#execute(KeyValueCallback)}. Allows for returning a result object created
+ * within the callback, i.e. a domain object or a collection of domain objects.
+ *
+ * @param adapter
+ * @return
+ */
+ T doInKeyValue(KeyValueAdapter adapter);
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java
new file mode 100644
index 0000000..3495408
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.keyvalue.annotation.KeySpace;
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+import org.springframework.data.mapping.context.MappingContext;
+
+/**
+ * Interface that specifies a basic set of key/value operations. Implemented by {@link KeyValueTemplate}.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public interface KeyValueOperations extends DisposableBean {
+
+ /**
+ * Add given object. Object needs to have id property to which a generated value will be assigned.
+ *
+ * @param objectToInsert
+ * @return
+ */
+ T insert(T objectToInsert);
+
+ /**
+ * Add object with given id.
+ *
+ * @param id must not be {@literal null}.
+ * @param objectToInsert must not be {@literal null}.
+ */
+ void insert(Serializable id, Object objectToInsert);
+
+ /**
+ * Get all elements of given type. Respects {@link KeySpace} if present and therefore returns all elements that can be
+ * assigned to requested type.
+ *
+ * @param type must not be {@literal null}.
+ * @return empty collection if no elements found.
+ */
+ List findAll(Class type);
+
+ /**
+ * Get all elements ordered by sort. Respects {@link KeySpace} if present and therefore returns all elements that can
+ * be assigned to requested type.
+ *
+ * @param sort must not be {@literal null}.
+ * @param type must not be {@literal null}.
+ * @return
+ */
+ List findAll(Sort sort, Class type);
+
+ /**
+ * Get element of given type with given id. Respects {@link KeySpace} if present and therefore returns all elements
+ * that can be assigned to requested type.
+ *
+ * @param id must not be {@literal null}.
+ * @param type must not be {@literal null}.
+ * @return null if not found.
+ */
+ T findById(Serializable id, Class type);
+
+ /**
+ * Execute operation against underlying store.
+ *
+ * @param action must not be {@literal null}.
+ * @return
+ */
+ T execute(KeyValueCallback action);
+
+ /**
+ * Get all elements matching the given query.
+ * Respects {@link KeySpace} if present and therefore returns all elements that can be assigned to requested type..
+ *
+ * @param query must not be {@literal null}.
+ * @param type must not be {@literal null}.
+ * @return empty collection if no match found.
+ */
+ List find(KeyValueQuery> query, Class type);
+
+ /**
+ * Get all elements in given range. Respects {@link KeySpace} if present and therefore returns all elements that can
+ * be assigned to requested type.
+ *
+ * @param offset
+ * @param rows
+ * @param type must not be {@literal null}.
+ * @return
+ */
+ List findInRange(int offset, int rows, Class type);
+
+ /**
+ * Get all elements in given range ordered by sort. Respects {@link KeySpace} if present and therefore returns all
+ * elements that can be assigned to requested type.
+ *
+ * @param offset
+ * @param rows
+ * @param sort
+ * @param type
+ * @return
+ */
+ List findInRange(int offset, int rows, Sort sort, Class type);
+
+ /**
+ * @param objectToUpdate must not be {@literal null}.
+ */
+ void update(Object objectToUpdate);
+
+ /**
+ * @param id must not be {@literal null}.
+ * @param objectToUpdate must not be {@literal null}.
+ */
+ void update(Serializable id, Object objectToUpdate);
+
+ /**
+ * Remove all elements of type. Respects {@link KeySpace} if present and therefore removes all elements that can be
+ * assigned to requested type.
+ *
+ * @param type must not be {@literal null}.
+ */
+ void delete(Class> type);
+
+ /**
+ * @param objectToDelete must not be {@literal null}.
+ * @return
+ */
+ T delete(T objectToDelete);
+
+ /**
+ * Delete item of type with given id.
+ *
+ * @param id must not be {@literal null}.
+ * @param type must not be {@literal null}.
+ * @return the deleted item or {@literal null} if no match found.
+ */
+ T delete(Serializable id, Class type);
+
+ /**
+ * Total number of elements with given type available. Respects {@link KeySpace} if present and therefore counts all
+ * elements that can be assigned to requested type.
+ *
+ * @param type must not be {@literal null}.
+ * @return
+ */
+ long count(Class> type);
+
+ /**
+ * Total number of elements matching given query. Respects {@link KeySpace} if present and therefore counts all
+ * elements that can be assigned to requested type.
+ *
+ * @param query
+ * @param type
+ * @return
+ */
+ long count(KeyValueQuery> query, Class> type);
+
+ /**
+ * @return mapping context in use.
+ */
+ MappingContext, ?> getMappingContext();
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValuePersistenceExceptionTranslator.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValuePersistenceExceptionTranslator.java
new file mode 100644
index 0000000..5a5ed66
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValuePersistenceExceptionTranslator.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.util.NoSuchElementException;
+
+import org.springframework.dao.DataAccessException;
+import org.springframework.dao.DataRetrievalFailureException;
+import org.springframework.dao.support.PersistenceExceptionTranslator;
+
+/**
+ * Simple {@link PersistenceExceptionTranslator} implementation for key/value stores that converts the given runtime
+ * exception to an appropriate exception from the {@code org.springframework.dao} hierarchy.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public class KeyValuePersistenceExceptionTranslator implements PersistenceExceptionTranslator {
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException)
+ */
+ @Override
+ public DataAccessException translateExceptionIfPossible(RuntimeException e) {
+
+ if (e == null || e instanceof DataAccessException) {
+ return (DataAccessException) e;
+ }
+
+ if (e instanceof NoSuchElementException || e instanceof IndexOutOfBoundsException
+ || e instanceof IllegalStateException) {
+ return new DataRetrievalFailureException(e.getMessage(), e);
+ }
+
+ if (e.getClass().getName().startsWith("java")) {
+ return new UncategorizedKeyValueException(e.getMessage(), e);
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java
new file mode 100644
index 0000000..331c270
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java
@@ -0,0 +1,452 @@
+/*
+ * Copyright 2014 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.core;
+
+import static org.springframework.data.keyvalue.core.KeySpaceUtils.*;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.springframework.dao.DataAccessException;
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.dao.support.PersistenceExceptionTranslator;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+import org.springframework.data.mapping.PersistentEntity;
+import org.springframework.data.mapping.PersistentProperty;
+import org.springframework.data.mapping.context.MappingContext;
+import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
+import org.springframework.util.StringUtils;
+
+/**
+ * Basic implementation of {@link KeyValueOperations}.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ */
+public class KeyValueTemplate implements KeyValueOperations {
+
+ private static final PersistenceExceptionTranslator DEFAULT_PERSISTENCE_EXCEPTION_TRANSLATOR = new KeyValuePersistenceExceptionTranslator();
+
+ private final KeyValueAdapter adapter;
+ private final ConcurrentHashMap, String> keySpaceCache = new ConcurrentHashMap, String>();
+ private final MappingContext extends PersistentEntity, ? extends PersistentProperty>>, ? extends PersistentProperty>> mappingContext;
+ private final IdentifierGenerator identifierGenerator;
+ private PersistenceExceptionTranslator exceptionTranslator = DEFAULT_PERSISTENCE_EXCEPTION_TRANSLATOR;
+
+ /**
+ * Create new {@link KeyValueTemplate} using the given {@link KeyValueAdapter} with a default
+ * {@link KeyValueMappingContext}.
+ *
+ * @param adapter must not be {@literal null}.
+ */
+ public KeyValueTemplate(KeyValueAdapter adapter) {
+ this(adapter, new KeyValueMappingContext());
+ }
+
+ /**
+ * Create new {@link KeyValueTemplate} using the given {@link KeyValueAdapter} and {@link MappingContext}.
+ *
+ * @param adapter must not be {@literal null}.
+ * @param mappingContext must not be {@literal null}.
+ */
+ @SuppressWarnings("rawtypes")
+ public KeyValueTemplate(
+ KeyValueAdapter adapter,
+ MappingContext extends PersistentEntity, ? extends PersistentProperty>, ? extends PersistentProperty>> mappingContext) {
+
+ Assert.notNull(adapter, "Adapter must not be null!");
+ Assert.notNull(mappingContext, "MappingContext must not be null!");
+
+ this.adapter = adapter;
+ this.mappingContext = mappingContext;
+ this.identifierGenerator = DefaultIdentifierGenerator.INSTANCE;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#insert(java.lang.Object)
+ */
+ @Override
+ public T insert(T objectToInsert) {
+
+ PersistentEntity, ?> entity = this.mappingContext.getPersistentEntity(ClassUtils.getUserClass(objectToInsert));
+
+ GeneratingIdAccessor generatingIdAccessor = new GeneratingIdAccessor(entity.getPropertyAccessor(objectToInsert),
+ entity.getIdProperty(), identifierGenerator);
+ Object id = generatingIdAccessor.getOrGenerateIdentifier();
+
+ insert((Serializable) id, objectToInsert);
+ return objectToInsert;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#insert(java.io.Serializable, java.lang.Object)
+ */
+ @Override
+ public void insert(final Serializable id, final Object objectToInsert) {
+
+ Assert.notNull(id, "Id for object to be inserted must not be null!");
+ Assert.notNull(objectToInsert, "Object to be inserted must not be null!");
+
+ execute(new KeyValueCallback() {
+
+ @Override
+ public Void doInKeyValue(KeyValueAdapter adapter) {
+
+ String typeKey = resolveKeySpace(objectToInsert.getClass());
+
+ if (adapter.contains(id, typeKey)) {
+ throw new InvalidDataAccessApiUsageException("Cannot insert existing object. Please use update.");
+ }
+
+ adapter.put(id, objectToInsert, typeKey);
+ return null;
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#update(java.lang.Object)
+ */
+ @SuppressWarnings("rawtypes")
+ @Override
+ public void update(Object objectToUpdate) {
+
+ PersistentEntity, ? extends PersistentProperty> entity = this.mappingContext.getPersistentEntity(ClassUtils
+ .getUserClass(objectToUpdate));
+
+ if (!entity.hasIdProperty()) {
+ throw new InvalidDataAccessApiUsageException(String.format("Cannot determine id for type %s",
+ ClassUtils.getUserClass(objectToUpdate)));
+ }
+
+ update((Serializable) entity.getIdentifierAccessor(objectToUpdate).getIdentifier(), objectToUpdate);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#update(java.io.Serializable, java.lang.Object)
+ */
+ @Override
+ public void update(final Serializable id, final Object objectToUpdate) {
+
+ Assert.notNull(id, "Id for object to be inserted must not be null!");
+ Assert.notNull(objectToUpdate, "Object to be updated must not be null!");
+
+ execute(new KeyValueCallback() {
+
+ @Override
+ public Void doInKeyValue(KeyValueAdapter adapter) {
+ adapter.put(id, objectToUpdate, resolveKeySpace(objectToUpdate.getClass()));
+ return null;
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#findAllOf(java.lang.Class)
+ */
+ @Override
+ public List findAll(final Class type) {
+
+ Assert.notNull(type, "Type to fetch must not be null!");
+
+ return execute(new KeyValueCallback>() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public List doInKeyValue(KeyValueAdapter adapter) {
+
+ Collection> x = adapter.getAllOf(resolveKeySpace(type));
+
+ if (getKeySpace(type) == null) {
+ return new ArrayList((Collection) x);
+ }
+
+ ArrayList filtered = new ArrayList();
+ for (Object candidate : x) {
+ if (typeCheck(type, candidate)) {
+ filtered.add((T) candidate);
+ }
+ }
+
+ return filtered;
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#findById(java.io.Serializable, java.lang.Class)
+ */
+ @Override
+ public T findById(final Serializable id, final Class type) {
+
+ Assert.notNull(id, "Id for object to be inserted must not be null!");
+ Assert.notNull(type, "Type to fetch must not be null!");
+
+ return execute(new KeyValueCallback() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public T doInKeyValue(KeyValueAdapter adapter) {
+
+ Object result = adapter.get(id, resolveKeySpace(type));
+
+ if (result == null || getKeySpace(type) == null || typeCheck(type, result)) {
+ return (T) result;
+ }
+
+ return null;
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#delete(java.lang.Class)
+ */
+ @Override
+ public void delete(final Class> type) {
+
+ Assert.notNull(type, "Type to delete must not be null!");
+
+ final String typeKey = resolveKeySpace(type);
+
+ execute(new KeyValueCallback() {
+
+ @Override
+ public Void doInKeyValue(KeyValueAdapter adapter) {
+
+ adapter.deleteAllOf(typeKey);
+ return null;
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#delete(java.lang.Object)
+ */
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Override
+ public T delete(T objectToDelete) {
+
+ Class type = (Class) ClassUtils.getUserClass(objectToDelete);
+ PersistentEntity, ? extends PersistentProperty> entity = this.mappingContext.getPersistentEntity(type);
+
+ return delete((Serializable) entity.getIdentifierAccessor(objectToDelete).getIdentifier(), type);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#delete(java.io.Serializable, java.lang.Class)
+ */
+ @Override
+ public T delete(final Serializable id, final Class type) {
+
+ Assert.notNull(id, "Id for object to be inserted must not be null!");
+ Assert.notNull(type, "Type to delete must not be null!");
+
+ return execute(new KeyValueCallback() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public T doInKeyValue(KeyValueAdapter adapter) {
+ return (T) adapter.delete(id, resolveKeySpace(type));
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#count(java.lang.Class)
+ */
+ @Override
+ public long count(Class> type) {
+
+ Assert.notNull(type, "Type for count must not be null!");
+ return findAll(type).size();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#execute(org.springframework.data.keyvalue.core.KeyValueCallback)
+ */
+ @Override
+ public T execute(KeyValueCallback action) {
+
+ Assert.notNull(action, "KeyValueCallback must not be null!");
+
+ try {
+ return action.doInKeyValue(this.adapter);
+ } catch (RuntimeException e) {
+ throw resolveExceptionIfPossible(e);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#find(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.lang.Class)
+ */
+ @Override
+ public List find(final KeyValueQuery> query, final Class type) {
+
+ return execute(new KeyValueCallback>() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public List doInKeyValue(KeyValueAdapter adapter) {
+
+ Collection> result = adapter.find(query, resolveKeySpace(type));
+
+ if (getKeySpace(type) == null) {
+ return new ArrayList((Collection) result);
+ }
+
+ List filtered = new ArrayList();
+
+ for (Object candidate : result) {
+ if (typeCheck(type, candidate)) {
+ filtered.add((T) candidate);
+ }
+ }
+
+ return filtered;
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#findAllOf(org.springframework.data.domain.Sort, java.lang.Class)
+ */
+ @SuppressWarnings("rawtypes")
+ @Override
+ public List findAll(Sort sort, Class type) {
+ return find(new KeyValueQuery(sort), type);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#findInRange(int, int, java.lang.Class)
+ */
+ @SuppressWarnings("rawtypes")
+ @Override
+ public List findInRange(int offset, int rows, Class type) {
+ return find(new KeyValueQuery().skip(offset).limit(rows), type);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#findInRange(int, int, org.springframework.data.domain.Sort, java.lang.Class)
+ */
+ @SuppressWarnings("rawtypes")
+ @Override
+ public List findInRange(int offset, int rows, Sort sort, Class type) {
+ return find(new KeyValueQuery(sort).skip(offset).limit(rows), type);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#count(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.lang.Class)
+ */
+ @Override
+ public long count(final KeyValueQuery> query, final Class> type) {
+
+ return execute(new KeyValueCallback() {
+
+ @Override
+ public Long doInKeyValue(KeyValueAdapter adapter) {
+ return adapter.count(query, resolveKeySpace(type));
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.KeyValueOperations#getMappingContext()
+ */
+ @Override
+ public MappingContext, ?> getMappingContext() {
+ return this.mappingContext;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.beans.factory.DisposableBean#destroy()
+ */
+ @Override
+ public void destroy() throws Exception {
+ this.adapter.clear();
+ }
+
+ /**
+ * Set the {@link PersistenceExceptionTranslator} used for converting {@link RuntimeException}.
+ *
+ * @param exceptionTranslator must not be {@literal null}.
+ */
+ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) {
+
+ Assert.notNull(exceptionTranslator, "ExceptionTranslator must not be null.");
+ this.exceptionTranslator = exceptionTranslator;
+ }
+
+ protected String resolveKeySpace(Class> type) {
+
+ Class> userClass = ClassUtils.getUserClass(type);
+
+ String potentialAlias = keySpaceCache.get(userClass);
+
+ if (potentialAlias != null) {
+ return potentialAlias;
+ }
+
+ String keySpaceString = null;
+ Object keySpace = getKeySpace(type);
+
+ if (keySpace != null) {
+ keySpaceString = keySpace.toString();
+ }
+
+ if (!StringUtils.hasText(keySpaceString)) {
+ keySpaceString = userClass.getName();
+ }
+
+ keySpaceCache.put(userClass, keySpaceString);
+ return keySpaceString;
+ }
+
+ private static boolean typeCheck(Class> requiredType, Object candidate) {
+ return candidate == null ? true : ClassUtils.isAssignable(requiredType, candidate.getClass());
+ }
+
+ private RuntimeException resolveExceptionIfPossible(RuntimeException e) {
+
+ DataAccessException translatedException = exceptionTranslator.translateExceptionIfPossible(e);
+ return translatedException != null ? translatedException : e;
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java b/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java
new file mode 100644
index 0000000..eea6679
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+
+/**
+ * Base implementation for accessing and executing {@link KeyValueQuery} against a {@link KeyValueAdapter}.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param
+ * @param
+ * @param
+ */
+public abstract class QueryEngine {
+
+ private final CriteriaAccessor criteriaAccessor;
+ private final SortAccessor sortAccessor;
+
+ private ADAPTER adapter;
+
+ public QueryEngine(CriteriaAccessor criteriaAccessor, SortAccessor sortAccessor) {
+
+ this.criteriaAccessor = criteriaAccessor;
+ this.sortAccessor = sortAccessor;
+ }
+
+ /**
+ * Extract query attributes and delegate to concrete execution.
+ *
+ * @param query
+ * @param keyspace
+ * @return
+ */
+ public Collection> execute(KeyValueQuery> query, Serializable keyspace) {
+
+ 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);
+ }
+
+ /**
+ * Extract query attributes and delegate to concrete execution.
+ *
+ * @param query
+ * @param keyspace
+ * @return
+ */
+ public long count(KeyValueQuery> query, Serializable keyspace) {
+
+ CRITERIA criteria = this.criteriaAccessor != null ? this.criteriaAccessor.resolve(query) : null;
+ return count(criteria, keyspace);
+ }
+
+ /**
+ * @param criteria
+ * @param sort
+ * @param offset
+ * @param rows
+ * @param keyspace
+ * @return
+ */
+ public abstract Collection> execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace);
+
+ /**
+ * @param criteria
+ * @param keyspace
+ * @return
+ */
+ public abstract long count(CRITERIA criteria, Serializable keyspace);
+
+ /**
+ * Get the {@link KeyValueAdapter} used.
+ *
+ * @return
+ */
+ protected ADAPTER getAdapter() {
+ return this.adapter;
+ }
+
+ /**
+ * @param adapter
+ */
+ @SuppressWarnings("unchecked")
+ public void registerAdapter(KeyValueAdapter adapter) {
+
+ if (this.adapter == null) {
+ this.adapter = (ADAPTER) adapter;
+ } else {
+ throw new IllegalArgumentException("Cannot register more than one adapter for this QueryEngine.");
+ }
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/SortAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/SortAccessor.java
new file mode 100644
index 0000000..363dd4d
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/SortAccessor.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2014 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.core;
+
+import org.springframework.data.domain.Sort;
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+
+/**
+ * Resolves the {@link Sort} object from given {@link KeyValueQuery} and potentially converts it into a store specific
+ * representation that can be used by the {@link QueryEngine} implementation.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param
+ */
+public interface SortAccessor {
+
+ /**
+ * Reads {@link KeyValueQuery#getSort()} of given {@link KeyValueQuery} and applies required transformation to match
+ * the desired type.
+ *
+ * @param query can be {@literal null}.
+ * @return {@literal null} in case {@link Sort} has not been defined on {@link KeyValueQuery}.
+ */
+ T resolve(KeyValueQuery> query);
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/SpelCriteriaAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/SpelCriteriaAccessor.java
new file mode 100644
index 0000000..9da2d71
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/SpelCriteriaAccessor.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2014 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.core;
+
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+import org.springframework.expression.spel.standard.SpelExpression;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.util.Assert;
+
+/**
+ * {@link CriteriaAccessor} implementation capable of {@link SpelExpression}s.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ */
+class SpelCriteriaAccessor implements CriteriaAccessor {
+
+ private final SpelExpressionParser parser;
+
+ /**
+ * @param parser must not be {@literal null}.
+ */
+ public SpelCriteriaAccessor(SpelExpressionParser parser) {
+
+ Assert.notNull(parser, "SpelExpressionParser must not be null!");
+ this.parser = parser;
+ }
+
+ @Override
+ public SpelExpression resolve(KeyValueQuery> query) {
+
+ if (query.getCritieria() == null) {
+ return null;
+ }
+
+ if (query.getCritieria() instanceof SpelExpression) {
+ return (SpelExpression) query.getCritieria();
+ }
+
+ if (query.getCritieria() instanceof String) {
+ return parser.parseRaw((String) query.getCritieria());
+ }
+
+ throw new IllegalArgumentException("Cannot create SpelCriteria for " + query.getCritieria());
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/SpelPropertyComparator.java b/src/main/java/org/springframework/data/keyvalue/core/SpelPropertyComparator.java
new file mode 100644
index 0000000..b145860
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/SpelPropertyComparator.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.util.Comparator;
+
+import org.springframework.expression.spel.standard.SpelExpression;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+
+/**
+ * {@link Comparator} implementation using {@link SpelExpression}.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ * @param
+ */
+public class SpelPropertyComparator implements Comparator {
+
+ private final String path;
+ private final SpelExpressionParser parser;
+
+ private boolean asc = true;
+ private boolean nullsFirst = true;
+ private SpelExpression expression;
+
+ /**
+ * Create new {@link SpelPropertyComparator} for the given property path an {@link SpelExpressionParser}..
+ *
+ * @param path must not be {@literal null} or empty.
+ * @param parser must not be {@literal null}.
+ */
+ public SpelPropertyComparator(String path, SpelExpressionParser parser) {
+
+ this.path = path;
+ this.parser = parser;
+ }
+
+ /**
+ * Sort {@literal ascending}.
+ *
+ * @return
+ */
+ public SpelPropertyComparator asc() {
+ this.asc = true;
+ return this;
+ }
+
+ /**
+ * Sort {@literal descending}.
+ *
+ * @return
+ */
+ public SpelPropertyComparator desc() {
+ this.asc = false;
+ return this;
+ }
+
+ /**
+ * Sort {@literal null} values first.
+ *
+ * @return
+ */
+ public SpelPropertyComparator nullsFirst() {
+ this.nullsFirst = true;
+ return this;
+ }
+
+ /**
+ * Sort {@literal null} values last.
+ *
+ * @return
+ */
+ public SpelPropertyComparator nullsLast() {
+ this.nullsFirst = false;
+ return this;
+ }
+
+ /**
+ * Parse values to {@link SpelExpression}
+ *
+ * @return
+ */
+ protected SpelExpression getExpression() {
+
+ if (this.expression == null) {
+ this.expression = parser.parseRaw(buildExpressionForPath());
+ }
+
+ return this.expression;
+ }
+
+ /**
+ * Create the expression raw value.
+ *
+ * @return
+ */
+ protected String buildExpressionForPath() {
+
+ StringBuilder rawExpression = new StringBuilder(
+ "new org.springframework.util.comparator.NullSafeComparator(new org.springframework.util.comparator.ComparableComparator(), "
+ + Boolean.toString(this.nullsFirst) + ").compare(");
+
+ rawExpression.append("#arg1?.");
+ rawExpression.append(path != null ? path.replace(".", "?.") : "");
+ rawExpression.append(",");
+ rawExpression.append("#arg2?.");
+ rawExpression.append(path != null ? path.replace(".", "?.") : "");
+ rawExpression.append(")");
+
+ return rawExpression.toString();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ @Override
+ public int compare(T arg1, T arg2) {
+
+ SpelExpression expressionToUse = getExpression();
+
+ expressionToUse.getEvaluationContext().setVariable("arg1", arg1);
+ expressionToUse.getEvaluationContext().setVariable("arg2", arg2);
+
+ return expressionToUse.getValue(Integer.class) * (asc ? 1 : -1);
+ }
+
+ /**
+ * Get dot path to property.
+ *
+ * @return
+ */
+ public String getPath() {
+ return path;
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java b/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java
new file mode 100644
index 0000000..671c5d2
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+import org.springframework.expression.spel.SpelEvaluationException;
+import org.springframework.expression.spel.standard.SpelExpression;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+
+/**
+ * {@link QueryEngine} implementation specific for executing {@link SpelExpression} based {@link KeyValueQuery} against
+ * {@link KeyValueAdapter}.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ * @param
+ */
+class SpelQueryEngine extends QueryEngine> {
+
+ private static final SpelExpressionParser PARSER = new SpelExpressionParser();
+
+ /**
+ * Creates a new {@link SpelQueryEngine}.
+ */
+ public SpelQueryEngine() {
+ super(new SpelCriteriaAccessor(PARSER), new SpelSortAccessor(PARSER));
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.QueryEngine#execute(java.lang.Object, java.lang.Object, int, int, java.io.Serializable)
+ */
+ @Override
+ public Collection> execute(SpelExpression criteria, Comparator> sort, int offset, int rows, Serializable keyspace) {
+ return sortAndFilterMatchingRange(getAdapter().getAllOf(keyspace), criteria, sort, offset, rows);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.QueryEngine#count(java.lang.Object, java.io.Serializable)
+ */
+ @Override
+ public long count(SpelExpression criteria, Serializable keyspace) {
+ return filterMatchingRange(getAdapter().getAllOf(keyspace), criteria, -1, -1).size();
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private List> sortAndFilterMatchingRange(Collection> source, SpelExpression criteria, Comparator sort,
+ int offset, int rows) {
+
+ List> tmp = new ArrayList(source);
+ if (sort != null) {
+ Collections.sort(tmp, sort);
+ }
+
+ return filterMatchingRange(tmp, criteria, offset, rows);
+ }
+
+ private static List filterMatchingRange(Iterable source, SpelExpression criteria, int offset, int rows) {
+
+ List result = new ArrayList();
+
+ boolean compareOffsetAndRows = 0 < offset || 0 <= rows;
+ int remainingRows = rows;
+ int curPos = 0;
+
+ for (S candidate : source) {
+
+ boolean matches = criteria == null;
+
+ if (!matches) {
+ try {
+ matches = criteria.getValue(candidate, Boolean.class);
+ } catch (SpelEvaluationException e) {
+ criteria.getEvaluationContext().setVariable("it", candidate);
+ matches = criteria.getValue(Boolean.class);
+ }
+ }
+
+ if (matches) {
+ if (compareOffsetAndRows) {
+ if (curPos >= offset && rows > 0) {
+ result.add(candidate);
+ remainingRows--;
+ if (remainingRows <= 0) {
+ break;
+ }
+ }
+ curPos++;
+ } else {
+ result.add(candidate);
+ }
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java
new file mode 100644
index 0000000..3f4a419
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2014 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.core;
+
+import java.util.Comparator;
+
+import org.springframework.data.domain.Sort.Direction;
+import org.springframework.data.domain.Sort.NullHandling;
+import org.springframework.data.domain.Sort.Order;
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.util.Assert;
+import org.springframework.util.comparator.CompoundComparator;
+
+/**
+ * {@link SortAccessor} implementation capable of creating {@link SpelPropertyComparator}.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ */
+class SpelSortAccessor implements SortAccessor> {
+
+ private final SpelExpressionParser parser;
+
+ /**
+ * @param parser must not be {@literal null}.
+ */
+ public SpelSortAccessor(SpelExpressionParser parser) {
+
+ Assert.notNull(parser, "SpelExpressionParser must not be null!");
+ this.parser = parser;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.keyvalue.core.SortAccessor#resolve(org.springframework.data.keyvalue.core.query.KeyValueQuery)
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Override
+ public Comparator> resolve(KeyValueQuery> query) {
+
+ if (query == null || query.getSort() == null) {
+ return null;
+ }
+
+ CompoundComparator compoundComperator = new CompoundComparator();
+ for (Order order : query.getSort()) {
+
+ SpelPropertyComparator> spelSort = new SpelPropertyComparator(order.getProperty(), parser);
+
+ if (Direction.DESC.equals(order.getDirection())) {
+
+ spelSort.desc();
+
+ if (order.getNullHandling() != null && !NullHandling.NATIVE.equals(order.getNullHandling())) {
+ spelSort = NullHandling.NULLS_FIRST.equals(order.getNullHandling()) ? spelSort.nullsFirst() : spelSort
+ .nullsLast();
+ }
+ }
+ compoundComperator.addComparator(spelSort);
+ }
+
+ return compoundComperator;
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/UncategorizedKeyValueException.java b/src/main/java/org/springframework/data/keyvalue/core/UncategorizedKeyValueException.java
new file mode 100644
index 0000000..47df713
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/UncategorizedKeyValueException.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2014 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.core;
+
+import org.springframework.dao.UncategorizedDataAccessException;
+
+/**
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public class UncategorizedKeyValueException extends UncategorizedDataAccessException {
+
+ private static final long serialVersionUID = -8087116071859122297L;
+
+ public UncategorizedKeyValueException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java
new file mode 100644
index 0000000..453b001
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2014 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.core.mapping;
+
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+
+import org.springframework.data.mapping.Association;
+import org.springframework.data.mapping.PersistentEntity;
+import org.springframework.data.mapping.PersistentProperty;
+import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
+import org.springframework.data.mapping.model.SimpleTypeHolder;
+
+/**
+ * Most trivial implementation of {@link PersistentProperty}.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public class KeyValuePersistentProperty extends AnnotationBasedPersistentProperty {
+
+ public KeyValuePersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
+ PersistentEntity, KeyValuePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
+ super(field, propertyDescriptor, owner, simpleTypeHolder);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.model.AbstractPersistentProperty#createAssociation()
+ */
+ @Override
+ protected Association createAssociation() {
+ return new Association(this, null);
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java
new file mode 100644
index 0000000..f3266fe
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2014 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.core.mapping.context;
+
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+
+import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
+import org.springframework.data.mapping.context.AbstractMappingContext;
+import org.springframework.data.mapping.model.BasicPersistentEntity;
+import org.springframework.data.mapping.model.SimpleTypeHolder;
+import org.springframework.data.util.TypeInformation;
+
+/**
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public class KeyValueMappingContext extends
+ AbstractMappingContext, KeyValuePersistentProperty> {
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.context.AbstractMappingContext#createPersistentEntity(org.springframework.data.util.TypeInformation)
+ */
+ @Override
+ protected BasicPersistentEntity, KeyValuePersistentProperty> createPersistentEntity(
+ TypeInformation typeInformation) {
+ return new BasicPersistentEntity(typeInformation);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mapping.context.AbstractMappingContext#createPersistentProperty(java.lang.reflect.Field, java.beans.PropertyDescriptor, org.springframework.data.mapping.model.MutablePersistentEntity, org.springframework.data.mapping.model.SimpleTypeHolder)
+ */
+ @Override
+ protected KeyValuePersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
+ BasicPersistentEntity, KeyValuePersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
+ return new KeyValuePersistentProperty(field, descriptor, owner, simpleTypeHolder);
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java b/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java
new file mode 100644
index 0000000..79d683e
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2014 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.core.query;
+
+import org.springframework.data.domain.Sort;
+
+/**
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param Criteria type
+ */
+public class KeyValueQuery {
+
+ private Sort sort;
+ private int offset = -1;
+ private int rows = -1;
+ private T criteria;
+
+ /**
+ * Creates new instance of {@link KeyValueQuery}.
+ */
+ public KeyValueQuery() {}
+
+ /**
+ * Creates new instance of {@link KeyValueQuery} with given criteria.
+ *
+ * @param criteria can be {@literal null}.
+ */
+ public KeyValueQuery(T criteria) {
+ this.criteria = criteria;
+ }
+
+ /**
+ * Creates new instance of {@link KeyValueQuery} with given {@link Sort}.
+ *
+ * @param sort can be {@literal null}.
+ */
+ public KeyValueQuery(Sort sort) {
+ this.sort = sort;
+ }
+
+ /**
+ * Get the criteria object.
+ *
+ * @return
+ */
+ public T getCritieria() {
+ return criteria;
+ }
+
+ /**
+ * Get {@link Sort}.
+ *
+ * @return
+ */
+ public Sort getSort() {
+ return sort;
+ }
+
+ /**
+ * Number of elements to skip.
+ *
+ * @return negative value if not set.
+ */
+ public int getOffset() {
+ return this.offset;
+ }
+
+ /**
+ * Number of elements to read.
+ *
+ * @return negative value if not set.
+ */
+ public int getRows() {
+ return this.rows;
+ }
+
+ /**
+ * Set the number of elements to skip.
+ *
+ * @param offset use negative value for none.
+ */
+ public void setOffset(int offset) {
+ this.offset = offset;
+ }
+
+ /**
+ * Set the number of elements to read.
+ *
+ * @param offset use negative value for all.
+ */
+ public void setRows(int rows) {
+ this.rows = rows;
+ }
+
+ /**
+ * Set {@link Sort} to be applied.
+ *
+ * @param sort
+ */
+ public void setSort(Sort sort) {
+ this.sort = sort;
+ }
+
+ /**
+ * Add given {@link Sort}.
+ *
+ * @param sort {@literal null} {@link Sort} will be ignored.
+ * @return
+ */
+ public KeyValueQuery orderBy(Sort sort) {
+
+ if (sort == null) {
+ return this;
+ }
+
+ if (this.sort != null) {
+ this.sort.and(sort);
+ } else {
+ this.sort = sort;
+ }
+ return this;
+ }
+
+ /**
+ * @see KeyValueQuery#setOffset(int)
+ * @param offset
+ * @return
+ */
+ public KeyValueQuery skip(int offset) {
+ setOffset(offset);
+ return this;
+ }
+
+ /**
+ * @see KeyValueQuery#setRows(int)
+ * @param rows
+ * @return
+ */
+ public KeyValueQuery limit(int rows) {
+ setRows(rows);
+ return this;
+ }
+
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/BasicKeyValueRepository.java.orig b/src/main/java/org/springframework/data/keyvalue/repository/BasicKeyValueRepository.java.orig
new file mode 100644
index 0000000..6b57e9a
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/BasicKeyValueRepository.java.orig
@@ -0,0 +1,205 @@
+/*
+ * Copyright 2014 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;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.keyvalue.core.KeyValueOperations;
+import org.springframework.data.repository.core.EntityInformation;
+import org.springframework.util.Assert;
+
+/**
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param
+ * @param
+ */
+public class BasicKeyValueRepository implements KeyValueRepository {
+
+ private final KeyValueOperations operations;
+ private final EntityInformation entityInformation;
+
+ public BasicKeyValueRepository(EntityInformation metadata, KeyValueOperations operations) {
+
+ Assert.notNull(metadata, "Cannot initialize repository for 'null' metadata");
+ Assert.notNull(operations, "Cannot initialize repository for 'null' operations");
+
+ this.entityInformation = metadata;
+ this.operations = operations;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
+ */
+ @Override
+ public Iterable findAll(Sort sort) {
+ return operations.findAll(sort, entityInformation.getJavaType());
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
+ */
+ @Override
+ public Page findAll(Pageable pageable) {
+
+ List content = null;
+ if (pageable.getSort() != null) {
+ content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), pageable.getSort(),
+ entityInformation.getJavaType());
+ } else {
+ content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), entityInformation.getJavaType());
+ }
+
+ return new PageImpl(content, pageable, this.operations.count(entityInformation.getJavaType()));
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
+ */
+ @Override
+ public S save(S entity) {
+
+ Assert.notNull(entity, "Entity must not be 'null' for save.");
+
+ if (entityInformation.isNew(entity)) {
+ operations.insert(entity);
+ } else {
+ operations.update(entityInformation.getId(entity), entity);
+ }
+ return entity;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
+ */
+ @Override
+ public Iterable save(Iterable entities) {
+
+ for (S entity : entities) {
+ save(entity);
+ }
+ return entities;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
+ */
+ @Override
+ public T findOne(ID id) {
+ return operations.findById(id, entityInformation.getJavaType());
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
+ */
+ @Override
+ public boolean exists(ID id) {
+ return findOne(id) != null;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#findAll()
+ */
+ @Override
+<<<<<<< Updated upstream:src/main/java/org/springframework/data/keyvalue/repository/BasicKeyValueRepository.java
+ public Iterable findAll() {
+ return operations.findAllOf(entityInformation.getJavaType());
+=======
+ public List findAll() {
+ return operations.findAll(entityInformation.getJavaType());
+>>>>>>> Stashed changes:src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
+ */
+ @Override
+ public Iterable findAll(Iterable ids) {
+
+ List result = new ArrayList();
+
+ for (ID id : ids) {
+ T candidate = findOne(id);
+ if (candidate != null) {
+ result.add(candidate);
+ }
+ }
+
+ return result;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#count()
+ */
+ @Override
+ public long count() {
+ return operations.count(entityInformation.getJavaType());
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
+ */
+ @Override
+ public void delete(ID id) {
+ operations.delete(id, entityInformation.getJavaType());
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
+ */
+ @Override
+ public void delete(T entity) {
+ delete(entityInformation.getId(entity));
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
+ */
+ @Override
+ public void delete(Iterable extends T> entities) {
+
+ for (T entity : entities) {
+ delete(entity);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.CrudRepository#deleteAll()
+ */
+ @Override
+ public void deleteAll() {
+ operations.delete(entityInformation.getJavaType());
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/KeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/KeyValueRepository.java
new file mode 100644
index 0000000..2a70394
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/KeyValueRepository.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2014 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;
+
+import java.io.Serializable;
+
+import org.springframework.data.repository.PagingAndSortingRepository;
+
+/**
+ * @author Christoph Strobl
+ * @since 1.10
+ * @param
+ * @param
+ */
+public interface KeyValueRepository extends PagingAndSortingRepository {
+
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/config/EnableKeyValueRepositories.java b/src/main/java/org/springframework/data/keyvalue/repository/config/EnableKeyValueRepositories.java
new file mode 100644
index 0000000..1b32a78
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/config/EnableKeyValueRepositories.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2014 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.config;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.context.annotation.ComponentScan.Filter;
+import org.springframework.context.annotation.Import;
+import org.springframework.data.keyvalue.core.KeyValueOperations;
+import org.springframework.data.keyvalue.repository.query.SpelQueryCreator;
+import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
+import org.springframework.data.repository.query.QueryLookupStrategy;
+import org.springframework.data.repository.query.QueryLookupStrategy.Key;
+
+/**
+ * Annotation to activate KeyValue repositories. If no base package is configured through either {@link #value()},
+ * {@link #basePackages()} or {@link #basePackageClasses()} it will trigger scanning of the package of annotated class.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Inherited
+@Import(KeyValueRepositoriesRegistrar.class)
+@QueryCreatorType(SpelQueryCreator.class)
+public @interface EnableKeyValueRepositories {
+
+ /**
+ * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
+ * {@code @EnableJpaRepositories("org.my.pkg")} instead of {@code @EnableJpaRepositories(basePackages="org.my.pkg")}.
+ */
+ String[] value() default {};
+
+ /**
+ * Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
+ * attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
+ */
+ String[] basePackages() default {};
+
+ /**
+ * Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
+ * package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
+ * each package that serves no purpose other than being referenced by this attribute.
+ */
+ Class>[] basePackageClasses() default {};
+
+ /**
+ * Specifies which types are not eligible for component scanning.
+ */
+ Filter[] excludeFilters() default {};
+
+ /**
+ * Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
+ * everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
+ */
+ Filter[] includeFilters() default {};
+
+ /**
+ * Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
+ * for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
+ * for {@code PersonRepositoryImpl}.
+ *
+ * @return
+ */
+ String repositoryImplementationPostfix() default "Impl";
+
+ /**
+ * Configures the location of where to find the Spring Data named queries properties file.
+ *
+ * @return
+ */
+ String namedQueriesLocation() default "";
+
+ /**
+ * Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
+ * {@link Key#CREATE_IF_NOT_FOUND}.
+ *
+ * @return
+ */
+ Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
+
+ /**
+ * Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
+ * {@link KeyValueRepositoryFactoryBean}.
+ *
+ * @return
+ */
+ Class> repositoryFactoryBeanClass() default KeyValueRepositoryFactoryBean.class;
+
+ /**
+ * Configures the name of the {@link KeyValueOperations} bean to be used with the repositories detected.
+ *
+ * @return
+ */
+ String keyValueTemplateRef() default "keyValueTemplate";
+
+ /**
+ * Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
+ * repositories infrastructure.
+ */
+ boolean considerNestedRepositories() default false;
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoriesRegistrar.java b/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoriesRegistrar.java
new file mode 100644
index 0000000..dba3689
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoriesRegistrar.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2014 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.config;
+
+import java.lang.annotation.Annotation;
+
+import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport;
+import org.springframework.data.repository.config.RepositoryConfigurationExtension;
+
+/**
+ * KeyValue specific {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public class KeyValueRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getAnnotation()
+ */
+ @Override
+ protected Class extends Annotation> getAnnotation() {
+ return EnableKeyValueRepositories.class;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension()
+ */
+ @Override
+ protected RepositoryConfigurationExtension getExtension() {
+ return new KeyValueRepositoryConfigurationExtension();
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java b/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java
new file mode 100644
index 0000000..4130c45
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2014 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.config;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.core.annotation.AnnotationAttributes;
+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.SpelQueryCreator;
+import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
+import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
+import org.springframework.data.repository.config.RepositoryConfigurationExtension;
+import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
+import org.springframework.data.repository.config.RepositoryConfigurationSource;
+
+/**
+ * {@link RepositoryConfigurationExtension} for {@link KeyValueRepository}.
+ *
+ * @author Christoph Strobl
+ * @since 1.10
+ */
+public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
+
+ private static final String MAPPING_CONTEXT_BEAN_NAME = "keyValueMappingContext";
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getRepositoryFactoryClassName()
+ */
+ @Override
+ public String getRepositoryFactoryClassName() {
+ return KeyValueRepositoryFactoryBean.class.getName();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModuleName()
+ */
+ @Override
+ public String getModuleName() {
+ return "KeyValue";
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
+ */
+ @Override
+ protected String getModulePrefix() {
+ return "keyvalue";
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getIdentifyingTypes()
+ */
+ @Override
+ protected Collection> getIdentifyingTypes() {
+ return Collections.> singleton(KeyValueRepository.class);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource)
+ */
+ @Override
+ public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
+
+ AnnotationAttributes attributes = config.getAttributes();
+
+ builder.addPropertyReference("keyValueOperations", attributes.getString("keyValueTemplateRef"));
+ builder.addPropertyValue("queryCreator", getQueryCreatorType(config));
+ builder.addPropertyReference("mappingContext", MAPPING_CONTEXT_BEAN_NAME);
+ }
+
+ /**
+ * 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> getQueryCreatorType(AnnotationRepositoryConfigurationSource config) {
+
+ AnnotationMetadata metadata = config.getEnableAnnotationMetadata();
+
+ Map queryCreatorFoo = metadata.getAnnotationAttributes(QueryCreatorType.class.getName());
+
+ if (queryCreatorFoo == null) {
+ return SpelQueryCreator.class;
+ }
+
+ AnnotationAttributes queryCreatorAttributes = new AnnotationAttributes(queryCreatorFoo);
+ return queryCreatorAttributes.getClass("value");
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#registerBeansForRoot(org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.data.repository.config.RepositoryConfigurationSource)
+ */
+ @Override
+ public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource) {
+
+ super.registerBeansForRoot(registry, configurationSource);
+
+ if (!registry.containsBeanDefinition(MAPPING_CONTEXT_BEAN_NAME)) {
+
+ RootBeanDefinition mappingContextDefinition = new RootBeanDefinition(KeyValueMappingContext.class);
+ mappingContextDefinition.setSource(configurationSource.getSource());
+ registry.registerBeanDefinition(MAPPING_CONTEXT_BEAN_NAME, mappingContextDefinition);
+ }
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/config/QueryCreatorType.java b/src/main/java/org/springframework/data/keyvalue/repository/config/QueryCreatorType.java
new file mode 100644
index 0000000..44b05a2
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/config/QueryCreatorType.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2014 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.config;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+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
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.ANNOTATION_TYPE)
+public @interface QueryCreatorType {
+
+ Class extends AbstractQueryCreator, ?>> value();
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/config/RepositoryNameSpaceHandler.java b/src/main/java/org/springframework/data/keyvalue/repository/config/RepositoryNameSpaceHandler.java
new file mode 100644
index 0000000..6efd4a5
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/config/RepositoryNameSpaceHandler.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2012 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.config;
+
+import org.springframework.beans.factory.xml.BeanDefinitionParser;
+import org.springframework.beans.factory.xml.NamespaceHandler;
+import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
+import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
+
+/**
+ * {@link NamespaceHandler} to register {@link BeanDefinitionParser}s for key-value repositories.
+ *
+ * @author Oliver Gierke
+ * @author Christoph Strobl
+ * @since 1.4
+ */
+public class RepositoryNameSpaceHandler extends NamespaceHandlerSupport {
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.beans.factory.xml.NamespaceHandler#init()
+ */
+ public void init() {
+
+ KeyValueRepositoryConfigurationExtension extension = new KeyValueRepositoryConfigurationExtension();
+ RepositoryBeanDefinitionParser repositoryBeanDefinitionParser = new RepositoryBeanDefinitionParser(extension);
+ registerBeanDefinitionParser("repositories", repositoryBeanDefinitionParser);
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java b/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java
new file mode 100644
index 0000000..f0c55af
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2014 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 java.lang.reflect.Constructor;
+import java.util.List;
+
+import org.springframework.beans.BeanUtils;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.Pageable;
+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.ParameterAccessor;
+import org.springframework.data.repository.query.ParametersParameterAccessor;
+import org.springframework.data.repository.query.QueryMethod;
+import org.springframework.data.repository.query.RepositoryQuery;
+import org.springframework.data.repository.query.parser.AbstractQueryCreator;
+import org.springframework.data.repository.query.parser.PartTree;
+import org.springframework.expression.EvaluationContext;
+import org.springframework.expression.spel.standard.SpelExpression;
+import org.springframework.util.ClassUtils;
+import org.springframework.util.CollectionUtils;
+
+/**
+ * {@link RepositoryQuery} implementation deriving queries from {@link PartTree} using a predefined
+ * {@link AbstractQueryCreator}.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ */
+public class KeyValuePartTreeQuery implements RepositoryQuery {
+
+ private final EvaluationContextProvider evaluationContextProvider;
+ private final QueryMethod queryMethod;
+ private final KeyValueOperations keyValueOperations;
+ private final Class extends AbstractQueryCreator, ?>> queryCreator;
+
+ private KeyValueQuery> query;
+
+ public KeyValuePartTreeQuery(QueryMethod queryMethod, EvaluationContextProvider evalContextProvider,
+ KeyValueOperations keyValueOperations, Class extends AbstractQueryCreator, ?>> queryCreator) {
+
+ this.queryMethod = queryMethod;
+ this.keyValueOperations = keyValueOperations;
+ this.evaluationContextProvider = evalContextProvider;
+ this.queryCreator = queryCreator;
+ }
+
+ @Override
+ public QueryMethod getQueryMethod() {
+ return queryMethod;
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Override
+ public Object execute(Object[] parameters) {
+
+ KeyValueQuery> query = prepareQuery(parameters);
+
+ if (queryMethod.isPageQuery() || queryMethod.isSliceQuery()) {
+
+ Pageable page = (Pageable) parameters[queryMethod.getParameters().getPageableIndex()];
+ query.setOffset(page.getOffset());
+ query.setRows(page.getPageSize());
+
+ List> result = this.keyValueOperations.find(query, queryMethod.getEntityInformation().getJavaType());
+
+ long count = queryMethod.isSliceQuery() ? 0 : keyValueOperations.count(query, queryMethod.getEntityInformation()
+ .getJavaType());
+
+ return new PageImpl(result, page, count);
+
+ } else if (queryMethod.isCollectionQuery()) {
+
+ return this.keyValueOperations.find(query, queryMethod.getEntityInformation().getJavaType());
+
+ } else if (queryMethod.isQueryForEntity()) {
+
+ List> result = this.keyValueOperations.find(query, queryMethod.getEntityInformation().getJavaType());
+ return CollectionUtils.isEmpty(result) ? null : result.get(0);
+
+ }
+
+ throw new UnsupportedOperationException("Query method not supported.");
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private KeyValueQuery> prepareQuery(Object[] parameters) {
+
+ ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
+
+ if (this.query == null) {
+ this.query = createQuery(accessor);
+ }
+
+ KeyValueQuery> q = new KeyValueQuery(this.query.getCritieria());
+
+ if (accessor.getPageable() != null) {
+ q.setOffset(accessor.getPageable().getOffset());
+ q.setRows(accessor.getPageable().getPageSize());
+ } else {
+ q.setOffset(-1);
+ q.setRows(-1);
+ }
+
+ if (accessor.getSort() != null) {
+ q.setSort(accessor.getSort());
+ } else {
+ q.setSort(this.query.getSort());
+ }
+
+ if (q.getCritieria() instanceof SpelExpression) {
+ EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(),
+ parameters);
+ ((SpelExpression) q.getCritieria()).setEvaluationContext(context);
+ }
+
+ return q;
+ }
+
+ public KeyValueQuery> createQuery(ParametersParameterAccessor accessor) {
+
+ PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType());
+
+ Constructor extends AbstractQueryCreator, ?>> constructor = (Constructor extends AbstractQueryCreator, ?>>) ClassUtils
+ .getConstructorIfAvailable(queryCreator, PartTree.class, ParameterAccessor.class);
+ return (KeyValueQuery>) BeanUtils.instantiateClass(constructor, tree, accessor).createQuery();
+ }
+}
diff --git a/src/main/java/org/springframework/data/keyvalue/repository/query/SpelQueryCreator.java b/src/main/java/org/springframework/data/keyvalue/repository/query/SpelQueryCreator.java
new file mode 100644
index 0000000..cf1e714
--- /dev/null
+++ b/src/main/java/org/springframework/data/keyvalue/repository/query/SpelQueryCreator.java
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2014 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 java.util.Iterator;
+
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.keyvalue.core.query.KeyValueQuery;
+import org.springframework.data.repository.query.ParameterAccessor;
+import org.springframework.data.repository.query.parser.AbstractQueryCreator;
+import org.springframework.data.repository.query.parser.Part;
+import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
+import org.springframework.data.repository.query.parser.PartTree;
+import org.springframework.data.repository.query.parser.PartTree.OrPart;
+import org.springframework.expression.spel.standard.SpelExpression;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+
+/**
+ * {@link AbstractQueryCreator} to create {@link SpelExpression} based {@link KeyValueQuery}s.
+ *
+ * @author Christoph Strobl
+ * @author Oliver Gierke
+ * @since 1.10
+ */
+public class SpelQueryCreator extends AbstractQueryCreator, String> {
+
+ private static final SpelExpressionParser PARSER = new SpelExpressionParser();
+
+ private final SpelExpression expression;
+
+ /**
+ * Creates a new {@link SpelQueryCreator} for the given {@link PartTree} and {@link ParameterAccessor}.
+ *
+ * @param tree must not be {@literal null}.
+ * @param parameters must not be {@literal null}.
+ */
+ public SpelQueryCreator(PartTree tree, ParameterAccessor parameters) {
+
+ super(tree, parameters);
+
+ this.expression = toPredicateExpression(tree);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator)
+ */
+ @Override
+ protected String create(Part part, Iterator