Skip to content
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

Change tag search condition from or to and #6752

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def fill_tags_database():
Tag(name='tag1', count=SHOW_THRESHOLD),
],
b'infohash2': [
Tag(name='tag2', count=SHOW_THRESHOLD - 1),
Tag(name='tag1', count=SHOW_THRESHOLD - 1),
]
})

Expand All @@ -110,7 +110,7 @@ def _add(infohash):
fill_mds()

# Then we try to query search for three tags: 'tag1', 'tag2', 'tag3'
parameters = {'first': 0, 'infohash_set': None, 'last': 100, 'tags': ['tag1', 'tag2', 'tag3']}
parameters = {'first': 0, 'infohash_set': None, 'last': 100, 'tags': ['tag1']}
json = dumps(parameters).encode('utf-8')

with db_session:
Expand Down
22 changes: 18 additions & 4 deletions src/tribler-core/tribler_core/components/tag/db/tag_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Callable, List, Optional, Set

from pony import orm
from pony.orm import select
from pony.orm import exists, select
from pony.utils import between

from tribler_core.components.tag.community.tag_payload import TagOperation, TagOperationEnum
Expand Down Expand Up @@ -191,10 +191,24 @@ def show_suggestions_condition(torrent_tag):
return self._get_tags(infohash, show_suggestions_condition)

def get_infohashes(self, tags: Set[str]) -> List[bytes]:
"""Get list of infohashes that belongs to the tag. Only tags with condition `_show_condition` will be returned
"""Get list of infohashes that belongs to the tag.
Only tags with condition `_show_condition` will be returned.
In the case that the tags set contains more than one tag,
only torrents that contain all `tags` will be returned.
"""
return select(tt.torrent.infohash for tt in self.instance.TorrentTag
if self._show_condition(tt) and tt.tag.name in tags).fetch()
query_results = select(
torrent.infohash for torrent in self.instance.Torrent
if not exists(
tag for tag in self.instance.Tag
if tag.name in tags and not exists(
torrent_tag for torrent_tag in self.instance.TorrentTag
if torrent_tag.torrent == torrent
and torrent_tag.tag == tag
and self._show_condition(torrent_tag)
)
)
).fetch()
return query_results

def get_clock(self, operation: TagOperation) -> int:
""" Get the clock (int) of operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,47 @@ async def test_get_tags_operations_for_gossip(self):
# assert that only one torrent returned (the old and the not auto generated one)
assert len(self.db.get_tags_operations_for_gossip(time_delta)) == 1

@db_session
async def test_get_infohashes_threshold(self):
# test that `get_infohashes` function returns only infohashes with tags
# above the threshold
self.add_operation_set(
self.db,
{
b'infohash1': [
Tag(name='tag1', count=SHOW_THRESHOLD),
],
b'infohash2': [
Tag(name='tag1', count=SHOW_THRESHOLD - 1)
]
}
)

assert self.db.get_infohashes({'tag1'}) == [b'infohash1']

@db_session
async def test_get_infohashes(self):
# test that `get_infohashes` function returns an intersection of result
# in case of more than one tag passed to the function
self.add_operation_set(
self.db,
{
b'infohash1': [
Tag(name='tag1', count=SHOW_THRESHOLD),
Tag(name='tag2', count=SHOW_THRESHOLD - 1)
Tag(name='tag2', count=SHOW_THRESHOLD)
],
b'infohash2': [
Tag(name='tag1', count=SHOW_THRESHOLD)
],
b'infohash3': [
Tag(name='tag1', count=SHOW_THRESHOLD - 1)
Tag(name='tag2', count=SHOW_THRESHOLD)
]
}
)

# test that only tags above the threshold are associated with infohases
assert self.db.get_infohashes('tag1') == [b'infohash1', b'infohash2']
assert not self.db.get_infohashes('tag2')
assert self.db.get_infohashes({'tag1'}) == [b'infohash1', b'infohash2']
assert self.db.get_infohashes({'tag2'}) == [b'infohash1', b'infohash3']
assert self.db.get_infohashes({'tag1', 'tag2'}) == [b'infohash1']

@db_session
async def test_show_condition(self):
Expand Down