| 1 | package com.hammurapi.store; |
| 2 | |
| 3 | import java.util.Comparator; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.concurrent.locks.Lock; |
| 6 | |
| 7 | import com.hammurapi.common.Util; |
| 8 | import com.hammurapi.extract.CommutativeAnd; |
| 9 | import com.hammurapi.extract.Extractor; |
| 10 | import com.hammurapi.extract.Predicate; |
| 11 | import com.hammurapi.store.Index.Type; |
| 12 | |
| 13 | public abstract class LiveView<T, PK, S extends Store<T,PK,S>> implements Store<T, PK, S> { |
| 14 | |
| 15 | protected S master; |
| 16 | private Predicate<T, S> selector; |
| 17 | |
| 18 | public LiveView(S master, Predicate<T, S> selector) { |
| 19 | this.master = master; |
| 20 | this.selector = selector; |
| 21 | } |
| 22 | |
| 23 | public Iterator<T> iterator() { |
| 24 | return master.get(selector).iterator(); |
| 25 | } |
| 26 | |
| 27 | public Handle<T, PK, S> put(T obj, Predicate<T, S>... validators) { |
| 28 | if (matches(obj)) { |
| 29 | return master.put(obj, validators); |
| 30 | } |
| 31 | throw new StoreException("Object does not match the view predicate."); |
| 32 | } |
| 33 | |
| 34 | public Iterable<T> getAll() { |
| 35 | return master.get(selector); |
| 36 | } |
| 37 | |
| 38 | public int queryAll(QueryTask<T, PK, S> processor) { |
| 39 | return master.query(selector, processor); |
| 40 | } |
| 41 | |
| 42 | public T getByPrimaryKey(PK primaryKey) { |
| 43 | T ret = master.getByPrimaryKey(primaryKey); |
| 44 | return matches(ret) ? ret : null; |
| 45 | } |
| 46 | |
| 47 | @SuppressWarnings("unchecked") |
| 48 | private Boolean matches(T ret) { |
| 49 | return selector.extract((S) this, null, Util.wrap(ret)); |
| 50 | } |
| 51 | |
| 52 | public Iterable<T> get(Predicate<T, S> selector) { |
| 53 | return master.get(and(selector)); |
| 54 | } |
| 55 | |
| 56 | @SuppressWarnings("unchecked") |
| 57 | private Predicate<T, S> and(Predicate<T, S> selector) { |
| 58 | return selector==null ? this.selector : new CommutativeAnd<T, S>(-1, null, selector, this.selector); |
| 59 | } |
| 60 | |
| 61 | public int query( |
| 62 | Predicate<T, S> selector, |
| 63 | QueryTask<T, PK, S> processor) { |
| 64 | |
| 65 | return master.query(and(selector), processor); |
| 66 | } |
| 67 | |
| 68 | public <V> int query( |
| 69 | Predicate<T, S> selector, |
| 70 | Extractor<T, V, S> extractor, |
| 71 | QueryTask<V, PK, S> processor) { |
| 72 | return master.query(and(selector), extractor, processor); |
| 73 | } |
| 74 | |
| 75 | public <V> int queryMultiple( |
| 76 | Predicate<T, S> selector, |
| 77 | Extractor<T, Iterable<V>, S> extractor, |
| 78 | Predicate<V, S> valueSelector, |
| 79 | QueryTask<V, PK, S> processor) { |
| 80 | |
| 81 | return master.queryMultiple( |
| 82 | and(selector), |
| 83 | extractor, |
| 84 | valueSelector, |
| 85 | processor); |
| 86 | } |
| 87 | |
| 88 | public Lock readLock() { |
| 89 | return master.readLock(); |
| 90 | } |
| 91 | |
| 92 | public Lock writeLock() { |
| 93 | return master.writeLock(); |
| 94 | } |
| 95 | |
| 96 | public <V> Iterable<V> get( |
| 97 | Predicate<T, S> selector, |
| 98 | Extractor<T, V, S> extractor, |
| 99 | boolean ordered, |
| 100 | Comparator<V> comparator) { |
| 101 | |
| 102 | return master.get( |
| 103 | and(selector), |
| 104 | extractor, |
| 105 | ordered, |
| 106 | comparator); |
| 107 | } |
| 108 | |
| 109 | public <V> Iterable<V> getMultiple( |
| 110 | Predicate<T, S> selector, |
| 111 | Extractor<T, Iterable<V>, S> extractor, |
| 112 | Predicate<V, S> valueSelector, |
| 113 | boolean ordered, |
| 114 | Comparator<V> comparator) { |
| 115 | |
| 116 | return master.getMultiple( |
| 117 | and(selector), |
| 118 | extractor, |
| 119 | valueSelector, |
| 120 | ordered, |
| 121 | comparator); |
| 122 | } |
| 123 | |
| 124 | public void clear() { |
| 125 | master.remove(selector); |
| 126 | } |
| 127 | |
| 128 | public boolean remove(T obj) { |
| 129 | return matches(obj) ? master.remove(obj) : false; |
| 130 | } |
| 131 | |
| 132 | public boolean removeByPrimaryKey(PK primaryKey) { |
| 133 | T obj = master.getByPrimaryKey(primaryKey); |
| 134 | if (obj==null) { |
| 135 | return false; |
| 136 | } |
| 137 | if (matches(obj)) { |
| 138 | return master.removeByPrimaryKey(primaryKey); |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | public int remove(Predicate<T, S> selector) { |
| 144 | return master.remove(and(selector)); |
| 145 | } |
| 146 | |
| 147 | public int update( |
| 148 | Predicate<T, S> selector, |
| 149 | UpdateTask<T, PK, S> updater) { |
| 150 | |
| 151 | return master.update(and(selector), updater); |
| 152 | } |
| 153 | |
| 154 | public <V> Index<T, PK, V, S> addIndex( |
| 155 | Predicate<T, S> predicate, |
| 156 | Extractor<T, V, S> extractor, |
| 157 | Type type, |
| 158 | boolean ordered, Comparator<V> comparator) { |
| 159 | |
| 160 | return master.addIndex( |
| 161 | and(predicate), |
| 162 | extractor, |
| 163 | type, |
| 164 | ordered, |
| 165 | comparator); |
| 166 | } |
| 167 | |
| 168 | public Extractor<T, PK, S> getPrimaryKeyExtractor() { |
| 169 | return master.getPrimaryKeyExtractor(); |
| 170 | } |
| 171 | |
| 172 | public S createView( |
| 173 | Predicate<T, S> selector, |
| 174 | ViewType viewType) { |
| 175 | |
| 176 | return master.createView(and(selector), viewType); |
| 177 | } |
| 178 | |
| 179 | protected S getMaster() { |
| 180 | return master; |
| 181 | } |
| 182 | |
| 183 | } |