Skip to content

Cache database lookups #5784

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import sys
import time
import unicodedata
from functools import cached_property
from functools import cache, cached_property
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -1284,6 +1284,7 @@ def _getters(cls):
getters["albumtotal"] = Album._albumtotal
return getters

@cache
def items(self):
"""Return an iterable over the items associated with this
album.
Expand Down Expand Up @@ -1541,6 +1542,7 @@ def try_sync(self, write, move, inherit=True):
# Query construction helpers.


@cache
def parse_query_parts(parts, model_cls):
"""Given a beets query string as a list of components, return the
`Query` and `Sort` they represent.
Expand Down Expand Up @@ -1582,7 +1584,7 @@ def parse_query_string(s, model_cls):
parts = shlex.split(s)
except ValueError as exc:
raise dbcore.InvalidQueryError(s, exc)
return parse_query_parts(parts, model_cls)
return parse_query_parts(tuple(parts), model_cls)


# The Library: interface to the database.
Expand Down Expand Up @@ -1652,6 +1654,7 @@ def add_album(self, items):

# Querying.

@cache
def _fetch(self, model_cls, query, sort=None):
"""Parse a query and fetch.

Expand All @@ -1664,7 +1667,7 @@ def _fetch(self, model_cls, query, sort=None):
if isinstance(query, str):
query, parsed_sort = parse_query_string(query, model_cls)
elif isinstance(query, (list, tuple)):
query, parsed_sort = parse_query_parts(query, model_cls)
query, parsed_sort = parse_query_parts(tuple(query), model_cls)
except dbcore.query.InvalidQueryArgumentValueError as exc:
raise dbcore.InvalidQueryError(query, exc)

Expand All @@ -1691,11 +1694,19 @@ def get_default_item_sort():

def albums(self, query=None, sort=None) -> Results[Album]:
"""Get :class:`Album` objects matching the query."""
return self._fetch(Album, query, sort or self.get_default_album_sort())
return self._fetch(
Album,
tuple(query) if isinstance(query, list) else query,
sort or self.get_default_album_sort(),
)

def items(self, query=None, sort=None) -> Results[Item]:
"""Get :class:`Item` objects matching the query."""
return self._fetch(Item, query, sort or self.get_default_item_sort())
return self._fetch(
Item,
tuple(query) if isinstance(query, list) else query,
sort or self.get_default_item_sort(),
)

# Convenience accessors.

Expand Down
Loading