[PATCH 2/3] sqlite: add lazy connection init

Zac Medico <[email protected]>
Newsgroups gmane.linux.gentoo.portage.devel
Message-ID <[email protected]>
Signed-off-by: Zac Medico <[email protected]>
---
 lib/portage/cache/sqlite.py | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/lib/portage/cache/sqlite.py b/lib/portage/cache/sqlite.py
index 55ae8f0e5..0395dd516 100644
--- a/lib/portage/cache/sqlite.py
+++ b/lib/portage/cache/sqlite.py
@@ -1,6 +1,7 @@
 # Copyright 1999-2020 Gentoo Authors
 # Distributed under the terms of the GNU General Public License v2
 
+import collections
 import re
 
 from portage.cache import fs_template
@@ -23,6 +24,9 @@ class database(fs_template.FsBased):
 	# equation: cache_bytes = page_bytes * page_count
 	cache_bytes = 1024 * 1024 * 10
 
+	_connection_info_entry = collections.namedtuple('_connection_info_entry',
+		('connection', 'cursor'))
+
 	def __init__(self, *args, **config):
 		super(database, self).__init__(*args, **config)
 		self._import_sqlite()
@@ -44,8 +48,8 @@ class database(fs_template.FsBased):
 		# Set longer timeout for throwing a "database is locked" exception.
 		# Default timeout in sqlite3 module is 5.0 seconds.
 		config.setdefault("timeout", 15)
-		self._db_init_connection(config)
-		self._db_init_structures()
+		self._config = config
+		self._db_connection_info = None
 
 	def _import_sqlite(self):
 		# sqlite3 is optional with >=python-2.5
@@ -65,7 +69,20 @@ class database(fs_template.FsBased):
 			s = str(s)
 		return "'%s'" % s.replace("'", "''")
 
-	def _db_init_connection(self, config):
+	@property
+	def _db_cursor(self):
+		if self._db_connection_info is None:
+			self._db_init_connection()
+		return self._db_connection_info.cursor
+
+	@property
+	def _db_connection(self):
+		if self._db_connection_info is None:
+			self._db_init_connection()
+		return self._db_connection_info.connection
+
+	def _db_init_connection(self):
+		config = self._config
 		self._dbpath = self.location + ".sqlite"
 		#if os.path.exists(self._dbpath):
 		#	os.unlink(self._dbpath)
@@ -74,14 +91,16 @@ class database(fs_template.FsBased):
 		try:
 			if not self.readonly:
 				self._ensure_dirs()
-			self._db_connection = self._db_module.connect(
+			connection = self._db_module.connect(
 				database=_unicode_decode(self._dbpath), **connection_kwargs)
-			self._db_cursor = self._db_connection.cursor()
+			cursor = connection.cursor()
+			self._db_connection_info = self._connection_info_entry(connection, cursor)
 			self._db_cursor.execute("PRAGMA encoding = %s" % self._db_escape_string("UTF-8"))
 			if not self.readonly and not self._ensure_access(self._dbpath):
 				raise cache_errors.InitializationError(self.__class__, "can't ensure perms on %s" % self._dbpath)
 			self._db_init_cache_size(config["cache_bytes"])
 			self._db_init_synchronous(config["synchronous"])
+			self._db_init_structures()
 		except self._db_error as e:
 			raise cache_errors.InitializationError(self.__class__, e)
 
-- 
2.25.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.