jayceslesar commented on code in PR #1958: URL: https://github.com/apache/iceberg-python/pull/1958#discussion_r2370284792
########## pyiceberg/table/maintenance.py: ########## @@ -0,0 +1,130 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, 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. +from __future__ import annotations + +import logging +from datetime import datetime, timedelta, timezone +from functools import reduce +from typing import TYPE_CHECKING, Optional, Set, cast +from urllib.parse import urlparse + +from pyiceberg.utils.concurrent import ExecutorFactory + +logger = logging.getLogger(__name__) + + +if TYPE_CHECKING: + from pyiceberg.table import Table + + +class MaintenanceTable: + tbl: Table + + def __init__(self, tbl: Table) -> None: + self.tbl = tbl + + try: + import pyarrow as pa # noqa + except ModuleNotFoundError as e: + raise ModuleNotFoundError("For metadata operations PyArrow needs to be installed") from e + + def _orphaned_files(self, location: str, older_than: Optional[timedelta] = None) -> Set[str]: + """Get all files which are not referenced in any metadata files of an Iceberg table and can thus be considered "orphaned". + + Args: + location: The location to check for orphaned files. + older_than: The time period to check for orphaned files. Defaults to 3 days. + + Returns: + A set of orphaned file paths. + """ + from pyiceberg.io.fsspec import FsspecFileIO + + if older_than is None: + older_than = timedelta(0) + as_of = datetime.now(timezone.utc) - older_than + + if isinstance(self.tbl.io, FsspecFileIO): + logger.info("Determined io to be FsspecFileIO.") + uri = urlparse(location) + fs = self.tbl.io.get_fs(uri.scheme) + all_files = [f for f in fs.glob(f"{location}/**") if fs.isfile(f) and fs.modified(f) < as_of] + else: + logger.info("Determined io to be PyArrowFileIO.") + try: + import pyarrow as pa # noqa: F401 + except ModuleNotFoundError as e: + raise ModuleNotFoundError( + "For deleting orphaned files with a PyArrowFileIO, PyArrow needs to be installed" + ) from e Review Comment: We dont ask if its pyarrowfilio we ask if it isnt fsspecfilio -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
