Skip to content

Commit

Permalink
Removed KnowledgeCommunity (#8427)
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink authored Jan 30, 2025
2 parents 4b06f9c + 3c5fde1 commit 648ba2f
Show file tree
Hide file tree
Showing 34 changed files with 18 additions and 3,050 deletions.
37 changes: 2 additions & 35 deletions src/tribler/core/components.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Type, cast
from typing import TYPE_CHECKING, cast

from ipv8.bootstrapping.dispersy.bootstrapper import DispersyBootstrapper
from ipv8.community import Community
Expand Down Expand Up @@ -91,7 +91,7 @@ def get_overlay_class(self) -> type[Community]:
"""
Create a fake Community.
"""
return cast(Type[Community], type(f"{self.__class__.__name__}", (Component,), {}))
return cast(type[Community], type(f"{self.__class__.__name__}", (Component,), {}))

def get_my_peer(self, ipv8: IPv8, session: Session) -> Peer:
"""
Expand Down Expand Up @@ -138,16 +138,12 @@ def prepare(self, ipv8: IPv8, session: Session) -> None:
Create the database instances we need for Tribler.
"""
from tribler.core.database.store import MetadataStore
from tribler.core.database.tribler_database import TriblerDatabase
from tribler.core.notifier import Notification

db_path = str(Path(session.config.get_version_state_dir()) / "sqlite" / "tribler.db")
mds_path = str(Path(session.config.get_version_state_dir()) / "sqlite" / "metadata.db")
if session.config.get("memory_db"):
db_path = ":memory:"
mds_path = ":memory:"

session.db = TriblerDatabase(db_path)
session.mds = MetadataStore(
mds_path,
session.ipv8.keys["anonymous id"].key,
Expand All @@ -166,7 +162,6 @@ def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None:
db_endpoint = session.rest_manager.get_endpoint("/api/metadata")
db_endpoint.download_manager = session.download_manager
db_endpoint.mds = session.mds
db_endpoint.tribler_db = session.db

def get_endpoints(self) -> list[RESTEndpoint]:
"""
Expand All @@ -177,34 +172,6 @@ def get_endpoints(self) -> list[RESTEndpoint]:
return [*super().get_endpoints(), DatabaseEndpoint()]


@set_in_session("knowledge_community")
@after("DatabaseComponent")
@precondition('session.config.get("database/enabled")')
@precondition('session.config.get("knowledge_community/enabled")')
@overlay("tribler.core.knowledge.community", "KnowledgeCommunity")
@kwargs(db="session.db", key='session.ipv8.keys["secondary"].key')
class KnowledgeComponent(BaseLauncher):
"""
Launch instructions for the knowledge community.
"""

def finalize(self, ipv8: IPv8, session: Session, community: Community) -> None:
"""
When we are done launching, register our REST API.
"""
endpoint = session.rest_manager.get_endpoint("/api/knowledge")
endpoint.db = session.db
endpoint.community = community

def get_endpoints(self) -> list[RESTEndpoint]:
"""
Add the knowledge endpoint.
"""
from tribler.core.knowledge.restapi.knowledge_endpoint import KnowledgeEndpoint

return [*super().get_endpoints(), KnowledgeEndpoint()]


@after("DatabaseComponent")
@precondition('session.config.get("rendezvous/enabled")')
@overlay("tribler.core.rendezvous.community", "RendezvousCommunity")
Expand Down
33 changes: 3 additions & 30 deletions src/tribler/core/content_discovery/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from binascii import hexlify, unhexlify
from importlib.metadata import PackageNotFoundError, version
from itertools import count
from typing import TYPE_CHECKING, Any, Callable, Sequence
from typing import TYPE_CHECKING, Any, Callable

from ipv8.community import Community, CommunitySettings
from ipv8.lazy_community import lazy_wrapper
Expand All @@ -24,18 +24,17 @@
VersionRequest,
VersionResponse,
)
from tribler.core.database.layers.knowledge import ResourceType
from tribler.core.database.orm_bindings.torrent_metadata import LZ4_EMPTY_ARCHIVE, entries_to_chunk
from tribler.core.database.store import MetadataStore, ObjState, ProcessingResult
from tribler.core.knowledge.community import is_valid_resource
from tribler.core.notifier import Notification, Notifier
from tribler.core.torrent_checker.dataclasses import HealthInfo

if TYPE_CHECKING:
from collections.abc import Sequence

from ipv8.types import Peer

from tribler.core.database.orm_bindings.torrent_metadata import TorrentMetadata
from tribler.core.database.tribler_database import TriblerDatabase
from tribler.core.torrent_checker.torrent_checker import TorrentChecker


Expand All @@ -55,7 +54,6 @@ class ContentDiscoverySettings(CommunitySettings):

metadata_store: MetadataStore
torrent_checker: TorrentChecker
tribler_db: TriblerDatabase | None = None
notifier: Notifier | None = None


Expand Down Expand Up @@ -316,33 +314,8 @@ async def process_rpc_query(self, sanitized_parameters: dict[str, Any]) -> list:
:raises ValueError: if no JSON could be decoded.
:raises pony.orm.dbapiprovider.OperationalError: if an illegal query was performed.
"""
if self.composition.tribler_db:
# tags should be extracted because `get_entries_threaded` doesn't expect them as a parameter
tags = sanitized_parameters.pop("tags", None)

infohash_set = self.composition.tribler_db.instance(self.search_for_tags, tags)
if infohash_set:
sanitized_parameters["infohash_set"] = {bytes.fromhex(s) for s in infohash_set}

# exclude_deleted should be extracted because `get_entries_threaded` doesn't expect it as a parameter
sanitized_parameters.pop("exclude_deleted", None)

return await self.composition.metadata_store.get_entries_threaded(**sanitized_parameters)

@db_session
def search_for_tags(self, tags: list[str] | None) -> set[str] | None:
"""
Query our local database for the given tags.
"""
if not tags or not self.composition.tribler_db:
return None
valid_tags = {tag for tag in tags if is_valid_resource(tag)}
return self.composition.tribler_db.knowledge.get_subjects_intersection(
subjects_type=ResourceType.TORRENT,
objects=valid_tags,
predicate=ResourceType.TAG,
case_sensitive=False
)

def send_db_results(self, peer: Peer, request_payload_id: int, db_results: list[TorrentMetadata]) -> None:
"""
Expand Down
Empty file.
171 changes: 0 additions & 171 deletions src/tribler/core/database/layers/health.py

This file was deleted.

Loading

0 comments on commit 648ba2f

Please sign in to comment.