Skip to content

Commit

Permalink
Using ulid for _id
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Feb 10, 2024
1 parent 7e118dc commit 788546d
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 110 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,3 @@ PantherDB is a <b>Simple</b>, <b>FileBase</b> and <b>Document Oriented</b> datab
- [x] Add encryption
- [ ] Complete tests TODO
- [ ] Add B+ tree
- [ ] Find better encryption solution
2 changes: 1 addition & 1 deletion pantherdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pantherdb.pantherdb import * # noqa: F403

__version__ = '1.3.6'
__version__ = '1.4.0'


__all__ = ('__version__', 'PantherDB', 'PantherCollection', 'PantherDocument', 'PantherDBException')
13 changes: 11 additions & 2 deletions pantherdb/pantherdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import orjson as json

from pantherdb.ulid import ULID


class PantherDBException(Exception):
pass
Expand All @@ -18,6 +20,7 @@ class PantherDB:
__fernet: typing.Any # type[cryptography.fernet.Fernet | None]
__return_dict: bool
__content: dict
__ulid: ULID

def __new__(cls, *args, **kwargs):
if cls.__name__ != 'PantherDB':
Expand Down Expand Up @@ -47,6 +50,7 @@ def __init__(
):
self.__return_dict = return_dict
self.__secret_key = secret_key
self.__ulid = ULID()
self.__content = {}
if self.__secret_key:
from cryptography.fernet import Fernet
Expand Down Expand Up @@ -76,6 +80,10 @@ def content(self) -> dict:
def return_dict(self) -> bool:
return self.__return_dict

@property
def ulid(self) -> ULID:
return self.__ulid

@property
def secret_key(self) -> bytes | None:
return self.__secret_key
Expand Down Expand Up @@ -243,7 +251,7 @@ def all(self) -> list[PantherDocument | dict]:

def insert_one(self, **kwargs) -> PantherDocument | dict:
documents = self._get_collection()
kwargs['_id'] = len(documents) + 1
kwargs['_id'] = self.ulid.new()
documents.append(kwargs)
self._write_collection(documents)
return self.__create_result(kwargs)
Expand Down Expand Up @@ -376,7 +384,7 @@ def __init__(
)

def __str__(self) -> str:
items = ', '.join(f'id={v}' if k == '_id' else f'{k}={v}' for k, v in self.data.items())
items = ', '.join(f'{k}={v}' for k, v in self.data.items())
return f'{self.collection_name}({items})'

__repr__ = __str__
Expand All @@ -394,6 +402,7 @@ def __setattr__(self, key, value):
'_PantherDB__secret_key',
'_PantherDB__content',
'_PantherDB__fernet',
'_PantherDB__ulid',
'_PantherCollection__collection_name',
'_PantherDocument__data',
]:
Expand Down
22 changes: 22 additions & 0 deletions pantherdb/ulid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import secrets
from datetime import datetime, timezone


class ULID:
"""https://github.com/ulid/spec"""

def __init__(self):
self.crockford_base32_characters = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'

def new(self):
current_timestamp = int(datetime.now(timezone.utc).timestamp() * 1000)
epoch_bits = '{0:050b}'.format(current_timestamp)
random_bits = '{0:080b}'.format(secrets.randbits(80))
bits = epoch_bits + random_bits
return self._generate(bits)

def _generate(self, bits: str) -> str:
return ''.join(
self.crockford_base32_characters[int(bits[i: i + 5], base=2)]
for i in range(0, 130, 5)
)
Loading

0 comments on commit 788546d

Please sign in to comment.