Skip to content

Commit

Permalink
crit upd
Browse files Browse the repository at this point in the history
  • Loading branch information
py660 authored Jun 30, 2023
1 parent 8580888 commit a1d9d53
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/replit2/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ async def get_raw(self, key: str) -> str:
response.raise_for_status()
return await response.text()

async def set(self, key: str, value: Any) -> None:
async def set(self, key: str, value: Any, skip_backup: bool=False) -> None:
"""Set a key in the database to the result of JSON encoding value.
Args:
key (str): The key to set
value (Any): The value to set it to. Must be JSON-serializable.
"""
await self.set_raw(key, _dumps(value))
if self.backup_mode == 0:
if self.backup_mode == 0 and not skip_backup:
await self.backup()

async def set_raw(self, key: str, value: str) -> None:
Expand Down Expand Up @@ -162,7 +162,7 @@ async def set_bulk_raw(self, values: Dict[str, str]) -> None:
async with self.sess.post(self.db_url, data=values) as response:
response.raise_for_status()

async def delete(self, key: str) -> None:
async def delete(self, key: str, skip_backup: bool=False) -> None:
"""Delete a key from the database.
Args:
Expand All @@ -177,7 +177,7 @@ async def delete(self, key: str) -> None:
if response.status == 404:
raise KeyError(key)
response.raise_for_status()
if self.backup_mode == 0:
if self.backup_mode == 0 and not skip_backup:
await self.backup()

async def list(self, prefix: str) -> Tuple[str, ...]:
Expand Down Expand Up @@ -260,9 +260,9 @@ async def load_backup(self, location: str="") -> None:
with open(location, "r") as fin:
back_db = json.load(fin)
for i in self.keys():
await self.delete(i)
await self.delete(i, skip_backup=True)
if back_db:
await self.set_bulk_raw(back_db)
await self.set_bulk_raw(back_db) #Overrides backup

# def __repr__(self) -> str:
# """A representation of the database.
Expand Down Expand Up @@ -539,15 +539,15 @@ def __setitem__(self, key: str, value: Any) -> None:
"""
self.set(key, value)

def set(self, key: str, value: Any) -> None:
def set(self, key: str, value: Any, skip_backup: bool=False) -> None:
"""Set a key in the database to value, JSON encoding it.
Args:
key (str): The key to set
value (Any): The value to set.
"""
self.set_raw(key, _dumps(value))
if self.backup_mode == 0:
if self.backup_mode == 0 and not skip_backup:
self.backup()

def set_raw(self, key: str, value: str) -> None:
Expand Down Expand Up @@ -577,7 +577,7 @@ def set_bulk_raw(self, values: Dict[str, str]) -> None:
r = self.sess.post(self.db_url, data=values)
r.raise_for_status()

def __delitem__(self, key: str) -> None:
def __delitem__(self, key: str, skip_backup: bool=False) -> None:
"""Delete a key from the database.
Args:
Expand All @@ -587,7 +587,7 @@ def __delitem__(self, key: str) -> None:
KeyError: Key is not set
"""
r = self.sess.delete(self.db_url + "/" + urllib.parse.quote(key))
if self.backup_mode == 0:
if self.backup_mode == 0 and not skip_backup:
self.backup()
if r.status_code == 404:
raise KeyError(key)
Expand Down Expand Up @@ -671,7 +671,7 @@ def load_backup(self, location: str="") -> None:
back_db = json.load(fin)
#print(back_db)
for i in self.keys():
self.__delitem__(i)
self.__delitem__(i, skip_backup=True)
if back_db:
self.set_bulk_raw(back_db)

Expand Down

0 comments on commit a1d9d53

Please sign in to comment.