-
Notifications
You must be signed in to change notification settings - Fork 9
Added methods and test cases for OpendalBufferedFile #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
umangbudhwar
wants to merge
2
commits into
fsspec:main
Choose a base branch
from
umangbudhwar:fsspec-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,22 +31,31 @@ def __init__( | |
| **kwargs, | ||
| ) | ||
|
|
||
| def _fetch_range(self, start: int, end: int): | ||
| """Download data between start and end""" | ||
| pass | ||
| def _fetch_range(self, start: int, end: int) -> bytes: | ||
| """Download data between start and end.""" | ||
| logger.debug(f"Fetching bytes from {start} to {end} for {self.path}") | ||
| data = self.fs.fs.read(self.path) # sync operator | ||
| return data[start:end] | ||
|
|
||
| def _upload_chunk(self, final: bool = False): | ||
| """Upload partial chunk of data""" | ||
| """No-op: we buffer until close and upload once.""" | ||
| pass | ||
|
|
||
| def _initiate_upload(self) -> None: | ||
| """Prepare for uploading""" | ||
| pass | ||
| logger.debug(f"Initiated upload for {self.path}") | ||
|
|
||
| def _commit_upload(self) -> None: | ||
| """Ensure upload is complete""" | ||
| pass | ||
| """Write the full buffer to the backend once""" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenDAL has it's own buffer, so we just need to implement |
||
| logger.debug(f"Committing full upload for {self.path}") | ||
| self.buffer.seek(0) | ||
| data = self.buffer.read() | ||
| self.fs.write(self.path, data) | ||
|
|
||
| def close(self): | ||
| """Ensure data is written before closing""" | ||
| pass | ||
| if not self.closed: | ||
| if self.mode in ("wb", "ab"): | ||
| self._commit_upload() | ||
| super().close() | ||
| logger.debug(f"Closed file {self.path}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import pytest | ||
| from opendalfs.file import OpendalBufferedFile | ||
| from opendalfs.fs import OpendalFileSystem | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fs_dir(): | ||
| return OpendalFileSystem("fs", root="/tmp/") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def file_path(): | ||
| return "testdir/testfile.txt" | ||
|
|
||
|
|
||
| def test_fetch_range_basic(fs_dir, file_path): | ||
| fs_dir.write(file_path, b"Hello, world!") | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="rb") | ||
| assert f._fetch_range(0, 5) == b"Hello" | ||
| assert f._fetch_range(7, 12) == b"world" | ||
|
|
||
|
|
||
| def test_fetch_range_out_of_bounds(fs_dir, file_path): | ||
| fs_dir.write(file_path, b"short") | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="rb") | ||
| assert f._fetch_range(0, 100) == b"short" | ||
| assert f._fetch_range(10, 20) == b"" | ||
|
|
||
|
|
||
| def test_initiate_upload(fs_dir, file_path): | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="wb") | ||
| f._initiate_upload() | ||
| assert f.buffer is not None | ||
|
|
||
|
|
||
| def test_commit_upload(fs_dir, file_path): | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="wb") | ||
| f.buffer.write(b"OpenDAL test") | ||
| f._commit_upload() | ||
| assert fs_dir.read(file_path) == b"OpenDAL test" | ||
|
|
||
|
|
||
| def test_commit_upload_empty_buffer(fs_dir, file_path): | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="wb") | ||
| f._commit_upload() # should not raise even if buffer is empty | ||
| assert fs_dir.exists(file_path) is True | ||
| assert fs_dir.read(file_path) == b"" | ||
|
|
||
|
|
||
| def test_upload_chunk_noop(fs_dir, file_path): | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="wb") | ||
| f._upload_chunk() # should not error | ||
| f._upload_chunk(final=True) # should not error | ||
|
|
||
|
|
||
| def test_close_writes_buffer(fs_dir, file_path): | ||
| with OpendalBufferedFile(fs_dir, file_path, mode="wb") as f: | ||
| f.buffer.write(b"closing buffer test") | ||
| assert fs_dir.read(file_path) == b"closing buffer test" | ||
|
|
||
|
|
||
| def test_close_does_not_write_on_read_mode(fs_dir, file_path): | ||
| fs_dir.write(file_path, b"read test") | ||
| f = OpendalBufferedFile(fs_dir, file_path, mode="rb") | ||
| f.close() | ||
| assert fs_dir.read(file_path) == b"read test" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now have native support for
read(self.path, offset, size)after apache/opendal#6086 was merged. Would you like to create an issue to track this, so we can update our usage and avoid reading the entire file?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#28 Created the issue