Chromium Code Reviews| Index: storage/browser/blob/blob_slice.cc |
| diff --git a/storage/browser/blob/blob_slice.cc b/storage/browser/blob/blob_slice.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c7537d41d44cbae971d6c8ee773998835ccffc4e |
| --- /dev/null |
| +++ b/storage/browser/blob/blob_slice.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "storage/browser/blob/blob_slice.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "storage/browser/blob/blob_data_item.h" |
| +#include "storage/browser/blob/blob_storage_context.h" |
| +#include "storage/browser/blob/shareable_blob_data_item.h" |
| + |
| +namespace storage { |
| + |
| +BlobSlice::BlobSlice(const InternalBlobData& source, |
| + uint64_t slice_offset, |
| + uint64_t slice_size) { |
| + const auto& source_items = source.items(); |
| + const auto& offsets = source.offsets(); |
| + DCHECK_LE(slice_offset + slice_size, source.total_size()); |
| + size_t item_index = |
| + std::upper_bound(offsets.begin(), offsets.end(), slice_offset) - |
| + offsets.begin(); |
| + uint64_t item_offset = |
| + item_index == 0 ? slice_offset : slice_offset - offsets[item_index - 1]; |
| + size_t num_items = source_items.size(); |
| + |
| + LOG(ERROR) << "doing a slice at " << slice_offset << " with size " |
| + << slice_size; |
| + size_t first_item_index = item_index; |
| + copying_memory_size = 0; |
| + |
| + bool first_item = true; |
|
michaeln
2016/07/14 01:44:43
probably don't need this local, could item_index =
dmurph
2016/07/15 20:18:15
Done.
|
| + bool last_item_memory_sliced = false; |
| + // Read starting from 'mid' and 'offset_from_mid'. |
|
michaeln
2016/07/14 01:44:43
comment is stale
dmurph
2016/07/15 20:18:15
Done.
|
| + for (uint64_t total_sliced = 0; |
| + item_index < num_items && total_sliced < slice_size; item_index++) { |
| + const scoped_refptr<BlobDataItem>& item = source_items[item_index]->item(); |
|
michaeln
2016/07/14 01:44:43
might call this source_item for clarity
dmurph
2016/07/15 20:18:15
Done.
|
| + uint64_t length = item->length(); |
|
michaeln
2016/07/14 01:44:43
probably dont need this local
(the value is not us
dmurph
2016/07/15 20:18:15
Replaced callsites, I like the local.
|
| + DCHECK_NE(length, std::numeric_limits<uint64_t>::max()); |
| + DCHECK_NE(length, 0ull); |
| + |
| + last_item_memory_sliced = false; |
| + |
| + if (item_offset == 0 && slice_size - total_sliced >= item->length()) { |
| + // We can share the entire item. |
| + LOG(ERROR) << "we can share"; |
| + dest_items.push_back(source_items[item_index]); |
| + total_sliced += length; |
| + first_item = false; |
| + continue; |
| + } |
| + |
| + uint64_t read_size = |
| + std::min(item->length() - item_offset, slice_size - total_sliced); |
| + total_sliced += read_size; |
|
michaeln
2016/07/14 01:44:43
might help readability to move the read_size compu
dmurph
2016/07/15 20:18:15
Done.
|
| + |
| + scoped_refptr<BlobDataItem> data_item; |
| + switch (item->type()) { |
| + case DataElement::TYPE_BYTES_DESCRIPTION: |
| + case DataElement::TYPE_BYTES: { |
| + if (first_item) { |
| + has_sliced_first_memory_item = true; |
| + first_item_slice_offset = item_offset; |
| + first_source_item = source_items[first_item_index]; |
| + } |
|
michaeln
2016/07/14 01:44:43
i think the handling of the last partial item mayb
dmurph
2016/07/15 20:18:15
Ah, much better.
|
| + LOG(ERROR) << "we're sliceing a bytes item!"; |
| + last_item_memory_sliced = true; |
| + copying_memory_size += read_size; |
| + // Since we don't have quota yet for memory, we create temporary items |
| + // for this data. When our blob is finished constructing, all dependent |
| + // blobs are done, and we have enough memory quota, we'll copy the data |
| + // over. |
| + std::unique_ptr<DataElement> element(new DataElement()); |
| + element->SetToBytesDescription(base::checked_cast<size_t>(read_size)); |
| + data_item = new BlobDataItem(std::move(element), nullptr); |
| + } break; |
| + case DataElement::TYPE_FILE: { |
| + std::unique_ptr<DataElement> element(new DataElement()); |
| + element->SetToFilePathRange(item->path(), item->offset() + item_offset, |
| + read_size, |
| + item->expected_modification_time()); |
| + data_item = new BlobDataItem(std::move(element), item->data_handle_); |
| + } break; |
| + case DataElement::TYPE_FILE_FILESYSTEM: { |
| + std::unique_ptr<DataElement> element(new DataElement()); |
| + element->SetToFileSystemUrlRange( |
| + item->filesystem_url(), item->offset() + item_offset, read_size, |
| + item->expected_modification_time()); |
| + data_item = new BlobDataItem(std::move(element), nullptr); |
| + } break; |
| + case DataElement::TYPE_DISK_CACHE_ENTRY: { |
| + std::unique_ptr<DataElement> element(new DataElement()); |
| + element->SetToDiskCacheEntryRange(item->offset() + item_offset, |
| + read_size); |
| + data_item = new BlobDataItem(std::move(element), item->data_handle_, |
| + item->disk_cache_entry(), |
| + item->disk_cache_stream_index(), |
| + item->disk_cache_side_stream_index()); |
| + } break; |
| + case DataElement::TYPE_BLOB: |
| + case DataElement::TYPE_UNKNOWN: |
| + CHECK(false) << "Illegal blob item type: " << item->type(); |
| + } |
| + dest_items.push_back(new ShareableBlobDataItem(std::move(data_item))); |
| + first_item = false; |
| + item_offset = 0; |
| + } |
| + if (last_item_memory_sliced && |
| + !(has_sliced_first_memory_item && dest_items.size() == 1)) { |
| + has_sliced_last_memory_item = true; |
| + last_source_item = source_items[item_index - 1]; |
| + } |
| +} |
| + |
| +BlobSlice::~BlobSlice() {} |
| + |
| +} // namespace storage |