source: trunk/lib/StatsCollection.cc @ 1124

Last change on this file since 1124 was 1124, checked in by Peter Johansson, 11 years ago

detecting old cache files and re-writing them in new format (version 8). closes #443 and #289

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