| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 8 #include <string> | 10 #include <string> |
| 9 #include <utility> | 11 #include <utility> |
| 10 | 12 |
| 11 #include "base/bind.h" | 13 #include "base/bind.h" |
| 12 #include "base/callback.h" | 14 #include "base/callback.h" |
| 13 #include "base/location.h" | 15 #include "base/location.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 16 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 17 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" | 18 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" |
| 18 #include "sync/internal_api/public/attachments/attachment_util.h" | 19 #include "sync/internal_api/public/attachments/attachment_util.h" |
| 19 #include "sync/protocol/attachments.pb.h" | 20 #include "sync/protocol/attachments.pb.h" |
| 20 #include "third_party/leveldatabase/env_chromium.h" | 21 #include "third_party/leveldatabase/env_chromium.h" |
| 21 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 22 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 22 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 23 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| 23 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/slice.h" |
| 24 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 UMA_HISTOGRAM_ENUMERATION("Sync.Attachments.StoreInitResult", result_code, | 166 UMA_HISTOGRAM_ENUMERATION("Sync.Attachments.StoreInitResult", result_code, |
| 166 AttachmentStore::RESULT_SIZE); | 167 AttachmentStore::RESULT_SIZE); |
| 167 PostCallback(base::Bind(callback, result_code)); | 168 PostCallback(base::Bind(callback, result_code)); |
| 168 } | 169 } |
| 169 | 170 |
| 170 void OnDiskAttachmentStore::Read( | 171 void OnDiskAttachmentStore::Read( |
| 171 AttachmentStore::Component component, | 172 AttachmentStore::Component component, |
| 172 const AttachmentIdList& ids, | 173 const AttachmentIdList& ids, |
| 173 const AttachmentStore::ReadCallback& callback) { | 174 const AttachmentStore::ReadCallback& callback) { |
| 174 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
| 175 scoped_ptr<AttachmentMap> result_map(new AttachmentMap()); | 176 std::unique_ptr<AttachmentMap> result_map(new AttachmentMap()); |
| 176 scoped_ptr<AttachmentIdList> unavailable_attachments(new AttachmentIdList()); | 177 std::unique_ptr<AttachmentIdList> unavailable_attachments( |
| 178 new AttachmentIdList()); |
| 177 | 179 |
| 178 AttachmentStore::Result result_code = | 180 AttachmentStore::Result result_code = |
| 179 AttachmentStore::STORE_INITIALIZATION_FAILED; | 181 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 180 | 182 |
| 181 if (db_) { | 183 if (db_) { |
| 182 result_code = AttachmentStore::SUCCESS; | 184 result_code = AttachmentStore::SUCCESS; |
| 183 for (const auto& id : ids) { | 185 for (const auto& id : ids) { |
| 184 scoped_ptr<Attachment> attachment; | 186 std::unique_ptr<Attachment> attachment; |
| 185 attachment = ReadSingleAttachment(id, component); | 187 attachment = ReadSingleAttachment(id, component); |
| 186 if (attachment) { | 188 if (attachment) { |
| 187 result_map->insert(std::make_pair(id, *attachment)); | 189 result_map->insert(std::make_pair(id, *attachment)); |
| 188 } else { | 190 } else { |
| 189 unavailable_attachments->push_back(id); | 191 unavailable_attachments->push_back(id); |
| 190 } | 192 } |
| 191 } | 193 } |
| 192 result_code = unavailable_attachments->empty() | 194 result_code = unavailable_attachments->empty() |
| 193 ? AttachmentStore::SUCCESS | 195 ? AttachmentStore::SUCCESS |
| 194 : AttachmentStore::UNSPECIFIED_ERROR; | 196 : AttachmentStore::UNSPECIFIED_ERROR; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 PostCallback(base::Bind(callback, result_code)); | 278 PostCallback(base::Bind(callback, result_code)); |
| 277 } | 279 } |
| 278 | 280 |
| 279 void OnDiskAttachmentStore::ReadMetadataById( | 281 void OnDiskAttachmentStore::ReadMetadataById( |
| 280 AttachmentStore::Component component, | 282 AttachmentStore::Component component, |
| 281 const AttachmentIdList& ids, | 283 const AttachmentIdList& ids, |
| 282 const AttachmentStore::ReadMetadataCallback& callback) { | 284 const AttachmentStore::ReadMetadataCallback& callback) { |
| 283 DCHECK(CalledOnValidThread()); | 285 DCHECK(CalledOnValidThread()); |
| 284 AttachmentStore::Result result_code = | 286 AttachmentStore::Result result_code = |
| 285 AttachmentStore::STORE_INITIALIZATION_FAILED; | 287 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 286 scoped_ptr<AttachmentMetadataList> metadata_list( | 288 std::unique_ptr<AttachmentMetadataList> metadata_list( |
| 287 new AttachmentMetadataList()); | 289 new AttachmentMetadataList()); |
| 288 if (db_) { | 290 if (db_) { |
| 289 result_code = AttachmentStore::SUCCESS; | 291 result_code = AttachmentStore::SUCCESS; |
| 290 for (const auto& id : ids) { | 292 for (const auto& id : ids) { |
| 291 attachment_store_pb::RecordMetadata record_metadata; | 293 attachment_store_pb::RecordMetadata record_metadata; |
| 292 if (!ReadSingleRecordMetadata(id, &record_metadata)) { | 294 if (!ReadSingleRecordMetadata(id, &record_metadata)) { |
| 293 result_code = AttachmentStore::UNSPECIFIED_ERROR; | 295 result_code = AttachmentStore::UNSPECIFIED_ERROR; |
| 294 continue; | 296 continue; |
| 295 } | 297 } |
| 296 if (!AttachmentHasReferenceFromComponent(record_metadata, | 298 if (!AttachmentHasReferenceFromComponent(record_metadata, |
| 297 ComponentToProto(component))) { | 299 ComponentToProto(component))) { |
| 298 result_code = AttachmentStore::UNSPECIFIED_ERROR; | 300 result_code = AttachmentStore::UNSPECIFIED_ERROR; |
| 299 continue; | 301 continue; |
| 300 } | 302 } |
| 301 metadata_list->push_back(MakeAttachmentMetadata(id, record_metadata)); | 303 metadata_list->push_back(MakeAttachmentMetadata(id, record_metadata)); |
| 302 } | 304 } |
| 303 } | 305 } |
| 304 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); | 306 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); |
| 305 } | 307 } |
| 306 | 308 |
| 307 void OnDiskAttachmentStore::ReadMetadata( | 309 void OnDiskAttachmentStore::ReadMetadata( |
| 308 AttachmentStore::Component component, | 310 AttachmentStore::Component component, |
| 309 const AttachmentStore::ReadMetadataCallback& callback) { | 311 const AttachmentStore::ReadMetadataCallback& callback) { |
| 310 DCHECK(CalledOnValidThread()); | 312 DCHECK(CalledOnValidThread()); |
| 311 AttachmentStore::Result result_code = | 313 AttachmentStore::Result result_code = |
| 312 AttachmentStore::STORE_INITIALIZATION_FAILED; | 314 AttachmentStore::STORE_INITIALIZATION_FAILED; |
| 313 scoped_ptr<AttachmentMetadataList> metadata_list( | 315 std::unique_ptr<AttachmentMetadataList> metadata_list( |
| 314 new AttachmentMetadataList()); | 316 new AttachmentMetadataList()); |
| 315 | 317 |
| 316 if (db_) { | 318 if (db_) { |
| 317 attachment_store_pb::RecordMetadata::Component proto_component = | 319 attachment_store_pb::RecordMetadata::Component proto_component = |
| 318 ComponentToProto(component); | 320 ComponentToProto(component); |
| 319 result_code = AttachmentStore::SUCCESS; | 321 result_code = AttachmentStore::SUCCESS; |
| 320 scoped_ptr<leveldb::Iterator> db_iterator( | 322 std::unique_ptr<leveldb::Iterator> db_iterator( |
| 321 db_->NewIterator(MakeNonCachingReadOptions())); | 323 db_->NewIterator(MakeNonCachingReadOptions())); |
| 322 DCHECK(db_iterator); | 324 DCHECK(db_iterator); |
| 323 for (db_iterator->Seek(kMetadataPrefix); db_iterator->Valid(); | 325 for (db_iterator->Seek(kMetadataPrefix); db_iterator->Valid(); |
| 324 db_iterator->Next()) { | 326 db_iterator->Next()) { |
| 325 leveldb::Slice key = db_iterator->key(); | 327 leveldb::Slice key = db_iterator->key(); |
| 326 if (!key.starts_with(kMetadataPrefix)) { | 328 if (!key.starts_with(kMetadataPrefix)) { |
| 327 break; | 329 break; |
| 328 } | 330 } |
| 329 // Make AttachmentId from levelDB key. | 331 // Make AttachmentId from levelDB key. |
| 330 key.remove_prefix(strlen(kMetadataPrefix)); | 332 key.remove_prefix(strlen(kMetadataPrefix)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 352 | 354 |
| 353 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); | 355 PostCallback(base::Bind(callback, result_code, base::Passed(&metadata_list))); |
| 354 } | 356 } |
| 355 | 357 |
| 356 AttachmentStore::Result OnDiskAttachmentStore::OpenOrCreate( | 358 AttachmentStore::Result OnDiskAttachmentStore::OpenOrCreate( |
| 357 const base::FilePath& path) { | 359 const base::FilePath& path) { |
| 358 DCHECK(!db_); | 360 DCHECK(!db_); |
| 359 base::FilePath leveldb_path = path.Append(kLeveldbDirectory); | 361 base::FilePath leveldb_path = path.Append(kLeveldbDirectory); |
| 360 | 362 |
| 361 leveldb::DB* db_raw; | 363 leveldb::DB* db_raw; |
| 362 scoped_ptr<leveldb::DB> db; | 364 std::unique_ptr<leveldb::DB> db; |
| 363 leveldb::Options options; | 365 leveldb::Options options; |
| 364 options.create_if_missing = true; | 366 options.create_if_missing = true; |
| 365 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 367 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 366 // TODO(pavely): crbug/424287 Consider adding info_log, block_cache and | 368 // TODO(pavely): crbug/424287 Consider adding info_log, block_cache and |
| 367 // filter_policy to options. | 369 // filter_policy to options. |
| 368 leveldb::Status status = | 370 leveldb::Status status = |
| 369 leveldb::DB::Open(options, leveldb_path.AsUTF8Unsafe(), &db_raw); | 371 leveldb::DB::Open(options, leveldb_path.AsUTF8Unsafe(), &db_raw); |
| 370 if (!status.ok()) { | 372 if (!status.ok()) { |
| 371 DVLOG(1) << "DB::Open failed: status=" << status.ToString() | 373 DVLOG(1) << "DB::Open failed: status=" << status.ToString() |
| 372 << ", path=" << path.AsUTF8Unsafe(); | 374 << ", path=" << path.AsUTF8Unsafe(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 396 | 398 |
| 397 if (metadata.schema_version() != kCurrentSchemaVersion) { | 399 if (metadata.schema_version() != kCurrentSchemaVersion) { |
| 398 DVLOG(1) << "Unknown schema version: " << metadata.schema_version(); | 400 DVLOG(1) << "Unknown schema version: " << metadata.schema_version(); |
| 399 return AttachmentStore::UNSPECIFIED_ERROR; | 401 return AttachmentStore::UNSPECIFIED_ERROR; |
| 400 } | 402 } |
| 401 | 403 |
| 402 db_ = std::move(db); | 404 db_ = std::move(db); |
| 403 return AttachmentStore::SUCCESS; | 405 return AttachmentStore::SUCCESS; |
| 404 } | 406 } |
| 405 | 407 |
| 406 scoped_ptr<Attachment> OnDiskAttachmentStore::ReadSingleAttachment( | 408 std::unique_ptr<Attachment> OnDiskAttachmentStore::ReadSingleAttachment( |
| 407 const AttachmentId& attachment_id, | 409 const AttachmentId& attachment_id, |
| 408 AttachmentStore::Component component) { | 410 AttachmentStore::Component component) { |
| 409 scoped_ptr<Attachment> attachment; | 411 std::unique_ptr<Attachment> attachment; |
| 410 attachment_store_pb::RecordMetadata record_metadata; | 412 attachment_store_pb::RecordMetadata record_metadata; |
| 411 if (!ReadSingleRecordMetadata(attachment_id, &record_metadata)) { | 413 if (!ReadSingleRecordMetadata(attachment_id, &record_metadata)) { |
| 412 return attachment; | 414 return attachment; |
| 413 } | 415 } |
| 414 if (!AttachmentHasReferenceFromComponent(record_metadata, | 416 if (!AttachmentHasReferenceFromComponent(record_metadata, |
| 415 ComponentToProto(component))) | 417 ComponentToProto(component))) |
| 416 return attachment; | 418 return attachment; |
| 417 | 419 |
| 418 const std::string key = MakeDataKeyFromAttachmentId(attachment_id); | 420 const std::string key = MakeDataKeyFromAttachmentId(attachment_id); |
| 419 std::string data_str; | 421 std::string data_str; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 return key; | 534 return key; |
| 533 } | 535 } |
| 534 | 536 |
| 535 AttachmentMetadata OnDiskAttachmentStore::MakeAttachmentMetadata( | 537 AttachmentMetadata OnDiskAttachmentStore::MakeAttachmentMetadata( |
| 536 const AttachmentId& attachment_id, | 538 const AttachmentId& attachment_id, |
| 537 const attachment_store_pb::RecordMetadata& record_metadata) { | 539 const attachment_store_pb::RecordMetadata& record_metadata) { |
| 538 return AttachmentMetadata(attachment_id, record_metadata.attachment_size()); | 540 return AttachmentMetadata(attachment_id, record_metadata.attachment_size()); |
| 539 } | 541 } |
| 540 | 542 |
| 541 } // namespace syncer | 543 } // namespace syncer |
| OLD | NEW |