Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a38c396
feat(zb-experimental): add async write object stream
chandra-siri Nov 13, 2025
aaee2f3
remove unused import and add license info
chandra-siri Nov 13, 2025
4db8bf1
remove unwated test
chandra-siri Nov 13, 2025
8dbd158
feat(zb-experimental): implement "open" for write_object_stream
chandra-siri Nov 14, 2025
03f1fde
remove unused import
chandra-siri Nov 14, 2025
f0d3439
feat(zb-experimental): implement close
chandra-siri Nov 14, 2025
e3b6f9e
feat(zb-experimental): implement send & recv
chandra-siri Nov 14, 2025
b24535f
feat(zb-experimental): Add Async_appendable_object_writer.py
chandra-siri Nov 14, 2025
eae1b36
feat(zb-experimental): implement state_lookup
chandra-siri Nov 14, 2025
788848a
implement tests for state_lookup
chandra-siri Nov 14, 2025
07c9b44
feat(zb-experimental): implement open in writer
chandra-siri Nov 14, 2025
3930823
add type annotation for state_lookup
chandra-siri Nov 14, 2025
222aef2
state_lookup should return persisted_size instead of proto
chandra-siri Nov 14, 2025
3eae403
Merge branch 'bidi-writes-6' of github.com:googleapis/python-storage …
chandra-siri Nov 14, 2025
79835fa
persisted size changes
chandra-siri Nov 14, 2025
cc5e12d
feat(zb-experimental): implement flush, close and finalize
chandra-siri Nov 16, 2025
57aa63e
Update doc strings
chandra-siri Nov 16, 2025
55e0e0d
feat(zb-experimental): implement append
chandra-siri Nov 16, 2025
55d495d
add test_append_data_two_times
chandra-siri Nov 16, 2025
6fa5e11
add doc string for AsyncAppendableObjectWriter
chandra-siri Nov 16, 2025
a8eba4f
Merge branch 'bidi-writes-5' of github.com:googleapis/python-storage …
chandra-siri Nov 16, 2025
d8859ee
Merge branch 'bidi-writes-6' of github.com:googleapis/python-storage …
chandra-siri Nov 16, 2025
c1cbd86
Merge branch 'bidi-writes-7' of github.com:googleapis/python-storage …
chandra-siri Nov 16, 2025
150556b
Merge branch 'bidi-writes-8' of github.com:googleapis/python-storage …
chandra-siri Nov 16, 2025
c4c79e6
correct comment size
chandra-siri Nov 16, 2025
96ff8dc
update the append logic to reduce copying data
chandra-siri Nov 17, 2025
244635f
Merge branch 'main' into bidi-writes-4
chandra-siri Nov 17, 2025
1981418
add missedout test after merge conflict resolution
chandra-siri Nov 17, 2025
ebd5c10
Merge branch 'bidi-writes-4' of github.com:googleapis/python-storage …
chandra-siri Nov 17, 2025
2a22cf7
Merge branch 'bidi-writes-5' of github.com:googleapis/python-storage …
chandra-siri Nov 17, 2025
aee1feb
Merge branch 'bidi-writes-6' of github.com:googleapis/python-storage …
chandra-siri Nov 17, 2025
cdaa25f
Merge branch 'bidi-writes-7' of github.com:googleapis/python-storage …
chandra-siri Nov 17, 2025
2766c46
Merge branch 'bidi-writes-8' of github.com:googleapis/python-storage …
chandra-siri Nov 17, 2025
62c8fd7
remove local testing code
chandra-siri Nov 18, 2025
ad9e4b9
Merge branch 'main' of github.com:googleapis/python-storage into bidi…
chandra-siri Nov 19, 2025
de4b74d
Merge branch 'main' into bidi-writes-9
chandra-siri Nov 20, 2025
9ede745
address comments and improve doc string
chandra-siri Nov 20, 2025
a18bf87
raise ValueError if stream is not open
chandra-siri Nov 20, 2025
dc2df55
Merge branch 'main' into bidi-writes-9
chandra-siri Nov 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if you want to use these Rapid Storage APIs.

"""
from typing import Optional
from typing import Optional, Union
from google.cloud import _storage_v2
from google.cloud.storage._experimental.asyncio.async_grpc_client import (
AsyncGrpcClient,
Expand All @@ -31,6 +31,10 @@
)


_MAX_CHUNK_SIZE_BYTES = 2 * 1024 * 1024 # 2 MiB
_MAX_BUFFER_SIZE_BYTES = 16 * 1024 * 1024 # 16 MiB


class AsyncAppendableObjectWriter:
"""Class for appending data to a GCS Appendable Object asynchronously."""

Expand Down Expand Up @@ -118,7 +122,13 @@ async def state_lookup(self) -> int:

:rtype: int
:returns: persisted size.

:raises ValueError: If the stream is not open (i.e., `open()` has not
been called).
"""
if not self._is_stream_open:
raise ValueError("Stream is not open. Call open() before state_lookup().")

await self.write_obj_stream.send(
_storage_v2.BidiWriteObjectRequest(
state_lookup=True,
Expand All @@ -129,7 +139,11 @@ async def state_lookup(self) -> int:
return self.persisted_size

async def open(self) -> None:
"""Opens the underlying bidi-gRPC stream."""
"""Opens the underlying bidi-gRPC stream.

:raises ValueError: If the stream is already open.

"""
if self._is_stream_open:
raise ValueError("Underlying bidi-gRPC stream is already open")

Expand All @@ -142,15 +156,65 @@ async def open(self) -> None:
# Update self.persisted_size
_ = await self.state_lookup()

async def append(self, data: bytes):
raise NotImplementedError("append is not implemented yet.")
async def append(self, data: bytes) -> None:
"""Appends data to the Appendable object.

This method sends the provided data to the GCS server in chunks. It
maintains an internal threshold `_MAX_BUFFER_SIZE_BYTES` and will
automatically flush the data to make it visible to readers when that
threshold has reached.

:type data: bytes
:param data: The bytes to append to the object.

:rtype: None

:raises ValueError: If the stream is not open (i.e., `open()` has not
been called).
"""

if not self._is_stream_open:
raise ValueError("Stream is not open. Call open() before append().")
total_bytes = len(data)
if total_bytes == 0:
# TODO: add warning.
return
if self.offset is None:
assert self.persisted_size is not None
self.offset = self.persisted_size

start_idx = 0
bytes_to_flush = 0
while start_idx < total_bytes:
end_idx = min(start_idx + _MAX_CHUNK_SIZE_BYTES, total_bytes)
await self.write_obj_stream.send(
_storage_v2.BidiWriteObjectRequest(
write_offset=self.offset,
checksummed_data=_storage_v2.ChecksummedData(
content=data[start_idx:end_idx]
),
)
)
chunk_size = end_idx - start_idx
self.offset += chunk_size
bytes_to_flush += chunk_size
if bytes_to_flush >= _MAX_BUFFER_SIZE_BYTES:
await self.flush()
bytes_to_flush = 0
start_idx = end_idx

async def flush(self) -> int:
"""Flushes the data to the server.

:rtype: int
:returns: The persisted size after flush.

:raises ValueError: If the stream is not open (i.e., `open()` has not
been called).
"""
if not self._is_stream_open:
raise ValueError("Stream is not open. Call open() before flush().")

await self.write_obj_stream.send(
_storage_v2.BidiWriteObjectRequest(
flush=True,
Expand All @@ -162,14 +226,34 @@ async def flush(self) -> int:
self.offset = self.persisted_size
return self.persisted_size

async def close(self, finalize_on_close=False) -> int:
"""Returns persisted_size"""
async def close(self, finalize_on_close=False) -> Union[int, _storage_v2.Object]:
"""Closes the underlying bidi-gRPC stream.

:type finalize_on_close: bool
:param finalize_on_close: Finalizes the Appendable Object. No more data
can be appended.

rtype: Union[int, _storage_v2.Object]
returns: Updated `self.persisted_size` by default after closing the
bidi-gRPC stream. However, if `finalize_on_close=True` is passed,
returns the finalized object resource.

:raises ValueError: If the stream is not open (i.e., `open()` has not
been called).

"""
if not self._is_stream_open:
raise ValueError("Stream is not open. Call open() before close().")

if finalize_on_close:
await self.finalize()
else:
await self.flush()
await self.write_obj_stream.close()

await self.write_obj_stream.close()
self._is_stream_open = False
self.offset = None
return self.object_resource if finalize_on_close else self.persisted_size

async def finalize(self) -> _storage_v2.Object:
"""Finalizes the Appendable Object.
Expand All @@ -178,12 +262,20 @@ async def finalize(self) -> _storage_v2.Object:

rtype: google.cloud.storage_v2.types.Object
returns: The finalized object resource.

:raises ValueError: If the stream is not open (i.e., `open()` has not
been called).
"""
if not self._is_stream_open:
raise ValueError("Stream is not open. Call open() before finalize().")

await self.write_obj_stream.send(
_storage_v2.BidiWriteObjectRequest(finish_write=True)
)
response = await self.write_obj_stream.recv()
self.object_resource = response.resource
self.persisted_size = self.object_resource.size
return self.object_resource

# helper methods.
async def append_from_string(self, data: str):
Expand Down
Loading