Skip to content

Commit

Permalink
added skel for expire,mget,mset, overloading, refactor, & linted
Browse files Browse the repository at this point in the history
FossilOrigin-Name: dec4fbdfa481e9477ff41e769d188623168c6d7427cf560fb79a3133b87e1390
  • Loading branch information
jcy committed Jul 22, 2024
1 parent ff46a4c commit f9bf23f
Showing 1 changed file with 89 additions and 55 deletions.
144 changes: 89 additions & 55 deletions litestash/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""
import orjson
from typing import overload
from typing import Dict
from typing import List
from sqlalchemy import insert
from sqlalchemy import select
from sqlalchemy import delete
Expand Down Expand Up @@ -43,24 +45,12 @@ def __init__(self):

@overload
def set(self, key: str, value: str = None) -> None:
"""Set String Data
Add a new key to the database.
If key already exists update the value.
Args:
key (str):
value (str):
"""
"""Overload set using key,value string"""


@overload
def set(self, data: LiteStashData) -> None:
"""Set LiteStashData
Add valid data to the database.
Args:
data: LiteStashData
"""
"""Overload set using LiteStashData"""


def set(self, data: str | LiteStashData, value: str = None):
Expand Down Expand Up @@ -112,24 +102,22 @@ def set(self, data: str | LiteStashData, value: str = None):
microsecond=data.microsecond
)
)
#print(f'sql: {sql_statement}')
with session() as set_session:
set_session.execute(sql_statement)
set_session.commit()


@overload
def get(self, data: LiteStashData):
"""Set LiteStashData
def get(self, data: LiteStashData) -> None:
"""Overload get using LiteStashData type"""

Get json data for the given data stash
Args:
data: LiteStashData

"""
@overload
def get(self, data: str) -> None:
"""Overload get using string type"""


def get(self, data: str) -> LiteStashData | None:
def get(self, data: str | LiteStashData) -> LiteStashData | None:
"""Retrieves a value from the cache by key.
Args:
Expand All @@ -149,23 +137,19 @@ def get(self, data: str) -> LiteStashData | None:

if isinstance(data, str):
data = LiteStashData(key=data)
#print(f'data: {data}')

hash_key = get_primary_key(data.key)
table_name = get_table_name(hash_key[0])
db_name = get_db_name(hash_key[0])
metadata = self.metadata.get(db_name).metadata
#print(f'metadata: {metadata}')
session = self.db_session.get(db_name).session
table = metadata.tables[table_name]
#print(f'table: {table}')
sql_statement = (
select(table).where(table.c.key_hash == hash_key)
)

with session() as get_session:
data = get_session.execute(sql_statement).first()
# print(f'data: {data}')
get_session.commit()

if data:
Expand All @@ -177,41 +161,54 @@ def get(self, data: str) -> LiteStashData | None:


@overload
def delete(self, data: LiteStashData):
"""LiteStash Delete
def mget(self, keys: List[LiteStashData]) -> List[LiteStashData]:
"""Get many keys using LiteStashData Objects"""

Remove a key-value pair from the stash
Args:
data (LiteStashData): Remove stored data with DTO
"""

@overload
def mget(self, keys: List[str]) -> List[LiteStashData]:
"""Get many keys by string list of key names"""

def delete(self, key: str):
"""Deletes a key-value pair from the database.

Args:
data: Either a `LiteStashData` object or a string key to delete.
def mget(self,
keys: List[str] | List[LiteStashData]) -> List[LiteStashData]:
"""Bulk get of multiple keys"""
pass

@overload
def mset(self, data: List[LiteStashData], ttl: int) -> None:
""""""


@overload
def mset(self, data: List[Dict], ttl: int) -> None:
""""""


@overload
def mset(self, data: List[str], ttl: int) -> None:
""""""


def mset(
self,
data: List[str] | List[Dict] | List[LiteStashData],
ttl: int) -> None:
"""
if not isinstance(key, str):
raise TypeError(
f'{StashError.KEY_TYPE.value} not {type(key).__name__}'
)
if isinstance(key, str):
key = LiteStashData(key=key)
todo
"""
pass

hash_key = get_primary_key(key.key)
table_name = get_table_name(hash_key[0])
db_name = get_db_name(hash_key[0])
metadata = self.metadata.get(db_name).metadata
session = self.db_session.get(db_name).session
table = metadata.tables[table_name]

with session() as delete_session:
delete_session.execute(
delete(table).where(table.c.key_hash == hash_key)
)
delete_session.commit()
def expire(self,
keys: str | List[str] | None = None,
ttl: int = None) -> int:
"""Expire a key, some keys, or all keys
Todo
"""
pass


def keys(self) -> list[str]:
Expand Down Expand Up @@ -241,6 +238,7 @@ def values(self) -> list[dict]:
values.append(table_values)
return values


def exists(self, key: str) -> bool:
"""Checks if a key exists in the database.
Expand All @@ -262,10 +260,8 @@ def exists(self, key: str) -> bool:
table_name = get_table_name(hash_key[0])
db_name = get_db_name(hash_key[0])
metadata = self.metadata.get(db_name).metadata
#print(f'metadata: {metadata}')
session = self.db_session.get(db_name).session
table = metadata.tables[table_name]
#print(f'table: {table}')
sql_statement = (
select(table).where(table.c.key_hash == hash_key)
)
Expand All @@ -280,6 +276,44 @@ def exists(self, key: str) -> bool:
return False


@overload
def delete(self, data: LiteStashData):
"""LiteStash Delete
Remove a key-value pair from the stash
Args:
data (LiteStashData): Remove stored data with DTO
"""


def delete(self, key: str):
"""Deletes a key-value pair from the database.
Args:
data: Either a `LiteStashData` object or a string key to delete.
"""
if not isinstance(key, str):
raise TypeError(
f'{StashError.KEY_TYPE.value} not {type(key).__name__}'
)

if isinstance(key, str):
key = LiteStashData(key=key)

hash_key = get_primary_key(key.key)
table_name = get_table_name(hash_key[0])
db_name = get_db_name(hash_key[0])
metadata = self.metadata.get(db_name).metadata
session = self.db_session.get(db_name).session
table = metadata.tables[table_name]

with session() as delete_session:
delete_session.execute(
delete(table).where(table.c.key_hash == hash_key)
)
delete_session.commit()


def clear(self) -> None:
"""Clears all entries from the database."""
for db in self.metadata.__slots__:
Expand Down

0 comments on commit f9bf23f

Please # to comment.