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