-
Notifications
You must be signed in to change notification settings - Fork 9
[FXC-5651] feat: add client-side OBB computation via DraftContext.compute_obb() #1931
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
71fe638
[FXC-5651] feat: add client-side OBB computation via DraftContext.com…
benflexcompute 0c2ad03
[FXC-5651] refactor: move rotation_axis_hint to compute_obb(), make a…
benflexcompute 524b8c5
linter
benflexcompute 5a6e6f0
[FXC-5651] fix: skip caching entries that exceed total cache size limit
benflexcompute 3d70686
[FXC-5651] fix: reject non-Surface selectors in compute_obb() upfront
benflexcompute c140dab
[FXC-5651] fix: prevent self-eviction and overwrite size overestimati…
benflexcompute 1e35f61
[FXC-5651] fix: face index collision detection + singleton CloudFileC…
benflexcompute b091ed9
format
benflexcompute ed456b3
[FXC-5651] fix: handle zero extents in circularity heuristic
benflexcompute c4c9e83
[FXC-5651] fix: filter out MirroredSurface in compute_obb()
benflexcompute 7eee5ee
format
benflexcompute 9fcfa64
[FXC-5651] refactor: deduplicate MockEntityList into shared _Selector…
benflexcompute d336e1c
[FXC-5651] fix: use monkeypatch instead of chmod for write-failure test
benflexcompute 78232b3
[FXC-5651] fix: descriptive error for unknown face IDs in tessellatio…
benflexcompute 52f738c
[FXC-5651] fix: clear error when faces yield no vertex data
benflexcompute aa4f393
[FXC-5651] fix: consistent cache size accounting + EntityRegistryView…
benflexcompute f5009cc
[FXC-5651] fix: type validation for else branch, path traversal guard…
benflexcompute e32bf53
[FXC-5651] fix: safe .name access in non-Surface warning log
benflexcompute 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| """General-purpose size-based LRU disk cache for cloud file downloads. | ||
|
|
||
| Stores files under ``~/.flow360/cache/<namespace>/<resource_id>/<file_path>`` | ||
| with a configurable total size limit. Eviction granularity is the resource | ||
| directory — all files for a resource are deleted together to avoid partial | ||
| state (e.g. manifest present but bin evicted). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| from pathlib import Path | ||
| from typing import List, Optional, Tuple | ||
|
|
||
| from ..log import log | ||
|
|
||
| CLOUD_FILE_CACHE_MAX_SIZE_MB: int = 2048 # default 2 GB, user-adjustable | ||
|
|
||
| _shared_cache_instance: Optional["CloudFileCache"] = None | ||
|
|
||
|
|
||
| def get_shared_cloud_file_cache() -> "CloudFileCache": | ||
| """Return the module-level shared CloudFileCache instance (created on first call).""" | ||
| global _shared_cache_instance # pylint: disable=global-statement | ||
| if _shared_cache_instance is None: | ||
| _shared_cache_instance = CloudFileCache() | ||
| return _shared_cache_instance | ||
|
|
||
|
|
||
| class CloudFileCache: | ||
| """Size-based LRU disk cache. | ||
|
|
||
| Keys are ``(namespace, resource_id, file_path)`` triples. | ||
| All namespaces share a single total-size budget. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| cache_root: Optional[Path] = None, | ||
| max_size_bytes: Optional[int] = None, | ||
| ) -> None: | ||
| self._cache_root = (cache_root or Path("~/.flow360/cache")).expanduser() | ||
| self._max_size_bytes = ( | ||
| CLOUD_FILE_CACHE_MAX_SIZE_MB * 1024 * 1024 if max_size_bytes is None else max_size_bytes | ||
| ) | ||
| self._disabled = False | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Public API | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def get(self, namespace: str, resource_id: str, file_path: str) -> Optional[bytes]: | ||
| """Return cached bytes or ``None``. Touches ``.last_access`` on hit.""" | ||
| if self._disabled: | ||
| return None | ||
|
|
||
| target = self._file_path(namespace, resource_id, file_path) | ||
| if not target.is_file(): | ||
| return None | ||
|
|
||
| try: | ||
| data = target.read_bytes() | ||
| except OSError: | ||
| return None | ||
|
|
||
| self._touch_last_access(namespace, resource_id) | ||
| return data | ||
|
|
||
| def put(self, namespace: str, resource_id: str, file_path: str, data: bytes) -> None: | ||
| """Write *data* to disk, evicting oldest resources if over size limit.""" | ||
| if self._disabled: | ||
| return | ||
|
|
||
| # Skip caching entries that exceed the entire cache budget | ||
| if len(data) > self._max_size_bytes: | ||
| return | ||
|
|
||
| try: | ||
| # Account for the file being overwritten (net size delta, not gross) | ||
| target = self._file_path(namespace, resource_id, file_path) | ||
| existing_size = target.stat().st_size if target.is_file() else 0 | ||
| net_incoming = len(data) - existing_size | ||
|
|
||
| current_resource_dir = self._resource_dir(namespace, resource_id) | ||
| self._evict_if_needed(net_incoming, protect=current_resource_dir) | ||
|
|
||
| target.parent.mkdir(parents=True, exist_ok=True) | ||
| target.write_bytes(data) | ||
| self._touch_last_access(namespace, resource_id) | ||
| except OSError as exc: | ||
| log.warning(f"CloudFileCache: disk write failed ({exc}), disabling cache") | ||
| self._disabled = True | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Internals | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def _file_path(self, namespace: str, resource_id: str, file_path: str) -> Path: | ||
| target = (self._cache_root / namespace / resource_id / file_path).resolve() | ||
| if not target.is_relative_to(self._cache_root.resolve()): | ||
| raise ValueError(f"Path traversal detected in cache key: {file_path!r}") | ||
| return target | ||
|
|
||
| def _resource_dir(self, namespace: str, resource_id: str) -> Path: | ||
| return self._cache_root / namespace / resource_id | ||
|
|
||
| def _last_access_path(self, namespace: str, resource_id: str) -> Path: | ||
| return self._resource_dir(namespace, resource_id) / ".last_access" | ||
|
|
||
| def _touch_last_access(self, namespace: str, resource_id: str) -> None: | ||
| """Create or update the ``.last_access`` sentinel in the resource dir.""" | ||
| path = self._last_access_path(namespace, resource_id) | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| path.touch() | ||
|
|
||
| def _collect_resource_dirs(self) -> Tuple[int, List[Tuple[float, int, Path]]]: | ||
| """Scan cache and return ``(total_size, [(mtime, size, dir), ...])``. | ||
|
|
||
| Single pass: computes both the aggregate size and per-resource metadata | ||
| needed for LRU eviction. | ||
| """ | ||
| entries: List[Tuple[float, int, Path]] = [] | ||
| total_size = 0 | ||
| if not self._cache_root.exists(): | ||
| return total_size, entries | ||
|
|
||
| for namespace_dir in self._cache_root.iterdir(): | ||
| if not namespace_dir.is_dir(): | ||
| continue | ||
| for resource_dir in namespace_dir.iterdir(): | ||
| if not resource_dir.is_dir(): | ||
| continue | ||
| last_access = resource_dir / ".last_access" | ||
| mtime = last_access.stat().st_mtime if last_access.exists() else 0.0 | ||
| size = sum( | ||
| f.stat().st_size | ||
| for f in resource_dir.rglob("*") | ||
| if f.is_file() and f.name != ".last_access" | ||
| ) | ||
| total_size += size | ||
| entries.append((mtime, size, resource_dir)) | ||
| return total_size, entries | ||
|
|
||
| def _evict_if_needed(self, incoming_bytes: int, protect: Optional[Path] = None) -> None: | ||
| """Delete oldest resource dirs until total size + *incoming_bytes* fits the budget. | ||
|
|
||
| *protect*, if given, is a resource directory that must not be evicted | ||
| (the resource currently being populated by the caller). | ||
| """ | ||
| current_size, entries = self._collect_resource_dirs() | ||
| if current_size + incoming_bytes <= self._max_size_bytes: | ||
| return | ||
|
|
||
| # Sort by last-access time ascending (oldest first) | ||
| entries.sort(key=lambda e: e[0]) | ||
|
|
||
| for _mtime, size, resource_dir in entries: | ||
| if current_size + incoming_bytes <= self._max_size_bytes: | ||
| break | ||
| if protect is not None and resource_dir == protect: | ||
| continue | ||
| shutil.rmtree(resource_dir, ignore_errors=True) | ||
| current_size -= size | ||
benflexcompute marked this conversation as resolved.
Show resolved
Hide resolved
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.