From 91ab6e80ca878b6fcf97d7a6572104dfb9b4e6ec Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Fri, 15 Apr 2016 09:46:45 -0400 Subject: [PATCH] DB-API in-memory replacement for DictionaryDB --- gramps/plugins/database/inmemorydb.gpr.py | 33 +++++++++++ gramps/plugins/database/inmemorydb.py | 68 +++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 gramps/plugins/database/inmemorydb.gpr.py create mode 100644 gramps/plugins/database/inmemorydb.py diff --git a/gramps/plugins/database/inmemorydb.gpr.py b/gramps/plugins/database/inmemorydb.gpr.py new file mode 100644 index 000000000..c9395e6a2 --- /dev/null +++ b/gramps/plugins/database/inmemorydb.gpr.py @@ -0,0 +1,33 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2016 Douglas Blank +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +register(DATABASE, + id = 'inmemorydb', + name = _("In-Memory Database Backend"), + name_accell = _("In-_Memory Database Backend"), + description = _("In-Memory Database Backend"), + version = '1.0.0', + gramps_target_version = "5.0", + status = STABLE, + fname = 'inmemorydb.py', + databaseclass = 'InMemoryDB', + authors=['Doug Blank'], + authors_email=["doug.blank@gmail.com"], +) diff --git a/gramps/plugins/database/inmemorydb.py b/gramps/plugins/database/inmemorydb.py new file mode 100644 index 000000000..ef87af758 --- /dev/null +++ b/gramps/plugins/database/inmemorydb.py @@ -0,0 +1,68 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2016 Douglas S. Blank +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +from dbapi import DBAPI +from dbapi_support.sqlite import Sqlite +from gramps.gen.db.generic import DbGeneric, DBBACKEND, LOG +import os + +class InMemoryDB(DBAPI): + """ + A DB-API 2.0 In-memory SQL database. + """ + + def initialize_backend(self, directory): + """ + Create an in-memory sqlite database. + """ + self.dbapi = Sqlite(":memory:") + self.update_schema() + + def write_version(self, directory): + """Write files for a newly created DB.""" + versionpath = os.path.join(directory, str(DBBACKEND)) + LOG.debug("Write database backend file to 'inmemorydb'") + with open(versionpath, "w") as version_file: + version_file.write("inmemorydb") + + def load(self, directory, callback=None, mode=None, + force_schema_upgrade=False, + force_bsddb_upgrade=False, + force_bsddb_downgrade=False, + force_python_upgrade=False): + DbGeneric.load(self, directory, + callback, + mode, + force_schema_upgrade, + force_bsddb_upgrade, + force_bsddb_downgrade, + force_python_upgrade) + # Dictionary-specific load: + from gramps.plugins.importer.importxml import importData + from gramps.cli.user import User + if self._directory: + backups = list(reversed(glob.glob(os.path.join( + self._directory, "backup-*.gramps")))) + if backups: + filename = backups[0] + if os.path.isfile(filename): + importData(self, filename, User()) + self.reindex_reference_map(lambda progress: None) + self.rebuild_secondary(lambda progress: None)