mirror of
https://github.com/aljazceru/plugins.git
synced 2025-12-20 06:34:20 +01:00
backup: optimize restore and backup-compact
fix comments and typos, enable the `restore` progress bar
This commit is contained in:
committed by
Christian Decker
parent
1f968b247c
commit
44cf6ecaea
@@ -6,18 +6,18 @@ from typing import Iterator
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
# A change that was proposed by c-lightning that needs saving to the
|
# A 'transaction' that was proposed by c-lightning and that needs saving to the
|
||||||
# backup. `version` is the database version before the transaction was
|
# backup. `version` is the `data_version` of the database **after** `transaction`
|
||||||
# applied. The optional snapshot reqpresents a complete copy of the database,
|
# has been applied. A 'snapshot' represents a complete copy of the database.
|
||||||
# as it was before applying the `transaction`. This is used by the plugin from
|
# This is used by the plugin from time to time to allow the backend to compress
|
||||||
# time to time to allow the backend to compress the changelog and forms a new
|
# the changelog and forms a new basis for the backup.
|
||||||
# basis for the backup.
|
# If `Change` contains a snapshot and a transaction, they apply in that order.
|
||||||
Change = namedtuple('Change', ['version', 'snapshot', 'transaction'])
|
Change = namedtuple('Change', ['version', 'snapshot', 'transaction'])
|
||||||
|
|
||||||
|
|
||||||
class Backend(object):
|
class Backend(object):
|
||||||
def __init__(self, destination: str):
|
def __init__(self, destination: str):
|
||||||
"""Read the metadata from the destination and prepare any necesary resources.
|
"""Read the metadata from the destination and prepare any necessary resources.
|
||||||
|
|
||||||
After this call the following members must be initialized:
|
After this call the following members must be initialized:
|
||||||
|
|
||||||
@@ -98,7 +98,6 @@ class Backend(object):
|
|||||||
for q in tx:
|
for q in tx:
|
||||||
q = self._rewrite_stmt(q)
|
q = self._rewrite_stmt(q)
|
||||||
cur.execute(q)
|
cur.execute(q)
|
||||||
self.db.commit()
|
|
||||||
|
|
||||||
def restore(self, dest: str, remove_existing: bool = False):
|
def restore(self, dest: str, remove_existing: bool = False):
|
||||||
"""Restore the backup in this backend to its former glory.
|
"""Restore the backup in this backend to its former glory.
|
||||||
@@ -118,8 +117,9 @@ class Backend(object):
|
|||||||
os.unlink(dest)
|
os.unlink(dest)
|
||||||
|
|
||||||
self.db = self._db_open(dest)
|
self.db = self._db_open(dest)
|
||||||
for c in tqdm(self.stream_changes()):
|
for c in tqdm(self.stream_changes(), total=self.version_count):
|
||||||
if c.snapshot is not None:
|
if c.snapshot is not None:
|
||||||
self._restore_snapshot(c.snapshot, dest)
|
self._restore_snapshot(c.snapshot, dest)
|
||||||
if c.transaction is not None:
|
if c.transaction is not None:
|
||||||
self._restore_transaction(c.transaction)
|
self._restore_transaction(c.transaction)
|
||||||
|
self.db.commit()
|
||||||
|
|||||||
@@ -153,6 +153,7 @@ class FileBackend(Backend):
|
|||||||
|
|
||||||
if change.transaction is not None:
|
if change.transaction is not None:
|
||||||
self._restore_transaction(change.transaction)
|
self._restore_transaction(change.transaction)
|
||||||
|
self.db.commit()
|
||||||
|
|
||||||
# If this assertion fails we are in a degenerate state: we
|
# If this assertion fails we are in a degenerate state: we
|
||||||
# have less than two changes in the backup (starting
|
# have less than two changes in the backup (starting
|
||||||
|
|||||||
Reference in New Issue
Block a user