refactored id's to be 64-bit integers

This commit is contained in:
Hellow 2023-04-12 12:15:12 +02:00
parent 1835e60cd1
commit 0bd4e96b27
3 changed files with 21 additions and 14 deletions

View File

@ -1,31 +1,31 @@
import random import random
import uuid
from collections import defaultdict from collections import defaultdict
from typing import Optional, Dict, Tuple, List from typing import Optional, Dict, Tuple, List
from .metadata import Metadata from .metadata import Metadata
from .option import Options from .option import Options
from ..utils.shared import OBJECT_LOGGER as LOGGER from ..utils.shared import ID_RANGE, OBJECT_LOGGER as LOGGER
class DatabaseObject: class DatabaseObject:
COLLECTION_ATTRIBUTES: tuple = tuple() COLLECTION_ATTRIBUTES: tuple = tuple()
SIMPLE_ATTRIBUTES: dict = dict() SIMPLE_ATTRIBUTES: dict = dict()
def __init__(self, _id: str = None, dynamic: bool = False, **kwargs) -> None: def __init__(self, _id: int = None, dynamic: bool = False, **kwargs) -> None:
self.automatic_id: bool = False self.automatic_id: bool = False
if _id is None and not dynamic: if _id is None and not dynamic:
""" """
generates a random UUID generates a random integer id
https://docs.python.org/3/library/uuid.html 64 bit integer, but this is defined in shared.py in ID_BITS
the range is defined in the Tuple ID_RANGE
""" """
_id = str(uuid.uuid4()) _id = random.randint(*ID_RANGE)
self.automatic_id = True self.automatic_id = True
LOGGER.debug(f"id for {type(self).__name__} isn't set. Setting to {_id}") LOGGER.debug(f"Id for {type(self).__name__} isn't set. Setting to {_id}")
# The id can only be None, if the object is dynamic (self.dynamic = True) # The id can only be None, if the object is dynamic (self.dynamic = True)
self.id: Optional[str] = _id self.id: Optional[int] = _id
self.dynamic = dynamic self.dynamic = dynamic
@ -35,6 +35,10 @@ class DatabaseObject:
if not isinstance(other, type(self)): if not isinstance(other, type(self)):
return False return False
# add the checks for dynamic, to not throw an exception
if not self.dynamic and not other.dynamic and self.id == other.id:
return True
temp_attribute_map: Dict[str, set] = defaultdict(set) temp_attribute_map: Dict[str, set] = defaultdict(set)
# building map with sets # building map with sets
@ -117,7 +121,7 @@ class MainObject(DatabaseObject):
but also some added functions as well. but also some added functions as well.
""" """
def __init__(self, _id: str = None, dynamic: bool = False, **kwargs): def __init__(self, _id: int = None, dynamic: bool = False, **kwargs):
DatabaseObject.__init__(self, _id=_id, dynamic=dynamic, **kwargs) DatabaseObject.__init__(self, _id=_id, dynamic=dynamic, **kwargs)
self.additional_arguments: dict = kwargs self.additional_arguments: dict = kwargs

View File

@ -46,7 +46,7 @@ class Song(MainObject):
def __init__( def __init__(
self, self,
_id: str = None, _id: int = None,
dynamic: bool = False, dynamic: bool = False,
title: str = None, title: str = None,
unified_title: str = None, unified_title: str = None,
@ -203,7 +203,7 @@ class Album(MainObject):
def __init__( def __init__(
self, self,
_id: str = None, _id: int = None,
title: str = None, title: str = None,
unified_title: str = None, unified_title: str = None,
language: pycountry.Languages = None, language: pycountry.Languages = None,
@ -408,7 +408,7 @@ class Artist(MainObject):
def __init__( def __init__(
self, self,
_id: str = None, _id: int = None,
dynamic: bool = False, dynamic: bool = False,
name: str = None, name: str = None,
unified_name: str = None, unified_name: str = None,
@ -579,7 +579,7 @@ class Label(MainObject):
def __init__( def __init__(
self, self,
_id: str = None, _id: int = None,
dynamic: bool = False, dynamic: bool = False,
name: str = None, name: str = None,
unified_name: str = None, unified_name: str = None,

View File

@ -1,4 +1,4 @@
from typing import List, Set from typing import List, Set, Tuple
import logging import logging
import tempfile import tempfile
import os import os
@ -36,6 +36,9 @@ def get_random_message() -> str:
return random.choice(HAPPY_MESSAGES) return random.choice(HAPPY_MESSAGES)
ID_BITS: int = 64
ID_RANGE: Tuple[int] = 0, 2**ID_BITS
TEMP_DIR = Path(tempfile.gettempdir(), "music-downloader") TEMP_DIR = Path(tempfile.gettempdir(), "music-downloader")
TEMP_DIR.mkdir(exist_ok=True) TEMP_DIR.mkdir(exist_ok=True)
LOG_PATH = Path(TEMP_DIR, "download_logs.log") LOG_PATH = Path(TEMP_DIR, "download_logs.log")