|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Any, List, Optional, Tuple |
| 18 | + |
| 19 | +from google.cloud.storage._experimental.asyncio.async_read_object_stream import ( |
| 20 | + _AsyncReadObjectStream, |
| 21 | +) |
| 22 | +from google.cloud.storage._experimental.asyncio.async_grpc_client import ( |
| 23 | + AsyncGrpcClient, |
| 24 | +) |
| 25 | + |
| 26 | +from io import BytesIO |
| 27 | + |
| 28 | + |
| 29 | +class AsyncMultiRangeDownloader: |
| 30 | + """Provides an interface for downloading multiple ranges of a GCS ``Object`` |
| 31 | + concurrently. |
| 32 | +
|
| 33 | + Example usage: |
| 34 | +
|
| 35 | + .. code-block:: python |
| 36 | +
|
| 37 | + client = AsyncGrpcClient().grpc_client |
| 38 | + mrd = await AsyncMultiRangeDownloader.create_mrd( |
| 39 | + client, bucket_name="chandrasiri-rs", object_name="test_open9" |
| 40 | + ) |
| 41 | + my_buff1 = BytesIO() |
| 42 | + my_buff2 = BytesIO() |
| 43 | + my_buff3 = BytesIO() |
| 44 | + my_buff4 = BytesIO() |
| 45 | + buffers = [my_buff1, my_buff2, my_buff3, my_buff4] |
| 46 | + await mrd.download_ranges( |
| 47 | + [ |
| 48 | + (0, 100, my_buff1), |
| 49 | + (100, 200, my_buff2), |
| 50 | + (200, 300, my_buff3), |
| 51 | + (300, 400, my_buff4), |
| 52 | + ] |
| 53 | + ) |
| 54 | + for buff in buffers: |
| 55 | + print("downloaded bytes", buff.getbuffer().nbytes) |
| 56 | +
|
| 57 | + """ |
| 58 | + |
| 59 | + @classmethod |
| 60 | + async def create_mrd( |
| 61 | + cls, |
| 62 | + client: AsyncGrpcClient.grpc_client, |
| 63 | + bucket_name: str, |
| 64 | + object_name: str, |
| 65 | + generation_number: Optional[int] = None, |
| 66 | + read_handle: Optional[bytes] = None, |
| 67 | + ) -> AsyncMultiRangeDownloader: |
| 68 | + """Initializes a MultiRangeDownloader and opens the underlying bidi-gRPC |
| 69 | + object for reading. |
| 70 | +
|
| 71 | + :type client: :class:`~google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client` |
| 72 | + :param client: The asynchronous client to use for making API requests. |
| 73 | +
|
| 74 | + :type bucket_name: str |
| 75 | + :param bucket_name: The name of the bucket containing the object. |
| 76 | +
|
| 77 | + :type object_name: str |
| 78 | + :param object_name: The name of the object to be read. |
| 79 | +
|
| 80 | + :type generation_number: int |
| 81 | + :param generation_number: (Optional) If present, selects a specific |
| 82 | + revision of this object. |
| 83 | +
|
| 84 | + :type read_handle: bytes |
| 85 | + :param read_handle: (Optional) An existing handle for reading the object. |
| 86 | + If provided, opening the bidi-gRPC connection will be faster. |
| 87 | +
|
| 88 | + :rtype: :class:`~google.cloud.storage._experimental.asyncio.async_multi_range_downloader.AsyncMultiRangeDownloader` |
| 89 | + :returns: An initialized AsyncMultiRangeDownloader instance for reading. |
| 90 | + """ |
| 91 | + mrd = cls(client, bucket_name, object_name, generation_number, read_handle) |
| 92 | + await mrd.open() |
| 93 | + return mrd |
| 94 | + |
| 95 | + def __init__( |
| 96 | + self, |
| 97 | + client: AsyncGrpcClient.grpc_client, |
| 98 | + bucket_name: str, |
| 99 | + object_name: str, |
| 100 | + generation_number: Optional[int] = None, |
| 101 | + read_handle: Optional[bytes] = None, |
| 102 | + ) -> None: |
| 103 | + """Constructor for AsyncMultiRangeDownloader, clients are not adviced to |
| 104 | + use it directly. Instead it's adviced to use the classmethod `create_mrd`. |
| 105 | +
|
| 106 | + :type client: :class:`~google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client` |
| 107 | + :param client: The asynchronous client to use for making API requests. |
| 108 | +
|
| 109 | + :type bucket_name: str |
| 110 | + :param bucket_name: The name of the bucket containing the object. |
| 111 | +
|
| 112 | + :type object_name: str |
| 113 | + :param object_name: The name of the object to be read. |
| 114 | +
|
| 115 | + :type generation_number: int |
| 116 | + :param generation_number: (Optional) If present, selects a specific revision of |
| 117 | + this object. |
| 118 | +
|
| 119 | + :type read_handle: bytes |
| 120 | + :param read_handle: (Optional) An existing read handle. |
| 121 | + """ |
| 122 | + self.client = client |
| 123 | + self.bucket_name = bucket_name |
| 124 | + self.object_name = object_name |
| 125 | + self.generation_number = generation_number |
| 126 | + self.read_handle = read_handle |
| 127 | + self.read_obj_str: _AsyncReadObjectStream = None |
| 128 | + |
| 129 | + async def open(self) -> None: |
| 130 | + """Opens the bidi-gRPC connection to read from the object. |
| 131 | +
|
| 132 | + This method initializes and opens an `_AsyncReadObjectStream` (bidi-gRPC stream) to |
| 133 | + for downloading ranges of data from GCS ``Object``. |
| 134 | +
|
| 135 | + "Opening" constitutes fetching object metadata such as generation number |
| 136 | + and read handle and sets them as attributes if not already set. |
| 137 | + """ |
| 138 | + self.read_obj_str = _AsyncReadObjectStream( |
| 139 | + client=self.client, |
| 140 | + bucket_name=self.bucket_name, |
| 141 | + object_name=self.object_name, |
| 142 | + generation_number=self.generation_number, |
| 143 | + read_handle=self.read_handle, |
| 144 | + ) |
| 145 | + await self.read_obj_str.open() |
| 146 | + if self.generation_number is None: |
| 147 | + self.generation_number = self.read_obj_str.generation_number |
| 148 | + self.read_handle = self.read_obj_str.read_handle |
| 149 | + return |
| 150 | + |
| 151 | + async def download_ranges(self, read_ranges: List[Tuple[int, int, BytesIO]]) -> Any: |
| 152 | + """Downloads multiple byte ranges from the object into the buffers |
| 153 | + provided by user. |
| 154 | +
|
| 155 | + :type read_ranges: List[Tuple[int, int, "BytesIO"]] |
| 156 | + :param read_ranges: A list of tuples, where each tuple represents a |
| 157 | + byte range (start_byte, end_byte, buffer) to download. Buffer has to |
| 158 | + be provided by the user, and user has to make sure appropriate |
| 159 | + memory is available in the application to avoid out-of-memory crash. |
| 160 | +
|
| 161 | +
|
| 162 | + Raises: |
| 163 | + NotImplementedError: This method is not yet implemented. |
| 164 | + """ |
| 165 | + raise NotImplementedError("TODO") |
0 commit comments