1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: dbobjcache.py 56295 2015-06-09 14:29:55Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Test Manager - Database object cache.
|
---|
6 | """
|
---|
7 |
|
---|
8 | __copyright__ = \
|
---|
9 | """
|
---|
10 | Copyright (C) 2012-2015 Oracle Corporation
|
---|
11 |
|
---|
12 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | available from http://www.virtualbox.org. This file is free software;
|
---|
14 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | General Public License (GPL) as published by the Free Software
|
---|
16 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 |
|
---|
20 | The contents of this file may alternatively be used under the terms
|
---|
21 | of the Common Development and Distribution License Version 1.0
|
---|
22 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | CDDL are applicable instead of those of the GPL.
|
---|
25 |
|
---|
26 | You may elect to license modified versions of this file under the
|
---|
27 | terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | """
|
---|
29 | __version__ = "$Revision: 56295 $"
|
---|
30 |
|
---|
31 |
|
---|
32 | # Validation Kit imports.
|
---|
33 | from testmanager.core.base import ModelLogicBase;
|
---|
34 |
|
---|
35 |
|
---|
36 | class DatabaseObjCache(ModelLogicBase):
|
---|
37 | """
|
---|
38 | Database object cache.
|
---|
39 |
|
---|
40 | This is mainly for reports and test results where we wish to get further
|
---|
41 | information on a data series or similar. The cache should reduce database
|
---|
42 | lookups as well as pyhon memory footprint.
|
---|
43 |
|
---|
44 | Note! Dependecies are imported when needed to avoid potential cylic dependency issues.
|
---|
45 | """
|
---|
46 |
|
---|
47 | ## @name Cache object types.
|
---|
48 | ## @{
|
---|
49 | ksObjType_TestResultStrTab_idStrName = 0;
|
---|
50 | ksObjType_BuildCategory_idBuildCategory = 1;
|
---|
51 | ksObjType_TestBox_idTestBox = 2;
|
---|
52 | ksObjType_TestBox_idGenTestBox = 3;
|
---|
53 | ksObjType_TestCase_idTestCase = 4;
|
---|
54 | ksObjType_TestCase_idGenTestCase = 5;
|
---|
55 | ksObjType_TestCaseArgs_idTestCaseArgs = 6;
|
---|
56 | ksObjType_TestCaseArgs_idGenTestCaseArgs = 7;
|
---|
57 | ksObjType_VcsRevision_sRepository_iRevision = 8;
|
---|
58 | ksObjType_End = 9;
|
---|
59 | ## @}
|
---|
60 |
|
---|
61 | def __init__(self, oDb, tsNow = None, sPeriodBack = None, cHoursBack = None):
|
---|
62 | ModelLogicBase.__init__(self, oDb);
|
---|
63 |
|
---|
64 | self.tsNow = tsNow;
|
---|
65 | self.sPeriodBack = sPeriodBack;
|
---|
66 | if sPeriodBack is None and cHoursBack is not None:
|
---|
67 | self.sPeriodBack = '%u hours' % cHoursBack;
|
---|
68 |
|
---|
69 | self._adCache = (
|
---|
70 | dict(), dict(), dict(), dict(),
|
---|
71 | dict(), dict(), dict(), dict(),
|
---|
72 | dict(),
|
---|
73 | );
|
---|
74 | assert(len(self._adCache) == self.ksObjType_End);
|
---|
75 |
|
---|
76 | def _handleDbException(self):
|
---|
77 | """ Deals with database exceptions. """
|
---|
78 | raise;
|
---|
79 | #self._oDb.rollback();
|
---|
80 | #return False;
|
---|
81 |
|
---|
82 | def getTestResultString(self, idStrName):
|
---|
83 | """ Gets a string from the TestResultStrTab. """
|
---|
84 | sRet = self._adCache[self.ksObjType_TestResultStrTab_idStrName].get(idStrName);
|
---|
85 | if sRet is None:
|
---|
86 | # Load cache entry.
|
---|
87 | self._oDb.execute('SELECT sValue FROM TestResultStrTab WHERE idStr = %s', (idStrName,));
|
---|
88 | sRet = self._oDb.fetchOne()[0];
|
---|
89 | self._adCache[self.ksObjType_TestResultStrTab_idStrName][idStrName] = sRet
|
---|
90 | return sRet;
|
---|
91 |
|
---|
92 | def getBuildCategory(self, idBuildCategory):
|
---|
93 | """ Gets the corresponding BuildCategoryData object. """
|
---|
94 | oRet = self._adCache[self.ksObjType_BuildCategory_idBuildCategory].get(idBuildCategory);
|
---|
95 | if oRet is None:
|
---|
96 | # Load cache entry.
|
---|
97 | from testmanager.core.build import BuildCategoryData;
|
---|
98 | oRet = BuildCategoryData();
|
---|
99 | try: oRet.initFromDbWithId(self._oDb, idBuildCategory);
|
---|
100 | except: self._handleDbException();
|
---|
101 | self._adCache[self.ksObjType_BuildCategory_idBuildCategory][idBuildCategory] = oRet;
|
---|
102 | return oRet;
|
---|
103 |
|
---|
104 | def getTestBox(self, idTestBox):
|
---|
105 | """ Gets the corresponding TestBoxData object. """
|
---|
106 | oRet = self._adCache[self.ksObjType_TestBox_idTestBox].get(idTestBox);
|
---|
107 | if oRet is None:
|
---|
108 | # Load cache entry.
|
---|
109 | from testmanager.core.testbox import TestBoxData;
|
---|
110 | oRet = TestBoxData();
|
---|
111 | try: oRet.initFromDbWithId(self._oDb, idTestBox, self.tsNow, self.sPeriodBack);
|
---|
112 | except: self._handleDbException();
|
---|
113 | else: self._adCache[self.ksObjType_TestBox_idGenTestBox][oRet.idGenTestBox] = oRet;
|
---|
114 | self._adCache[self.ksObjType_TestBox_idTestBox][idTestBox] = oRet;
|
---|
115 | return oRet;
|
---|
116 |
|
---|
117 | def getTestCase(self, idTestCase):
|
---|
118 | """ Gets the corresponding TestCaseData object. """
|
---|
119 | oRet = self._adCache[self.ksObjType_TestCase_idTestCase].get(idTestCase);
|
---|
120 | if oRet is None:
|
---|
121 | # Load cache entry.
|
---|
122 | from testmanager.core.testcase import TestCaseData;
|
---|
123 | oRet = TestCaseData();
|
---|
124 | try: oRet.initFromDbWithId(self._oDb, idTestCase, self.tsNow, self.sPeriodBack);
|
---|
125 | except: self._handleDbException();
|
---|
126 | else: self._adCache[self.ksObjType_TestCase_idGenTestCase][oRet.idGenTestCase] = oRet;
|
---|
127 | self._adCache[self.ksObjType_TestCase_idTestCase][idTestCase] = oRet;
|
---|
128 | return oRet;
|
---|
129 |
|
---|
130 | def getTestCaseArgs(self, idTestCaseArgs):
|
---|
131 | """ Gets the corresponding TestCaseArgsData object. """
|
---|
132 | oRet = self._adCache[self.ksObjType_TestCaseArgs_idTestCaseArgs].get(idTestCaseArgs);
|
---|
133 | if oRet is None:
|
---|
134 | # Load cache entry.
|
---|
135 | from testmanager.core.testcaseargs import TestCaseArgsData;
|
---|
136 | oRet = TestCaseArgsData();
|
---|
137 | try: oRet.initFromDbWithId(self._oDb, idTestCaseArgs, self.tsNow, self.sPeriodBack);
|
---|
138 | except: self._handleDbException();
|
---|
139 | else: self._adCache[self.ksObjType_TestCaseArgs_idGenTestCaseArgs][oRet.idGenTestCaseArgs] = oRet;
|
---|
140 | self._adCache[self.ksObjType_TestCaseArgs_idTestCaseArgs][idTestCaseArgs] = oRet;
|
---|
141 | return oRet;
|
---|
142 |
|
---|
143 | def preloadVcsRevInfo(self, sRepository, aiRevisions):
|
---|
144 | """
|
---|
145 | Preloads VCS revision information.
|
---|
146 | ASSUMES aiRevisions does not contain duplicate keys.
|
---|
147 | """
|
---|
148 | from testmanager.core.vcsrevisions import VcsRevisionData;
|
---|
149 | dRepo = self._adCache[self.ksObjType_VcsRevision_sRepository_iRevision].get(sRepository);
|
---|
150 | if dRepo is None:
|
---|
151 | dRepo = dict();
|
---|
152 | self._adCache[self.ksObjType_VcsRevision_sRepository_iRevision][sRepository] = dRepo;
|
---|
153 | aiFiltered = aiRevisions;
|
---|
154 | else:
|
---|
155 | aiFiltered = [];
|
---|
156 | for iRevision in aiRevisions:
|
---|
157 | if iRevision not in dRepo:
|
---|
158 | aiFiltered.append(iRevision);
|
---|
159 | if len(aiFiltered) > 0:
|
---|
160 | self._oDb.execute('SELECT *\n'
|
---|
161 | 'FROM VcsRevisions\n'
|
---|
162 | 'WHERE sRepository = %s\n'
|
---|
163 | ' AND iRevision IN (' + ','.join([str(i) for i in aiFiltered]) + ')'
|
---|
164 | , ( sRepository, ));
|
---|
165 | for aoRow in self._oDb.fetchAll():
|
---|
166 | oInfo = VcsRevisionData().initFromDbRow(aoRow);
|
---|
167 | dRepo[oInfo.iRevision] = oInfo;
|
---|
168 | return True;
|
---|
169 |
|
---|
170 | def getVcsRevInfo(self, sRepository, iRevision):
|
---|
171 | """
|
---|
172 | Gets the corresponding VcsRevisionData object.
|
---|
173 | May return a default (all NULLs) VcsRevisionData object if the revision
|
---|
174 | information isn't available in the database yet.
|
---|
175 | """
|
---|
176 | dRepo = self._adCache[self.ksObjType_VcsRevision_sRepository_iRevision].get(sRepository);
|
---|
177 | if dRepo is not None:
|
---|
178 | oRet = dRepo.get(iRevision);
|
---|
179 | else:
|
---|
180 | dRepo = dict();
|
---|
181 | self._adCache[self.ksObjType_VcsRevision_sRepository_iRevision][sRepository] = dRepo;
|
---|
182 | oRet = None;
|
---|
183 | if oRet is None:
|
---|
184 | from testmanager.core.vcsrevisions import VcsRevisionLogic;
|
---|
185 | oRet = VcsRevisionLogic(self._oDb).tryFetch(sRepository, iRevision);
|
---|
186 | if oRet is None:
|
---|
187 | from testmanager.core.vcsrevisions import VcsRevisionData;
|
---|
188 | oRet = VcsRevisionData();
|
---|
189 | dRepo[iRevision] = oRet;
|
---|
190 | return oRet;
|
---|
191 |
|
---|