-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
a58251c
commit 03d3ebb
Showing
5 changed files
with
430 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/main/java/org/springframework/data/keyvalue/core/AdapterBackedKeyValueStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2015 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.List; | ||
|
||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
* @param <K> | ||
* @param <V> | ||
*/ | ||
public class AdapterBackedKeyValueStore<K extends Serializable, V> implements KeyValueStore<K, V> { | ||
|
||
private final Serializable keyspace; | ||
private final KeyValueAdapter adapter; | ||
|
||
public AdapterBackedKeyValueStore(KeyValueAdapter adapter, Serializable keyspace) { | ||
|
||
Assert.notNull(adapter, "Adapter must not be null!"); | ||
Assert.notNull(keyspace, "Keyspace must not be null!"); | ||
this.adapter = adapter; | ||
this.keyspace = keyspace; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#put(java.io.Serializable, java.lang.Object) | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public V put(K id, V item) { | ||
return (V) adapter.put(id, item, keyspace); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#contains(java.io.Serializable) | ||
*/ | ||
@Override | ||
public boolean contains(K id) { | ||
return adapter.contains(id, keyspace); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#get(java.io.Serializable) | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public V get(K id) { | ||
return (V) adapter.get(id, keyspace); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#getAll(java.util.Collection) | ||
*/ | ||
@Override | ||
public Iterable<V> getAll(Collection<K> keys) { | ||
|
||
if (CollectionUtils.isEmpty(keys)) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
List<V> result = new ArrayList<V>(keys.size()); | ||
for (K key : keys) { | ||
result.add(get(key)); | ||
} | ||
return result; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#delete(java.io.Serializable) | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public V remove(K id) { | ||
return (V) adapter.delete(id, keyspace); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#removeAll(java.util.Collection) | ||
*/ | ||
@Override | ||
public Iterable<V> removeAll(Collection<K> keys) { | ||
|
||
if (CollectionUtils.isEmpty(keys)) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
List<V> result = new ArrayList<V>(keys.size()); | ||
for (K key : keys) { | ||
result.add(remove(key)); | ||
} | ||
return result; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#size() | ||
*/ | ||
@Override | ||
public long size() { | ||
return adapter.count(keyspace); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.keyvalue.core.KeyValueStore#clear() | ||
*/ | ||
@Override | ||
public void clear() { | ||
adapter.deleteAllOf(keyspace); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/org/springframework/data/keyvalue/core/KeyValueStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2015 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; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
* @param <K> | ||
* @param <V> | ||
*/ | ||
public interface KeyValueStore<K extends Serializable, V> { | ||
|
||
/** | ||
* Add object with given key. | ||
* | ||
* @param key must not be {@literal null}. | ||
* @return the item previously associated with the key. | ||
*/ | ||
V put(K key, V item); | ||
|
||
/** | ||
* Check if given key is present in KeyValue store. | ||
* | ||
* @param key must not be {@literal null}. | ||
* @return | ||
*/ | ||
boolean contains(K key); | ||
|
||
/** | ||
* Get the Object associated with the given key. | ||
* | ||
* @param key must not be {@literal null}. | ||
* @return {@literal null} if key not available. | ||
*/ | ||
V get(K key); | ||
|
||
/** | ||
* Get all Objects associated with the given keys. | ||
* | ||
* @param keys must not be {@literal null}. | ||
* @return an empty {@link Iterable} if no matching key found. | ||
*/ | ||
Iterable<V> getAll(Collection<K> keys); | ||
|
||
/** | ||
* Delete and return the element associated with the given key. | ||
* | ||
* @param key must not be {@literal null}. | ||
* @return | ||
*/ | ||
V remove(K key); | ||
|
||
/** | ||
* Delete and return all elements associated with the given keys. | ||
* | ||
* @param keys must not be {@literal null}. | ||
* @return | ||
*/ | ||
Iterable<V> removeAll(Collection<K> keys); | ||
|
||
/** | ||
* Get the number of total elements contained. | ||
* | ||
* @return | ||
*/ | ||
long size(); | ||
|
||
/** | ||
* Removes all elements. The {@link KeyValueStore} will be empty after this call returns. | ||
*/ | ||
void clear(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.