Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / database_string_table.h
blob6c2304039965282870851f398bc9517dad4d61b3
1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_DATABASE_STRING_TABLE_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_DATABASE_STRING_TABLE_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
14 namespace sql {
15 class Connection;
16 } // namespace sql
18 namespace extensions {
20 // A class for maintaining a persistent mapping between strings and integers.
21 // This is used to help compress the contents of the activity log database on
22 // disk by replacing repeated strings by smaller integers.
24 // The mapping from integers to strings is maintained in a database table, but
25 // the mapping is also cached in memory.
27 // The database table used to store the strings is configurable, but its layout
28 // is fixed: it always consists of just two columns, "id" and "value".
30 // All calls to DatabaseStringTable must occur on the database thread.
31 class DatabaseStringTable {
32 public:
33 explicit DatabaseStringTable(const std::string& table);
35 ~DatabaseStringTable();
37 // Initialize the database table. This will create the table if it does not
38 // exist. Returns true on success; false on error.
39 bool Initialize(sql::Connection* connection);
41 // Interns a string in the database table and sets *id to the corresponding
42 // integer. If the string already exists, the existing number is returned;
43 // otherwise, new database row is inserted with the new string. Returns true
44 // on success and false on database error.
45 bool StringToInt(sql::Connection* connection,
46 const std::string& value,
47 int64* id);
49 // Looks up an integer value and converts it to a string (which is stored in
50 // *value). Returns true on success. A false return does not necessarily
51 // indicate a database error; it might simply be that the value cannot be
52 // found.
53 bool IntToString(sql::Connection* connection, int64 id, std::string* value);
55 // Clears the in-memory cache; this should be called if the underlying
56 // database table has been manipulated and the cache may be stale.
57 void ClearCache();
59 private:
60 // Reduces the size of the cache if too many entries are held in it.
61 void PruneCache();
63 // In-memory caches of recently accessed values.
64 std::map<int64, std::string> id_to_value_;
65 std::map<std::string, int64> value_to_id_;
67 // The name of the database table where the mapping is stored.
68 std::string table_;
70 FRIEND_TEST_ALL_PREFIXES(DatabaseStringTableTest, Prune);
72 DISALLOW_COPY_AND_ASSIGN(DatabaseStringTable);
75 } // namespace extensions
77 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_DATABASE_STRING_TABLE_H_