private Map < String , List < Map < String , Object > > > buildEventMap ( String projectId , Map < String , List < Object > > eventHandles ) throws IOException { Map < String , List < Map < String , Object > > > result = new HashMap < String , List < Map < String , Object > > > ( ) ; for ( Map . Entry < String , List < Object > > entry : eventHandles . entrySet ( ) ) { String eventCollection = entry . getKey ( ) ; List < Object > handles = entry . getValue ( ) ; // Skip event collections that don't contain any events. if ( handles == null || handles . size ( ) == 0 ) { continue ; } // Build the event list by retrieving events from the store. List < Map < String , Object > > events = new ArrayList < Map < String , Object > > ( handles . size ( ) ) ; Map < String , Integer > attempts ; if ( eventStore instanceof KeenAttemptCountingEventStore ) { synchronized ( attemptsLock ) { try { attempts = getAttemptsMap ( projectId , eventCollection ) ; } catch ( IOException ex ) { // setting this to a fresh map will effectively declare this the "last attempt" for // these events. attempts = new HashMap < String , Integer > ( ) ; KeenLogging . log ( "Failed to read attempt counts map. Events will still be POSTed. " + "Exception: " + ex ) ; } for ( Object handle : handles ) { Map < String , Object > event = getEvent ( handle ) ; String attemptsKey = "" + handle . hashCode ( ) ; Integer remainingAttempts = attempts . get ( attemptsKey ) ; if ( remainingAttempts == null ) { // treat null as "this is the last attempt" remainingAttempts = 1 ; } // decrement the remaining attempts count and put the new value on the map remainingAttempts -- ; attempts . put ( attemptsKey , remainingAttempts ) ; if ( remainingAttempts >= 0 ) { // if we had some remaining attempts, then try again events . add ( event ) ; } else { // otherwise remove it from the store eventStore . remove ( handle ) ; // iff eventStore.remove succeeds we can do some housekeeping and remove the // key from the attempts hash. attempts . remove ( attemptsKey ) ; } } try { setAttemptsMap ( projectId , eventCollection , attempts ) ; } catch ( IOException ex ) { KeenLogging . log ( "Failed to update event POST attempts counts while sending queued " + "events. Events will still be POSTed. Exception: " + ex ) ; } } } else { for ( Object handle : handles ) { events . add ( getEvent ( handle ) ) ; } } result . put ( eventCollection , events ) ; } return result ; }
Builds a map from collection name to a list of event maps given a map from collection name to a list of event handles . This method just uses the event store to retrieve each event by its handle .