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 "storage/browser/blob/blob_storage_context.h" |
| 8 #include "storage/browser/blob/blob_data_item.h" |
| 9 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 10 |
| 11 namespace storage { |
| 12 |
| 13 BlobSlice::BlobSlice() {} |
| 14 |
| 15 BlobSlice::BlobSlice(const InternalBlobData& source, |
| 16 uint64_t blob_offset, |
| 17 uint64_t size, |
| 18 bool done_building) |
| 19 : done_building_(done_building) { |
| 20 const auto& items = source.items(); |
| 21 size_t mid; |
| 22 uint64_t offset_from_mid; |
| 23 size_t num_items = items.size(); |
| 24 BinarySearch(source, blob_offset, &mid, &offset_from_mid); |
| 25 |
| 26 LOG(ERROR) << "doing a slice at " << blob_offset << " with size " << size; |
| 27 first_item_index_ = mid; |
| 28 copying_memory_size_ = 0; |
| 29 |
| 30 bool first_item = true; |
| 31 bool last_item_memory_sliced = false; |
| 32 // Read starting from 'mid' and 'offset_from_mid'. |
| 33 for (uint64_t memory_read = 0; mid < num_items && memory_read < size; mid++) { |
| 34 const scoped_refptr<BlobDataItem>& item = items[mid]->item(); |
| 35 uint64_t length = item->length(); |
| 36 if (length == 0) { |
| 37 continue; |
| 38 } |
| 39 DCHECK_NE(length, std::numeric_limits<uint64_t>::max()); |
| 40 DCHECK_NE(length, 0ull); |
| 41 |
| 42 last_item_memory_sliced = false; |
| 43 LOG(ERROR) << item->length(); |
| 44 bool can_share_item = |
| 45 offset_from_mid == 0 && size - memory_read >= item->length(); |
| 46 |
| 47 if (can_share_item) { |
| 48 LOG(ERROR) << "we can share"; |
| 49 items_.push_back(items[mid]); |
| 50 memory_read += length; |
| 51 first_item = false; |
| 52 continue; |
| 53 } |
| 54 |
| 55 uint64_t read_size = |
| 56 std::min(item->length() - offset_from_mid, size - memory_read); |
| 57 memory_read += read_size; |
| 58 |
| 59 scoped_refptr<BlobDataItem> data_item; |
| 60 switch (item->type()) { |
| 61 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 62 case DataElement::TYPE_BYTES: { |
| 63 if (first_item) { |
| 64 has_sliced_first_memory_item_ = true; |
| 65 first_item_slice_offset_ = offset_from_mid; |
| 66 } |
| 67 LOG(ERROR) << "we're sliceing a bytes item!"; |
| 68 last_item_memory_sliced = true; |
| 69 copying_memory_size_ += read_size; |
| 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), nullptr); |
| 73 } break; |
| 74 case DataElement::TYPE_FILE: { |
| 75 std::unique_ptr<DataElement> element(new DataElement()); |
| 76 element->SetToFilePathRange(item->path(), |
| 77 item->offset() + offset_from_mid, read_size, |
| 78 item->expected_modification_time()); |
| 79 data_item = new BlobDataItem(std::move(element), item->data_handle_); |
| 80 } break; |
| 81 case DataElement::TYPE_FILE_FILESYSTEM: { |
| 82 std::unique_ptr<DataElement> element(new DataElement()); |
| 83 element->SetToFileSystemUrlRange( |
| 84 item->filesystem_url(), item->offset() + offset_from_mid, read_size, |
| 85 item->expected_modification_time()); |
| 86 data_item = new BlobDataItem(std::move(element), nullptr); |
| 87 } break; |
| 88 case DataElement::TYPE_DISK_CACHE_ENTRY: { |
| 89 std::unique_ptr<DataElement> element(new DataElement()); |
| 90 element->SetToDiskCacheEntryRange(item->offset() + offset_from_mid, |
| 91 read_size); |
| 92 data_item = new BlobDataItem(std::move(element), item->data_handle_, |
| 93 item->disk_cache_entry(), |
| 94 item->disk_cache_stream_index(), |
| 95 item->disk_cache_side_stream_index()); |
| 96 } break; |
| 97 case DataElement::TYPE_BLOB: |
| 98 case DataElement::TYPE_UNKNOWN: |
| 99 CHECK(false) << "Illegal blob item type: " << item->type(); |
| 100 } |
| 101 items_.push_back(new ShareableBlobDataItem( |
| 102 BlobStorageContext::GetAndIncrementItemId(), std::move(data_item))); |
| 103 first_item = false; |
| 104 offset_from_mid = 0; |
| 105 } |
| 106 if (last_item_memory_sliced && |
| 107 !(has_sliced_first_memory_item_ && items_.size() == 1)) { |
| 108 has_sliced_last_memory_item_ = true; |
| 109 last_item_index_ = mid - 1; |
| 110 } |
| 111 } |
| 112 |
| 113 BlobSlice::BlobSlice(BlobSlice&&) = default; |
| 114 |
| 115 void BlobSlice::BinarySearch(const InternalBlobData& source_blob, |
| 116 uint64_t blob_offset, |
| 117 size_t* item_index, |
| 118 uint64_t* offset_in_item) { |
| 119 DCHECK(item_index != nullptr && offset_in_item != nullptr); |
| 120 const auto& items = source_blob.items(); |
| 121 const auto& offsets = source_blob.offsets(); |
| 122 size_t mid = 0; |
| 123 uint64_t offset_from_mid = blob_offset; |
| 124 size_t num_items = items.size(); |
| 125 if (!offsets.empty()) { |
| 126 size_t low = 0; |
| 127 size_t high = num_items - 1; |
| 128 while (true) { |
| 129 mid = (high + low) / 2; |
| 130 // Note: we don't include the implicit '0' for the first item in offsets. |
| 131 size_t item_offset = (mid == 0 ? 0 : offsets[mid - 1]); |
| 132 offset_from_mid = blob_offset - item_offset; |
| 133 uint64_t next_item_offset = (mid + 1 == num_items ? 0 : offsets[mid]); |
| 134 if (item_offset == blob_offset) { |
| 135 // We found an exact match! |
| 136 break; |
| 137 } else if (item_offset > blob_offset) { |
| 138 high = mid - 1; |
| 139 } else if (mid + 1 == num_items || next_item_offset > blob_offset) { |
| 140 // We're at the last item, or the next offset is greater than the one we |
| 141 // want, so the current item wins. |
| 142 break; |
| 143 } else { |
| 144 low = mid + 1; |
| 145 } |
| 146 } |
| 147 } |
| 148 DCHECK_LT(offset_from_mid, items[mid]->item()->length()); |
| 149 *item_index = mid; |
| 150 *offset_in_item = offset_from_mid; |
| 151 } |
| 152 |
| 153 BlobSlice::~BlobSlice() {} |
| 154 |
| 155 } // namespace storage |
OLD | NEW |