1 // Copyright 2014 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 SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ON_DISK_ATTACHMENT_STORE_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ON_DISK_ATTACHMENT_STORE_H_
8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "sync/api/attachments/attachment.h"
12 #include "sync/api/attachments/attachment_id.h"
13 #include "sync/api/attachments/attachment_store.h"
14 #include "sync/api/attachments/attachment_store_backend.h"
15 #include "sync/base/sync_export.h"
17 namespace attachment_store_pb
{
19 } // namespace attachment_store_pb
22 class SequencedTaskRunner
;
27 } // namespace leveldb
31 // On-disk implementation of AttachmentStore. Stores attachments in leveldb
32 // database in |path| directory.
33 class SYNC_EXPORT OnDiskAttachmentStore
: public AttachmentStoreBackend
,
34 public base::NonThreadSafe
{
36 // Constructs attachment store.
37 OnDiskAttachmentStore(
38 const scoped_refptr
<base::SequencedTaskRunner
>& callback_task_runner
,
39 const base::FilePath
& path
);
40 ~OnDiskAttachmentStore() override
;
42 // AttachmentStoreBackend implementation.
43 void Init(const AttachmentStore::InitCallback
& callback
) override
;
44 void Read(AttachmentStore::Component component
,
45 const AttachmentIdList
& ids
,
46 const AttachmentStore::ReadCallback
& callback
) override
;
47 void Write(AttachmentStore::Component component
,
48 const AttachmentList
& attachments
,
49 const AttachmentStore::WriteCallback
& callback
) override
;
50 void SetReference(AttachmentStore::Component component
,
51 const AttachmentIdList
& ids
) override
;
52 void DropReference(AttachmentStore::Component component
,
53 const AttachmentIdList
& ids
,
54 const AttachmentStore::DropCallback
& callback
) override
;
55 void ReadMetadataById(
56 AttachmentStore::Component component
,
57 const AttachmentIdList
& ids
,
58 const AttachmentStore::ReadMetadataCallback
& callback
) override
;
60 AttachmentStore::Component component
,
61 const AttachmentStore::ReadMetadataCallback
& callback
) override
;
64 friend class OnDiskAttachmentStoreSpecificTest
;
66 // Opens leveldb database at |path|, creating it if needed. In the future
67 // upgrade code will be invoked from OpenOrCreate as well. If open fails
68 // result is UNSPECIFIED_ERROR.
69 AttachmentStore::Result
OpenOrCreate(const base::FilePath
& path
);
70 // Reads single attachment from store. Returns nullptr in case of errors.
71 scoped_ptr
<Attachment
> ReadSingleAttachment(
72 const AttachmentId
& attachment_id
,
73 AttachmentStore::Component component
);
74 // Writes single attachment to store. Returns false in case of errors.
75 bool WriteSingleAttachment(const Attachment
& attachment
,
76 AttachmentStore::Component component
);
77 // Reads single attachment_store_pb::RecordMetadata from levelDB into the
78 // provided buffer. Returns false in case of an error.
79 bool ReadSingleRecordMetadata(
80 const AttachmentId
& attachment_id
,
81 attachment_store_pb::RecordMetadata
* record_metadata
);
82 // Writes single attachment_store_pb::RecordMetadata to levelDB. Returns false
83 // in case of an error.
84 bool WriteSingleRecordMetadata(
85 const AttachmentId
& attachment_id
,
86 const attachment_store_pb::RecordMetadata
& record_metadata
);
88 static std::string
MakeDataKeyFromAttachmentId(
89 const AttachmentId
& attachment_id
);
90 static std::string
MakeMetadataKeyFromAttachmentId(
91 const AttachmentId
& attachment_id
);
92 static AttachmentMetadata
MakeAttachmentMetadata(
93 const AttachmentId
& attachment_id
,
94 const attachment_store_pb::RecordMetadata
& record_metadata
);
96 const base::FilePath path_
;
97 scoped_ptr
<leveldb::DB
> db_
;
99 DISALLOW_COPY_AND_ASSIGN(OnDiskAttachmentStore
);
102 } // namespace syncer
104 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ON_DISK_ATTACHMENT_STORE_H_