Skip to content

Commit 03d3ebb

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAKV-103 - Add KeyValueStore abstraction.
The KeyValueStore abstraction allows more direct interaction with the underlying adapter through a simple keyspace bound interface. This allows to bypass keyspace and id lookup performed by the template. Original pull request: #9.
1 parent a58251c commit 03d3ebb

File tree

5 files changed

+430
-0
lines changed

5 files changed

+430
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue.core;
17+
18+
import java.io.Serializable;
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.CollectionUtils;
26+
27+
/**
28+
* @author Christoph Strobl
29+
* @param <K>
30+
* @param <V>
31+
*/
32+
public class AdapterBackedKeyValueStore<K extends Serializable, V> implements KeyValueStore<K, V> {
33+
34+
private final Serializable keyspace;
35+
private final KeyValueAdapter adapter;
36+
37+
public AdapterBackedKeyValueStore(KeyValueAdapter adapter, Serializable keyspace) {
38+
39+
Assert.notNull(adapter, "Adapter must not be null!");
40+
Assert.notNull(keyspace, "Keyspace must not be null!");
41+
this.adapter = adapter;
42+
this.keyspace = keyspace;
43+
}
44+
45+
/*
46+
* (non-Javadoc)
47+
* @see org.springframework.data.keyvalue.core.KeyValueStore#put(java.io.Serializable, java.lang.Object)
48+
*/
49+
@Override
50+
@SuppressWarnings("unchecked")
51+
public V put(K id, V item) {
52+
return (V) adapter.put(id, item, keyspace);
53+
}
54+
55+
/*
56+
* (non-Javadoc)
57+
* @see org.springframework.data.keyvalue.core.KeyValueStore#contains(java.io.Serializable)
58+
*/
59+
@Override
60+
public boolean contains(K id) {
61+
return adapter.contains(id, keyspace);
62+
}
63+
64+
/*
65+
* (non-Javadoc)
66+
* @see org.springframework.data.keyvalue.core.KeyValueStore#get(java.io.Serializable)
67+
*/
68+
@Override
69+
@SuppressWarnings("unchecked")
70+
public V get(K id) {
71+
return (V) adapter.get(id, keyspace);
72+
}
73+
74+
/*
75+
* (non-Javadoc)
76+
* @see org.springframework.data.keyvalue.core.KeyValueStore#getAll(java.util.Collection)
77+
*/
78+
@Override
79+
public Iterable<V> getAll(Collection<K> keys) {
80+
81+
if (CollectionUtils.isEmpty(keys)) {
82+
return Collections.emptySet();
83+
}
84+
85+
List<V> result = new ArrayList<V>(keys.size());
86+
for (K key : keys) {
87+
result.add(get(key));
88+
}
89+
return result;
90+
}
91+
92+
/*
93+
* (non-Javadoc)
94+
* @see org.springframework.data.keyvalue.core.KeyValueStore#delete(java.io.Serializable)
95+
*/
96+
@Override
97+
@SuppressWarnings("unchecked")
98+
public V remove(K id) {
99+
return (V) adapter.delete(id, keyspace);
100+
}
101+
102+
/*
103+
* (non-Javadoc)
104+
* @see org.springframework.data.keyvalue.core.KeyValueStore#removeAll(java.util.Collection)
105+
*/
106+
@Override
107+
public Iterable<V> removeAll(Collection<K> keys) {
108+
109+
if (CollectionUtils.isEmpty(keys)) {
110+
return Collections.emptySet();
111+
}
112+
113+
List<V> result = new ArrayList<V>(keys.size());
114+
for (K key : keys) {
115+
result.add(remove(key));
116+
}
117+
return result;
118+
}
119+
120+
/*
121+
* (non-Javadoc)
122+
* @see org.springframework.data.keyvalue.core.KeyValueStore#size()
123+
*/
124+
@Override
125+
public long size() {
126+
return adapter.count(keyspace);
127+
}
128+
129+
/*
130+
* (non-Javadoc)
131+
* @see org.springframework.data.keyvalue.core.KeyValueStore#clear()
132+
*/
133+
@Override
134+
public void clear() {
135+
adapter.deleteAllOf(keyspace);
136+
}
137+
138+
}

src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java

+8
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ public interface KeyValueOperations extends DisposableBean {
169169
*/
170170
long count(KeyValueQuery<?> query, Class<?> type);
171171

172+
/**
173+
* Returns a {@link KeyValueStore} scoped to the given {@code keyspace}.
174+
*
175+
* @param keyspace must not be {@literal null}.
176+
* @return
177+
*/
178+
<K extends Serializable, V> KeyValueStore<K, V> getKeyValueStore(Serializable keyspace);
179+
172180
/**
173181
* @return mapping context in use.
174182
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue.core;
17+
18+
import java.io.Serializable;
19+
import java.util.Collection;
20+
21+
/**
22+
* @author Christoph Strobl
23+
* @param <K>
24+
* @param <V>
25+
*/
26+
public interface KeyValueStore<K extends Serializable, V> {
27+
28+
/**
29+
* Add object with given key.
30+
*
31+
* @param key must not be {@literal null}.
32+
* @return the item previously associated with the key.
33+
*/
34+
V put(K key, V item);
35+
36+
/**
37+
* Check if given key is present in KeyValue store.
38+
*
39+
* @param key must not be {@literal null}.
40+
* @return
41+
*/
42+
boolean contains(K key);
43+
44+
/**
45+
* Get the Object associated with the given key.
46+
*
47+
* @param key must not be {@literal null}.
48+
* @return {@literal null} if key not available.
49+
*/
50+
V get(K key);
51+
52+
/**
53+
* Get all Objects associated with the given keys.
54+
*
55+
* @param keys must not be {@literal null}.
56+
* @return an empty {@link Iterable} if no matching key found.
57+
*/
58+
Iterable<V> getAll(Collection<K> keys);
59+
60+
/**
61+
* Delete and return the element associated with the given key.
62+
*
63+
* @param key must not be {@literal null}.
64+
* @return
65+
*/
66+
V remove(K key);
67+
68+
/**
69+
* Delete and return all elements associated with the given keys.
70+
*
71+
* @param keys must not be {@literal null}.
72+
* @return
73+
*/
74+
Iterable<V> removeAll(Collection<K> keys);
75+
76+
/**
77+
* Get the number of total elements contained.
78+
*
79+
* @return
80+
*/
81+
long size();
82+
83+
/**
84+
* Removes all elements. The {@link KeyValueStore} will be empty after this call returns.
85+
*/
86+
void clear();
87+
88+
}

src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java

+9
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,15 @@ public Long doInKeyValue(KeyValueAdapter adapter) {
428428
});
429429
}
430430

431+
/*
432+
* (non-Javadoc)
433+
* @see org.springframework.data.keyvalue.core.KeyValueOperations#getKeyValueStore(java.io.Serializable)
434+
*/
435+
@Override
436+
public <K extends Serializable, V> KeyValueStore<K, V> getKeyValueStore(Serializable keyspace) {
437+
return new AdapterBackedKeyValueStore<K, V>(this.adapter, keyspace);
438+
}
439+
431440
/*
432441
* (non-Javadoc)
433442
* @see org.springframework.data.keyvalue.core.KeyValueOperations#getMappingContext()

0 commit comments

Comments
 (0)