Changeset 1188
- Timestamp:
- 10/27/11 18:56:07 (19 months ago)
- Location:
- raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store
- Files:
-
- 5 added
- 5 edited
- 2 moved
-
EventHandler.java (modified) (1 diff)
-
EventStorageEngine.java (moved) (moved from raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/StorageEngine.java) (5 diffs)
-
IncrementalEventHandler.java (modified) (1 diff)
-
QueryableEventHandler.java (added)
-
ResourceClassificationBackgroundService.java (added)
-
ResourceStorageEngine.java (added)
-
SaveAndApplyResourceClassificationCallbackInterface.java (added)
-
SaveAndApplyResourceClassificationTask.java (added)
-
dao/RaptorDataConnection.java (modified) (1 diff)
-
impl/LogFileIncrementalMemoryEventHandler.java (moved) (moved from raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/LogFileMemoryEventHandler.java) (12 diffs)
-
impl/MemoryEventHandler.java (modified) (1 diff)
-
impl/PersistantEventHandler.java (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/EventHandler.java
r1043 r1188 31 31 import uk.ac.cardiff.raptor.store.dao.StorageException; 32 32 33 34 33 public interface EventHandler { 35 34 36 /** 37 * 38 * @param entries 39 */ 40 public void addEvents(List<Event> entries) throws StorageException; 35 /** 36 * Adds a list of Events to this event handler. 37 * 38 * @param entries a <code>List</code> of events to add to this event handler. 39 */ 40 public void addEvents(List<Event> events) throws StorageException; 41 41 42 /** 43 * 44 * @param event 45 */ 46 public boolean addEvent(Event event); 42 /** 43 * Adds the <code>event</code> to this event handler. 44 * 45 * @param event the single event to add 46 */ 47 public boolean addEvent(Event event); 47 48 48 /** 49 * 50 * @return 51 */ 52 public DateTime getLatestEventTime(); 49 /** 50 * Returns the date and time of the latest event (chronologically) this event handler has stored. 51 * 52 * @return the date and time of the latest event this event handler has stored. 53 */ 54 public DateTime getLatestEventTime(); 53 55 54 /** 55 * 56 * @return 57 */ 58 public List<Event> getEvents(); 56 /** 57 * Returns a list of ALL events this event handler as stored. 58 * 59 * @return the <code>List</code> of events stored by this event handler 60 */ 61 public List<Event> getEvents(); 59 62 60 /** 61 * 62 * @param entries 63 */ 64 public void setEvents(Set<Event> entries); 63 /** 64 * Removes all events stored by the event handler. 65 */ 66 public void removeAllEvents(); 65 67 66 /**67 * 68 */69 public void removeAllEvents();68 /** 69 * Initialises this event handler. 70 */ 71 public void initialise(); 70 72 71 public void update(String query, Object[] parameters) throws StorageException; 73 /** 74 * Saves the Event <code>event</code>. 75 * 76 * @param event the Collection of Events to store in batch 77 * @throws StorageException 78 */ 79 public void save(Event event) throws StorageException; 72 80 73 /** 74 * 75 */ 76 public void initialise(); 81 /** 82 * Saves (in batch) every object in the collection (Which must themselves be a subclass of {@link Event}. 83 * 84 * @param object the Collection of Events to store in batch 85 * @throws StorageException 86 */ 87 public void saveAll(Collection<? extends Event> object) throws StorageException; 77 88 78 /**79 * 80 * @param query 81 * @return 82 */83 public List query(String query);89 /** 90 * Returns the number of events this event handler has stored. 91 * 92 * @return the number of events this event handler has stored 93 */ 94 public long getNumberOfEvents(); 84 95 85 /** 86 * 87 * @param query 88 * @return 89 */ 90 public List query(String query, Object[] parameters); 91 92 /** 93 * 94 * @param query 95 * @param parameters 96 * @return 97 */ 98 public List query(String query, Object[] parameters, int maxNoResults); 99 100 public void save(Object object) throws StorageException; 101 102 public void saveAll(Collection object) throws StorageException; 103 /** 104 * 105 * @param query 106 * @return 107 */ 108 public Object queryUnique(String query); 109 110 /** 111 * 112 * @return 113 */ 114 public long getNumberOfEvents(); 115 116 /** 117 * @param query 118 * @param parameters 119 * @return 120 */ 121 public Object queryUnique(String query, Object[] parameters); 122 123 public void removeEventsBefore(DateTime earliestReleaseTime, Set<Integer> latestEqualEntries); 124 96 /** 97 * Removes events from this event handler that are chronologically before the <code>earliestReleaseTime</code>. Also 98 * removes events from this event hanlder that are equal to <code>earliestReleaseTime</code> and are contained in 99 * the set <code>latestEqualEntries</code> 100 * 101 * @param earliestReleaseTime the date and time used to prune the set of events this event handler holds. 102 * @param latestEqualEvents a set of events that have equal dates and times to the <code>earliestReleaseTime</code> 103 */ 104 public void removeEventsBefore(DateTime earliestReleaseTime, Set<Integer> latestEqualEvents); 125 105 126 106 } -
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/EventStorageEngine.java
r1176 r1188 37 37 * 38 38 */ 39 public class StorageEngine implements StoreEntriesTaskCallbackInterface {39 public class EventStorageEngine implements StoreEntriesTaskCallbackInterface { 40 40 41 41 /** Class logger */ 42 private final Logger log = LoggerFactory.getLogger( StorageEngine.class);42 private final Logger log = LoggerFactory.getLogger(EventStorageEngine.class); 43 43 44 44 /** Responsible for storing all entries (e.g. events) */ 45 private EventHandler eventHandler;45 private QueryableEventHandler eventHandler; 46 46 47 47 /** Engine used to associate attributes to existing events in the MUA */ … … 55 55 56 56 /** Default Constructor */ 57 public StorageEngine() {57 public EventStorageEngine() { 58 58 59 59 } … … 141 141 142 142 /** 143 * Sets the configured e ntryhandler. Must also then initialise that entry handler143 * Sets the configured event handler. Must also then initialise that entry handler 144 144 * 145 145 * @param entryHandler the entryHandler to set 146 146 */ 147 public void setE ntryHandler(EventHandler entryHandler) {147 public void setEventHandler(QueryableEventHandler entryHandler) { 148 148 this.eventHandler = entryHandler; 149 149 entryHandler.initialise(); … … 153 153 * @return the entryHandler 154 154 */ 155 public EventHandler getEntryHandler() {155 public QueryableEventHandler getEventHandler() { 156 156 return eventHandler; 157 157 } … … 183 183 try { 184 184 String query = "select " + fieldName + " from Event group by (" + fieldName + ")"; 185 List results = eventHandler.query(query );185 List results = eventHandler.query(query, null); 186 186 log.trace("Looking for possible values for field {} using query [{}]", fieldName, query); 187 187 int noResults = 0; -
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/IncrementalEventHandler.java
r1046 r1188 1 /**2 * Copyright (C) 2010 Cardiff University, Wales <smartp@cf.ac.uk>3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 *17 * @author philsmart18 * {@literal} this class manages interaction with the internal model of the ICA19 *20 */21 1 22 2 package uk.ac.cardiff.raptor.store; 23 3 24 import java.util.List; 25 import java.util.Set; 4 /** 5 * An incremental event handler should be capable of storing only 'new' events, and capable of removing 'old' events 6 * while still be capable of parsing events from the date and time of the last removed event. 7 * 8 */ 9 public interface IncrementalEventHandler extends EventHandler { 26 10 27 import org.joda.time.DateTime; 28 29 import uk.ac.cardiff.model.event.Event; 30 import uk.ac.cardiff.raptor.store.dao.StorageException; 31 32 33 public interface IncrementalEventHandler { 34 35 /** 36 * 37 * @param entries 38 */ 39 public void addEntries(List<Event> entries) throws StorageException; 40 41 /** 42 * 43 * @param event 44 */ 45 public boolean addEntry(Event event); 46 47 /** 48 * 49 * @return 50 */ 51 public DateTime getLatestEntryTime(); 52 53 /** 54 * 55 * @return 56 */ 57 public List<Event> getEntries(); 58 59 /** 60 * 61 * @param entries 62 */ 63 public void setEntries(Set<Event> entries); 64 65 /** 66 * 67 */ 68 public void removeAllEntries(); 69 70 /** 71 * 72 */ 73 public void initialise(); 74 75 /** 76 * 77 * @return 78 */ 79 public int getNumberOfEntries(); 80 81 public void removeEventsBefore(DateTime earliestReleaseTime, Set<Integer> latestEqualEntries); 82 83 /** 84 * Rests this <code>IncrementalEntryHandler</code> back to its initial state 85 * 86 */ 87 public void reset(); 88 89 90 11 /** 12 * Rests this <code>IncrementalEntryHandler</code> back to its initial state 13 * 14 */ 15 public void reset(); 91 16 92 17 } -
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/dao/RaptorDataConnection.java
r1142 r1188 31 31 public interface RaptorDataConnection { 32 32 33 /** 34 * Performs the update query <code>query</code> with the parameters <code>parameters</code>. Concrete 35 * implementations of the interface may require the query in a specific, specified, language e.g. HQL or JPQL. 36 * 37 * @param query the query to run as an update. 38 * @param parameters the parameters to substitute into the query string <code>query</code> 39 * @return a <code>List</code> of objects that are the result of running the query <code>query</code> with the 40 * parameters <code>parameters</code> 41 */ 33 42 public List runQuery(String query, Object[] parameters) throws DataAccessException; 34 43 44 /** 45 * Runs the query <code>query</code> with the parameters <code>parameters</code> and returns a single unique result. 46 * Concrete implementations of the interface may require the query in a specific, specified, language e.g. HQL or 47 * JPQL. 48 * 49 * @param query the query to run. 50 * @param parameters the parameters to substitute into the query string <code>query</code> 51 * @return a single object that is the result of running the query <code>query</code> with the parameters 52 * <code>parameters</code> 53 */ 35 54 public Object runQueryUnique(String query, Object[] parameters) throws DataAccessException; 36 55 56 /** 57 * Saves the Object <code>object</code>. 58 * 59 * @param event the Collection of Events to store in batch 60 * @throws StorageException 61 */ 37 62 public void save(Object object) throws DataAccessException; 38 63 39 public void deleteAllEntries(Collection<?> entries) throws DataAccessException; 64 /** 65 * Removes all the objects <code>objects</code> from the underlaying database. 66 * 67 * @param objects the objects to remove. 68 * @throws DataAccessException 69 */ 70 public void deleteAllEntries(Collection<?> objects) throws DataAccessException; 40 71 72 /** 73 * Saves (in batch) every object in the collection. 74 * 75 * @param object the Collection of Events to store in batch 76 * @throws StorageException 77 */ 41 78 public void saveAll(Collection<?> collection) throws DataAccessException; 42 79 80 /** 81 * Runs the query <code>query</code> with the parameters <code>parameters</code> and returns a <code>List</code> of 82 * results up to the specified <code>maxNoResults</code>. Concrete implementations of the interface may require the 83 * query in a specific, specified, language e.g. HQL or JPQL. 84 * 85 * @param query the query to run. 86 * @param parameters the parameters to substitute into the query string <code>query</code> 87 * @return a <code>List</code> of objects that are the result of running the query <code>query</code> with the 88 * parameters <code>parameters</code> 89 */ 43 90 public List runQuery(String query, Object[] parameters, int maxResultSize) throws DataAccessException; 44 91 92 /** 93 * Performs the update query <code>query</code> with the parameters <code>parameters</code>. Concrete 94 * implementations of the interface may require the query in a specific, specified, language e.g. HQL or JPQL. 95 * 96 * @param query the query to run as an update. 97 * @param parameters the parameters to substitute into the query string <code>query</code> 98 * @return a <code>List</code> of objects that are the result of running the query <code>query</code> with the 99 * parameters <code>parameters</code> 100 */ 45 101 public void runUpdate(String query, Object[] parameters) throws DataAccessException; 46 102 -
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/LogFileIncrementalMemoryEventHandler.java
r1176 r1188 25 25 26 26 import java.util.ArrayList; 27 import java.util.Collection; 27 28 import java.util.LinkedHashSet; 28 29 import java.util.List; … … 36 37 import uk.ac.cardiff.raptor.runtimeutils.ReflectionHelper; 37 38 import uk.ac.cardiff.raptor.store.IncrementalEventHandler; 38 39 public class LogFileMemoryEventHandler implements IncrementalEventHandler { 39 import uk.ac.cardiff.raptor.store.dao.StorageException; 40 41 public class LogFileIncrementalMemoryEventHandler implements IncrementalEventHandler { 40 42 41 43 /** Class logger */ … … 43 45 44 46 /** Pointer to the last recorded entry, for incremental update. */ 45 private DateTime latestE ntryTime;47 private DateTime latestEventTime; 46 48 47 49 /** Set of all entries stored by this EntryHandler. */ 48 private Set<Event> e ntries;50 private Set<Event> events; 49 51 50 52 /** … … 55 57 private Set<Event> latestEqualEntries; 56 58 57 public LogFile MemoryEventHandler() {58 e ntries = new LinkedHashSet<Event>();59 public LogFileIncrementalMemoryEventHandler() { 60 events = new LinkedHashSet<Event>(); 59 61 latestEqualEntries = new LinkedHashSet<Event>(); 60 62 } 61 63 62 public void addEntries(List<Event> entries) { 63 log.debug("Current: " + this.entries.size() + " in: " + entries.size()); 64 65 for (Event event : entries) { 66 addEntry(event); 67 68 } 69 log.debug("Total No. of Entries " + this.entries.size() + " Latest Entry at: " + getLatestEntryTime()); 64 public void addEvents(List<Event> events) throws StorageException { 65 log.debug("Current: " + this.events.size() + " in: " + events.size()); 66 for (Event event : events) { 67 addEvent(event); 68 } 69 log.debug("Total No. of Entries " + this.events.size() + " Latest Entry at: " + getLatestEventTime()); 70 70 } 71 71 … … 77 77 * @return true if this event was added to the entry handler, false otherwise 78 78 */ 79 public boolean addE ntry(Event event) {79 public boolean addEvent(Event event) { 80 80 addEventIdIfNull(event); 81 81 boolean isAfter = isAfter(event); 82 82 boolean isEqual = isEqual(event); 83 83 if (isAfter) { 84 e ntries.add(event);85 updateLastE ntry(event);84 events.add(event); 85 updateLastEvent(event); 86 86 return true; 87 87 } else if (isEqual) { … … 92 92 } 93 93 if (!isAlreadyInLatest) { 94 e ntries.add(event);95 updateLastE ntry(event);94 events.add(event); 95 updateLastEvent(event); 96 96 return true; 97 97 } … … 106 106 */ 107 107 public void reset() { 108 e ntries.clear();108 events.clear(); 109 109 latestEqualEntries.clear(); 110 latestE ntryTime = null;110 latestEventTime = null; 111 111 } 112 112 … … 131 131 earliestReleaseTime, latestEqualEntries.size()); 132 132 ArrayList<Event> toRemove = new ArrayList<Event>(); 133 for (Event event : e ntries) {133 for (Event event : events) { 134 134 if (event.getEventTime().isBefore(earliestReleaseTime)) 135 135 toRemove.add(event); … … 140 140 } 141 141 } 142 e ntries.removeAll(toRemove);143 144 } 145 146 private void updateLastE ntry(Event entry) {142 events.removeAll(toRemove); 143 144 } 145 146 private void updateLastEvent(Event entry) { 147 147 DateTime entryTime = entry.getEventTime(); 148 if (getLatestE ntryTime() == null)149 setLatestE ntryTime(entryTime);150 if (entryTime.isAfter(getLatestE ntryTime())) {151 setLatestE ntryTime(entryTime);148 if (getLatestEventTime() == null) 149 setLatestEventTime(entryTime); 150 if (entryTime.isAfter(getLatestEventTime())) { 151 setLatestEventTime(entryTime); 152 152 latestEqualEntries.clear(); 153 153 latestEqualEntries.add(entry); 154 154 } 155 if (entryTime.isEqual(getLatestE ntryTime())) {155 if (entryTime.isEqual(getLatestEventTime())) { 156 156 latestEqualEntries.add(entry); 157 157 } 158 158 } 159 159 160 public void setLatestEntryTime(DateTime latestEntryTime) { 161 this.latestEntryTime = latestEntryTime; 162 } 163 164 public DateTime getLatestEntryTime() { 165 return latestEntryTime; 166 } 167 168 /** 169 * @param authE 170 * @return 171 */ 172 public boolean isNewerOrEqual(Event event) { 173 if (latestEntryTime == null) 174 return true; 175 if (!event.getEventTime().isBefore(latestEntryTime)) 160 /** 161 * @param event the event to check if its after or equal to the <code>latestEventTime</code> or not. 162 * @return true if the recorded date and time attribute of <code>event</code> is equal to or newer than the date and 163 * time recorded by <code>latestEventTime</code> 164 */ 165 public boolean isAfterOrEqual(Event event) { 166 if (latestEventTime == null) 167 return true; 168 if (!event.getEventTime().isBefore(latestEventTime)) 176 169 return true; 177 170 return false; 178 171 } 179 172 173 /** 174 * @param event the event to check if its equal to the <code>latestEventTime</code> or not. 175 * @return true if the recorded date and time attribute of <code>event</code> is equal to or newer than the date and 176 * time recorded by <code>latestEventTime</code> 177 */ 180 178 public boolean isEqual(Event event) { 181 if (latestE ntryTime == null)179 if (latestEventTime == null) 182 180 return false; 183 return event.getEventTime().isEqual(latestEntryTime); 184 } 185 181 return event.getEventTime().isEqual(latestEventTime); 182 } 183 184 /** 185 * @param event the event to check if its after to the <code>latestEventTime</code> or not. 186 * @return true if the recorded date and time attribute of <code>event</code> is after the date and time recorded by 187 * <code>latestEventTime</code> 188 */ 186 189 public boolean isAfter(Event event) { 187 if (latestE ntryTime == null)188 return true; 189 return event.getEventTime().isAfter(latestE ntryTime);190 if (latestEventTime == null) 191 return true; 192 return event.getEventTime().isAfter(latestEventTime); 190 193 } 191 194 … … 194 197 */ 195 198 public void endTransaction() { 196 latestE ntryTime = new DateTime(latestEntryTime.getMillis() + 1);199 latestEventTime = new DateTime(latestEventTime.getMillis() + 1); 197 200 198 201 } … … 201 204 * @return the list of entries currently stored by the entry handler 202 205 */ 203 public List<Event> getE ntries() {204 return new ArrayList<Event>(e ntries);205 206 } 207 208 /** 209 * Removes all e ntries, but does not reset last entry, as this is still used so as not to add previously parsed206 public List<Event> getEvents() { 207 return new ArrayList<Event>(events); 208 209 } 210 211 /** 212 * Removes all events, but does not reset last entry, as this is still used so as not to add previously parsed 210 213 * entries. 211 214 */ 212 public void removeAllE ntries() {213 e ntries.clear();215 public void removeAllEvents() { 216 events.clear(); 214 217 } 215 218 … … 221 224 } 222 225 223 public int getNumberOfEntries() { 224 return entries.size(); 225 } 226 227 public void setEntries(Set<Event> entries) { 228 this.entries = entries; 229 226 public long getNumberOfEvents() { 227 return events.size(); 228 } 229 230 public DateTime getLatestEventTime() { 231 return latestEventTime; 232 } 233 234 /** 235 * @param latestEventTime the latestEventTime to set 236 */ 237 public void setLatestEventTime(DateTime latestEventTime) { 238 this.latestEventTime = latestEventTime; 239 } 240 241 public void save(Event event) throws StorageException { 242 // no-op 243 } 244 245 public void saveAll(Collection<? extends Event> object) throws StorageException { 246 // no-op 230 247 } 231 248 -
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/MemoryEventHandler.java
r1043 r1188 26 26 import java.util.ArrayList; 27 27 import java.util.Collection; 28 import java.util.HashMap;29 28 import java.util.LinkedHashSet; 30 29 import java.util.List; 31 import java.util.Map;32 30 import java.util.Set; 31 32 import org.joda.time.DateTime; 33 import org.slf4j.Logger; 34 import org.slf4j.LoggerFactory; 33 35 34 36 import uk.ac.cardiff.model.event.Event; 35 37 import uk.ac.cardiff.raptor.runtimeutils.ReflectionHelper; 36 38 import uk.ac.cardiff.raptor.store.EventHandler; 37 38 import org.joda.time.DateTime; 39 import org.slf4j.Logger; 40 import org.slf4j.LoggerFactory; 41 39 import uk.ac.cardiff.raptor.store.dao.StorageException; 40 41 /** 42 * The Class MemoryEventHandler. 43 */ 42 44 public class MemoryEventHandler implements EventHandler { 43 45 44 /** Class logger */ 45 private final Logger log = LoggerFactory.getLogger(MemoryEventHandler.class); 46 47 /** pointer to the last recorded entry, for incremental update */ 48 private DateTime latestEntryTime; 49 50 /** record of the last entry that was sent over SOAP */ 51 private DateTime lastPublishedEntryTime; 52 53 /** set of all entries stored by this EntryHandler */ 54 private Set<Event> entries; 55 56 public MemoryEventHandler() { 57 entries = new LinkedHashSet<Event>(); 58 } 59 60 public void addEvents(List<Event> entries) { 61 log.debug("Current: " + this.entries.size() + " in: " + entries.size()); 62 int notAdded = 0; 63 for (Event event : entries) { 64 boolean didAdd = addEvent(event); 65 if (didAdd == false) 66 notAdded++; 67 68 } 69 log.debug("Total No. of Entries {}, Lastest Entry at: {}, with {} duplicates", new Object[] { this.entries.size(), getLatestEventTime(), notAdded }); 70 } 71 72 /** 73 * Checks whether this event is already stored, if not, then it adds the 74 * <code>event</code> to the <code>entries<code> set. 75 * 76 * @param event 77 * the event to store 78 */ 79 public boolean addEvent(Event event) { 80 addEventIdIfNull(event); 81 if (!entries.contains(event)) { 82 entries.add(event); 83 updateLastEntry(event); 84 return true; 85 } else { 86 return false; 87 } 88 89 } 90 91 /** 92 * Stores the hashcode of the event as the <code>eventId</code> iff there is 93 * no existing eventId (defined as 0), and the event has a valid hashcode. 94 * 95 * @param event 96 */ 97 private void addEventIdIfNull(Event event) { 98 if (event.getEventId() != 0) { 99 return; 100 } 101 int hashCode = ReflectionHelper.getHashCodeFromEventOrNull(event); 102 if (hashCode != 0) { 103 event.setEventId(hashCode); 104 } 105 } 106 107 private void updateLastEntry(Event event) { 108 DateTime entryTime = event.getEventTime(); 109 if (getLatestEventTime() == null) 110 setLatestEntryTime(entryTime); 111 if (entryTime.isAfter(getLatestEventTime())) { 112 setLatestEntryTime(entryTime); 113 114 } 115 } 116 117 public void removeEventsBefore(DateTime earliestReleaseTime, Set<Integer> latestEqualEntries) { 118 log.debug("Removing events earlier than {}, or in the set of last equal events sent (from {} events)", earliestReleaseTime, latestEqualEntries.size()); 119 ArrayList<Event> toRemove = new ArrayList<Event>(); 120 for (Event event : entries) { 121 if (event.getEventTime().isBefore(earliestReleaseTime)) 122 toRemove.add(event); 123 if (event.getEventTime().isEqual(earliestReleaseTime)) { 124 if (latestEqualEntries.contains(event.getEventId())) { 125 toRemove.add(event); 126 } 127 } 128 } 129 entries.removeAll(toRemove); 130 131 } 132 133 public void setLatestEntryTime(DateTime latestEntryTime) { 134 this.latestEntryTime = latestEntryTime; 135 } 136 137 public DateTime getLatestEventTime() { 138 return latestEntryTime; 139 } 140 141 /** 142 * pushes the latestEntryTime by 1 millisecond (see LogFileParser.java for 143 * explanation) 144 */ 145 public void endTransaction() { 146 latestEntryTime = new DateTime(latestEntryTime.getMillis() + 1); 147 148 } 149 150 /** 151 * @return the list of entries currently stored by the entry handler 152 */ 153 public List<Event> getEvents() { 154 return new ArrayList<Event>(entries); 155 156 } 157 158 /** 159 * Removes all entries, but does not reset last entry, as this is still used 160 * so as not to add previously parsed entries. 161 */ 162 public void removeAllEvents() { 163 entries.clear(); 164 } 165 166 public void setLastPublishedEntryTime(DateTime lastPublishedEntryTime) { 167 this.lastPublishedEntryTime = lastPublishedEntryTime; 168 } 169 170 public DateTime getLastPublishedEntryTime() { 171 return lastPublishedEntryTime; 172 } 173 174 /** 175 * This is a no-op method for all in-memory entry handlers 176 */ 177 public void initialise() { 178 179 } 180 181 public long getNumberOfEvents() { 182 return entries.size(); 183 } 184 185 /** 186 * This is a no-op method for all in-memory entry handlers 187 */ 188 public List query(String query) { 189 return null; 190 } 191 192 /** 193 * T This is a no-op method for all in-memory entry handlers 194 */ 195 public Object queryUnique(String query) { 196 return null; 197 } 198 199 /** 200 * This is a no-op method for all in-memory entry handlers 201 */ 202 public Object queryUnique(String query, Object[] parameters) { 203 return null; 204 } 205 206 public void setEvents(Set<Event> entries) { 207 this.entries = entries; 208 209 } 210 211 /** 212 * This is a no-op method for all in-memory entry handlers 213 */ 214 public List query(String query, Object[] parameters) { 215 return null; 216 } 217 218 /** 219 * This is a no-op method for all in-memory entry handlers 220 */ 221 public List query(String query, Object[] parameters, int maxNoResults) { 222 return null; 223 } 224 225 /** 226 * This is a no-op method for all in-memory entry handlers 227 */ 228 public void save(Object object) { 229 230 } 231 232 /** 233 * This is a no-op method for all in-memory entry handlers 234 */ 235 public void saveAll(Collection object) { 236 237 } 238 239 /** 240 * This is a no-op method for all in-memory entry handlers 241 */ 242 public void update(String query, Object[] parameters) { 243 244 } 46 /** Class logger. */ 47 private final Logger log = LoggerFactory.getLogger(MemoryEventHandler.class); 48 49 /** pointer to the last recorded entry, for incremental update. */ 50 private DateTime latestEntryTime; 51 52 /** set of all entries stored by this EntryHandler. */ 53 private Set<Event> events; 54 55 /** 56 * Instantiates a new memory event handler. 57 */ 58 public MemoryEventHandler() { 59 events = new LinkedHashSet<Event>(); 60 } 61 62 public void addEvents(List<Event> entries) { 63 log.debug("Current: " + this.events.size() + " in: " + entries.size()); 64 int notAdded = 0; 65 for (Event event : entries) { 66 boolean didAdd = addEvent(event); 67 if (didAdd == false) 68 notAdded++; 69 70 } 71 log.debug("Total No. of Entries {}, Lastest Entry at: {}, with {} duplicates", new Object[] { 72 this.events.size(), getLatestEventTime(), notAdded}); 73 } 74 75 /** 76 * Checks whether this event is already stored, if not, then it adds the <code>event</code> to the 77 * <code>entries<code> set. 78 * 79 * @param event the event to store 80 * @return true, if successful 81 */ 82 public boolean addEvent(Event event) { 83 addEventIdIfNull(event); 84 if (!events.contains(event)) { 85 events.add(event); 86 updateLastEntry(event); 87 return true; 88 } else { 89 return false; 90 } 91 92 } 93 94 /** 95 * Stores the hashcode of the event as the <code>eventId</code> iff there is no existing eventId (defined as 0), and 96 * the event has a valid hashcode. 97 * 98 * @param event the event 99 */ 100 private void addEventIdIfNull(Event event) { 101 if (event.getEventId() != 0) { 102 return; 103 } 104 int hashCode = ReflectionHelper.getHashCodeFromEventOrNull(event); 105 if (hashCode != 0) { 106 event.setEventId(hashCode); 107 } 108 } 109 110 /** 111 * Update last entry. 112 * 113 * @param event the event 114 */ 115 private void updateLastEntry(Event event) { 116 DateTime entryTime = event.getEventTime(); 117 if (getLatestEventTime() == null) 118 setLatestEntryTime(entryTime); 119 if (entryTime.isAfter(getLatestEventTime())) { 120 setLatestEntryTime(entryTime); 121 122 } 123 } 124 125 public void removeEventsBefore(DateTime earliestReleaseTime, Set<Integer> latestEqualEntries) { 126 log.debug("Removing events earlier than {}, or in the set of last equal events sent (from {} events)", 127 earliestReleaseTime, latestEqualEntries.size()); 128 ArrayList<Event> toRemove = new ArrayList<Event>(); 129 for (Event event : events) { 130 if (event.getEventTime().isBefore(earliestReleaseTime)) 131 toRemove.add(event); 132 if (event.getEventTime().isEqual(earliestReleaseTime)) { 133 if (latestEqualEntries.contains(event.getEventId())) { 134 toRemove.add(event); 135 } 136 } 137 } 138 events.removeAll(toRemove); 139 140 } 141 142 /** 143 * Sets the latest entry time. 144 * 145 * @param latestEntryTime the new latest entry time 146 */ 147 public void setLatestEntryTime(DateTime latestEntryTime) { 148 this.latestEntryTime = latestEntryTime; 149 } 150 151 public DateTime getLatestEventTime() { 152 return latestEntryTime; 153 } 154 155 /** 156 * pushes the latestEntryTime by 1 millisecond (see LogFileParser.java for explanation) 157 */ 158 public void endTransaction() { 159 latestEntryTime = new DateTime(latestEntryTime.getMillis() + 1); 160 161 } 162 163 /** 164 * Get all the events stored by the event handler. 165 * 166 * @return the list of entries currently stored by the entry handler 167 */ 168 public List<Event> getEvents() { 169 return new ArrayList<Event>(events); 170 171 } 172 173 /** 174 * Removes all entries, but does not reset last entry, as this is still used so as not to add previously parsed 175 * entries. 176 */ 177 public void removeAllEvents() { 178 events.clear(); 179 } 180 181 /** 182 * This is a no-op method for all in-memory entry handlers. 183 */ 184 public void initialise() { 185 186 } 187 188 public long getNumberOfEvents() { 189 return events.size(); 190 } 191 192 public void save(Event event) throws StorageException { 193 // no-op 194 195 } 196 197 public void saveAll(Collection<? extends Event> object) throws StorageException { 198 // no-op 199 } 245 200 246 201 } -
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/PersistantEventHandler.java
r1176 r1188 23 23 import java.util.Collection; 24 24 import java.util.HashSet; 25 import java.util.Iterator;26 25 import java.util.List; 27 26 import java.util.Set; … … 33 32 34 33 import uk.ac.cardiff.model.event.Event; 35 import uk.ac.cardiff.raptor.store. EventHandler;34 import uk.ac.cardiff.raptor.store.QueryableEventHandler; 36 35 import uk.ac.cardiff.raptor.store.dao.RaptorDataConnection; 37 36 import uk.ac.cardiff.raptor.store.dao.StorageException; … … 41 40 * 42 41 */ 43 public class PersistantEventHandler implements EventHandler {42 public class PersistantEventHandler implements QueryableEventHandler { 44 43 45 44 /** class logger. */ … … 57 56 58 57 /** 59 * If true, COUNT queries to the database are sep erated per table as described in <code>countTableNames</code>.58 * If true, COUNT queries to the database are separated per table as described in <code>countTableNames</code>. 60 59 * Otherwise a COUNT of the highest level <code>Event</code> class is issued which uses table joins to determine 61 60 * counts from all subclasses thereof. … … 114 113 } 115 114 116 public void save( Object object) throws StorageException {117 try { 118 dataConnection.save( object);115 public void save(Event event) throws StorageException { 116 try { 117 dataConnection.save(event); 119 118 } catch (DataAccessException e) { 120 119 throw new StorageException("Could not save object", e); … … 193 192 194 193 public void removeAllEvents() { 195 log.error("Method removeAllEntries not yet implemented");194 // no-op method 196 195 } 197 196 … … 202 201 public RaptorDataConnection getDataConnection() { 203 202 return dataConnection; 204 }205 206 public void setEvents(final Set<Event> entries) {207 208 203 } 209 204 … … 240 235 } 241 236 242 // TODO Implementation may not work - PLEASE DO USE243 public void removeEventsBefore(final DateTime earliestReleaseTime, final Set<Integer> latestEqualEntries) {244 dataConnection.runQueryUnique("delete from Event where eventTime < ?",245 new Object[] {earliestReleaseTime.toDate()});246 for (Iterator<Integer> entries = latestEqualEntries.iterator(); entries.hasNext();) {247 Integer hash = entries.next();248 dataConnection.runQueryUnique("delete from Event where hashCode = ?", new Object[] {hash});249 }250 251 }252 253 237 /** 254 238 * @param optimiseCountQueries the optimiseCountQueries to set … … 279 263 } 280 264 265 /** 266 * This is a no-op method for this event handler 267 */ 268 public void removeEventsBefore(DateTime earliestReleaseTime, Set<Integer> latestEqualEntries) { 269 // no op method. 270 } 271 281 272 }
Note: See TracChangeset
for help on using the changeset viewer.
