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..0b211be6a288dfb60511d640ca66f5cf202edaed |
| --- /dev/null |
| +++ b/storage/browser/blob/blob_slice.cc |
| @@ -0,0 +1,116 @@ |
| +// 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 blob_offset, |
| + uint64_t size) { |
|
michaeln
2016/07/07 20:05:23
some naming suggestions: source, slice_offset, sli
dmurph
2016/07/11 23:33:27
Done.
|
| + const auto& source_items = source.items(); |
|
michaeln
2016/07/07 20:05:23
what if source_items is empty? can that happen?
dmurph
2016/07/11 23:33:27
That shouldn't happen, as we check to see if we ne
|
| + const auto& offsets = source.offsets(); |
| + size_t mid = std::upper_bound(offsets.begin(), offsets.end(), blob_offset) - |
| + offsets.begin(); |
| + uint64_t offset_from_mid = |
|
michaeln
2016/07/07 20:05:23
for these two locals, names like item_index and it
dmurph
2016/07/11 23:33:27
Done.
|
| + mid == 0 ? blob_offset : blob_offset - offsets[mid - 1]; |
| + size_t num_items = source_items.size(); |
| + |
| + LOG(ERROR) << "doing a slice at " << blob_offset << " with size " << size; |
| + size_t first_item_index = mid; |
| + copying_memory_size = 0; |
| + |
| + bool first_item = true; |
| + bool last_item_memory_sliced = false; |
| + // Read starting from 'mid' and 'offset_from_mid'. |
| + for (uint64_t memory_read = 0; mid < num_items && memory_read < size; mid++) { |
|
michaeln
2016/07/07 20:05:23
it looks like |memory_read| also accumulates the s
dmurph
2016/07/11 23:33:27
Done.
|
| + const scoped_refptr<BlobDataItem>& item = source_items[mid]->item(); |
| + uint64_t length = item->length(); |
| + if (length == 0) { |
|
michaeln
2016/07/07 20:05:23
do we have zero length items? i think at some poin
dmurph
2016/07/11 23:33:27
We shouldn't. I'll dcheck this.
|
| + continue; |
| + } |
| + DCHECK_NE(length, std::numeric_limits<uint64_t>::max()); |
| + DCHECK_NE(length, 0ull); |
| + |
| + last_item_memory_sliced = false; |
| + LOG(ERROR) << item->length(); |
| + bool can_share_item = |
|
michaeln
2016/07/07 20:05:23
since this local is only used in the if below, get
dmurph
2016/07/11 23:33:27
Done.
|
| + offset_from_mid == 0 && size - memory_read >= item->length(); |
| + |
| + if (can_share_item) { |
| + LOG(ERROR) << "we can share"; |
| + dest_items.push_back(source_items[mid]); |
| + memory_read += length; |
| + first_item = false; |
| + continue; |
| + } |
| + |
| + uint64_t read_size = |
| + std::min(item->length() - offset_from_mid, size - memory_read); |
| + memory_read += read_size; |
| + |
| + 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 = offset_from_mid; |
| + first_source_item = source_items[first_item_index]; |
| + } |
| + LOG(ERROR) << "we're sliceing a bytes item!"; |
| + last_item_memory_sliced = true; |
| + copying_memory_size += read_size; |
| + std::unique_ptr<DataElement> element(new DataElement()); |
| + element->SetToBytesDescription(base::checked_cast<size_t>(read_size)); |
| + data_item = new BlobDataItem(std::move(element), nullptr); |
|
michaeln
2016/07/07 20:05:23
when do we really copy the data?
dmurph
2016/07/11 23:33:27
When the blob is finished constructing. The flatte
|
| + } break; |
| + case DataElement::TYPE_FILE: { |
| + std::unique_ptr<DataElement> element(new DataElement()); |
| + element->SetToFilePathRange(item->path(), |
| + item->offset() + offset_from_mid, 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() + offset_from_mid, 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() + offset_from_mid, |
| + 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( |
| + BlobStorageContext::GetAndIncrementItemId(), std::move(data_item))); |
| + first_item = false; |
| + offset_from_mid = 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[mid - 1]; |
| + } |
| +} |
| + |
| +BlobSlice::~BlobSlice() {} |
| + |
| +} // namespace storage |