Skip to content

Commit

Permalink
Extract knowledge_db to the DatabaseComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Sep 26, 2023
1 parent a83b384 commit afa20e5
Show file tree
Hide file tree
Showing 40 changed files with 82 additions and 55 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
@@ -1,3 +1,4 @@
from tribler.core.components.database.database_component import DatabaseComponent
from tribler.core.components.gigachannel.gigachannel_component import GigaChannelComponent
from tribler.core.components.ipv8.ipv8_component import Ipv8Component
from tribler.core.components.key.key_component import KeyComponent
Expand All @@ -13,7 +14,7 @@ async def test_giga_channel_component(tribler_config):
tribler_config.ipv8.enabled = True
tribler_config.libtorrent.enabled = True
tribler_config.chant.enabled = True
components = [KnowledgeComponent(), MetadataStoreComponent(), KeyComponent(), Ipv8Component(),
components = [DatabaseComponent(), KnowledgeComponent(), MetadataStoreComponent(), KeyComponent(), Ipv8Component(),
GigaChannelComponent()]
async with Session(tribler_config, components) as session:
comp = session.get_instance(GigaChannelComponent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tribler.core.components.database.database_component import DatabaseComponent
from tribler.core.components.gigachannel_manager.gigachannel_manager_component import GigachannelManagerComponent
from tribler.core.components.ipv8.ipv8_component import Ipv8Component
from tribler.core.components.key.key_component import KeyComponent
Expand All @@ -12,9 +13,8 @@


async def test_gigachannel_manager_component(tribler_config):
components = [Ipv8Component(), KnowledgeComponent(), SocksServersComponent(), KeyComponent(),
MetadataStoreComponent(),
LibtorrentComponent(), GigachannelManagerComponent()]
components = [DatabaseComponent(), Ipv8Component(), KnowledgeComponent(), SocksServersComponent(), KeyComponent(),
MetadataStoreComponent(), LibtorrentComponent(), GigachannelManagerComponent()]
async with Session(tribler_config, components) as session:
comp = session.get_instance(GigachannelManagerComponent)
assert comp.started_event.is_set() and not comp.failed
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
@@ -1,3 +1,4 @@
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.knowledge_component import KnowledgeComponent
Expand All @@ -9,7 +10,7 @@


async def test_tag_component(tribler_config):
components = [MetadataStoreComponent(), KeyComponent(), Ipv8Component(), KnowledgeComponent()]
components = [DatabaseComponent(), MetadataStoreComponent(), KeyComponent(), Ipv8Component(), KnowledgeComponent()]
async with Session(tribler_config, components) as session:
comp = session.get_instance(KnowledgeComponent)
assert comp.started_event.is_set() and not comp.failed
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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.knowledge_component import KnowledgeComponent
Expand All @@ -9,7 +10,7 @@


async def test_metadata_store_component(tribler_config):
components = [KnowledgeComponent(), Ipv8Component(), KeyComponent(), MetadataStoreComponent()]
components = [DatabaseComponent(), KnowledgeComponent(), Ipv8Component(), KeyComponent(), MetadataStoreComponent()]
async with Session(tribler_config, components) as session:
comp = session.get_instance(MetadataStoreComponent)
assert comp.started_event.is_set() and not comp.failed
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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.knowledge_component import KnowledgeComponent
Expand All @@ -13,8 +14,9 @@


async def test_popularity_component(tribler_config):
components = [SocksServersComponent(), LibtorrentComponent(), TorrentCheckerComponent(), KnowledgeComponent(),
MetadataStoreComponent(), KeyComponent(), Ipv8Component(), PopularityComponent()]
components = [DatabaseComponent(), SocksServersComponent(), LibtorrentComponent(), TorrentCheckerComponent(),
KnowledgeComponent(), MetadataStoreComponent(), KeyComponent(), Ipv8Component(),
PopularityComponent()]
async with Session(tribler_config, components) as session:
comp = session.get_instance(PopularityComponent)
assert comp.community
Expand Down
Loading

0 comments on commit afa20e5

Please sign in to comment.