model_type_store_base.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2018 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_SYNC_MODEL_MODEL_TYPE_STORE_BASE_H_
  5. #define COMPONENTS_SYNC_MODEL_MODEL_TYPE_STORE_BASE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "components/sync/model/metadata_change_list.h"
  10. namespace syncer {
  11. // Base class for leveldb-based storage layers.
  12. class ModelTypeStoreBase {
  13. public:
  14. // Output of read operations is passed back as list of Record structures.
  15. struct Record {
  16. Record(const std::string& id, const std::string& value)
  17. : id(id), value(value) {}
  18. std::string id;
  19. std::string value;
  20. };
  21. // WriteBatch object is used in all modification operations.
  22. class WriteBatch {
  23. public:
  24. // Creates a MetadataChangeList that will accumulate metadata changes and
  25. // can later be passed to a WriteBatch via TransferChanges. Use this when
  26. // you need a MetadataChangeList and do not have a WriteBatch in scope.
  27. static std::unique_ptr<MetadataChangeList> CreateMetadataChangeList();
  28. WriteBatch();
  29. WriteBatch(const WriteBatch&) = delete;
  30. WriteBatch& operator=(const WriteBatch&) = delete;
  31. virtual ~WriteBatch();
  32. // Write the given |value| for data with |id|.
  33. virtual void WriteData(const std::string& id, const std::string& value) = 0;
  34. // Delete the record for data with |id|.
  35. virtual void DeleteData(const std::string& id) = 0;
  36. // Provides access to a MetadataChangeList that will pass its changes
  37. // directly into this WriteBatch.
  38. virtual MetadataChangeList* GetMetadataChangeList() = 0;
  39. // Transfers the changes from a MetadataChangeList into this WriteBatch.
  40. // |mcl| must have previously been created by CreateMetadataChangeList().
  41. // TODO(mastiz): Revisit whether the last requirement above can be removed
  42. // and make this API more type-safe.
  43. void TakeMetadataChangesFrom(std::unique_ptr<MetadataChangeList> mcl);
  44. };
  45. using RecordList = std::vector<Record>;
  46. using IdList = std::vector<std::string>;
  47. ModelTypeStoreBase(const ModelTypeStoreBase&) = delete;
  48. ModelTypeStoreBase& operator=(const ModelTypeStoreBase&) = delete;
  49. protected:
  50. ModelTypeStoreBase();
  51. virtual ~ModelTypeStoreBase();
  52. };
  53. } // namespace syncer
  54. #endif // COMPONENTS_SYNC_MODEL_MODEL_TYPE_STORE_BASE_H_