Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAKV-103 - Add KeyValueStore abstraction. #9

Open
wants to merge 2 commits into
base: 2.7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
<version>0.1.0.BUILD-SNAPSHOT</version>
<version>0.1.0.DATAKV-103-SNAPSHOT</version>

<name>Spring Data KeyValue</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* 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;

/**
* A {@link KeyValueStore} scoped to a keyspace that stores the values in via a provided {@link KeyValueAdapter}.
*
* @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;

/**
* @param adapter
* @param keyspace
*/
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);
}

}
27 changes: 0 additions & 27 deletions src/main/java/org/springframework/data/keyvalue/core/Entry.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.springframework.data.keyvalue.core;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -38,16 +37,14 @@ public boolean hasNext() {
}

@Override
public Entry<K, V> next() {
public Map.Entry<K, V> next() {
return new ForwardingEntry(delegate.next());
}

@Override
public void close() throws IOException {
public void close() {}

}

class ForwardingEntry implements Entry<K, V> {
class ForwardingEntry implements Map.Entry<K, V> {

private final Map.Entry<K, V> entry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package org.springframework.data.keyvalue.core;

import java.io.Closeable;
import java.util.Iterator;
import java.util.Map;

import org.springframework.data.util.CloseableIterator;

/**
* @author Christoph Strobl
* @param <K>
* @param <V>
*/
public interface KeyValueIterator<K, V> extends Iterator<Entry<K, V>>, Closeable {
public interface KeyValueIterator<K, V> extends CloseableIterator<Map.Entry<K, V>> {

}
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,91 @@
/*
* 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;

/**
* A key-value store that supports put, get, delete, and queries.
*
* @author Christoph Strobl
* @author Thomas Darimont
* @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