Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH 2/3] sqlite: add lazy connection init
Date: Sat, 08 Aug 2020 04:13:30
Message-Id: 20200808040857.77352-3-zmedico@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH 0/3] sqlite: fork safety (bug 736334) by Zac Medico
1 Signed-off-by: Zac Medico <zmedico@g.o>
2 ---
3 lib/portage/cache/sqlite.py | 29 ++++++++++++++++++++++++-----
4 1 file changed, 24 insertions(+), 5 deletions(-)
5
6 diff --git a/lib/portage/cache/sqlite.py b/lib/portage/cache/sqlite.py
7 index 55ae8f0e5..0395dd516 100644
8 --- a/lib/portage/cache/sqlite.py
9 +++ b/lib/portage/cache/sqlite.py
10 @@ -1,6 +1,7 @@
11 # Copyright 1999-2020 Gentoo Authors
12 # Distributed under the terms of the GNU General Public License v2
13
14 +import collections
15 import re
16
17 from portage.cache import fs_template
18 @@ -23,6 +24,9 @@ class database(fs_template.FsBased):
19 # equation: cache_bytes = page_bytes * page_count
20 cache_bytes = 1024 * 1024 * 10
21
22 + _connection_info_entry = collections.namedtuple('_connection_info_entry',
23 + ('connection', 'cursor'))
24 +
25 def __init__(self, *args, **config):
26 super(database, self).__init__(*args, **config)
27 self._import_sqlite()
28 @@ -44,8 +48,8 @@ class database(fs_template.FsBased):
29 # Set longer timeout for throwing a "database is locked" exception.
30 # Default timeout in sqlite3 module is 5.0 seconds.
31 config.setdefault("timeout", 15)
32 - self._db_init_connection(config)
33 - self._db_init_structures()
34 + self._config = config
35 + self._db_connection_info = None
36
37 def _import_sqlite(self):
38 # sqlite3 is optional with >=python-2.5
39 @@ -65,7 +69,20 @@ class database(fs_template.FsBased):
40 s = str(s)
41 return "'%s'" % s.replace("'", "''")
42
43 - def _db_init_connection(self, config):
44 + @property
45 + def _db_cursor(self):
46 + if self._db_connection_info is None:
47 + self._db_init_connection()
48 + return self._db_connection_info.cursor
49 +
50 + @property
51 + def _db_connection(self):
52 + if self._db_connection_info is None:
53 + self._db_init_connection()
54 + return self._db_connection_info.connection
55 +
56 + def _db_init_connection(self):
57 + config = self._config
58 self._dbpath = self.location + ".sqlite"
59 #if os.path.exists(self._dbpath):
60 # os.unlink(self._dbpath)
61 @@ -74,14 +91,16 @@ class database(fs_template.FsBased):
62 try:
63 if not self.readonly:
64 self._ensure_dirs()
65 - self._db_connection = self._db_module.connect(
66 + connection = self._db_module.connect(
67 database=_unicode_decode(self._dbpath), **connection_kwargs)
68 - self._db_cursor = self._db_connection.cursor()
69 + cursor = connection.cursor()
70 + self._db_connection_info = self._connection_info_entry(connection, cursor)
71 self._db_cursor.execute("PRAGMA encoding = %s" % self._db_escape_string("UTF-8"))
72 if not self.readonly and not self._ensure_access(self._dbpath):
73 raise cache_errors.InitializationError(self.__class__, "can't ensure perms on %s" % self._dbpath)
74 self._db_init_cache_size(config["cache_bytes"])
75 self._db_init_synchronous(config["synchronous"])
76 + self._db_init_structures()
77 except self._db_error as e:
78 raise cache_errors.InitializationError(self.__class__, e)
79
80 --
81 2.25.3