Skip to content

Commit

Permalink
DATAKV-103 - Add KeyValueStore abstraction.
Browse files Browse the repository at this point in the history
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
christophstrobl authored and Thomas Darimont committed May 12, 2015
1 parent a58251c commit 03d3ebb
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 0 deletions.
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ public interface KeyValueOperations extends DisposableBean {
*/
long count(KeyValueQuery<?> query, Class<?> type);

/**
* Returns a {@link KeyValueStore} scoped to the given {@code keyspace}.
*
* @param keyspace must not be {@literal null}.
* @return
*/
<K extends Serializable, V> KeyValueStore<K, V> getKeyValueStore(Serializable keyspace);

/**
* @return mapping context in use.
*/
Expand Down
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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,15 @@ public Long doInKeyValue(KeyValueAdapter adapter) {
});
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueOperations#getKeyValueStore(java.io.Serializable)
*/
@Override
public <K extends Serializable, V> KeyValueStore<K, V> getKeyValueStore(Serializable keyspace) {
return new AdapterBackedKeyValueStore<K, V>(this.adapter, keyspace);
}

/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.KeyValueOperations#getMappingContext()
Expand Down
Loading

0 comments on commit 03d3ebb

Please sign in to comment.