tile_store.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "components/query_tiles/internal/tile_store.h"
  5. #include <utility>
  6. #include "components/query_tiles/internal/proto_conversion.h"
  7. namespace leveldb_proto {
  8. void DataToProto(query_tiles::TileGroup* data,
  9. query_tiles::proto::TileGroup* proto) {
  10. TileGroupToProto(data, proto);
  11. }
  12. void ProtoToData(query_tiles::proto::TileGroup* proto,
  13. query_tiles::TileGroup* data) {
  14. TileGroupFromProto(proto, data);
  15. }
  16. } // namespace leveldb_proto
  17. namespace query_tiles {
  18. TileStore::TileStore(TileProtoDb db) : db_(std::move(db)) {}
  19. TileStore::~TileStore() = default;
  20. void TileStore::InitAndLoad(LoadCallback callback) {
  21. db_->Init(base::BindOnce(&TileStore::OnDbInitialized,
  22. weak_ptr_factory_.GetWeakPtr(),
  23. std::move(callback)));
  24. }
  25. void TileStore::Update(const std::string& key,
  26. const TileGroup& group,
  27. UpdateCallback callback) {
  28. auto entries_to_save = std::make_unique<KeyEntryVector>();
  29. TileGroup entry_to_save = group;
  30. entries_to_save->emplace_back(key, std::move(entry_to_save));
  31. db_->UpdateEntries(std::move(entries_to_save),
  32. std::make_unique<KeyVector>() /*keys_to_remove*/,
  33. std::move(callback));
  34. }
  35. void TileStore::Delete(const std::string& key, DeleteCallback callback) {
  36. auto keys_to_delete = std::make_unique<KeyVector>();
  37. keys_to_delete->emplace_back(key);
  38. db_->UpdateEntries(std::make_unique<KeyEntryVector>() /*entries_to_save*/,
  39. std::move(keys_to_delete), std::move(callback));
  40. }
  41. void TileStore::OnDbInitialized(LoadCallback callback,
  42. leveldb_proto::Enums::InitStatus status) {
  43. if (status != leveldb_proto::Enums::InitStatus::kOK) {
  44. std::move(callback).Run(false, KeysAndEntries());
  45. return;
  46. }
  47. db_->LoadKeysAndEntries(base::BindOnce(&TileStore::OnDataLoaded,
  48. weak_ptr_factory_.GetWeakPtr(),
  49. std::move(callback)));
  50. }
  51. void TileStore::OnDataLoaded(
  52. LoadCallback callback,
  53. bool success,
  54. std::unique_ptr<std::map<std::string, TileGroup>> loaded_keys_and_entries) {
  55. if (!success || !loaded_keys_and_entries) {
  56. std::move(callback).Run(success, KeysAndEntries());
  57. return;
  58. }
  59. KeysAndEntries keys_and_entries;
  60. for (auto& it : *loaded_keys_and_entries) {
  61. std::unique_ptr<TileGroup> group =
  62. std::make_unique<TileGroup>(std::move(it.second));
  63. keys_and_entries.emplace(it.first, std::move(group));
  64. }
  65. std::move(callback).Run(true, std::move(keys_and_entries));
  66. }
  67. } // namespace query_tiles