Skip to content

Commit

Permalink
[TOSFS #8] Implement _rm inner API
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Aug 19, 2024
1 parent b6d1655 commit 69fdc87
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
27 changes: 27 additions & 0 deletions tosfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def __init__(
self.version_aware = version_aware
super().__init__(**kwargs)

def invalidate_cache(self, path: Optional[str] = None) -> None:
"""Invalidate the cache for the given path."""
if path is None:
self.dircache.clear()
else:
path = self._strip_protocol(path)
self.dircache.pop(path, None)
while path:
self.dircache.pop(path, None)
path = self._parent(path)

def ls(
self,
path: str,
Expand Down Expand Up @@ -270,6 +281,22 @@ def _listdir(
logger.error("Tosfs failed with unknown error: %s", e)
raise TosfsError(f"Tosfs failed with unknown error: {e}") from e

def _rm(self, path: str) -> None:
bucket, key, _ = self._split_path(path)
self.invalidate_cache(path)

try:
self.tos_client.delete_object(bucket, key)
except tos.exceptions.TosClientError as e:
logger.error("Tosfs failed with client error: %s", e)
raise e
except tos.exceptions.TosServerError as e:
logger.error("Tosfs failed with server error: %s", e)
raise e
except Exception as e:
logger.error("Tosfs failed with unknown error: %s", e)
raise TosfsError(f"Tosfs failed with unknown error: {e}") from e

def _split_path(self, path: str) -> Tuple[str, str, Optional[str]]:
"""Normalise tos path string into bucket and key.
Expand Down
34 changes: 25 additions & 9 deletions tosfs/tests/test_tosfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from unittest.mock import MagicMock

import pytest
from tos.exceptions import TosServerError

from tosfs.core import TosFileSystem
from tosfs.utils import random_path


def test_ls_bucket(tosfs: TosFileSystem, bucket: str) -> None:
Expand Down Expand Up @@ -51,19 +52,34 @@ def test_ls_dir(tosfs: TosFileSystem, bucket: str, temporary_workspace: str) ->


def test_ls_cache(tosfs: TosFileSystem, bucket: str) -> None:
tosfs.tos_client.list_objects_type2 = MagicMock(
with unittest.mock.patch.object(
tosfs.tos_client,
"list_objects_type2",
return_value=MagicMock(
is_truncated=False,
contents=[MagicMock(key="mock_key", size=123)],
common_prefixes=[],
next_continuation_token=None,
)
),
):
# Call ls method and get result from server
tosfs.ls(bucket, detail=False, refresh=True)
# Get result from cache
tosfs.ls(bucket, detail=False, refresh=False)

# Verify that list_objects_type2 was called only once
assert tosfs.tos_client.list_objects_type2.call_count == 1


def test_inner_rm(tosfs: TosFileSystem, bucket: str, temporary_workspace: str) -> None:
file_name = random_path()
tosfs.tos_client.put_object(bucket=bucket, key=f"{temporary_workspace}/{file_name}")
assert f"{bucket}/{temporary_workspace}/{file_name}" in tosfs.ls(
f"{bucket}/{temporary_workspace}", detail=False
)

# Call ls method and get result from server
tosfs.ls(bucket, detail=False, refresh=True)
# Get result from cache
tosfs.ls(bucket, detail=False, refresh=False)
tosfs._rm(f"{bucket}/{temporary_workspace}/{file_name}")

assert tosfs.ls(f"{bucket}/{temporary_workspace}", detail=False) == []

# Verify that list_objects_type2 was called only once
assert tosfs.tos_client.list_objects_type2.call_count == 1
tosfs._rm(f"{bucket}/{temporary_workspace}/{file_name}")

0 comments on commit 69fdc87

Please sign in to comment.