OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" | 5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
12 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" | |
12 #include "sync/protocol/attachments.pb.h" | 13 #include "sync/protocol/attachments.pb.h" |
13 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 14 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
14 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 15 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
15 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
16 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
17 | 18 |
18 namespace syncer { | 19 namespace syncer { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Prefix for records containing attachment data. | 23 // Prefix for records containing attachment data. |
23 const char kDataPrefix[] = "data-"; | 24 const char kDataPrefix[] = "data-"; |
24 | 25 |
26 const char kDatabaseMetadataKey[] = "database-metadata"; | |
27 | |
28 const int32 kCurrentSchemaVersion = 1; | |
29 | |
25 const base::FilePath::CharType kLeveldbDirectory[] = | 30 const base::FilePath::CharType kLeveldbDirectory[] = |
26 FILE_PATH_LITERAL("leveldb"); | 31 FILE_PATH_LITERAL("leveldb"); |
27 } // namespace | 32 } // namespace |
28 | 33 |
29 OnDiskAttachmentStore::OnDiskAttachmentStore( | 34 OnDiskAttachmentStore::OnDiskAttachmentStore( |
30 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) | 35 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) |
31 : callback_task_runner_(callback_task_runner) { | 36 : callback_task_runner_(callback_task_runner) { |
32 } | 37 } |
33 | 38 |
34 OnDiskAttachmentStore::~OnDiskAttachmentStore() { | 39 OnDiskAttachmentStore::~OnDiskAttachmentStore() { |
35 } | 40 } |
36 | 41 |
37 void OnDiskAttachmentStore::Read(const AttachmentIdList& ids, | 42 void OnDiskAttachmentStore::Read(const AttachmentIdList& ids, |
38 const ReadCallback& callback) { | 43 const ReadCallback& callback) { |
39 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
40 DCHECK(db_); | 45 DCHECK(db_); |
41 scoped_ptr<AttachmentMap> result_map(new AttachmentMap()); | 46 scoped_ptr<AttachmentMap> result_map(new AttachmentMap()); |
42 scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList()); | 47 scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList()); |
43 | 48 |
44 leveldb::ReadOptions read_options; | 49 leveldb::ReadOptions read_options; |
45 // Attachment content is typically large and only read once. Don't cache it on | 50 // Attachment content is typically large and only read once. Don't cache it on |
46 // db level. | 51 // db level. |
47 read_options.fill_cache = false; | 52 read_options.fill_cache = false; |
48 read_options.verify_checksums = true; | 53 read_options.verify_checksums = true; |
49 | 54 |
50 AttachmentIdList::const_iterator iter = ids.begin(); | 55 AttachmentIdList::const_iterator iter = ids.begin(); |
51 const AttachmentIdList::const_iterator end = ids.end(); | 56 const AttachmentIdList::const_iterator end = ids.end(); |
52 for (; iter != end; ++iter) { | 57 for (; iter != end; ++iter) { |
53 const std::string key = CreateDataKeyFromAttachmentId(*iter); | 58 const std::string key = MakeDataKeyFromAttachmentId(*iter); |
54 std::string data_str; | 59 std::string data_str; |
55 leveldb::Status status = db_->Get(read_options, key, &data_str); | 60 leveldb::Status status = db_->Get(read_options, key, &data_str); |
56 if (!status.ok()) { | 61 if (!status.ok()) { |
57 DVLOG(1) << "DB::Get failed: status=" << status.ToString(); | 62 DVLOG(1) << "DB::Get failed: status=" << status.ToString(); |
58 unavailable_attachments->push_back(*iter); | 63 unavailable_attachments->push_back(*iter); |
59 continue; | 64 continue; |
60 } | 65 } |
61 scoped_refptr<base::RefCountedMemory> data = | 66 scoped_refptr<base::RefCountedMemory> data = |
62 base::RefCountedString::TakeString(&data_str); | 67 base::RefCountedString::TakeString(&data_str); |
63 Attachment attachment = Attachment::CreateWithId(*iter, data); | 68 Attachment attachment = Attachment::CreateWithId(*iter, data); |
(...skipping 19 matching lines...) Expand all Loading... | |
83 leveldb::ReadOptions read_options; | 88 leveldb::ReadOptions read_options; |
84 read_options.fill_cache = false; | 89 read_options.fill_cache = false; |
85 read_options.verify_checksums = true; | 90 read_options.verify_checksums = true; |
86 | 91 |
87 leveldb::WriteOptions write_options; | 92 leveldb::WriteOptions write_options; |
88 write_options.sync = true; | 93 write_options.sync = true; |
89 | 94 |
90 AttachmentList::const_iterator iter = attachments.begin(); | 95 AttachmentList::const_iterator iter = attachments.begin(); |
91 const AttachmentList::const_iterator end = attachments.end(); | 96 const AttachmentList::const_iterator end = attachments.end(); |
92 for (; iter != end; ++iter) { | 97 for (; iter != end; ++iter) { |
93 const std::string key = CreateDataKeyFromAttachmentId(iter->GetId()); | 98 const std::string key = MakeDataKeyFromAttachmentId(iter->GetId()); |
94 | 99 |
95 std::string data_str; | 100 std::string data_str; |
96 // TODO(pavely): crbug/424304 This read is expensive. When I add metadata | 101 // TODO(pavely): crbug/424304 This read is expensive. When I add metadata |
97 // records this read will target metadata record instead of payload record. | 102 // records this read will target metadata record instead of payload record. |
98 leveldb::Status status = db_->Get(read_options, key, &data_str); | 103 leveldb::Status status = db_->Get(read_options, key, &data_str); |
99 if (status.ok()) { | 104 if (status.ok()) { |
100 // Entry exists, don't overwrite. | 105 // Entry exists, don't overwrite. |
101 continue; | 106 continue; |
102 } else if (!status.IsNotFound()) { | 107 } else if (!status.IsNotFound()) { |
103 // Entry exists but failed to read. | 108 // Entry exists but failed to read. |
(...skipping 18 matching lines...) Expand all Loading... | |
122 void OnDiskAttachmentStore::Drop(const AttachmentIdList& ids, | 127 void OnDiskAttachmentStore::Drop(const AttachmentIdList& ids, |
123 const DropCallback& callback) { | 128 const DropCallback& callback) { |
124 DCHECK(CalledOnValidThread()); | 129 DCHECK(CalledOnValidThread()); |
125 DCHECK(db_); | 130 DCHECK(db_); |
126 Result result_code = SUCCESS; | 131 Result result_code = SUCCESS; |
127 leveldb::WriteOptions write_options; | 132 leveldb::WriteOptions write_options; |
128 write_options.sync = true; | 133 write_options.sync = true; |
129 AttachmentIdList::const_iterator iter = ids.begin(); | 134 AttachmentIdList::const_iterator iter = ids.begin(); |
130 const AttachmentIdList::const_iterator end = ids.end(); | 135 const AttachmentIdList::const_iterator end = ids.end(); |
131 for (; iter != end; ++iter) { | 136 for (; iter != end; ++iter) { |
132 const std::string key = CreateDataKeyFromAttachmentId(*iter); | 137 const std::string key = MakeDataKeyFromAttachmentId(*iter); |
133 leveldb::Status status = db_->Delete(write_options, key); | 138 leveldb::Status status = db_->Delete(write_options, key); |
134 if (!status.ok()) { | 139 if (!status.ok()) { |
135 // DB::Delete doesn't check if record exists, it returns ok just like | 140 // DB::Delete doesn't check if record exists, it returns ok just like |
136 // AttachmentStore::Drop should. | 141 // AttachmentStore::Drop should. |
137 DVLOG(1) << "DB::Delete failed: status=" << status.ToString(); | 142 DVLOG(1) << "DB::Delete failed: status=" << status.ToString(); |
138 result_code = UNSPECIFIED_ERROR; | 143 result_code = UNSPECIFIED_ERROR; |
139 } | 144 } |
140 } | 145 } |
141 callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result_code)); | 146 callback_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result_code)); |
142 } | 147 } |
143 | 148 |
144 AttachmentStore::Result OnDiskAttachmentStore::OpenOrCreate( | 149 AttachmentStore::Result OnDiskAttachmentStore::OpenOrCreate( |
145 const base::FilePath& path) { | 150 const base::FilePath& path) { |
146 DCHECK(CalledOnValidThread()); | 151 DCHECK(CalledOnValidThread()); |
147 DCHECK(!db_); | 152 DCHECK(!db_); |
148 Result result_code = UNSPECIFIED_ERROR; | |
149 base::FilePath leveldb_path = path.Append(kLeveldbDirectory); | 153 base::FilePath leveldb_path = path.Append(kLeveldbDirectory); |
150 | 154 |
151 leveldb::DB* db; | 155 leveldb::DB* db_raw; |
156 scoped_ptr<leveldb::DB> db; | |
152 leveldb::Options options; | 157 leveldb::Options options; |
153 options.create_if_missing = true; | 158 options.create_if_missing = true; |
154 // TODO(pavely): crbug/424287 Consider adding info_log, block_cache and | 159 // TODO(pavely): crbug/424287 Consider adding info_log, block_cache and |
155 // filter_policy to options. | 160 // filter_policy to options. |
156 leveldb::Status status = | 161 leveldb::Status status = |
157 leveldb::DB::Open(options, leveldb_path.AsUTF8Unsafe(), &db); | 162 leveldb::DB::Open(options, leveldb_path.AsUTF8Unsafe(), &db_raw); |
158 if (!status.ok()) { | 163 if (!status.ok()) { |
159 DVLOG(1) << "DB::Open failed: status=" << status.ToString() | 164 DVLOG(1) << "DB::Open failed: status=" << status.ToString() |
160 << ", path=" << path.AsUTF8Unsafe(); | 165 << ", path=" << path.AsUTF8Unsafe(); |
161 } else { | 166 return UNSPECIFIED_ERROR; |
162 db_.reset(db); | |
163 result_code = SUCCESS; | |
164 } | 167 } |
165 return result_code; | 168 |
169 db.reset(db_raw); | |
170 | |
171 attachment_store_pb::AttachmentStoreMetadata metadata; | |
172 status = ReadStoreMetadata(db.get(), &metadata); | |
173 if (!status.ok() && !status.IsNotFound()) { | |
174 DVLOG(1) << "ReadStoreMetadata failed: status=" << status.ToString(); | |
175 return UNSPECIFIED_ERROR; | |
176 } | |
177 if (status.IsNotFound()) { | |
178 // Brand new database. | |
179 metadata.set_schema_version(kCurrentSchemaVersion); | |
180 status = UpdateStoreMetadata(db.get(), metadata); | |
181 if (!status.ok()) { | |
182 DVLOG(1) << "UpdateStoreMetadata failed: status=" << status.ToString(); | |
183 return UNSPECIFIED_ERROR; | |
184 } | |
185 } | |
186 DCHECK(status.ok()); | |
187 | |
188 // Upgrade code goes here. | |
189 | |
190 if (metadata.schema_version() != kCurrentSchemaVersion) { | |
191 DVLOG(1) << "Unknown schema version: " << metadata.schema_version(); | |
192 return UNSPECIFIED_ERROR; | |
193 } | |
194 | |
195 db_ = db.Pass(); | |
196 return SUCCESS; | |
166 } | 197 } |
167 | 198 |
168 std::string OnDiskAttachmentStore::CreateDataKeyFromAttachmentId( | 199 leveldb::Status OnDiskAttachmentStore::ReadStoreMetadata( |
200 leveldb::DB* db, | |
201 attachment_store_pb::AttachmentStoreMetadata* metadata) { | |
202 std::string data_str; | |
203 leveldb::ReadOptions read_options; | |
maniscalco
2014/10/21 21:41:03
Since we do this in a couple places, consider crea
pavely
2014/10/24 20:42:55
I've created function for WriteOptions.
I want to
maniscalco
2014/10/24 21:52:39
SGTM
| |
204 read_options.fill_cache = false; | |
205 read_options.verify_checksums = true; | |
206 | |
207 leveldb::Status status = | |
208 db->Get(read_options, kDatabaseMetadataKey, &data_str); | |
209 if (!status.ok()) | |
210 return status; | |
211 if (!metadata->ParseFromString(data_str)) | |
212 return leveldb::Status::Corruption("Metadata record corruption"); | |
213 return leveldb::Status::OK(); | |
214 } | |
215 | |
216 leveldb::Status OnDiskAttachmentStore::UpdateStoreMetadata( | |
maniscalco
2014/10/21 21:41:03
Light suggestion: since this function replaces the
pavely
2014/10/24 20:42:55
Done.
| |
217 leveldb::DB* db, | |
218 const attachment_store_pb::AttachmentStoreMetadata& metadata) { | |
219 std::string data_str; | |
220 leveldb::WriteOptions write_options; | |
221 write_options.sync = true; | |
222 | |
223 metadata.SerializeToString(&data_str); | |
224 return db->Put(write_options, kDatabaseMetadataKey, data_str); | |
225 } | |
226 | |
227 std::string OnDiskAttachmentStore::MakeDataKeyFromAttachmentId( | |
169 const AttachmentId& attachment_id) { | 228 const AttachmentId& attachment_id) { |
170 std::string key = kDataPrefix + attachment_id.GetProto().unique_id(); | 229 std::string key = kDataPrefix + attachment_id.GetProto().unique_id(); |
171 return key; | 230 return key; |
172 } | 231 } |
173 | 232 |
174 } // namespace syncer | 233 } // namespace syncer |
OLD | NEW |