diff options
Diffstat (limited to 'bitbake/lib/prserv/db.py')
-rw-r--r-- | bitbake/lib/prserv/db.py | 65 |
1 files changed, 51 insertions, 14 deletions
diff --git a/bitbake/lib/prserv/db.py b/bitbake/lib/prserv/db.py index cb2a2461e0..2710d4a225 100644 --- a/bitbake/lib/prserv/db.py +++ b/bitbake/lib/prserv/db.py | |||
@@ -30,21 +30,29 @@ if sqlversion[0] < 3 or (sqlversion[0] == 3 and sqlversion[1] < 3): | |||
30 | # | 30 | # |
31 | 31 | ||
32 | class PRTable(object): | 32 | class PRTable(object): |
33 | def __init__(self, conn, table, nohist): | 33 | def __init__(self, conn, table, nohist, read_only): |
34 | self.conn = conn | 34 | self.conn = conn |
35 | self.nohist = nohist | 35 | self.nohist = nohist |
36 | self.read_only = read_only | ||
36 | self.dirty = False | 37 | self.dirty = False |
37 | if nohist: | 38 | if nohist: |
38 | self.table = "%s_nohist" % table | 39 | self.table = "%s_nohist" % table |
39 | else: | 40 | else: |
40 | self.table = "%s_hist" % table | 41 | self.table = "%s_hist" % table |
41 | 42 | ||
42 | self._execute("CREATE TABLE IF NOT EXISTS %s \ | 43 | if self.read_only: |
43 | (version TEXT NOT NULL, \ | 44 | table_exists = self._execute( |
44 | pkgarch TEXT NOT NULL, \ | 45 | "SELECT count(*) FROM sqlite_master \ |
45 | checksum TEXT NOT NULL, \ | 46 | WHERE type='table' AND name='%s'" % (self.table)) |
46 | value INTEGER, \ | 47 | if not table_exists: |
47 | PRIMARY KEY (version, pkgarch, checksum));" % self.table) | 48 | raise prserv.NotFoundError |
49 | else: | ||
50 | self._execute("CREATE TABLE IF NOT EXISTS %s \ | ||
51 | (version TEXT NOT NULL, \ | ||
52 | pkgarch TEXT NOT NULL, \ | ||
53 | checksum TEXT NOT NULL, \ | ||
54 | value INTEGER, \ | ||
55 | PRIMARY KEY (version, pkgarch, checksum));" % self.table) | ||
48 | 56 | ||
49 | def _execute(self, *query): | 57 | def _execute(self, *query): |
50 | """Execute a query, waiting to acquire a lock if necessary""" | 58 | """Execute a query, waiting to acquire a lock if necessary""" |
@@ -59,8 +67,9 @@ class PRTable(object): | |||
59 | raise exc | 67 | raise exc |
60 | 68 | ||
61 | def sync(self): | 69 | def sync(self): |
62 | self.conn.commit() | 70 | if not self.read_only: |
63 | self._execute("BEGIN EXCLUSIVE TRANSACTION") | 71 | self.conn.commit() |
72 | self._execute("BEGIN EXCLUSIVE TRANSACTION") | ||
64 | 73 | ||
65 | def sync_if_dirty(self): | 74 | def sync_if_dirty(self): |
66 | if self.dirty: | 75 | if self.dirty: |
@@ -75,6 +84,15 @@ class PRTable(object): | |||
75 | return row[0] | 84 | return row[0] |
76 | else: | 85 | else: |
77 | #no value found, try to insert | 86 | #no value found, try to insert |
87 | if self.read_only: | ||
88 | data = self._execute("SELECT ifnull(max(value)+1,0) FROM %s where version=? AND pkgarch=?;" % (self.table), | ||
89 | (version, pkgarch)) | ||
90 | row = data.fetchone() | ||
91 | if row is not None: | ||
92 | return row[0] | ||
93 | else: | ||
94 | return 0 | ||
95 | |||
78 | try: | 96 | try: |
79 | self._execute("INSERT INTO %s VALUES (?, ?, ?, (select ifnull(max(value)+1,0) from %s where version=? AND pkgarch=?));" | 97 | self._execute("INSERT INTO %s VALUES (?, ?, ?, (select ifnull(max(value)+1,0) from %s where version=? AND pkgarch=?));" |
80 | % (self.table,self.table), | 98 | % (self.table,self.table), |
@@ -103,6 +121,15 @@ class PRTable(object): | |||
103 | return row[0] | 121 | return row[0] |
104 | else: | 122 | else: |
105 | #no value found, try to insert | 123 | #no value found, try to insert |
124 | if self.read_only: | ||
125 | data = self._execute("SELECT ifnull(max(value)+1,0) FROM %s where version=? AND pkgarch=?;" % (self.table), | ||
126 | (version, pkgarch)) | ||
127 | row = data.fetchone() | ||
128 | if row is not None: | ||
129 | return row[0] | ||
130 | else: | ||
131 | return 0 | ||
132 | |||
106 | try: | 133 | try: |
107 | self._execute("INSERT OR REPLACE INTO %s VALUES (?, ?, ?, (select ifnull(max(value)+1,0) from %s where version=? AND pkgarch=?));" | 134 | self._execute("INSERT OR REPLACE INTO %s VALUES (?, ?, ?, (select ifnull(max(value)+1,0) from %s where version=? AND pkgarch=?));" |
108 | % (self.table,self.table), | 135 | % (self.table,self.table), |
@@ -128,6 +155,9 @@ class PRTable(object): | |||
128 | return self._getValueHist(version, pkgarch, checksum) | 155 | return self._getValueHist(version, pkgarch, checksum) |
129 | 156 | ||
130 | def _importHist(self, version, pkgarch, checksum, value): | 157 | def _importHist(self, version, pkgarch, checksum, value): |
158 | if self.read_only: | ||
159 | return None | ||
160 | |||
131 | val = None | 161 | val = None |
132 | data = self._execute("SELECT value FROM %s WHERE version=? AND pkgarch=? AND checksum=?;" % self.table, | 162 | data = self._execute("SELECT value FROM %s WHERE version=? AND pkgarch=? AND checksum=?;" % self.table, |
133 | (version, pkgarch, checksum)) | 163 | (version, pkgarch, checksum)) |
@@ -152,6 +182,9 @@ class PRTable(object): | |||
152 | return val | 182 | return val |
153 | 183 | ||
154 | def _importNohist(self, version, pkgarch, checksum, value): | 184 | def _importNohist(self, version, pkgarch, checksum, value): |
185 | if self.read_only: | ||
186 | return None | ||
187 | |||
155 | try: | 188 | try: |
156 | #try to insert | 189 | #try to insert |
157 | self._execute("INSERT INTO %s VALUES (?, ?, ?, ?);" % (self.table), | 190 | self._execute("INSERT INTO %s VALUES (?, ?, ?, ?);" % (self.table), |
@@ -245,19 +278,23 @@ class PRTable(object): | |||
245 | 278 | ||
246 | class PRData(object): | 279 | class PRData(object): |
247 | """Object representing the PR database""" | 280 | """Object representing the PR database""" |
248 | def __init__(self, filename, nohist=True): | 281 | def __init__(self, filename, nohist=True, read_only=False): |
249 | self.filename=os.path.abspath(filename) | 282 | self.filename=os.path.abspath(filename) |
250 | self.nohist=nohist | 283 | self.nohist=nohist |
284 | self.read_only = read_only | ||
251 | #build directory hierarchy | 285 | #build directory hierarchy |
252 | try: | 286 | try: |
253 | os.makedirs(os.path.dirname(self.filename)) | 287 | os.makedirs(os.path.dirname(self.filename)) |
254 | except OSError as e: | 288 | except OSError as e: |
255 | if e.errno != errno.EEXIST: | 289 | if e.errno != errno.EEXIST: |
256 | raise e | 290 | raise e |
257 | self.connection=sqlite3.connect(self.filename, isolation_level="EXCLUSIVE", check_same_thread = False) | 291 | uri = "file:%s%s" % (self.filename, "?mode=ro" if self.read_only else "") |
292 | logger.debug("Opening PRServ database '%s'" % (uri)) | ||
293 | self.connection=sqlite3.connect(uri, uri=True, isolation_level="EXCLUSIVE", check_same_thread = False) | ||
258 | self.connection.row_factory=sqlite3.Row | 294 | self.connection.row_factory=sqlite3.Row |
259 | self.connection.execute("pragma synchronous = off;") | 295 | if not self.read_only: |
260 | self.connection.execute("PRAGMA journal_mode = MEMORY;") | 296 | self.connection.execute("pragma synchronous = off;") |
297 | self.connection.execute("PRAGMA journal_mode = MEMORY;") | ||
261 | self._tables={} | 298 | self._tables={} |
262 | 299 | ||
263 | def disconnect(self): | 300 | def disconnect(self): |
@@ -270,7 +307,7 @@ class PRData(object): | |||
270 | if tblname in self._tables: | 307 | if tblname in self._tables: |
271 | return self._tables[tblname] | 308 | return self._tables[tblname] |
272 | else: | 309 | else: |
273 | tableobj = self._tables[tblname] = PRTable(self.connection, tblname, self.nohist) | 310 | tableobj = self._tables[tblname] = PRTable(self.connection, tblname, self.nohist, self.read_only) |
274 | return tableobj | 311 | return tableobj |
275 | 312 | ||
276 | def __delitem__(self, tblname): | 313 | def __delitem__(self, tblname): |