async copy

This commit is contained in:
AF 2022-02-02 20:39:31 +03:00
parent 25727aabd7
commit 6aea9d7044

View File

@ -159,7 +159,7 @@ class Db:
async def _reload_if_oversized(self):
if self.__file.tell() > 2 * self.__initial_size:
await self.__loop.run_in_executor(None, self._reload)
await self._reload()
async def _dump_buffer(self):
if self.__buffer.tell():
@ -188,32 +188,33 @@ class Db:
while True:
await self._background_cycle()
def _rebuild_file(self, db: {}):
async def _finish_recovery(self):
await self.__loop.run_in_executor(None, shutil.copy, self.__path_backup, self.__path)
self.__path_recover.unlink()
self.__path_backup.unlink()
async 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()
await self._finish_recovery()
self.__path.touch()
with open(self.__path) as file:
self.io2db(file, db)
with open(self.__path_backup, "w") as 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()
await self._finish_recovery()
def _reload(self):
async def _reload(self):
self.__file.close()
self._rebuild_file({})
await self._rebuild_file({})
self.__file = open(self.__path, "a")
def _load_from_file(self):
self._rebuild_file(self.__mmdb)
async def _load_from_file(self):
await self._rebuild_file(self.__mmdb)
async def _initialize_mmdb(self):
self.__mmdb = {}
await self.__loop.run_in_executor(None, self._load_from_file)
await self._load_from_file()
async def _initialize_queue(self):
self.__queue = asyncio.Queue()
@ -240,9 +241,7 @@ class Db:
with open(self.__path_backup, "w") as file:
self.__initial_size = self.db2io(self.__mmdb, file)
self.__path_recover.touch()
shutil.copy(self.__path_backup, self.__path)
self.__path_recover.unlink()
self.__path_backup.unlink()
await self._finish_recovery()
def _uninitialize(self):
self.__mmdb = None