Skip to content

Commit

Permalink
Merge pull request #7606 from drew2a/feature/tribler.db
Browse files Browse the repository at this point in the history
Migrate `knowledge.db` to `tribler.db`
  • Loading branch information
drew2a committed Sep 28, 2023
2 parents a83b384 + 8f23ed1 commit fd21083
Show file tree
Hide file tree
Showing 52 changed files with 723 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from tribler.core.components.bandwidth_accounting.db import history, misc, transaction as db_transaction
from tribler.core.components.bandwidth_accounting.db.transaction import BandwidthTransactionData
from tribler.core.utilities.pony_utils import TriblerDatabase
from tribler.core.utilities.pony_utils import TrackedDatabase
from tribler.core.utilities.utilities import MEMORY_DB


Expand All @@ -28,7 +28,7 @@ def __init__(self, db_path: Union[Path, type(MEMORY_DB)], my_pub_key: bytes,
self.my_pub_key = my_pub_key
self.store_all_transactions = store_all_transactions

self.database = TriblerDatabase()
self.database = TrackedDatabase()

# This attribute is internally called by Pony on startup, though pylint cannot detect it
# with the static analysis.
Expand Down
6 changes: 3 additions & 3 deletions 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.tribler_database import TriblerDatabase
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 Expand Up @@ -107,8 +107,8 @@ def metadata_store(tmp_path):


@pytest.fixture
def knowledge_db():
db = KnowledgeDatabase()
def tribler_db():
db = TriblerDatabase()
yield db
db.shutdown()

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.tribler_database import TriblerDatabase
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR


class DatabaseComponent(Component):
tribler_should_stop_on_component_error = True

db: TriblerDatabase = 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 = TriblerDatabase(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,22 +3,22 @@

from pony.orm import commit, db_session

from tribler.core.components.knowledge.db.knowledge_db import KnowledgeDatabase, Operation, \
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.knowledge.db.tests.test_knowledge_db_base import Resource, TestTagDBBase
from tribler.core.utilities.pony_utils import TriblerDatabase, get_or_create
from tribler.core.components.database.db.tests.test_tribler_database_base import Resource, TestTagDBBase
from tribler.core.utilities.pony_utils import TrackedDatabase, get_or_create


# pylint: disable=protected-access
class TestTagDB(TestTagDBBase):
@patch.object(TriblerDatabase, 'generate_mapping')
@patch.object(TrackedDatabase, 'generate_mapping')
def test_constructor_create_tables_true(self, mocked_generate_mapping: Mock):
KnowledgeDatabase(':memory:')
TriblerDatabase(':memory:')
mocked_generate_mapping.assert_called_with(create_tables=True)

@patch.object(TriblerDatabase, 'generate_mapping')
@patch.object(TrackedDatabase, 'generate_mapping')
def test_constructor_create_tables_false(self, mocked_generate_mapping: Mock):
KnowledgeDatabase(':memory:', create_tables=False)
TriblerDatabase(':memory:', create_tables=False)
mocked_generate_mapping.assert_called_with(create_tables=False)

@db_session
Expand Down Expand Up @@ -446,9 +446,9 @@ def _results(objects, predicate=ResourceType.TAG, case_sensitive=True):

@db_session
def test_show_condition(self):
assert KnowledgeDatabase._show_condition(SimpleNamespace(local_operation=Operation.ADD))
assert KnowledgeDatabase._show_condition(SimpleNamespace(local_operation=None, score=SHOW_THRESHOLD))
assert not KnowledgeDatabase._show_condition(SimpleNamespace(local_operation=None, score=0))
assert TriblerDatabase._show_condition(SimpleNamespace(local_operation=Operation.ADD))
assert TriblerDatabase._show_condition(SimpleNamespace(local_operation=None, score=SHOW_THRESHOLD))
assert not TriblerDatabase._show_condition(SimpleNamespace(local_operation=None, score=0))

@db_session
def test_get_random_operations_by_condition_less_than_count(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from ipv8.test.base import TestBase
from pony.orm import commit, db_session

from tribler.core.components.database.db.tribler_database import Operation, ResourceType, SHOW_THRESHOLD, \
TriblerDatabase
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.utilities.pony_utils import get_or_create


Expand All @@ -22,7 +23,7 @@ class Resource:
class TestTagDBBase(TestBase):
def setUp(self):
super().setUp()
self.db = KnowledgeDatabase()
self.db = TriblerDatabase()

async def tearDown(self):
if self._outcome.errors:
Expand Down Expand Up @@ -56,7 +57,7 @@ def create_operation(subject_type: ResourceType = ResourceType.TORRENT, subject=
operation=operation, clock=clock, creator_public_key=peer)

@staticmethod
def add_operation(tag_db: KnowledgeDatabase, subject_type: ResourceType = ResourceType.TORRENT,
def add_operation(tag_db: TriblerDatabase, subject_type: ResourceType = ResourceType.TORRENT,
subject: str = 'infohash',
predicate: ResourceType = ResourceType.TAG, obj: str = 'tag', peer=b'',
operation: Operation = None,
Expand All @@ -70,7 +71,7 @@ def add_operation(tag_db: KnowledgeDatabase, subject_type: ResourceType = Resour
return result

@staticmethod
def add_operation_set(tag_db: KnowledgeDatabase, dictionary):
def add_operation_set(tag_db: TriblerDatabase, dictionary):
index = count(0)

def generate_n_peer_names(n):
Expand Down

0 comments on commit fd21083

Please sign in to comment.