Skip to content

Commit

Permalink
Use get_file_status API to reduce rpc requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Nov 11, 2024
1 parent c4439c0 commit bf1c9d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
33 changes: 5 additions & 28 deletions tosfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,38 +573,15 @@ def exists(self, path: str, **kwargs: Any) -> bool:
return self._exists_bucket(bucket)

try:
return retryable_func_executor(
lambda: self.tos_client.head_object(bucket, key) and True,
resp = retryable_func_executor(
lambda: self.tos_client.get_file_status(bucket, key),
max_retry_num=self.max_retry_num,
)
return resp.key is not None
except TosServerError as e:
if e.status_code == TOS_SERVER_STATUS_CODE_NOT_FOUND:
try:
return retryable_func_executor(
lambda: self.tos_client.head_object(
bucket, key.rstrip("/") + "/"
)
and True,
max_retry_num=self.max_retry_num,
)
except TosServerError as ex:
if e.status_code == TOS_SERVER_STATUS_CODE_NOT_FOUND:
resp = retryable_func_executor(
lambda: self.tos_client.list_objects_type2(
bucket,
key.rstrip("/") + "/",
start_after=key.rstrip("/") + "/",
max_keys=1,
),
max_retry_num=self.max_retry_num,
)
return len(resp.contents) > 0
else:
raise ex
else:
raise e
except Exception as ex:
raise TosfsError(f"Tosfs failed with unknown error: {ex}") from ex
return False
raise e

def rm_file(self, path: str) -> None:
"""Delete a file."""
Expand Down
12 changes: 12 additions & 0 deletions tosfs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ def get_brange(size: int, block: int) -> Generator[Tuple[int, int], None, None]:
"""
for offset in range(0, size, block):
yield offset, min(offset + block - 1, size - 1)


def is_dir(key: str) -> bool:
"""Check if the key is a directory.
Parameters
----------
key : str
The key to check.
"""
return key.endswith("/")

0 comments on commit bf1c9d4

Please sign in to comment.