| 1 | package com.hammurapi.store.local; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.Comparator; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.Map; |
| 8 | import java.util.TreeMap; |
| 9 | import java.util.concurrent.ExecutorService; |
| 10 | import java.util.concurrent.locks.Lock; |
| 11 | import java.util.concurrent.locks.ReadWriteLock; |
| 12 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 13 | |
| 14 | import com.hammurapi.extract.Extractor; |
| 15 | import com.hammurapi.extract.Predicate; |
| 16 | import com.hammurapi.store.AbstractIndex; |
| 17 | import com.hammurapi.store.Store; |
| 18 | import com.hammurapi.store.Store.Handle; |
| 19 | import com.hammurapi.store.StoreException; |
| 20 | |
| 21 | class LocalIndex<T, PK, V, S extends Store<T,PK,S>> extends AbstractIndex<T, PK, V, S> { |
| 22 | |
| 23 | private Map<V, Handle<T, PK, S>> uniqueStore; |
| 24 | private Map<V, Collection<Handle<T, PK, S>>> multiStore; |
| 25 | private ReadWriteLock lock = new ReentrantReadWriteLock(); |
| 26 | |
| 27 | protected LocalIndex( |
| 28 | Predicate<T, S> predicate, |
| 29 | Extractor<T, V, S> extractor, |
| 30 | Type type, |
| 31 | boolean ordered, |
| 32 | Comparator<V> comparator, |
| 33 | LocalStoreBase<T, PK, S> store) { |
| 34 | |
| 35 | super(predicate, extractor, type, ordered, comparator, store); |
| 36 | |
| 37 | if (Type.UNIQUE.equals(type)) { |
| 38 | if (ordered) { |
| 39 | if (comparator==null) { |
| 40 | uniqueStore = new TreeMap<V, Handle<T, PK, S>>(); |
| 41 | } else { |
| 42 | uniqueStore = new TreeMap<V, Handle<T, PK, S>>(comparator); |
| 43 | } |
| 44 | } else { |
| 45 | uniqueStore = new HashMap<V, Handle<T, PK, S>>(); |
| 46 | } |
| 47 | } else { |
| 48 | if (ordered) { |
| 49 | if (comparator==null) { |
| 50 | multiStore = new TreeMap<V, Collection<Handle<T, PK, S>>>(); |
| 51 | } else { |
| 52 | multiStore = new TreeMap<V, Collection<Handle<T, PK, S>>>(comparator); |
| 53 | } |
| 54 | } else { |
| 55 | multiStore = new HashMap<V, Collection<Handle<T, PK, S>>>(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected Map<V, Handle<T, PK, S>> getIndexUniqueStore() { |
| 62 | return uniqueStore; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected Map<V, Collection<Handle<T, PK, S>>> getIndexMultiStore() { |
| 67 | return multiStore; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected Lock readLock() { |
| 72 | return lock.readLock(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected Lock writeLock() { |
| 77 | return lock.writeLock(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void upgrade(Type type, boolean ordered, Comparator<V> comparator) { |
| 82 | if (!Type.UNIQUE.equals(this.type) && Type.UNIQUE.equals(type)) { |
| 83 | // Check uniqueness |
| 84 | for (Collection<Handle<T, PK, S>> h: multiStore.values()) { |
| 85 | if (h.size()>1) { |
| 86 | throw new StoreException("Cannot upgrade index to unique, there are duplicate values"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (ordered || this.ordered) { |
| 91 | if (comparator==null) { |
| 92 | uniqueStore = new TreeMap<V, Handle<T, PK, S>>(); |
| 93 | } else { |
| 94 | uniqueStore = new TreeMap<V, Handle<T, PK, S>>(comparator); |
| 95 | } |
| 96 | } else { |
| 97 | uniqueStore = new HashMap<V, Handle<T, PK, S>>(); |
| 98 | } |
| 99 | |
| 100 | for (Map.Entry<V,Collection<Handle<T, PK, S>>> entry: multiStore.entrySet()) { |
| 101 | for (Handle<T, PK, S> h: entry.getValue()) { |
| 102 | if (h.isValid()) { |
| 103 | uniqueStore.put(entry.getKey(), h); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | multiStore.clear(); |
| 109 | multiStore = null; |
| 110 | } else if (!this.ordered && ordered) { |
| 111 | if (Type.UNIQUE.equals(type)) { |
| 112 | if (comparator==null) { |
| 113 | uniqueStore = new TreeMap<V, Handle<T, PK, S>>(uniqueStore); |
| 114 | } else { |
| 115 | TreeMap<V, Handle<T, PK, S>> newStore = new TreeMap<V, Handle<T, PK, S>>(comparator); |
| 116 | newStore.putAll(uniqueStore); |
| 117 | uniqueStore = newStore; |
| 118 | } |
| 119 | } else { |
| 120 | if (comparator==null) { |
| 121 | multiStore = new TreeMap<V, Collection<Handle<T, PK, S>>>(multiStore); |
| 122 | } else { |
| 123 | TreeMap<V, Collection<Handle<T, PK, S>>> newStore = new TreeMap<V, Collection<Handle<T, PK, S>>>(comparator); |
| 124 | newStore.putAll(multiStore); |
| 125 | multiStore = newStore; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | this.type = type; |
| 131 | this.ordered = ordered; |
| 132 | this.comparator = comparator; |
| 133 | } |
| 134 | |
| 135 | private Collection<UpdateEntry> pendingUpdates = new LinkedList<AbstractIndex<T,PK,V,S>.UpdateEntry>(); |
| 136 | |
| 137 | @Override |
| 138 | protected Collection<UpdateEntry> getPendingUpdates() { |
| 139 | return pendingUpdates; |
| 140 | } |
| 141 | |
| 142 | @SuppressWarnings("rawtypes") |
| 143 | @Override |
| 144 | protected ExecutorService getExecutorService() { |
| 145 | return ((LocalStoreBase) store).getExecutorService(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Override to customize handling of asynchronous index exceptions. |
| 150 | */ |
| 151 | @SuppressWarnings("rawtypes") |
| 152 | @Override |
| 153 | protected void handleAsynchException(Exception e) { |
| 154 | ((LocalStoreBase) store).handleIndexAsynchException(e); |
| 155 | } |
| 156 | |
| 157 | } |