source: trunk/lib/StatsCollection.cc @ 834

Last change on this file since 834 was 803, checked in by Peter Johansson, 12 years ago

fixes #338 - partial cache now works

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: StatsCollection.cc 803 2009-07-10 23:23:45Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://dev.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "StatsCollection.h"
23
24#include "AddStats.h"
25#include "BlameStats.h"
26#include "ClassicStats.h"
27#include "Stats.h"
28
29#include <cassert>
30#include <map>
31#include <stdexcept>
32#include <string>
33
34#include <iostream>
35
36namespace theplu{
37namespace svndigest{
38
39
40  StatsCollection::StatsCollection(const std::string& path)
41    : path_(path)
42  {
43    stats_["classic"] = new ClassicStats(path);
44    stats_["blame"] = new BlameStats(path);
45    stats_["add"] = new AddStats(path);
46  }
47
48
49  StatsCollection::~StatsCollection(void)
50  {
51    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
52      assert(i->second);
53      delete i->second;
54    }
55
56  }
57
58
59  bool StatsCollection::load_cache(std::istream& is)
60  {
61    bool result = true;
62    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
63      svn_revnum_t cache_rev = i->second->load_cache(is);
64      if (cache_rev < i->second->last_changed_rev()) {
65        result = false;
66        // reset if load cache failed
67        if (!cache_rev)
68          i->second->reset();
69        i->second->parse(path_, cache_rev+1);
70      }
71    }
72    return result;
73  }
74
75
76  const std::map<std::string, Stats*>& StatsCollection::stats(void) const
77  {
78    return stats_;
79  }
80
81
82  void StatsCollection::parse(const std::string& path)
83  {
84    for (map::iterator i(stats_.begin()); i!=stats_.end(); ++i) {
85      i->second->parse(path);
86    }
87  }
88
89
90  void StatsCollection::print(std::ostream& os)
91  {
92    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
93      i->second->print(os);
94    }
95  }
96
97
98  void StatsCollection::reset(void)
99  {
100    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
101      i->second->reset();
102    }
103  }
104
105
106  const StatsCollection& StatsCollection::operator+=(const StatsCollection& rhs)
107  {
108    assert(stats_.size()==rhs.stats_.size());
109    for (map::const_iterator i(stats_.begin()); i!=stats_.end(); ++i) {
110      *(i->second) += rhs[i->first];
111    }
112    return *this;
113  }
114
115
116  const Stats& StatsCollection::operator[](const std::string& key) const
117  {
118    map::const_iterator iter = stats_.find(key);
119    if (iter==stats_.end())
120      throw std::runtime_error(key + 
121                               std::string(" not found in StatsCollection"));
122    return *(iter->second);
123  }
124
125
126  Stats& StatsCollection::operator[](const std::string& key)
127  {
128    assert(stats_.find(key)!=stats_.end());
129    return *(stats_[key]);
130  }
131
132
133}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.