Do not use undo log for in-memory db

This commit is contained in:
David Straub 2023-04-11 20:20:34 +02:00
parent f760bffb56
commit d632c9b923

View File

@ -80,10 +80,11 @@ class DbGenericUndo(DbUndo):
table_txn = "transactions"
table_undo = "commits"
def __init__(self, grampsdb, path) -> None:
def __init__(self, grampsdb: DbWriteBase, path: Optional[str] = None) -> None:
super(DbGenericUndo, self).__init__(grampsdb)
self.path = path
self._session_id: Optional[int] = None
self.undodb: List[bytes] = []
@property
def session_id(self) -> int:
@ -100,6 +101,7 @@ class DbGenericUndo(DbUndo):
"""
Open the backing storage.
"""
if self.path:
self._create_tables()
def _create_tables(self) -> None:
@ -110,13 +112,13 @@ class DbGenericUndo(DbUndo):
f"""CREATE TABLE IF NOT EXISTS {self.table_undo} (
session INTEGER,
id INTEGER,
obj_class STRING,
obj_class TEXT,
trans_type INTEGER,
obj_handle STRING,
ref_handle STRING,
obj_handle TEXT,
ref_handle TEXT,
old_data BLOB,
new_data BLOB,
json STRING,
json TEXT,
timestamp INTEGER,
PRIMARY KEY (session, id)
)
@ -133,7 +135,7 @@ class DbGenericUndo(DbUndo):
f"""CREATE TABLE IF NOT EXISTS {self.table_txn} (
id INTEGER PRIMARY KEY,
session INTEGER,
description STRING,
description TEXT,
timestamp INTEGER,
first INTEGER,
last INTEGER,
@ -164,6 +166,9 @@ class DbGenericUndo(DbUndo):
"""
Add a new entry on the end.
"""
if not self.path:
self.undodb.append(value)
return
(obj_type, trans_type, handle, old_data, new_data) = pickle.loads(value)
if isinstance(handle, tuple):
obj_handle, ref_handle = handle
@ -201,6 +206,8 @@ class DbGenericUndo(DbUndo):
self, transaction: DbTxn, undo: bool = False, redo: bool = False
) -> None:
"""Post-transaction commit processing."""
if not self.path:
return
msg = transaction.get_description()
if redo:
msg = _("_Redo %s") % msg
@ -231,6 +238,8 @@ class DbGenericUndo(DbUndo):
"""
Returns an entry by index number.
"""
if not self.path:
return self.undodb[index]
with self._connect() as connection:
cursor = connection.cursor()
cursor.execute(
@ -259,6 +268,8 @@ class DbGenericUndo(DbUndo):
"""
Set an entry to a value.
"""
if not self.path:
self.undodb[index] = value
(obj_type, trans_type, handle, old_data, new_data) = pickle.loads(value)
if isinstance(handle, tuple):
obj_handle, ref_handle = handle
@ -297,6 +308,8 @@ class DbGenericUndo(DbUndo):
"""
Returns the number of entries.
"""
if not self.path:
return len(self.undodb)
with self._connect() as connection:
cursor = connection.cursor()
cursor.execute(
@ -823,7 +836,7 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback):
self._set_save_path(directory)
if self._directory:
if self._directory and self._directory != ":memory:":
self.undolog = os.path.join(self._directory, DBUNDOFN)
else:
self.undolog = None