Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: storage/browser/blob/internal_blob_data.cc

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Finished comments, added new pending enum state Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "storage/browser/blob/internal_blob_data.h" 5 #include "storage/browser/blob/internal_blob_data.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/containers/hash_tables.h" 12 #include "base/containers/hash_tables.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "storage/browser/blob/blob_data_item.h" 14 #include "storage/browser/blob/blob_data_item.h"
15 #include "storage/browser/blob/shareable_blob_data_item.h"
15 #include "storage/common/data_element.h" 16 #include "storage/common/data_element.h"
16 17
17 namespace storage { 18 namespace storage {
19 namespace {
20 bool IsBytes(DataElement::Type type) {
21 return type == DataElement::TYPE_BYTES ||
22 type == DataElement::TYPE_BYTES_DESCRIPTION;
23 }
24 } // namespace
18 25
19 InternalBlobData::Builder::Builder() : data_(new InternalBlobData()) { 26 InternalBlobData::InternalBlobData() {}
20 }
21 InternalBlobData::Builder::~Builder() {
22 }
23 27
24 void InternalBlobData::Builder::AppendSharedBlobItem( 28 InternalBlobData::~InternalBlobData() {}
29
30 void InternalBlobData::AppendSharedBlobItem(
31 const std::string& my_uuid,
25 scoped_refptr<ShareableBlobDataItem> item) { 32 scoped_refptr<ShareableBlobDataItem> item) {
26 DCHECK(item); 33 if (!items_.empty()) {
27 DCHECK(data_); 34 offsets_.push_back(size_);
28 data_->items_.push_back(item); 35 }
29 } 36 size_ += item->item()->length();
30 37 item->referencing_blobs_.insert(my_uuid);
31 void InternalBlobData::Builder::RemoveBlobFromShareableItems( 38 items_.push_back(std::move(item));
32 const std::string& blob_uuid) {
33 DCHECK(data_);
34 data_->RemoveBlobFromShareableItems(blob_uuid);
35 }
36
37 size_t InternalBlobData::Builder::GetNonsharedMemoryUsage() const {
38 DCHECK(data_);
39 return data_->GetUnsharedMemoryUsage();
40 }
41
42 std::unique_ptr<InternalBlobData> InternalBlobData::Builder::Build() {
43 DCHECK(data_);
44 return std::move(data_);
45 }
46
47 InternalBlobData::InternalBlobData() {
48 }
49
50 InternalBlobData::~InternalBlobData() {
51 } 39 }
52 40
53 const std::vector<scoped_refptr<ShareableBlobDataItem>>& 41 const std::vector<scoped_refptr<ShareableBlobDataItem>>&
54 InternalBlobData::items() const { 42 InternalBlobData::items() const {
55 return items_; 43 return items_;
56 } 44 }
57 45
58 void InternalBlobData::RemoveBlobFromShareableItems( 46 void InternalBlobData::RemoveBlobFromShareableItems(
59 const std::string& blob_uuid) { 47 const std::string& blob_uuid) {
60 for (auto& data_item : items_) { 48 for (auto& data_item : items_) {
61 data_item->referencing_blobs().erase(blob_uuid); 49 data_item->referencing_blobs_.erase(blob_uuid);
62 } 50 }
63 } 51 }
64 52
65 size_t InternalBlobData::GetUnsharedMemoryUsage() const { 53 size_t InternalBlobData::GetUnsharedMemoryUsage() const {
66 size_t memory = 0; 54 size_t memory = 0;
67 base::hash_set<void*> seen_items; 55 base::hash_set<void*> seen_items;
68 for (const auto& data_item : items_) { 56 for (const auto& data_item : items_) {
69 if (data_item->item()->type() != DataElement::TYPE_BYTES || 57 if (!IsBytes(data_item->item()->type()) ||
70 data_item->referencing_blobs().size() > 1 || 58 data_item->referencing_blobs().size() > 1 ||
71 seen_items.find(data_item.get()) != seen_items.end()) { 59 seen_items.find(data_item.get()) != seen_items.end()) {
72 continue; 60 continue;
73 } 61 }
62 LOG(ERROR) << "no one else has item " << data_item->item_id();
74 memory += data_item->item()->length(); 63 memory += data_item->item()->length();
75 seen_items.insert(data_item.get()); 64 seen_items.insert(data_item.get());
76 } 65 }
77 return memory; 66 return memory;
78 } 67 }
79 68
80 void InternalBlobData::GetMemoryUsage(size_t* total_memory,
81 size_t* unshared_memory) {
82 *total_memory = 0;
83 *unshared_memory = 0;
84 base::hash_set<void*> seen_items;
85 for (const auto& data_item : items_) {
86 if (data_item->item()->type() == DataElement::TYPE_BYTES) {
87 *total_memory += data_item->item()->length();
88 if (data_item->referencing_blobs().size() == 1 &&
89 seen_items.find(data_item.get()) == seen_items.end()) {
90 *unshared_memory += data_item->item()->length();
91 seen_items.insert(data_item.get());
92 }
93 }
94 }
95 }
96
97 } // namespace storage 69 } // namespace storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698