Skip to content

Commit

Permalink
Extract knowledge_db to DatabaseComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Sep 26, 2023
1 parent a83b384 commit f18c74e
Show file tree
Hide file tree
Showing 33 changed files with 65 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/tribler/core/components/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ipv8.keyvault.private.libnaclkey import LibNaCLSK
from ipv8.util import succeed

from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.components.libtorrent.download_manager.download_manager import DownloadManager
from tribler.core.components.libtorrent.settings import LibtorrentSettings
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions src/tribler/core/components/database/database_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from tribler.core.components.component import Component
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR


class DatabaseComponent(Component):
tribler_should_stop_on_component_error = True

db: KnowledgeDatabase = None

async def run(self):
await super().run()

db_path = self.session.config.state_dir / STATEDIR_DB_DIR / "tribler.db"
if self.session.config.gui_test_mode:
db_path = ":memory:"

self.db = KnowledgeDatabase(str(db_path), create_tables=True)

async def shutdown(self):
await super().shutdown()
if self.db:
self.db.shutdown()
Empty file.
Empty file.
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.knowledge.db.knowledge_db import KnowledgeDatabase, Operation, \
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, Operation, \
PUBLIC_KEY_FOR_AUTO_GENERATED_OPERATIONS, ResourceType, SHOW_THRESHOLD, SimpleStatement
from tribler.core.components.knowledge.db.tests.test_knowledge_db_base import Resource, TestTagDBBase
from tribler.core.components.database.db.tests.test_knowledge_db_base import Resource, TestTagDBBase
from tribler.core.utilities.pony_utils import TriblerDatabase, get_or_create


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pony.orm import commit, db_session

from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType, SHOW_THRESHOLD
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType, SHOW_THRESHOLD
from tribler.core.utilities.pony_utils import get_or_create


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ipv8.peerdiscovery.network import Network

from tribler.core.components.component import Component
from tribler.core.components.database.database_component import DatabaseComponent
from tribler.core.components.gigachannel.community.gigachannel_community import (
GigaChannelCommunity,
GigaChannelTestnetCommunity,
Expand All @@ -25,7 +26,7 @@ async def run(self):

self._ipv8_component = await self.require_component(Ipv8Component)
metadata_store_component = await self.require_component(MetadataStoreComponent)
knowledge_component = await self.get_component(KnowledgeComponent)
db_component = await self.get_component(DatabaseComponent)

giga_channel_cls = GigaChannelTestnetCommunity if config.general.testnet else GigaChannelCommunity
community = giga_channel_cls(
Expand All @@ -37,7 +38,7 @@ async def run(self):
rqc_settings=config.remote_query_community,
metadata_store=metadata_store_component.mds,
max_peers=50,
knowledge_db=knowledge_component.knowledge_db if knowledge_component else None
knowledge_db=db_component.db if db_component else None
)
self.community = community
self._ipv8_component.initialise_community_by_default(community, default_random_walk_max_peers=30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from tribler.core.components.knowledge.community.knowledge_validator import validate_operation, validate_resource, \
validate_resource_type
from tribler.core.components.knowledge.community.operations_requests import OperationsRequests, PeerValidationError
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase

REQUESTED_OPERATIONS_COUNT = 10

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tribler.core.components.knowledge.db.knowledge_db import Operation, ResourceType
from tribler.core.components.database.db.knowledge_db import Operation, ResourceType
from tribler.core.components.knowledge.knowledge_constants import MAX_RESOURCE_LENGTH, MIN_RESOURCE_LENGTH


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tribler.core.components.knowledge.community.knowledge_community import KnowledgeCommunity
from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType

REQUEST_INTERVAL_FOR_RANDOM_OPERATIONS = 0.1 # in seconds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from tribler.core.components.knowledge.community.knowledge_validator import is_valid_resource, validate_operation, \
validate_resource, validate_resource_type
from tribler.core.components.knowledge.db.knowledge_db import Operation, ResourceType
from tribler.core.components.database.db.knowledge_db import Operation, ResourceType

VALID_TAGS = [
'nl',
Expand Down
18 changes: 5 additions & 13 deletions src/tribler/core/components/knowledge/knowledge_component.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import tribler.core.components.metadata_store.metadata_store_component as metadata_store_component
from tribler.core.components.component import Component
from tribler.core.components.database.database_component import DatabaseComponent
from tribler.core.components.ipv8.ipv8_component import Ipv8Component
from tribler.core.components.key.key_component import KeyComponent
from tribler.core.components.knowledge.community.knowledge_community import KnowledgeCommunity
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.knowledge.rules.knowledge_rules_processor import KnowledgeRulesProcessor
from tribler.core.components.metadata_store.utils import generate_test_channels
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR


class KnowledgeComponent(Component):
tribler_should_stop_on_component_error = False

community: KnowledgeCommunity = None
knowledge_db: KnowledgeDatabase = None
rules_processor: KnowledgeRulesProcessor = None
_ipv8_component: Ipv8Component = None

Expand All @@ -23,36 +21,30 @@ async def run(self):
self._ipv8_component = await self.require_component(Ipv8Component)
key_component = await self.require_component(KeyComponent)
mds_component = await self.require_component(metadata_store_component.MetadataStoreComponent)
db_component = await self.require_component(DatabaseComponent)

db_path = self.session.config.state_dir / STATEDIR_DB_DIR / "knowledge.db"
if self.session.config.gui_test_mode:
db_path = ":memory:"

self.knowledge_db = KnowledgeDatabase(str(db_path), create_tables=True)
self.community = KnowledgeCommunity(
self._ipv8_component.peer,
self._ipv8_component.ipv8.endpoint,
self._ipv8_component.ipv8.network,
db=self.knowledge_db,
db=db_component.db,
key=key_component.secondary_key
)
self.rules_processor = KnowledgeRulesProcessor(
notifier=self.session.notifier,
db=self.knowledge_db,
db=db_component.db,
mds=mds_component.mds,
)
self.rules_processor.start()

self._ipv8_component.initialise_community_by_default(self.community)

if self.session.config.gui_test_mode:
generate_test_channels(mds_component.mds, self.knowledge_db)
generate_test_channels(mds_component.mds, db_component.db)

async def shutdown(self):
await super().shutdown()
if self._ipv8_component and self.community:
await self._ipv8_component.unload_community(self.community)
if self.rules_processor:
await self.rules_processor.shutdown()
if self.knowledge_db:
self.knowledge_db.shutdown()
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tribler.core.components.knowledge.community.knowledge_community import KnowledgeCommunity
from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.community.knowledge_validator import is_valid_resource
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.restapi.rest.rest_endpoint import HTTP_BAD_REQUEST, RESTEndpoint, RESTResponse
from tribler.core.components.restapi.rest.schema import HandledErrorSchema
from tribler.core.utilities.utilities import froze_it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from tribler.core.components.conftest import TEST_PERSONAL_KEY
from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.db.knowledge_db import Operation, ResourceType
from tribler.core.components.database.db.knowledge_db import Operation, ResourceType
from tribler.core.components.knowledge.restapi.knowledge_endpoint import KnowledgeEndpoint
from tribler.core.components.restapi.rest.base_api_test import do_request
from tribler.core.utilities.date_utils import freeze_time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pony.orm import db_session

from tribler.core import notifications
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.knowledge.rules.rules_content_items import content_items_rules
from tribler.core.components.knowledge.rules.rules_general_tags import general_rules
from tribler.core.components.knowledge.rules.tag_rules_base import extract_only_valid_tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ipv8.keyvault.private.libnaclkey import LibNaCLSK
from pony.orm import db_session

from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.knowledge.rules.knowledge_rules_processor import KnowledgeRulesProcessor
from tribler.core.components.metadata_store.db.serialization import REGULAR_TORRENT
from tribler.core.components.metadata_store.db.store import MetadataStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tribler.core.components.ipv8.eva.result import TransferResult
from tribler.core.components.ipv8.tribler_community import TriblerCommunity
from tribler.core.components.knowledge.community.knowledge_validator import is_valid_resource
from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.components.database.db.knowledge_db import ResourceType
from tribler.core.components.metadata_store.db.orm_bindings.channel_metadata import LZ4_EMPTY_ARCHIVE, entries_to_chunk
from tribler.core.components.metadata_store.db.serialization import CHANNEL_TORRENT, COLLECTION_NODE, REGULAR_TORRENT
from tribler.core.components.metadata_store.db.store import MetadataStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from ipv8.test.base import TestBase
from pony.orm import db_session

from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, ResourceType, SHOW_THRESHOLD
from tribler.core.components.knowledge.db.tests.test_knowledge_db import Resource, TestTagDB
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, ResourceType, SHOW_THRESHOLD
from tribler.core.components.database.db.tests.test_knowledge_db import Resource, TestTagDB
from tribler.core.components.metadata_store.db.orm_bindings.channel_node import NEW
from tribler.core.components.metadata_store.db.store import MetadataStore
from tribler.core.components.metadata_store.remote_query_community.remote_query_community import RemoteQueryCommunity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pony.orm import db_session

from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.knowledge.rules.knowledge_rules_processor import KnowledgeRulesProcessor
from tribler.core.components.metadata_store.category_filter.family_filter import default_xxx_filter
from tribler.core.components.metadata_store.db.serialization import CHANNEL_TORRENT, COLLECTION_NODE, REGULAR_TORRENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from marshmallow.fields import Integer, String
from pony.orm import db_session

from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.components.database.db.knowledge_db import ResourceType
from tribler.core.components.metadata_store.db.serialization import SNIPPET
from tribler.core.components.metadata_store.db.store import MetadataStore
from tribler.core.components.metadata_store.restapi.metadata_endpoint import MetadataEndpointBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pony.orm import db_session

from tribler.core.components.gigachannel.community.gigachannel_community import NoChannelSourcesException
from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.components.database.db.knowledge_db import ResourceType
from tribler.core.components.libtorrent.torrentdef import TorrentDef
from tribler.core.components.metadata_store.category_filter.family_filter import default_xxx_filter
from tribler.core.components.metadata_store.db.orm_bindings.channel_node import NEW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from pony.orm import db_session

from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.metadata_store.db.serialization import REGULAR_TORRENT, SNIPPET
from tribler.core.components.metadata_store.restapi.search_endpoint import SearchEndpoint
from tribler.core.components.restapi.rest.base_api_test import do_request
Expand Down
2 changes: 1 addition & 1 deletion src/tribler/core/components/metadata_store/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pony.orm import db_session

from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.knowledge.knowledge_constants import MIN_RESOURCE_LENGTH
from tribler.core.components.metadata_store.db.store import MetadataStore
from tribler.core.tests.tools.common import PNG_FILE
Expand Down
10 changes: 6 additions & 4 deletions src/tribler/core/components/restapi/restapi_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tribler.core.components.bandwidth_accounting.bandwidth_accounting_component import BandwidthAccountingComponent
from tribler.core.components.bandwidth_accounting.restapi.bandwidth_endpoint import BandwidthEndpoint
from tribler.core.components.component import Component
from tribler.core.components.database.database_component import DatabaseComponent
from tribler.core.components.exceptions import NoneComponent
from tribler.core.components.gigachannel.gigachannel_component import GigaChannelComponent
from tribler.core.components.gigachannel_manager.gigachannel_manager_component import GigachannelManagerComponent
Expand Down Expand Up @@ -84,6 +85,7 @@ async def run(self):
tunnel_component = await self.maybe_component(TunnelsComponent)
torrent_checker_component = await self.maybe_component(TorrentCheckerComponent)
gigachannel_manager_component = await self.maybe_component(GigachannelManagerComponent)
db_component = await self.maybe_component(DatabaseComponent)

public_key = key_component.primary_key.key.pk if not isinstance(key_component, NoneComponent) else b''
self._events_endpoint = EventsEndpoint(notifier, public_key=hexlify(public_key))
Expand All @@ -109,15 +111,15 @@ async def run(self):
self.maybe_add(LibTorrentEndpoint, libtorrent_component.download_manager)
self.maybe_add(TorrentInfoEndpoint, libtorrent_component.download_manager)
self.maybe_add(MetadataEndpoint, torrent_checker, metadata_store_component.mds,
knowledge_db=knowledge_component.knowledge_db,
knowledge_db=db_component.db,
tag_rules_processor=knowledge_component.rules_processor)
self.maybe_add(ChannelsEndpoint, libtorrent_component.download_manager, gigachannel_manager,
gigachannel_component.community, metadata_store_component.mds,
knowledge_db=knowledge_component.knowledge_db,
knowledge_db=db_component.db,
tag_rules_processor=knowledge_component.rules_processor)
self.maybe_add(SearchEndpoint, metadata_store_component.mds, knowledge_db=knowledge_component.knowledge_db)
self.maybe_add(SearchEndpoint, metadata_store_component.mds, knowledge_db=db_component.db)
self.maybe_add(RemoteQueryEndpoint, gigachannel_component.community, metadata_store_component.mds)
self.maybe_add(KnowledgeEndpoint, db=knowledge_component.knowledge_db, community=knowledge_component.community)
self.maybe_add(KnowledgeEndpoint, db=db_component.db, community=knowledge_component.community)

if not isinstance(ipv8_component, NoneComponent):
ipv8_root_endpoint = IPV8RootEndpoint()
Expand Down
2 changes: 2 additions & 0 deletions src/tribler/core/start_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from tribler.core.components.bandwidth_accounting.bandwidth_accounting_component import BandwidthAccountingComponent
from tribler.core.components.component import Component
from tribler.core.components.database.database_component import DatabaseComponent
from tribler.core.components.gigachannel.gigachannel_component import GigaChannelComponent
from tribler.core.components.gigachannel_manager.gigachannel_manager_component import GigachannelManagerComponent
from tribler.core.components.gui_process_watcher.gui_process_watcher import GuiProcessWatcher
Expand Down Expand Up @@ -55,6 +56,7 @@ def components_gen(config: TriblerConfig):
"""
yield ReporterComponent()
yield GuiProcessWatcherComponent()
yield DatabaseComponent()
yield RESTComponent()
if config.chant.enabled or config.torrent_checking.enabled:
yield MetadataStoreComponent()
Expand Down
2 changes: 1 addition & 1 deletion src/tribler/core/upgrade/tags_to_knowledge/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pony.orm import db_session, select

from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.upgrade.tags_to_knowledge.tags_db import TagDatabase
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pony.orm import db_session

from tribler.core.components.knowledge.community.knowledge_payload import StatementOperation
from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase
from tribler.core.upgrade.tags_to_knowledge.migration import MigrationTagsToKnowledge
from tribler.core.upgrade.tags_to_knowledge.tags_db import TagDatabase
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR
Expand Down
2 changes: 1 addition & 1 deletion src/tribler/gui/dialogs/editmetadatadialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from PyQt5.QtCore import QModelIndex, QPoint, Qt, pyqtSignal
from PyQt5.QtWidgets import QComboBox, QSizePolicy, QWidget

from tribler.core.components.knowledge.db.knowledge_db import ResourceType
from tribler.core.components.database.db.knowledge_db import ResourceType
from tribler.core.components.knowledge.knowledge_constants import MAX_RESOURCE_LENGTH, MIN_RESOURCE_LENGTH
from tribler.gui.defs import TAG_HORIZONTAL_MARGIN
from tribler.gui.dialogs.dialogcontainer import DialogContainer
Expand Down
Loading

0 comments on commit f18c74e

Please sign in to comment.