Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.tectonica.kvs;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Set;
Simple, yet powerful, framework (and approach) for handling a key-value store. It allows for multiple concurrent readers but a single concurrent writer on each entry. It also provides a read-before-update mechanism which makes life simpler and keeps data consistent. Indexing is also supported as part of the framework. The interface is intuitive and straightforward. This class itself is abstract and subclasses are included for several backend persistence engines, including in-memory (which is great for development).

The usage of this framework would probably yield the most benefit when used to prototype a data model, where changes are frequent and not backwards-compatible. However, in more than a few scenarios, this framework can be used in production. The heavy-lifting is done by the backend database and cache either way.

Zach Melamed
 public interface KeyValueStore<K, V> extends Iterable<KeyValue<K, V>>
 	public static interface KeyValue<K, V>
 		K getKey();
 		V getValue();
 	public static interface KeyMapper<K, V>
 		public K getKeyOf(V value);
 	public static interface ValueGenerator<K, V>
 		V generate(K key);
 	public static enum EventType
 		PreUpdate, PrePersistUpdate, PreAdd, PrePut;
 	public interface EventHandler<K, V>
 		public void handle(EventType type, K key, V value);
 	V get(K key);
 	V get(K keyboolean cacheResult);

Executes iteratorFor(java.util.Collection,boolean) with postponeCaching set to false
 	Iterator<KeyValue<K, V>> iteratorFor(Collection<K> keys);

Returns an iterator for a list of entries whose keys were passed to the method

IMPORTANT: Unlike simple 'SELECT WHERE KEY IN (..)', the list returned here is guaranteed to be at the exact same order of the keys passed. Moreover, if a certain key is passed more than once, so will its corresponding entry. Clearly, if a key doesn't exist in the store, it will be skipped (i.e. the returning list will never contain nulls).

keys ordered collection of keys to lookup (this is not necessarily a set, as keys can appear more than once in the collection)
postponeCaching indicates whether cache entries individually as each is read from the backend storage, or cache them all in a single call at the end of the iteration (use only if you'll certainly iterate through the entire result)
 	Iterator<KeyValue<K, V>> iteratorFor(Collection<K> keysboolean postponeCaching);
 	Iterator<V> valueIteratorFor(Collection<K> keysboolean postponeCaching);
 	Set<K> keySet();
 	List<V> values();
 	List<V> valuesFor(Collection<K> keys);
 	boolean containsKey(K key);
inserts a new entry, whose key doesn't already exist in storage. it's a faster and more resource-efficient way to insert entries compared to put(java.lang.Object,java.lang.Object) as it doesn't use any locking. do not use if you're not completely sure whether the key already exists. the behavior of the store in such case is undetermined and implementation-dependent.
	void add(K key, V value);

inserts or updates an entry. if you're sure that the entry is new (i.e. its key doesn't already exist), use the more efficient add(java.lang.Object,java.lang.Object) instead
	void put(K key, V value);

insert and entry only if the key is not yet taken

the newly added value, or null if nothing was inserted
putIfAbsent(K keyValueGenerator<K, V> generator);
update(K keyUpdater<V> updater);
	void update(Collection<K> keysUpdater<V> updater);

convenience method to update all entries
	void updateAll(Updater<V> updater);

convenience method applicable when keyMapper is provided

updateValue(V valueUpdater<V> updater);

convenience method applicable when keyMapper is provided

	void addValue(V value);

convenience method applicable when keyMapper is provided

	void putValue(V value);
	boolean delete(K key);
	int deleteAll();
	// ETC
	<F> Index<K, V, F> createIndex(String indexNameIndexMapper<V, F> mapFunc);
	void clearCache();
	void addListener(EventType typeEventHandler<K, V> handler);
New to GrepCode? Check out our FAQ X