DATAKV-117 - Pass on type information to KV Adapter.
Original pull request: #18.
This commit is contained in:
committed by
Mark Paluch
parent
551b27deb2
commit
fae6d418b6
@@ -57,6 +57,33 @@ public abstract class AbstractKeyValueAdapter implements KeyValueAdapter {
|
||||
return engine;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> T get(Serializable id, Serializable keyspace, Class<T> type) {
|
||||
return (T) get(id, keyspace);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> T delete(Serializable id, Serializable keyspace, Class<T> type) {
|
||||
return (T) delete(id, keyspace);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#get(java.io.Serializable, java.io.Serializable, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T> Iterable<T> find(KeyValueQuery<?> query, Serializable keyspace, Class<T> type) {
|
||||
return (Iterable<T>) engine.execute(query, keyspace, type);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#find(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.io.Serializable)
|
||||
|
||||
@@ -58,6 +58,15 @@ public interface KeyValueAdapter extends DisposableBean {
|
||||
*/
|
||||
Object get(Serializable id, Serializable keyspace);
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param keyspace
|
||||
* @param type
|
||||
* @return
|
||||
* @since 1.1
|
||||
*/
|
||||
<T> T get(Serializable id, Serializable keyspace, Class<T> type);
|
||||
|
||||
/**
|
||||
* Delete and return the obect with given type and id.
|
||||
*
|
||||
@@ -67,6 +76,15 @@ public interface KeyValueAdapter extends DisposableBean {
|
||||
*/
|
||||
Object delete(Serializable id, Serializable keyspace);
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param keyspace
|
||||
* @param type
|
||||
* @return
|
||||
* @since 1.1
|
||||
*/
|
||||
<T> T delete(Serializable id, Serializable keyspace, Class<T> type);
|
||||
|
||||
/**
|
||||
* Get all elements for given keyspace.
|
||||
*
|
||||
@@ -104,6 +122,15 @@ public interface KeyValueAdapter extends DisposableBean {
|
||||
*/
|
||||
Iterable<?> find(KeyValueQuery<?> query, Serializable keyspace);
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @param keyspace
|
||||
* @param type
|
||||
* @return
|
||||
* @since 1.1
|
||||
*/
|
||||
<T> Iterable<T> find(KeyValueQuery<?> query, Serializable keyspace, Class<T> type);
|
||||
|
||||
/**
|
||||
* Count number of objects within {@literal keyspace}.
|
||||
*
|
||||
|
||||
@@ -271,7 +271,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
@Override
|
||||
public T doInKeyValue(KeyValueAdapter adapter) {
|
||||
|
||||
Object result = adapter.get(id, keyspace);
|
||||
Object result = adapter.get(id, keyspace, type);
|
||||
|
||||
if (result == null || typeCheck(type, result)) {
|
||||
return (T) result;
|
||||
@@ -342,10 +342,9 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
|
||||
T result = execute(new KeyValueCallback<T>() {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T doInKeyValue(KeyValueAdapter adapter) {
|
||||
return (T) adapter.delete(id, keyspace);
|
||||
return (T) adapter.delete(id, keyspace, type);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -394,7 +393,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
|
||||
@Override
|
||||
public Iterable<T> doInKeyValue(KeyValueAdapter adapter) {
|
||||
|
||||
Iterable<?> result = adapter.find(query, resolveKeySpace(type));
|
||||
Iterable<?> result = adapter.find(query, resolveKeySpace(type), type);
|
||||
if (result == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@@ -56,6 +56,21 @@ public abstract class QueryEngine<ADAPTER extends KeyValueAdapter, CRITERIA, SOR
|
||||
return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract query attributes and delegate to concrete execution.
|
||||
*
|
||||
* @param query
|
||||
* @param keyspace
|
||||
* @return
|
||||
*/
|
||||
public <T> Collection<T> execute(KeyValueQuery<?> query, Serializable keyspace, Class<T> type) {
|
||||
|
||||
CRITERIA criteria = this.criteriaAccessor != null ? this.criteriaAccessor.resolve(query) : null;
|
||||
SORT sort = this.sortAccessor != null ? this.sortAccessor.resolve(query) : null;
|
||||
|
||||
return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract query attributes and delegate to concrete execution.
|
||||
*
|
||||
@@ -79,6 +94,21 @@ public abstract class QueryEngine<ADAPTER extends KeyValueAdapter, CRITERIA, SOR
|
||||
*/
|
||||
public abstract Collection<?> execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace);
|
||||
|
||||
/**
|
||||
* @param criteria
|
||||
* @param sort
|
||||
* @param offset
|
||||
* @param rows
|
||||
* @param keyspace
|
||||
* @param type
|
||||
* @return
|
||||
* @since 1.1
|
||||
*/
|
||||
public <T> Collection<T> execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace,
|
||||
Class<T> type) {
|
||||
return (Collection<T>) execute(criteria, sort, offset, rows, keyspace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param criteria
|
||||
* @param keyspace
|
||||
|
||||
@@ -215,7 +215,7 @@ public class KeyValueTemplateUnitTests {
|
||||
|
||||
template.findById("1", Foo.class);
|
||||
|
||||
verify(adapterMock, times(1)).get("1", Foo.class.getName());
|
||||
verify(adapterMock, times(1)).get("1", Foo.class.getName(), Foo.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,7 +253,7 @@ public class KeyValueTemplateUnitTests {
|
||||
|
||||
template.find(STRING_QUERY, Foo.class);
|
||||
|
||||
verify(adapterMock, times(1)).find(STRING_QUERY, Foo.class.getName());
|
||||
verify(adapterMock, times(1)).find(STRING_QUERY, Foo.class.getName(), Foo.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,7 +267,7 @@ public class KeyValueTemplateUnitTests {
|
||||
|
||||
template.findInRange(1, 5, Foo.class);
|
||||
|
||||
verify(adapterMock, times(1)).find(captor.capture(), eq(Foo.class.getName()));
|
||||
verify(adapterMock, times(1)).find(captor.capture(), eq(Foo.class.getName()), eq(Foo.class));
|
||||
assertThat(captor.getValue().getOffset(), is(1));
|
||||
assertThat(captor.getValue().getRows(), is(5));
|
||||
assertThat(captor.getValue().getCritieria(), nullValue());
|
||||
@@ -330,7 +330,7 @@ public class KeyValueTemplateUnitTests {
|
||||
|
||||
template.delete("1", Foo.class);
|
||||
|
||||
verify(adapterMock, times(1)).delete("1", Foo.class.getName());
|
||||
verify(adapterMock, times(1)).delete("1", Foo.class.getName(), Foo.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -344,7 +344,7 @@ public class KeyValueTemplateUnitTests {
|
||||
|
||||
template.delete(source);
|
||||
|
||||
verify(adapterMock, times(1)).delete("some-id", ClassWithStringId.class.getName());
|
||||
verify(adapterMock, times(1)).delete("some-id", ClassWithStringId.class.getName(), ClassWithStringId.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,7 +619,7 @@ public class KeyValueTemplateUnitTests {
|
||||
public void shouldPublishAfterDeleteEventCorrectly() {
|
||||
|
||||
setEventsToPublish(AfterDeleteEvent.class);
|
||||
when(adapterMock.delete(eq("1"), eq(FOO_ONE.getClass().getName()))).thenReturn(FOO_ONE);
|
||||
when(adapterMock.delete(eq("1"), eq(FOO_ONE.getClass().getName()), eq(Foo.class))).thenReturn(FOO_ONE);
|
||||
|
||||
template.delete("1", FOO_ONE.getClass());
|
||||
|
||||
@@ -666,7 +666,7 @@ public class KeyValueTemplateUnitTests {
|
||||
|
||||
setEventsToPublish(AfterGetEvent.class);
|
||||
|
||||
when(adapterMock.get(eq("1"), eq(FOO_ONE.getClass().getName()))).thenReturn(FOO_ONE);
|
||||
when(adapterMock.get(eq("1"), eq(FOO_ONE.getClass().getName()), eq(Foo.class))).thenReturn(FOO_ONE);
|
||||
|
||||
template.findById("1", FOO_ONE.getClass());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user