Skip to content

Instantly share code, notes, and snippets.

@mattwigway
Created March 16, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwigway/a94ae43ce7e7ffe57f26 to your computer and use it in GitHub Desktop.
Save mattwigway/a94ae43ce7e7ffe57f26 to your computer and use it in GitHub Desktop.
MapDB inverse secondary keys
/**
* Create a set of Tuple2<key in original map, value> - secondary keys, backwards.
*/
public static <K, V, V2> void inverseSecondaryKeys (MapWithModificationListener<K, V> map,
final NavigableSet<Fun.Tuple2<K, V2>> set, final Function2<V2[], K, V> fun) {
if (set.isEmpty()) {
for (Map.Entry<K, V> e : map.entrySet()) {
for (V2 val : fun.run(e.getKey(), e.getValue())) {
set.add(new Tuple2<K, V2>(e.getKey(), val));
}
}
}
map.modificationListenerAdd(new MapListener<K, V> () {
@Override
public void update(K key, V oldVal, V newVal) {
// clear this key
set.subSet(new Tuple2(key, null), new Tuple2(key, Fun.HI)).clear();
for (V2 val : fun.run(key, newVal)) {
set.add(new Tuple2<K, V2>(key, val));
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment