Skip to content

Commit

Permalink
Revert "refactor(internals): use a weakref proxy object for the refco…
Browse files Browse the repository at this point in the history
…unted cache"

This reverts commit b38d80f.
  • Loading branch information
ncclementi committed Aug 26, 2024
1 parent 0a215fd commit 9287a91
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
17 changes: 8 additions & 9 deletions ibis/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
class TablesAccessor(collections.abc.Mapping):
"""A mapping-like object for accessing tables off a backend.
::: {.callout-note}
## The `tables` accessor is tied to the lifetime of the backend.
If the backend goes out of scope, the `tables` accessor is no longer valid.
:::
Tables may be accessed by name using either index or attribute access:
Examples
Expand Down Expand Up @@ -82,15 +76,15 @@ def _tables(self) -> list[str]:

def __getitem__(self, name) -> ir.Table:
try:
return self._backend.table(name)
return self._backend().table(name)
except Exception as exc:
raise KeyError(name) from exc

def __getattr__(self, name) -> ir.Table:
if name.startswith("_"):
raise AttributeError(name)
try:
return self._backend.table(name)
return self._backend().table(name)
except Exception as exc:
raise AttributeError(name) from exc

Expand Down Expand Up @@ -879,7 +873,12 @@ def __init__(self, *args, **kwargs):
self._con_args: tuple[Any] = args
self._con_kwargs: dict[str, Any] = kwargs
self._can_reconnect: bool = True
self._query_cache = RefCountedCache(weakref.proxy(self))
# expression cache
self._query_cache = RefCountedCache(
populate=self._load_into_cache,
lookup=lambda name: self.table(name).op(),
finalize=self._clean_up_cached_table,
)

@property
@abc.abstractmethod
Expand Down
27 changes: 17 additions & 10 deletions ibis/common/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import functools
import sys
import weakref
from collections import namedtuple
from typing import TYPE_CHECKING, Any
from weakref import finalize, ref

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -39,8 +39,17 @@ class RefCountedCache:
We can implement that interface if and when we need to.
"""

def __init__(self, backend: weakref.proxy) -> None:
self.backend = backend
def __init__(
self,
*,
populate: Callable[[str, Any], None],
lookup: Callable[[str], Any],
finalize: Callable[[Any], None],
) -> None:
self.populate = populate
self.lookup = lookup
self.finalize = finalize

self.cache: dict[Any, CacheEntry] = dict()

def get(self, key, default=None):
Expand All @@ -61,13 +70,11 @@ def store(self, input):

key = input.op()
name = gen_name("cache")
self.populate(name, input)
cached = self.lookup(name)
finalizer = finalize(cached, self._release, key)

self.backend._load_into_cache(name, input)

cached = self.backend.table(name).op()
finalizer = weakref.finalize(cached, self._release, key)

self.cache[key] = CacheEntry(name, weakref.ref(cached), finalizer)
self.cache[key] = CacheEntry(name, ref(cached), finalizer)

return cached

Expand All @@ -81,7 +88,7 @@ def release(self, name: str) -> None:
def _release(self, key) -> None:
entry = self.cache.pop(key)
try:
self.backend._clean_up_cached_table(entry.name)
self.finalize(entry.name)
except Exception:
# suppress exceptions during interpreter shutdown
if not sys.is_finalizing():
Expand Down

0 comments on commit 9287a91

Please sign in to comment.