MongoDB  2.7.0
progress_meter.h
1 // progress_meter.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "mongo/util/goodies.h"
21 #include <boost/noncopyable.hpp>
22 
23 #include <string>
24 
25 namespace mongo {
26 
27  class ProgressMeter : boost::noncopyable {
28  public:
29  ProgressMeter(unsigned long long total,
30  int secondsBetween = 3,
31  int checkInterval = 100,
32  std::string units = "",
33  std::string name = "Progress")
34  : _showTotal(true),
35  _units(units) {
36  _name = name.c_str();
37  reset( total , secondsBetween , checkInterval );
38  }
39 
40  ProgressMeter() : _active(0), _showTotal(true), _units("") {
41  _name = "Progress";
42  }
43 
44  // typically you do ProgressMeterHolder
45  void reset( unsigned long long total , int secondsBetween = 3 , int checkInterval = 100 );
46 
47  void finished() { _active = 0; }
48  bool isActive() const { return _active; }
49 
54  bool hit( int n = 1 );
55 
56  void setUnits( const std::string& units ) { _units = units; }
57  std::string getUnit() const { return _units; }
58 
59  void setName(std::string name) { _name = name.c_str(); }
60  std::string getName() const { return _name.toString(); }
61 
62  void setTotalWhileRunning( unsigned long long total ) {
63  _total = total;
64  }
65 
66  unsigned long long done() const { return _done; }
67 
68  unsigned long long hits() const { return _hits; }
69 
70  unsigned long long total() const { return _total; }
71 
72  void showTotal(bool doShow) {
73  _showTotal = doShow;
74  }
75 
76  std::string toString() const;
77 
78  bool operator==( const ProgressMeter& other ) const { return this == &other; }
79 
80  private:
81 
82  bool _active;
83 
84  unsigned long long _total;
85  bool _showTotal;
86  int _secondsBetween;
87  int _checkInterval;
88 
89  unsigned long long _done;
90  unsigned long long _hits;
91  int _lastTime;
92 
93  std::string _units;
94  ThreadSafeString _name;
95  };
96 
97  // e.g.:
98  // CurOp * op = cc().curop();
99  // ProgressMeterHolder pm(op->setMessage("index: (1/3) external sort", "Index: External Sort Progress", d->stats.nrecords, 10));
100  // loop { pm.hit(); }
101  class ProgressMeterHolder : boost::noncopyable {
102  public:
104  : _pm( pm ) {
105  }
106 
108  _pm.finished();
109  }
110 
111  ProgressMeter* operator->() { return &_pm; }
112 
113  ProgressMeter* get() { return &_pm; }
114 
115  bool hit( int n = 1 ) { return _pm.hit( n ); }
116 
117  void finished() { _pm.finished(); }
118 
119  bool operator==( const ProgressMeter& other ) { return _pm == other; }
120 
121  private:
122  ProgressMeter& _pm;
123  };
124 
125 
126 }
Definition: progress_meter.h:27
Definition: progress_meter.h:101
bool hit(int n=1)
Definition: progress_meter.cpp:41
this is a thread safe string you will never get a bad pointer, though data may be mungedd ...
Definition: goodies.h:133