Skip to content

Commit

Permalink
Rename KnowledgeDatabase to TriblerDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Sep 27, 2023
1 parent 47b13d5 commit 33d59bb
Show file tree
Hide file tree
Showing 27 changed files with 103 additions and 102 deletions.
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.database.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
6 changes: 3 additions & 3 deletions src/tribler/core/components/database/database_component.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from tribler.core.components.component import Component
from tribler.core.components.database.db.knowledge_db import KnowledgeDatabase
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: KnowledgeDatabase = None
db: TriblerDatabase = None

async def run(self):
await super().run()
Expand All @@ -15,7 +15,7 @@ async def run(self):
if self.session.config.gui_test_mode:
db_path = ":memory:"

Check warning on line 16 in src/tribler/core/components/database/database_component.py

View check run for this annotation

Codecov / codecov/patch

src/tribler/core/components/database/database_component.py#L16

Added line #L16 was not covered by tests

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

async def shutdown(self):
await super().shutdown()
Expand Down
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.database.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.database.db.tests.test_knowledge_db_base import Resource, TestTagDBBase
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(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(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.database.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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SimpleStatement:
subject: str


class KnowledgeDatabase:
class TriblerDatabase:
def __init__(self, filename: Optional[str] = None, *, create_tables: bool = True, **generate_mapping_kwargs):
self.instance = TrackedDatabase()
self.define_binding(self.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def run(self):
rqc_settings=config.remote_query_community,
metadata_store=metadata_store_component.mds,
max_peers=50,
knowledge_db=db_component.db if db_component else None
tribler_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.database.db.knowledge_db import KnowledgeDatabase
from tribler.core.components.database.db.tribler_database import TriblerDatabase

REQUESTED_OPERATIONS_COUNT = 10

Expand All @@ -34,7 +34,7 @@ class KnowledgeCommunity(TriblerCommunity):

community_id = unhexlify('d7f7bdc8bcd3d9ad23f06f25aa8aab6754eb23a0')

def __init__(self, *args, db: KnowledgeDatabase, key: LibNaCLSK, request_interval=REQUEST_INTERVAL,
def __init__(self, *args, db: TriblerDatabase, key: LibNaCLSK, request_interval=REQUEST_INTERVAL,
**kwargs):
super().__init__(*args, **kwargs)
self.db = db
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tribler.core.components.database.db.knowledge_db import Operation, ResourceType
from tribler.core.components.database.db.tribler_database 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.database.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.database.db.tribler_database import TriblerDatabase, Operation, ResourceType

REQUEST_INTERVAL_FOR_RANDOM_OPERATIONS = 0.1 # in seconds

Expand All @@ -22,7 +22,7 @@ async def tearDown(self):
await super().tearDown()

def create_node(self, *args, **kwargs):
return MockIPv8("curve25519", KnowledgeCommunity, db=KnowledgeDatabase(), key=LibNaCLSK(),
return MockIPv8("curve25519", KnowledgeCommunity, db=TriblerDatabase(), key=LibNaCLSK(),
request_interval=REQUEST_INTERVAL_FOR_RANDOM_OPERATIONS)

def create_operation(self, subject='1' * 20, obj=''):
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.database.db.knowledge_db import Operation, ResourceType
from tribler.core.components.database.db.tribler_database import Operation, ResourceType

VALID_TAGS = [
'nl',
Expand Down
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.database.db.knowledge_db import KnowledgeDatabase, Operation, ResourceType
from tribler.core.components.database.db.tribler_database import TriblerDatabase, 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 All @@ -23,9 +23,9 @@ class KnowledgeEndpoint(RESTEndpoint):
"""
path = '/knowledge'

def __init__(self, db: KnowledgeDatabase, community: KnowledgeCommunity):
def __init__(self, db: TriblerDatabase, community: KnowledgeCommunity):
super().__init__()
self.db: KnowledgeDatabase = db
self.db: TriblerDatabase = db
self.community: KnowledgeCommunity = community

@staticmethod
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.database.db.knowledge_db import Operation, ResourceType
from tribler.core.components.database.db.tribler_database 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 All @@ -17,8 +17,8 @@
# pylint: disable=redefined-outer-name

@pytest.fixture
def endpoint(knowledge_db):
return KnowledgeEndpoint(knowledge_db, Mock(key=TEST_PERSONAL_KEY, sign=Mock(return_value=b'')))
def endpoint(tribler_db):
return KnowledgeEndpoint(tribler_db, Mock(key=TEST_PERSONAL_KEY, sign=Mock(return_value=b'')))


def tag_to_statement(tag: str) -> Dict:
Expand Down Expand Up @@ -48,7 +48,7 @@ async def test_add_invalid_tag(rest_api):
post_data=post_data)


async def test_modify_tags(rest_api, knowledge_db):
async def test_modify_tags(rest_api, tribler_db):
"""
Test modifying tags
"""
Expand All @@ -58,7 +58,7 @@ async def test_modify_tags(rest_api, knowledge_db):
await do_request(rest_api, f'knowledge/{infohash}', request_type="PATCH", expected_code=200,
post_data=post_data)
with db_session:
tags = knowledge_db.get_objects(subject=infohash, predicate=ResourceType.TAG)
tags = tribler_db.get_objects(subject=infohash, predicate=ResourceType.TAG)
assert len(tags) == 2

# Now remove a tag
Expand All @@ -67,17 +67,17 @@ async def test_modify_tags(rest_api, knowledge_db):
await do_request(rest_api, f'knowledge/{infohash}', request_type="PATCH", expected_code=200,
post_data=post_data)
with db_session:
tags = knowledge_db.get_objects(subject=infohash, predicate=ResourceType.TAG)
tags = tribler_db.get_objects(subject=infohash, predicate=ResourceType.TAG)
assert tags == ["abc"]


async def test_modify_tags_no_community(knowledge_db, endpoint):
async def test_modify_tags_no_community(tribler_db, endpoint):
endpoint.community = None
infohash = 'a' * 20
endpoint.modify_statements(infohash, [tag_to_statement("abc"), tag_to_statement("def")])

with db_session:
tags = knowledge_db.get_objects(subject=infohash, predicate=ResourceType.TAG)
tags = tribler_db.get_objects(subject=infohash, predicate=ResourceType.TAG)

assert len(tags) == 0

Expand All @@ -90,7 +90,7 @@ async def test_get_suggestions_invalid_infohash(rest_api):
await do_request(rest_api, 'knowledge/3f3f/tag_suggestions', expected_code=400)


async def test_get_suggestions(rest_api, knowledge_db):
async def test_get_suggestions(rest_api, tribler_db):
"""
Test whether we can successfully fetch suggestions from content
"""
Expand All @@ -107,7 +107,7 @@ def _add_operation(op=Operation.ADD):
operation = StatementOperation(subject_type=ResourceType.TORRENT, subject=infohash_str,
predicate=ResourceType.TAG, object="test", operation=op, clock=0,
creator_public_key=random_key.pub().key_to_bin())
knowledge_db.add_operation(operation, b"")
tribler_db.add_operation(operation, b"")

_add_operation(op=Operation.ADD)
_add_operation(op=Operation.REMOVE)
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.database.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.database.db.tribler_database import TriblerDatabase, 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 Expand Up @@ -41,7 +41,7 @@ class KnowledgeRulesProcessor(TaskManager):
# this value must be incremented in the case of new rules set has been applied
version: int = 5

def __init__(self, notifier: Notifier, db: KnowledgeDatabase, mds: MetadataStore,
def __init__(self, notifier: Notifier, db: TriblerDatabase, mds: MetadataStore,
batch_size: int = DEFAULT_BATCH_SIZE, batch_interval: float = DEFAULT_BATCH_INTERVAL,
queue_interval: float = DEFAULT_QUEUE_INTERVAL, queue_batch_size: float = DEFAULT_QUEUE_BATCH_SIZE,
queue_max_size: int = DEFAULT_QUEUE_MAX_SIZE):
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.database.db.knowledge_db import KnowledgeDatabase, ResourceType
from tribler.core.components.database.db.tribler_database import TriblerDatabase, 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 All @@ -20,8 +20,8 @@
@pytest.fixture
async def tag_rules_processor(tmp_path: Path):
mds = MetadataStore(db_filename=MEMORY_DB, channels_dir=tmp_path, my_key=LibNaCLSK())
knowledge_db = KnowledgeDatabase(filename=':memory:')
processor = KnowledgeRulesProcessor(notifier=MagicMock(), db=knowledge_db, mds=mds,
db = TriblerDatabase(filename=':memory:')
processor = KnowledgeRulesProcessor(notifier=MagicMock(), db=db, mds=mds,
batch_size=TEST_BATCH_SIZE,
batch_interval=TEST_INTERVAL)
yield processor
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.database.db.knowledge_db import ResourceType
from tribler.core.components.database.db.tribler_database 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 Expand Up @@ -136,13 +136,13 @@ class RemoteQueryCommunity(TriblerCommunity):
def __init__(self, my_peer, endpoint, network,
rqc_settings: RemoteQueryCommunitySettings = None,
metadata_store=None,
knowledge_db=None,
tribler_db=None,
**kwargs):
super().__init__(my_peer, endpoint, network=network, **kwargs)

self.rqc_settings = rqc_settings
self.mds: MetadataStore = metadata_store
self.knowledge_db = knowledge_db
self.tribler_db = tribler_db
# This object stores requests for "select" queries that we sent to other hosts.
# We keep track of peers we actually requested for data so people can't randomly push spam at us.
# Also, this keeps track of hosts we responded to. There is a possibility that
Expand Down Expand Up @@ -214,22 +214,22 @@ 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.knowledge_db:
if self.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 = await run_threaded(self.knowledge_db.instance, self.search_for_tags, tags)
infohash_set = await run_threaded(self.tribler_db.instance, self.search_for_tags, tags)
if infohash_set:
sanitized_parameters['infohash_set'] = {bytes.fromhex(s) for s in infohash_set}

return await self.mds.get_entries_threaded(**sanitized_parameters)

@db_session
def search_for_tags(self, tags: Optional[List[str]]) -> Optional[Set[str]]:
if not tags or not self.knowledge_db:
if not tags or not self.tribler_db:
return None
valid_tags = {tag for tag in tags if is_valid_resource(tag)}
result = self.knowledge_db.get_subjects_intersection(
result = self.tribler_db.get_subjects_intersection(
subjects_type=ResourceType.TORRENT,
objects=valid_tags,
predicate=ResourceType.TAG,
Expand Down
Loading

0 comments on commit 33d59bb

Please sign in to comment.