Skip to content

Commit

Permalink
Use subjects_type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Sep 28, 2023
1 parent fd21083 commit e343773
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from pony.orm import commit, db_session

from tribler.core.components.database.db.tribler_database import TriblerDatabase, Operation, \
PUBLIC_KEY_FOR_AUTO_GENERATED_OPERATIONS, ResourceType, SHOW_THRESHOLD, SimpleStatement
from tribler.core.components.database.db.tests.test_tribler_database_base import Resource, TestTagDBBase
from tribler.core.components.database.db.tribler_database import Operation, PUBLIC_KEY_FOR_AUTO_GENERATED_OPERATIONS, \
ResourceType, SHOW_THRESHOLD, SimpleStatement, TriblerDatabase
from tribler.core.utilities.pony_utils import TrackedDatabase, get_or_create


Expand Down Expand Up @@ -402,6 +402,12 @@ def test_get_subjects_intersection(self):
self.add_operation_set(
self.db,
{
('zero', ResourceType.TITLE): [
# It should not appear in the results due to the differing subject type, although it possesses all
# the necessary tags.
Resource(name='tag1'),
Resource(name='tag2'),
],
'infohash1': [
Resource(name='tag1'),
Resource(name='tag2'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ def generate_n_peer_names(n):
yield f'peer{next(index)}'.encode('utf8')

for subject, objects in dictionary.items():
subject_type = ResourceType.TORRENT
if isinstance(subject, tuple):
subject, subject_type = subject

for obj in objects:
for peer in generate_n_peer_names(obj.count):
# assume that for test purposes all subject by default could be `Predicate.TORRENT`
TestTagDBBase.add_operation(tag_db, ResourceType.TORRENT, subject, obj.predicate, obj.name, peer,
TestTagDBBase.add_operation(tag_db, subject_type, subject, obj.predicate, obj.name, peer,
is_auto_generated=obj.auto_generated)
11 changes: 5 additions & 6 deletions src/tribler/core/components/database/db/tribler_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,19 +361,18 @@ def get_suggestions(self, subject_type: Optional[ResourceType] = None, subject:
)
return suggestions

def get_subjects_intersection(self, subjects_type: Optional[ResourceType], objects: Set[str],
predicate: Optional[ResourceType],
case_sensitive: bool = True) -> Set[str]:
def get_subjects_intersection(self, objects: Set[str], predicate: Optional[ResourceType],
subjects_type: Optional[ResourceType] = ResourceType.TORRENT,
case_sensitive: bool = True) -> Set[str]: # pylint: disable=unused-argument
if not objects:
return set()

if case_sensitive:
name_condition = '"obj"."name" = $obj_name'
else:
name_condition = 'py_lower("obj"."name") = py_lower($obj_name)'

query = select(r.name for r in self.instance.Resource)
for obj_name in objects:
query = select(r.name for r in self.instance.Resource if r.type == subjects_type.value)
for obj_name in objects: # pylint: disable=unused-variable
query = query.filter(raw_sql(f"""
r.id IN (
SELECT "s"."subject"
Expand Down

0 comments on commit e343773

Please sign in to comment.