Changeset 1188 for raptor-client


Ignore:
Timestamp:
10/27/11 18:56:07 (9 years ago)
Author:
philsmart
Message:
 
Location:
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store
Files:
5 added
5 edited
2 moved

Legend:

Unmodified
Added
Removed
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/EventHandler.java

    r1043 r1188  
    3131import uk.ac.cardiff.raptor.store.dao.StorageException; 
    3232 
    33  
    3433public interface EventHandler { 
    3534 
    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; 
    4141 
    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); 
    4748 
    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(); 
    5355 
    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(); 
    5962 
    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(); 
    6567 
    66         /** 
    67          * 
    68         */ 
    69         public void removeAllEvents(); 
     68    /** 
     69     * Initialises this event handler. 
     70    */ 
     71    public void initialise(); 
    7072 
    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; 
    7280 
    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; 
    7788 
    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(); 
    8495 
    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); 
    125105 
    126106} 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/EventStorageEngine.java

    r1176 r1188  
    3737 *  
    3838 */ 
    39 public class StorageEngine implements StoreEntriesTaskCallbackInterface { 
     39public class EventStorageEngine implements StoreEntriesTaskCallbackInterface { 
    4040 
    4141    /** Class logger */ 
    42     private final Logger log = LoggerFactory.getLogger(StorageEngine.class); 
     42    private final Logger log = LoggerFactory.getLogger(EventStorageEngine.class); 
    4343 
    4444    /** Responsible for storing all entries (e.g. events) */ 
    45     private EventHandler eventHandler; 
     45    private QueryableEventHandler eventHandler; 
    4646 
    4747    /** Engine used to associate attributes to existing events in the MUA */ 
     
    5555 
    5656    /** Default Constructor */ 
    57     public StorageEngine() { 
     57    public EventStorageEngine() { 
    5858 
    5959    } 
     
    141141 
    142142    /** 
    143      * Sets the configured entry handler. Must also then initialise that entry handler 
     143     * Sets the configured event handler. Must also then initialise that entry handler 
    144144     *  
    145145     * @param entryHandler the entryHandler to set 
    146146     */ 
    147     public void setEntryHandler(EventHandler entryHandler) { 
     147    public void setEventHandler(QueryableEventHandler entryHandler) { 
    148148        this.eventHandler = entryHandler; 
    149149        entryHandler.initialise(); 
     
    153153     * @return the entryHandler 
    154154     */ 
    155     public EventHandler getEntryHandler() { 
     155    public QueryableEventHandler getEventHandler() { 
    156156        return eventHandler; 
    157157    } 
     
    183183            try { 
    184184                String query = "select " + fieldName + " from Event group by (" + fieldName + ")"; 
    185                 List results = eventHandler.query(query); 
     185                List results = eventHandler.query(query, null); 
    186186                log.trace("Looking for possible values for field {} using query [{}]", fieldName, query); 
    187187                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 at 
    7  * 
    8  *         http://www.apache.org/licenses/LICENSE-2.0 
    9  * 
    10  * Unless required by applicable law or agreed to in writing, software 
    11  * 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 and 
    14  * limitations under the License. 
    15  * 
    16  * 
    17  * @author philsmart 
    18  * {@literal} this class manages interaction with the internal model of the ICA 
    19  * 
    20  */ 
    211 
    222package uk.ac.cardiff.raptor.store; 
    233 
    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 */ 
     9public interface IncrementalEventHandler extends EventHandler { 
    2610 
    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(); 
    9116 
    9217} 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/dao/RaptorDataConnection.java

    r1142 r1188  
    3131public interface RaptorDataConnection { 
    3232 
     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     */ 
    3342    public List runQuery(String query, Object[] parameters) throws DataAccessException; 
    3443 
     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     */ 
    3554    public Object runQueryUnique(String query, Object[] parameters) throws DataAccessException; 
    3655 
     56    /** 
     57     * Saves the Object <code>object</code>. 
     58     *  
     59     * @param event the Collection of Events to store in batch 
     60     * @throws StorageException 
     61     */ 
    3762    public void save(Object object) throws DataAccessException; 
    3863 
    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; 
    4071 
     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     */ 
    4178    public void saveAll(Collection<?> collection) throws DataAccessException; 
    4279 
     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     */ 
    4390    public List runQuery(String query, Object[] parameters, int maxResultSize) throws DataAccessException; 
    4491 
     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     */ 
    45101    public void runUpdate(String query, Object[] parameters) throws DataAccessException; 
    46102 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/LogFileIncrementalMemoryEventHandler.java

    r1176 r1188  
    2525 
    2626import java.util.ArrayList; 
     27import java.util.Collection; 
    2728import java.util.LinkedHashSet; 
    2829import java.util.List; 
     
    3637import uk.ac.cardiff.raptor.runtimeutils.ReflectionHelper; 
    3738import uk.ac.cardiff.raptor.store.IncrementalEventHandler; 
    38  
    39 public class LogFileMemoryEventHandler implements IncrementalEventHandler { 
     39import uk.ac.cardiff.raptor.store.dao.StorageException; 
     40 
     41public class LogFileIncrementalMemoryEventHandler implements IncrementalEventHandler { 
    4042 
    4143    /** Class logger */ 
     
    4345 
    4446    /** Pointer to the last recorded entry, for incremental update. */ 
    45     private DateTime latestEntryTime; 
     47    private DateTime latestEventTime; 
    4648 
    4749    /** Set of all entries stored by this EntryHandler. */ 
    48     private Set<Event> entries; 
     50    private Set<Event> events; 
    4951 
    5052    /** 
     
    5557    private Set<Event> latestEqualEntries; 
    5658 
    57     public LogFileMemoryEventHandler() { 
    58         entries = new LinkedHashSet<Event>(); 
     59    public LogFileIncrementalMemoryEventHandler() { 
     60        events = new LinkedHashSet<Event>(); 
    5961        latestEqualEntries = new LinkedHashSet<Event>(); 
    6062    } 
    6163 
    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()); 
    7070    } 
    7171 
     
    7777     * @return true if this event was added to the entry handler, false otherwise 
    7878     */ 
    79     public boolean addEntry(Event event) { 
     79    public boolean addEvent(Event event) { 
    8080        addEventIdIfNull(event); 
    8181        boolean isAfter = isAfter(event); 
    8282        boolean isEqual = isEqual(event); 
    8383        if (isAfter) { 
    84             entries.add(event); 
    85             updateLastEntry(event); 
     84            events.add(event); 
     85            updateLastEvent(event); 
    8686            return true; 
    8787        } else if (isEqual) { 
     
    9292            } 
    9393            if (!isAlreadyInLatest) { 
    94                 entries.add(event); 
    95                 updateLastEntry(event); 
     94                events.add(event); 
     95                updateLastEvent(event); 
    9696                return true; 
    9797            } 
     
    106106     */ 
    107107    public void reset() { 
    108         entries.clear(); 
     108        events.clear(); 
    109109        latestEqualEntries.clear(); 
    110         latestEntryTime = null; 
     110        latestEventTime = null; 
    111111    } 
    112112 
     
    131131                earliestReleaseTime, latestEqualEntries.size()); 
    132132        ArrayList<Event> toRemove = new ArrayList<Event>(); 
    133         for (Event event : entries) { 
     133        for (Event event : events) { 
    134134            if (event.getEventTime().isBefore(earliestReleaseTime)) 
    135135                toRemove.add(event); 
     
    140140            } 
    141141        } 
    142         entries.removeAll(toRemove); 
    143  
    144     } 
    145  
    146     private void updateLastEntry(Event entry) { 
     142        events.removeAll(toRemove); 
     143 
     144    } 
     145 
     146    private void updateLastEvent(Event entry) { 
    147147        DateTime entryTime = entry.getEventTime(); 
    148         if (getLatestEntryTime() == null) 
    149             setLatestEntryTime(entryTime); 
    150         if (entryTime.isAfter(getLatestEntryTime())) { 
    151             setLatestEntryTime(entryTime); 
     148        if (getLatestEventTime() == null) 
     149            setLatestEventTime(entryTime); 
     150        if (entryTime.isAfter(getLatestEventTime())) { 
     151            setLatestEventTime(entryTime); 
    152152            latestEqualEntries.clear(); 
    153153            latestEqualEntries.add(entry); 
    154154        } 
    155         if (entryTime.isEqual(getLatestEntryTime())) { 
     155        if (entryTime.isEqual(getLatestEventTime())) { 
    156156            latestEqualEntries.add(entry); 
    157157        } 
    158158    } 
    159159 
    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)) 
    176169            return true; 
    177170        return false; 
    178171    } 
    179172 
     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     */ 
    180178    public boolean isEqual(Event event) { 
    181         if (latestEntryTime == null) 
     179        if (latestEventTime == null) 
    182180            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     */ 
    186189    public boolean isAfter(Event event) { 
    187         if (latestEntryTime == null) 
    188             return true; 
    189         return event.getEventTime().isAfter(latestEntryTime); 
     190        if (latestEventTime == null) 
     191            return true; 
     192        return event.getEventTime().isAfter(latestEventTime); 
    190193    } 
    191194 
     
    194197     */ 
    195198    public void endTransaction() { 
    196         latestEntryTime = new DateTime(latestEntryTime.getMillis() + 1); 
     199        latestEventTime = new DateTime(latestEventTime.getMillis() + 1); 
    197200 
    198201    } 
     
    201204     * @return the list of entries currently stored by the entry handler 
    202205     */ 
    203     public List<Event> getEntries() { 
    204         return new ArrayList<Event>(entries); 
    205  
    206     } 
    207  
    208     /** 
    209      * Removes all entries, but does not reset last entry, as this is still used so as not to add previously parsed 
     206    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 
    210213     * entries. 
    211214     */ 
    212     public void removeAllEntries() { 
    213         entries.clear(); 
     215    public void removeAllEvents() { 
     216        events.clear(); 
    214217    } 
    215218 
     
    221224    } 
    222225 
    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 
    230247    } 
    231248 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/MemoryEventHandler.java

    r1043 r1188  
    2626import java.util.ArrayList; 
    2727import java.util.Collection; 
    28 import java.util.HashMap; 
    2928import java.util.LinkedHashSet; 
    3029import java.util.List; 
    31 import java.util.Map; 
    3230import java.util.Set; 
     31 
     32import org.joda.time.DateTime; 
     33import org.slf4j.Logger; 
     34import org.slf4j.LoggerFactory; 
    3335 
    3436import uk.ac.cardiff.model.event.Event; 
    3537import uk.ac.cardiff.raptor.runtimeutils.ReflectionHelper; 
    3638import uk.ac.cardiff.raptor.store.EventHandler; 
    37  
    38 import org.joda.time.DateTime; 
    39 import org.slf4j.Logger; 
    40 import org.slf4j.LoggerFactory; 
    41  
     39import uk.ac.cardiff.raptor.store.dao.StorageException; 
     40 
     41/** 
     42 * The Class MemoryEventHandler. 
     43 */ 
    4244public class MemoryEventHandler implements EventHandler { 
    4345 
    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    } 
    245200 
    246201} 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/impl/PersistantEventHandler.java

    r1176 r1188  
    2323import java.util.Collection; 
    2424import java.util.HashSet; 
    25 import java.util.Iterator; 
    2625import java.util.List; 
    2726import java.util.Set; 
     
    3332 
    3433import uk.ac.cardiff.model.event.Event; 
    35 import uk.ac.cardiff.raptor.store.EventHandler; 
     34import uk.ac.cardiff.raptor.store.QueryableEventHandler; 
    3635import uk.ac.cardiff.raptor.store.dao.RaptorDataConnection; 
    3736import uk.ac.cardiff.raptor.store.dao.StorageException; 
     
    4140 *  
    4241 */ 
    43 public class PersistantEventHandler implements EventHandler { 
     42public class PersistantEventHandler implements QueryableEventHandler { 
    4443 
    4544    /** class logger. */ 
     
    5756 
    5857    /** 
    59      * If true, COUNT queries to the database are seperated 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>. 
    6059     * Otherwise a COUNT of the highest level <code>Event</code> class is issued which uses table joins to determine 
    6160     * counts from all subclasses thereof. 
     
    114113    } 
    115114 
    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); 
    119118        } catch (DataAccessException e) { 
    120119            throw new StorageException("Could not save object", e); 
     
    193192 
    194193    public void removeAllEvents() { 
    195         log.error("Method removeAllEntries not yet implemented"); 
     194        // no-op method 
    196195    } 
    197196 
     
    202201    public RaptorDataConnection getDataConnection() { 
    203202        return dataConnection; 
    204     } 
    205  
    206     public void setEvents(final Set<Event> entries) { 
    207  
    208203    } 
    209204 
     
    240235    } 
    241236 
    242     // TODO Implementation may not work - PLEASE DO USE 
    243     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  
    253237    /** 
    254238     * @param optimiseCountQueries the optimiseCountQueries to set 
     
    279263    } 
    280264 
     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 
    281272} 
Note: See TracChangeset for help on using the changeset viewer.