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