Skip to content

Commit

Permalink
Cache object information when checking if a file exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
asnare committed Jul 16, 2024
1 parent 9423c6c commit 2e9a3ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/databricks/labs/blueprint/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def exists(self, *, follow_symlinks: bool = True) -> bool:
if not follow_symlinks:
raise NotImplementedError("follow_symlinks=False is not supported for DBFS")
try:
self._ws.dbfs.get_status(self.as_posix())
self._cached_file_info = self._ws.dbfs.get_status(self.as_posix())
return True
except NotFound:
return False
Expand Down Expand Up @@ -741,7 +741,7 @@ def exists(self, *, follow_symlinks: bool = True) -> bool:
if not follow_symlinks:
raise NotImplementedError("follow_symlinks=False is not supported for Databricks Workspace")
try:
self._ws.workspace.get_status(self.as_posix())
self._cached_object_info = self._ws.workspace.get_status(self.as_posix())
return True
except NotFound:
return False
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,21 @@ def test_iterdir() -> None:
def test_exists_when_path_exists() -> None:
ws = create_autospec(WorkspaceClient)
workspace_path = WorkspacePath(ws, "/test/path")
ws.workspace.get_status.return_value = True
ws.workspace.get_status.return_value = ObjectInfo(path="/test/path")
assert workspace_path.exists()


def test_exists_caches_info() -> None:
ws = create_autospec(WorkspaceClient)
workspace_path = WorkspacePath(ws, "/test/path")
ws.workspace.get_status.return_value = ObjectInfo(path="/test/path", object_type=ObjectType.FILE)
_ = workspace_path.exists()

ws.workspace.get_status.reset_mock()
_ = workspace_path.is_file()
assert not ws.workspace.get_status.called


def test_exists_when_path_does_not_exist() -> None:
ws = create_autospec(WorkspaceClient)
workspace_path = WorkspacePath(ws, "/test/path")
Expand Down

0 comments on commit 2e9a3ec

Please sign in to comment.