Merge remote-tracking branch 'origin/master'

# Conflicts:
#	ptvp35/__init__.py
This commit is contained in:
AF 2021-12-08 14:39:05 +03:00
commit 4dcca8067f

View File

@ -2,7 +2,6 @@ import asyncio
import json import json
import pathlib import pathlib
import pickle import pickle
import shutil
from io import StringIO from io import StringIO
from typing import Any, Optional, IO, Type from typing import Any, Optional, IO, Type
@ -81,12 +80,9 @@ class Db:
__task: Optional[asyncio.Future] __task: Optional[asyncio.Future]
def __init__(self, path: str, *, kvrequest_type: Type[KVRequest], buffersize=1048576): def __init__(self, path: str, *, kvrequest_type: Type[KVRequest], buffersize=1048576):
path = str(path)
self.kvrequest_type = kvrequest_type self.kvrequest_type = kvrequest_type
self.buffersize = buffersize self.buffersize = buffersize
self.__path = pathlib.Path(path) self.__path = pathlib.Path(path)
self.__path_backup = pathlib.Path(path + '.backup')
self.__path_recover = pathlib.Path(path + '.recover')
self.__task = None self.__task = None
def io2db(self, io: IO[str], db: dict) -> int: def io2db(self, io: IO[str], db: dict) -> int:
@ -178,19 +174,11 @@ class Db:
await self._background_cycle() await self._background_cycle()
def _rebuild_file(self, db: {}): def _rebuild_file(self, db: {}):
if self.__path_recover.exists():
shutil.copy(self.__path_backup, self.__path)
self.__path_recover.unlink()
self.__path_backup.unlink()
self.__path.touch() self.__path.touch()
with open(self.__path) as file: with open(self.__path) as file:
self.io2db(file, db) self.io2db(file, db)
with open(self.__path_backup, "w") as file: with open(self.__path, "w") as file:
self.__initial_size = self.db2io(db, file) self.__initial_size = self.db2io(db, file)
self.__path_recover.touch()
shutil.copy(self.__path_backup, self.__path)
self.__path_recover.unlink()
self.__path_backup.unlink()
def _reload(self): def _reload(self):
self.__file.close() self.__file.close()