| 1 | package com.hammurapi.eventbus.tests.familyties; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.Map; |
| 7 | import java.util.concurrent.locks.ReadWriteLock; |
| 8 | |
| 9 | import com.hammurapi.eventbus.AbstractEventBus; |
| 10 | import com.hammurapi.eventbus.HandleExtractor; |
| 11 | import com.hammurapi.eventbus.tests.familyties.model.Person; |
| 12 | import com.hammurapi.eventbus.tests.familyties.model.Relative; |
| 13 | import com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules; |
| 14 | import com.hammurapi.extract.Extractor; |
| 15 | import com.hammurapi.extract.Predicate; |
| 16 | import com.hammurapi.store.AbstractStore; |
| 17 | import com.hammurapi.store.DeputyStore; |
| 18 | import com.hammurapi.store.Index; |
| 19 | import com.hammurapi.store.LiveView; |
| 20 | import com.hammurapi.store.MultiIndex; |
| 21 | import com.hammurapi.store.UnmodifiableStore; |
| 22 | import com.hammurapi.store.local.LocalStoreBase; |
| 23 | |
| 24 | /** |
| 25 | * Implementation of event store. |
| 26 | * @author Pavel Vlasov |
| 27 | * |
| 28 | * @param <E> |
| 29 | * @param <P> |
| 30 | * @param <C> |
| 31 | */ |
| 32 | public class FamilyTiesEventStoreImpl extends LocalStoreBase<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, com.hammurapi.eventbus.tests.familyties.model.Relative, FamilyTiesEventStore> implements FamilyTiesEventStore { |
| 33 | |
| 34 | /** |
| 35 | * Config class with bound generic parameters. |
| 36 | * @author Pavel Vlasov |
| 37 | * |
| 38 | */ |
| 39 | public static class Config extends LocalStoreBase.Config<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, com.hammurapi.eventbus.tests.familyties.model.Relative, FamilyTiesEventStore> { |
| 40 | |
| 41 | public Config() { |
| 42 | setPrimaryKeyExtractor(new HandleExtractor<com.hammurapi.eventbus.tests.familyties.model.Relative, java.lang.Integer, com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules, Long, AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, FamilyTiesEventStore>()); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private MultiIndex<AbstractEventBus.Handle<Relative, Integer, FamilyTiesRules, Long>, Relative, Person, FamilyTiesEventStore> subjectIndex; |
| 47 | |
| 48 | public FamilyTiesEventStoreImpl(Config config) { |
| 49 | super(config); |
| 50 | |
| 51 | FamilyTiesEventStoreExtractor<Person> subjectExtractor = new FamilyTiesEventStoreAbstractExtractor<Person>(0, null, false) { |
| 52 | |
| 53 | @Override |
| 54 | protected Person extractInternal( |
| 55 | FamilyTiesEventStore context, |
| 56 | Map<FamilyTiesEventStore, Map<Extractor<com.hammurapi.eventbus.AbstractEventBus.Handle<Relative, Integer, FamilyTiesRules, Long>, ? super Person, FamilyTiesEventStore>, ? super Person>> cache, |
| 57 | com.hammurapi.eventbus.AbstractEventBus.Handle<Relative, Integer, FamilyTiesRules, Long>... obj) { |
| 58 | |
| 59 | return obj[0].getEvent().getSubject(); |
| 60 | } |
| 61 | |
| 62 | }; |
| 63 | subjectIndex = (MultiIndex<com.hammurapi.eventbus.AbstractEventBus.Handle<Relative, Integer, FamilyTiesRules, Long>, Relative, Person, FamilyTiesEventStore>) addIndex(null, subjectExtractor, Index.Type.LAZY, false, null); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Iterable<Relative> getRelatives(Person subject) { |
| 68 | Collection<Relative> ret = new ArrayList<Relative>(); |
| 69 | Z: for (AbstractEventBus.Handle<Relative, Integer, FamilyTiesRules, Long> h: subjectIndex.find(subject)) { |
| 70 | if (h.isValid()) { |
| 71 | Relative newRelative = h.getEvent(); |
| 72 | Iterator<Relative> rit = ret.iterator(); |
| 73 | while (rit.hasNext()) { |
| 74 | Relative eRelative = rit.next(); |
| 75 | switch (supercedes(eRelative, newRelative)) { |
| 76 | case 0: |
| 77 | // Unrelated - go further. |
| 78 | break; |
| 79 | case 1: |
| 80 | // No need to add this new relative, jump to next new relative |
| 81 | continue Z; |
| 82 | case -1: |
| 83 | // New relative supercedes this one. |
| 84 | rit.remove(); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | ret.add(newRelative); |
| 89 | } |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * |
| 96 | * @param r0 |
| 97 | * @param r1 |
| 98 | * @return -1 - r1 is more specific (e.g. Daughter instead of Child) than r0, 0 - unrelated, 1 - r0 is more specific or equal. |
| 99 | */ |
| 100 | private int supercedes(Relative r0, Relative r1) { |
| 101 | if (r0.equals(r1)) { |
| 102 | return 1; |
| 103 | } |
| 104 | if (r0.getSubject().equals(r1.getSubject()) && r0.getObject().equals(r1.getObject())) { |
| 105 | if (r0.getClass().isInstance(r1)) { // r1 is more specific |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | if (r1.getClass().isInstance(r0)) { // r0 is more specific |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | protected static class FamilyTiesEventDeputyStore extends DeputyStore<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, com.hammurapi.eventbus.tests.familyties.model.Relative, FamilyTiesEventStore> implements FamilyTiesEventStore { |
| 118 | |
| 119 | public FamilyTiesEventDeputyStore( |
| 120 | AbstractStore<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative, java.lang.Integer, com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules, Long>, com.hammurapi.eventbus.tests.familyties.model.Relative, FamilyTiesEventStore> master, |
| 121 | ReadWriteLock masterLock) { |
| 122 | super(master, masterLock); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | protected FamilyTiesEventStore createDeputy() { |
| 127 | return new FamilyTiesEventDeputyStore(this, createMasterLock()); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public FamilyTiesEventStore createUnmodifiableFacade() { |
| 132 | return new FamilyTiesEventUnmodifiableStore(this); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected FamilyTiesEventStore createLiveView(Predicate<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, FamilyTiesEventStore> selector) { |
| 137 | return new FamilyTiesEventLiveView(this, selector); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Iterable<Relative> getRelatives(Person subject) { |
| 142 | return ((FamilyTiesEventStore) master).getRelatives(subject); |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | protected static class FamilyTiesEventUnmodifiableStore extends UnmodifiableStore<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, com.hammurapi.eventbus.tests.familyties.model.Relative, FamilyTiesEventStore> implements FamilyTiesEventStore { |
| 148 | |
| 149 | public FamilyTiesEventUnmodifiableStore(FamilyTiesEventStore master) { |
| 150 | super(master); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Iterable<Relative> getRelatives(Person subject) { |
| 155 | return ((FamilyTiesEventStore) master).getRelatives(subject); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | |
| 160 | protected static class FamilyTiesEventLiveView extends LiveView<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, com.hammurapi.eventbus.tests.familyties.model.Relative, FamilyTiesEventStore> implements FamilyTiesEventStore { |
| 161 | |
| 162 | public FamilyTiesEventLiveView( |
| 163 | FamilyTiesEventStore master, |
| 164 | Predicate<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative, java.lang.Integer, com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules, Long>, FamilyTiesEventStore> selector) { |
| 165 | super(master, selector); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public FamilyTiesEventStore createUnmodifiableFacade() { |
| 170 | return new FamilyTiesEventUnmodifiableStore(this); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Iterable<Relative> getRelatives(Person subject) { |
| 175 | return ((FamilyTiesEventStore) master).getRelatives(subject); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | protected FamilyTiesEventStore createDeputy() { |
| 182 | return new FamilyTiesEventDeputyStore(this, createMasterLock()); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public FamilyTiesEventStore createUnmodifiableFacade() { |
| 187 | return new FamilyTiesEventUnmodifiableStore(this); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | protected FamilyTiesEventStore createLiveView(Predicate<AbstractEventBus.Handle<com.hammurapi.eventbus.tests.familyties.model.Relative,java.lang.Integer,com.hammurapi.eventbus.tests.familyties.rules.FamilyTiesRules,Long>, FamilyTiesEventStore> selector) { |
| 192 | return new FamilyTiesEventLiveView(this, selector); |
| 193 | } |
| 194 | |
| 195 | } |