CARMA C++
AgingPolicy.h
1 #ifndef CARMA_CONTROL_AGINGPOLICY_H
2 #define CARMA_CONTROL_AGINGPOLICY_H
3 
4 #include "carma/util/types.h"
5 
6 #include <boost/date_time/gregorian/gregorian_types.hpp>
7 #include <boost/date_time/posix_time/posix_time.hpp>
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 namespace carma {
13 namespace control {
14 
15 class StateManager;
16 
17 // Map to resolve string names into posix time objects.
18 // Note list is automatically sorted by frame with the oldest files first.
19 typedef std::map< boost::posix_time::ptime, std::string > FileTimeMap;
20 typedef FileTimeMap::iterator FileTimeMapIter;
21 typedef FileTimeMap::const_iterator FileTimeMapConstIter;
22 
44 class AgingPolicy {
45 public:
46 
47  AgingPolicy( const std::string & directory,
48  ::boost::posix_time::time_duration interval,
49  int intervalDays, int intervalWeeks,int intervalMonths,
50  int durationDays, int durationMonths, int durationYears );
51 
52  std::string getDirectory( ) const;
53 
54  // Sync internal data structures to input time.
55  void syncToTime( const boost::posix_time::ptime & policyEndDate );
56 
57  // Apply the policy to retrieve a map of files which are candidates
58  // for deletion and no longer fit the policy. Aged files are
59  // candidates for ingestion into subsequent policies.
60  FileTimeMap reapAgedCandidates( );
61 
62  // Given a single candidate, either move it into the policy or delete it.
63  // If deleted, the corresponding frame is removed from the manager's index.
64  void
65  handleCandidate( const FileTimeMap::value_type candidate,
66  AgingPolicy & previousPolicy,
67  StateManager & manager );
68 
69  boost::posix_time::ptime
70  subtractTotalDuration( const boost::posix_time::ptime & to ) const;
71 
72  boost::posix_time::ptime
73  subtractTotalInterval( const boost::posix_time::ptime & to ) const;
74 
75  bool frameIsInPolicy( carma::util::frameType frame ) const;
76 
77  std::string getFilenameForFrame( const carma::util::frameType frame ) const;
78 
79 private:
80 
81 
82  // Vector of open time slots for this policy
83  typedef std::vector<boost::posix_time::time_period> TimeSlots;
84  void updateFirstOpenTimeSlots( );
85 
86  void traceDeletion( const FileTimeMap::value_type & candidate,
87  const AgingPolicy & previousPolicy,
88  const std::string & spiel );
89 
90  void traceOpenTimeSlots( );
91 
92  void removeFileFromPolicy( FileTimeMap::value_type entry,
93  StateManager & manager );
94 
95  std::string directory_;
96  boost::posix_time::time_duration interval_;
97  boost::gregorian::days intervalDays_;
98  boost::gregorian::weeks intervalWeeks_;
99  boost::gregorian::months intervalMonths_;
100  boost::gregorian::days durationDays_;
101  boost::gregorian::months durationMonths_;
102  boost::gregorian::years durationYears_;
103 
104  FileTimeMap fileTimeMap_;
105  TimeSlots openTimeSlots_;
106  boost::posix_time::ptime policyStartDate_; // Valid any given update cycle.
107  boost::posix_time::ptime policyEndDate_; // Valid any given update cycle.
108  boost::posix_time::time_period policyPeriod_; // Ditto
109 
110 }; // Class AgingPolicy
111 
112 typedef std::vector< AgingPolicy > PolicyChain;
113 
114 PolicyChain createAgingPolicyChain( const std::string & dirname );
115 
116 std::string retrieveFilenameForFrame( const PolicyChain & policies,
117  carma::util::frameType frame );
118 
119 } // namespace control
120 } // namespace carma
121 #endif
unsigned int frameType
Half second frames since Jan 1, 2000.
Definition: types.h:29
Class to describe an aging policy and associated methods for adding and removing files from it...
Definition: AgingPolicy.h:44
Various type definitions for util classes.