OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 | |
5 #include "storage/browser/blob/blob_slice.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "storage/browser/blob/blob_data_builder.h" | |
10 #include "storage/browser/blob/blob_data_item.h" | |
11 #include "storage/browser/blob/internal_blob_data.h" | |
12 #include "storage/browser/blob/shareable_blob_data_item.h" | |
13 | |
14 namespace storage { | |
15 | |
16 BlobSlice::BlobSlice(const InternalBlobData& source, | |
17 uint64_t slice_offset, | |
18 uint64_t slice_size) { | |
19 const auto& source_items = source.items(); | |
20 const auto& offsets = source.offsets(); | |
21 LOG(ERROR) << "doing a slice at " << slice_offset << " with size " | |
22 << slice_size; | |
23 DCHECK_LE(slice_offset + slice_size, source.total_size()); | |
24 size_t item_index = | |
25 std::upper_bound(offsets.begin(), offsets.end(), slice_offset) - | |
26 offsets.begin(); | |
27 uint64_t item_offset = | |
28 item_index == 0 ? slice_offset : slice_offset - offsets[item_index - 1]; | |
29 size_t num_items = source_items.size(); | |
30 | |
31 size_t first_item_index = item_index; | |
32 copying_memory_size = 0; | |
33 | |
34 // Read starting from 'first_item_index' and 'item_offset'. | |
35 for (uint64_t total_sliced = 0; | |
36 item_index < num_items && total_sliced < slice_size; item_index++) { | |
37 const scoped_refptr<BlobDataItem>& source_item = | |
38 source_items[item_index]->item(); | |
39 uint64_t source_length = source_item->length(); | |
40 DCHECK_NE(source_length, std::numeric_limits<uint64_t>::max()); | |
41 DCHECK_NE(source_length, 0ull); | |
42 | |
43 uint64_t read_size = | |
44 std::min(source_length - item_offset, slice_size - total_sliced); | |
45 total_sliced += read_size; | |
46 | |
47 if (read_size == source_length) { | |
48 // We can share the entire item. | |
49 LOG(ERROR) << "we can share"; | |
50 dest_items.push_back(source_items[item_index]); | |
51 continue; | |
52 } | |
53 | |
54 scoped_refptr<BlobDataItem> data_item; | |
55 switch (source_item->type()) { | |
56 case DataElement::TYPE_BYTES_DESCRIPTION: | |
57 case DataElement::TYPE_BYTES: { | |
58 if (item_index == first_item_index) { | |
59 first_item_slice_offset = item_offset; | |
60 first_source_item = source_items[item_index]; | |
61 } else { | |
62 last_source_item = source_items[item_index]; | |
63 } | |
64 LOG(ERROR) << "we're sliceing a bytes item!"; | |
kinuko
2016/07/17 16:15:47
(typo, but I assume we're removing these LOG(ERROR
dmurph
2016/07/19 02:26:27
They will all be removed, I have them to verify we
| |
65 copying_memory_size += read_size; | |
66 // Since we don't have quota yet for memory, we create temporary items | |
67 // for this data. When our blob is finished constructing, all dependent | |
68 // blobs are done, and we have enough memory quota, we'll copy the data | |
69 // over. | |
70 std::unique_ptr<DataElement> element(new DataElement()); | |
71 element->SetToBytesDescription(base::checked_cast<size_t>(read_size)); | |
72 data_item = new BlobDataItem(std::move(element)); | |
73 } break; | |
kinuko
2016/07/17 16:15:47
nit: is this style common? I think I see followin
dmurph
2016/07/19 02:26:27
Done.
| |
74 case DataElement::TYPE_FILE: { | |
75 std::unique_ptr<DataElement> element(new DataElement()); | |
76 element->SetToFilePathRange( | |
77 source_item->path(), source_item->offset() + item_offset, read_size, | |
78 source_item->expected_modification_time()); | |
79 data_item = | |
80 new BlobDataItem(std::move(element), source_item->data_handle_); | |
81 | |
82 if (source_item->path().value() == | |
83 std::string(BlobDataBuilder::kAppendFutureFileTemporaryFileName)) { | |
84 // Since we don't have file path / reference for our future file, we | |
85 // create another temporary file item. When our blob is finished | |
86 // constructing, all dependent blobs are done, and we can copy the | |
87 // handle over. | |
88 LOG(ERROR) << "we're slicing a temp file item!"; | |
89 if (item_index == first_item_index) { | |
90 first_item_slice_offset = item_offset; | |
91 first_source_item = source_items[item_index]; | |
92 } else { | |
93 last_source_item = source_items[item_index]; | |
94 } | |
95 } | |
96 } break; | |
97 case DataElement::TYPE_FILE_FILESYSTEM: { | |
98 std::unique_ptr<DataElement> element(new DataElement()); | |
99 element->SetToFileSystemUrlRange( | |
100 source_item->filesystem_url(), source_item->offset() + item_offset, | |
101 read_size, source_item->expected_modification_time()); | |
102 data_item = new BlobDataItem(std::move(element)); | |
103 } break; | |
104 case DataElement::TYPE_DISK_CACHE_ENTRY: { | |
105 std::unique_ptr<DataElement> element(new DataElement()); | |
106 element->SetToDiskCacheEntryRange(source_item->offset() + item_offset, | |
107 read_size); | |
108 data_item = | |
109 new BlobDataItem(std::move(element), source_item->data_handle_, | |
110 source_item->disk_cache_entry(), | |
111 source_item->disk_cache_stream_index(), | |
112 source_item->disk_cache_side_stream_index()); | |
113 } break; | |
114 case DataElement::TYPE_BLOB: | |
115 case DataElement::TYPE_UNKNOWN: | |
116 CHECK(false) << "Illegal blob item type: " << source_item->type(); | |
117 } | |
118 dest_items.push_back(new ShareableBlobDataItem(std::move(data_item))); | |
119 item_offset = 0; | |
120 } | |
121 } | |
122 | |
123 BlobSlice::~BlobSlice() {} | |
124 | |
125 } // namespace storage | |
OLD | NEW |