Skip to content

Include the query path in Query.fragment's cache key - #650

Open
waw666waw666 wants to merge 1 commit into
msiemens:masterfrom
waw666waw666:fix-fragment-query-cache-key
Open

waw666waw666 wants to merge 1 commit into
msiemens:masterfrom
waw666waw666:fix-fragment-query-cache-key

Conversation

@waw666waw666

Copy link
Copy Markdown

Query.fragment() builds its cache hash from the fragment only and leaves out self._path. Every other query type includes the path. As a result, the same fragment on two different fields shares one entry in the table's query cache, and Table.search() returns the first query's results for the second one:

db = TinyDB(storage=MemoryStorage)
db.insert({'home': {'city': 'Paris'}, 'work': {'city': 'Rome'}})

db.search(Query().home.fragment({'city': 'Paris'}))  # [{'home': ..., 'work': ...}]
db.search(Query().work.fragment({'city': 'Paris'}))  # same document again, but should be []

The fix adds the path to the hash. I added a test for the hash and a test with search() on two fields. Both fail without the change.

Query.fragment() left the path out of the hash used as the query cache
key, so Query().home.fragment(x) and Query().work.fragment(x) shared one
cache entry and Table.search() returned the first query's cached results
for the second one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant