OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
6 #define CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 #include <map> | |
Aaron Boodman
2012/06/11 22:52:13
I think this is no longer needed.
eaugusti
2012/06/12 00:12:57
Done.
| |
11 #include <string> | |
12 | |
13 #include "base/file_path.h" | |
14 #include "base/gtest_prod_util.h" | |
15 #include "base/memory/linked_ptr.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/memory/scoped_vector.h" | |
19 #include "base/time.h" | |
20 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
21 | |
22 namespace performance_monitor { | |
23 | |
24 struct TimeRange { | |
25 TimeRange(); | |
26 TimeRange(base::Time start_time, base::Time end_time); | |
27 ~TimeRange(); | |
28 | |
29 base::Time start; | |
30 base::Time end; | |
31 }; | |
32 | |
33 // The class supporting all performance monitor storage. This class wraps | |
34 // multiple leveldb::DB objects. Most methods must be called on a background | |
35 // thread and are named to reflect that. Callers should use | |
36 // BrowserThread::PostBlockingPoolSequencedTask using | |
37 // performance_monitor::kDBSequenceToken as the sequence token. | |
38 // | |
39 // Different schemas are used for the different leveldb::DB's based off of the | |
40 // structure of the data and the common ways that it will need to be accessed. | |
41 // The following specifies the schema of each type of leveldb::DB. Delimiters | |
42 // are denoted with a '-'. | |
43 // | |
44 // State DB: | |
45 // Stores information about the configuration or 'state' of the browser. Things | |
46 // like browser version go in here. | |
47 // Key: Unique Identifier | |
48 // Value: State Value | |
49 // | |
50 // Active Interval DB: | |
51 // Stores information about when there is data in the database. When the | |
52 // database is constructed, the time is noted as the start of the active | |
53 // interval. Then, every write operation the current time is marked as the end | |
54 // of the current active interval. If the database has no write operations for | |
55 // a certian amount of time, then the database is considered inactive for that | |
56 // time period and a new start time is noted. Having the key be the beginning | |
57 // of the active interval allows for efficient upserts to the current active | |
58 // interval. If the end of the active interval was in the key, then every update | |
59 // to the active interval would have to remove a key and insert a new one. | |
60 // Key: Beginning of ActiveInterval | |
61 // Value: End of ActiveInterval | |
62 // | |
63 // Event DB: | |
64 // Stores all events. A time and type is enough to uniquely identify an event. | |
65 // Using the time that the event took place as the beginning of the key allows | |
66 // us to efficiently answer the question: "What are all the events that took | |
67 // place in this time range?". | |
68 // Key: Time - Type | |
69 // Value: Event in JSON | |
70 // | |
71 // Recent DB: | |
72 // Stores the most recent metric statistics to go into the database. This | |
73 // database becomes useful when it is necessary to find all the active metrics | |
74 // within a timerange. Without it, all the metric databases would need to be | |
75 // searched to see if that metric is active. | |
76 // Key: Time - Metric - Activity | |
77 // Value: Statistic | |
78 // | |
79 // Metric DB: | |
80 // Stores the statistics for different metrics. Having the activity before the | |
81 // time in the key esures that all statistics for a specific acticity are | |
82 // spatially local. | |
83 // Key: Metric - Activity - Time | |
84 // Value: Statistic | |
85 class Database : public base::RefCountedThreadSafe<Database> { | |
86 public: | |
87 // The class that the database will use to infer time. Abstracting out the | |
88 // time mechanism allows for easy testing and mock data insetion. | |
89 class Clock { | |
90 public: | |
91 Clock() {} | |
92 virtual ~Clock() {} | |
93 virtual base::Time GetTime() = 0; | |
94 }; | |
95 | |
96 // A "state" value is anything that can only have one value at a time, and | |
97 // usually describes the state of the browser eg. version. | |
98 bool AddStateValueOnBackgroundThread(const std::string& key, | |
Aaron Boodman
2012/06/11 22:52:13
It looks to me like this class is meant to be used
eaugusti
2012/06/12 00:12:57
Done.
| |
99 const std::string& value); | |
100 | |
101 std::string GetStateValueOnBackgroundThread(const std::string& key); | |
102 | |
103 // Erase everything in the database. Warning - No undo! | |
104 void ClearOnBackgroundThread(); | |
105 | |
106 // Returns the times for which there is data in the database. | |
107 std::vector<TimeRange> GetActiveIntervalOnBackgroundThread( | |
108 const base::Time& start, const base::Time& end); | |
109 | |
110 FilePath path() const { return path_; } | |
111 | |
112 void set_clock(linked_ptr<Clock> clock) { | |
113 clock_ = clock; | |
114 } | |
115 | |
116 private: | |
117 friend class base::RefCountedThreadSafe<Database>; | |
118 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
119 OpenCloseDefaultTest); | |
120 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
121 OpenCloseAlternatePathTest); | |
122 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorDatabaseSetupTest, | |
123 ActiveIntervalTest); | |
124 | |
125 // By default, the database uses a clock that simply returns the current time. | |
126 class SystemClock : public Clock { | |
127 public: | |
128 SystemClock() {} | |
129 virtual ~SystemClock() {} | |
130 virtual base::Time GetTime() OVERRIDE; | |
131 }; | |
132 | |
133 Database(const FilePath& path, linked_ptr<Clock> clock); | |
134 virtual ~Database(); | |
135 | |
136 static scoped_refptr<Database> InitOnBackgroundThread( | |
137 FilePath path, linked_ptr<Clock> clock); | |
Aaron Boodman
2012/06/11 22:52:13
Nit: I think it is cleaner to just let the tests u
eaugusti
2012/06/12 00:12:57
Done.
| |
138 | |
139 static scoped_refptr<Database> InitOnBackgroundThread(FilePath path) { | |
Aaron Boodman
2012/06/11 22:52:13
I think this should become a public static factory
eaugusti
2012/06/12 00:12:57
Done.
| |
140 return InitOnBackgroundThread(path, linked_ptr<Clock>(new SystemClock())); | |
141 } | |
142 | |
143 static scoped_refptr<Database> InitOnBackgroundThread() { | |
144 return InitOnBackgroundThread(FilePath(), | |
145 linked_ptr<Clock>(new SystemClock())); | |
146 } | |
147 | |
148 bool CloseOnBackgroundThread(); | |
149 | |
150 // Mark the database as being active for the current time. | |
151 void UpdateActiveIntervalOnBackgroundThread(); | |
152 | |
153 // The directory where all the databases will reside. | |
154 FilePath path_; | |
155 | |
156 // The key for the beginning of the active interval. | |
157 std::string start_time_key_; | |
158 | |
159 // The last time the database had a transaction. | |
160 base::Time last_update_time_; | |
161 | |
162 linked_ptr<Clock> clock_; | |
Aaron Boodman
2012/06/11 22:52:13
I don't understand why this is not scoped_ptr.
eaugusti
2012/06/12 00:12:57
When testing, the tests want direct access to the
Aaron Boodman
2012/06/12 03:57:59
That's fine, you can just have the Database take o
| |
163 | |
164 linked_ptr<leveldb::DB> recent_db_; | |
Aaron Boodman
2012/06/11 22:52:13
I don't understand why these databases are linked_
eaugusti
2012/06/12 00:12:57
You are right, they should be linked_ptr.
| |
165 | |
166 linked_ptr<leveldb::DB> state_db_; | |
167 | |
168 linked_ptr<leveldb::DB> active_interval_db_; | |
169 | |
170 linked_ptr<leveldb::DB> metric_db_; | |
171 | |
172 leveldb::ReadOptions read_options_; | |
173 leveldb::WriteOptions write_options_; | |
174 | |
175 DISALLOW_COPY_AND_ASSIGN(Database); | |
176 }; | |
177 } // namespace performance_monitor | |
178 | |
179 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_DATABASE_H_ | |
OLD | NEW |