CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FragmentStore.cc
Go to the documentation of this file.
1 // $Id: FragmentStore.cc,v 1.10 2011/03/30 15:16:48 mommsen Exp $
3 
6 
7 using namespace stor;
8 
9 
10 FragmentStore::FragmentStore(size_t maxMemoryUsageMB)
11  : maxMemoryUsage_(maxMemoryUsageMB*1024*1024)
12 {
13  clear();
14 }
15 
17 {
18  // the trivial case that the chain is already complete
19  if ( chain.complete() ) return true;
20 
21  memoryUsed_ += chain.memoryUsed();
22  const FragKey newKey = chain.fragmentKey();
23 
24  // Use efficientAddOrUpdates pattern suggested by Item 24 of
25  // 'Effective STL' by Scott Meyers
26  fragmentMap::iterator pos = store_.lower_bound(newKey);
27 
28  if(pos != store_.end() && !(store_.key_comp()(newKey, pos->first)))
29  {
30  // key already exists
31  pos->second.addToChain(chain);
32 
33  if ( pos->second.complete() )
34  {
35  chain = pos->second;
36  store_.erase(pos);
37  memoryUsed_ -= chain.memoryUsed();
38  return true;
39  }
40  }
41  else
42  {
44 
45  // The key does not exist in the map, add it to the map
46  // Use pos as a hint to insert, so it can avoid another lookup
47  store_.insert(pos, fragmentMap::value_type(newKey, chain));
48  chain.release();
49 
50  // We already handled the trivial case that the chain is complete.
51  // Thus, store_ will not have a complete event.
52  }
53 
54  return false;
55 }
56 
58 {
59  for (
60  fragmentMap::iterator it = store_.begin(), itEnd = store_.end();
61  it != itEnd;
62  ++it
63  )
64  {
65  it->second.addToStaleWindowStartTime(duration);
66  }
67 }
68 
70 {
71  for (
72  fragmentMap::iterator it = store_.begin(), itEnd = store_.end();
73  it != itEnd;
74  ++it
75  )
76  {
77  it->second.resetStaleWindowStartTime();
78  }
79 }
80 
82 {
83  const utils::TimePoint_t cutOffTime = utils::getCurrentTime() - timeout;
84 
85  fragmentMap::iterator pos = store_.begin();
86  fragmentMap::iterator end = store_.end();
87 
88  while ( (pos != end) && (pos->second.staleWindowStartTime() > cutOffTime ) )
89  {
90  ++pos;
91  }
92 
93  if ( pos == end )
94  {
95  chain.release();
96  return false;
97  }
98  else
99  {
100  chain = pos->second;
101  store_.erase(pos);
102  memoryUsed_ -= chain.memoryUsed();
103  chain.markFaulty();
104  return true;
105  }
106 }
107 
108 
109 
TimePoint_t getCurrentTime()
Definition: Utils.h:158
const bool addFragment(I2OChain &)
bool complete() const
Definition: I2OChain.cc:118
FragmentStore(size_t maxMemoryUsageMB)
boost::posix_time::time_duration Duration_t
Definition: Utils.h:41
boost::posix_time::ptime TimePoint_t
Definition: Utils.h:35
#define end
Definition: vmac.h:38
Container::value_type value_type
void addToStaleEventTimes(const utils::Duration_t)
void release()
Definition: I2OChain.cc:210
const bool getStaleEvent(I2OChain &, utils::Duration_t timeout)
size_t memoryUsed() const
Definition: I2OChain.cc:426
FragKey fragmentKey() const
Definition: I2OChain.cc:278
void resetStaleWindowStartTime()
Definition: I2OChain.cc:314
void markFaulty()
Definition: I2OChain.cc:198