proto_table_manager.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2020 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. #ifndef COMPONENTS_SQLITE_PROTO_PROTO_TABLE_MANAGER_H_
  5. #define COMPONENTS_SQLITE_PROTO_PROTO_TABLE_MANAGER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/containers/span.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "components/sqlite_proto/table_manager.h"
  13. namespace sqlite_proto {
  14. // ProtoTableManager is the instance of TableManager intended to be used
  15. // in the common case of storing string-to-proto tables (alongside
  16. // KeyValueData<T> and KeyValueTable<T>).
  17. //
  18. // End-to-end use:
  19. // 1. Initialize a database with sql::Database::Open.
  20. // 2. Initialize a ProtoTableManager on top of this database.
  21. // 3. For each table, construct a pair of KeyValueTable and KeyValueData
  22. // on the main sequence; initialize the KeyValueData on the database sequence.
  23. // 4. Now, initialization is finished and the KeyValueData objects can be
  24. // used (on the main thread) for get/put/delete operations against the database.
  25. //
  26. // TODO(crbug.com/1046825): This interface is a bit complex and could do with
  27. // a refactor.
  28. class ProtoTableManager : public TableManager {
  29. public:
  30. // Constructor. |db_task_runner| must run tasks on the
  31. // sequence that subsequently calls |InitializeOnDbSequence|.
  32. explicit ProtoTableManager(
  33. scoped_refptr<base::SequencedTaskRunner> db_task_runner);
  34. // Initialization:
  35. // - |table_names| specifies the names of the tables to initialize.
  36. // These must be unique.
  37. // - |db| must be null or point to an open sql::Database.
  38. // - |schema_version|, which must be positive, will be compared to the version
  39. // stored on disk if any; in case of a mismatch (either there's no version
  40. // stored, or |schema_version| != the stored version), clears and
  41. // reinitializes the tables and writes the new version. It generally makes
  42. // sense to change the version whenever the database's layout changes in a
  43. // backwards-incompatible manner, for instance when removing or renaming
  44. // tables or columns.
  45. //
  46. // If |db| is null or initialization fails, subsequent calls to
  47. // ExecuteTaskOnDBThread will silently no-op. (When this class provides the
  48. // backing store for a collection of KeyValueData/KeyValueTable, this leads to
  49. // reasonable fallback behavior of the KeyValueData objects caching data in
  50. // memory.)
  51. void InitializeOnDbSequence(sql::Database* db,
  52. base::span<const std::string> table_names,
  53. int schema_version);
  54. protected:
  55. ~ProtoTableManager() override;
  56. private:
  57. // TableManager implementation.
  58. void CreateOrClearTablesIfNecessary() override;
  59. // Currently unused.
  60. void LogDatabaseStats() override {}
  61. std::vector<std::string> table_names_;
  62. int schema_version_;
  63. };
  64. } // namespace sqlite_proto
  65. #endif // COMPONENTS_SQLITE_PROTO_PROTO_TABLE_MANAGER_H_