Belle II Software light-2406-ragdoll
LocalMetadataProvider Class Reference

Public Member Functions

def __init__ (self, filename, mode="read")
 
def get_payload_count (self)
 
def add_globaltag (self, tag_id, name, state, iovs)
 
def get_globaltags (self)
 
def get_payloads (self)
 
def get_all_iovs (self, globalTag, exp=None, run=None, message=None)
 

Static Public Attributes

int APPLICATION_ID = 0xb2cdb
 Application ID to be stored int the sqlite file.
 
int SCHEMA_VERSION = 1
 Schema version, to be increased when the table definitions change so that we can check for safe append operation.
 
str SCHEMA_SQL
 SQL script to create all necessary tables and views.
 

Protected Member Functions

def _resolve_id (self, name, entity)
 

Protected Attributes

 _cache
 Cache name->id mappings from the database.
 
 _database
 sqlite Database connection
 

Detailed Description

Class to handle local sqlite dump of conditions database metadata

This class can create and read sqlite dumps of the central database in a format
compatible with the local metadata provider in basf2.

Definition at line 20 of file local_metadata.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  filename,
  mode = "read" 
)
Open an sqlite database and make sure that the schema exists in the
correct version or create it if ``mode=overwrite``

Arguments:
  filename (str): name of the database file
  readonly (str): how to open the file. Can be one of ``read`` to open
    the file readonly, ``append` to append new data to an existing file
    and ``overwrite`` to recreate all tables and overwrite the contents.

Definition at line 124 of file local_metadata.py.

124 def __init__(self, filename, mode="read"):
125 """
126 Open an sqlite database and make sure that the schema exists in the
127 correct version or create it if ``mode=overwrite``
128
129 Arguments:
130 filename (str): name of the database file
131 readonly (str): how to open the file. Can be one of ``read`` to open
132 the file readonly, ``append` to append new data to an existing file
133 and ``overwrite`` to recreate all tables and overwrite the contents.
134 """
135
136
137 self._cache = {}
138
139 self._database = None
140
141 # connect to the database file ...
142 if mode == "read":
143 self._database = sqlite3.connect(f"file:{filename}?mode=ro", uri=True)
144 elif mode in ["append", "overwrite"]:
145 self._database = sqlite3.connect(filename)
146 else:
147 raise RuntimeError("invalid mode: please supply one of 'read', 'append', 'overwrite'")
148
149 if mode == "overwrite":
150 # drop and recreate all tables
151 self._database.executescript(self.SCHEMA_SQL)
152 # and set the application id/schema version correctly
153 self._database.execute(f"PRAGMA application_id = {self.APPLICATION_ID}")
154 self._database.execute(f"PRAGMA user_version = {self.SCHEMA_VERSION}")
155 self._database.commit()
156 else:
157 # make sure the application id/schema version is the same
158 application_id = self._database.execute("PRAGMA application_id").fetchone()[0]
159 if application_id != self.APPLICATION_ID:
160 raise RuntimeError("Not a b2conditionsdb database file")
161 schema_version = self._database.execute("PRAGMA user_version").fetchone()[0]
162 if schema_version != self.SCHEMA_VERSION:
163 raise RuntimeError("Cannot use sqlite file: different schema version, please recreate")
164

Member Function Documentation

◆ _resolve_id()

def _resolve_id (   self,
  name,
  entity 
)
protected
Resolve the id for a named entity in the database file.

Create new entities on demand and cache all known entities

Parameters:
  name (str): name to lookup
  entity (str): type of the entity, currently ``baseUrl`` or ``payloadName``

Definition at line 170 of file local_metadata.py.

170 def _resolve_id(self, name, entity):
171 """
172 Resolve the id for a named entity in the database file.
173
174 Create new entities on demand and cache all known entities
175
176 Parameters:
177 name (str): name to lookup
178 entity (str): type of the entity, currently ``baseUrl`` or ``payloadName``
179 """
180 # fill existing entries on first access
181 if entity not in self._cache:
182 self._cache[entity] = {row[0]: row[1] for row in self._database.execute(f"SELECT {entity}, {entity}Id from {entity}s")}
183 # and then check if we have this entry, otherwise make a new one
184 cache = self._cache[entity]
185 if name not in cache:
186 cache[name] = len(cache) + 1
187 self._database.execute(f"INSERT INTO {entity}s ({entity}Id, {entity}) VALUES (?,?)", (cache[name], name))
188 return cache[name]
189

◆ add_globaltag()

def add_globaltag (   self,
  tag_id,
  name,
  state,
  iovs 
)
Add a globaltag to the database file. If the globaltag already exists in
the file its contents will be replaced.

Parameters: tag_id (str): id of the globaltag in the central database
  name (str): name of the globaltag in the central database state (str):
  state of the globaltag in the central database iovs
  (list(PayloadInformation)): all iovs valid for this globaltag

Definition at line 190 of file local_metadata.py.

190 def add_globaltag(self, tag_id, name, state, iovs):
191 """
192 Add a globaltag to the database file. If the globaltag already exists in
193 the file its contents will be replaced.
194
195 Parameters: tag_id (str): id of the globaltag in the central database
196 name (str): name of the globaltag in the central database state (str):
197 state of the globaltag in the central database iovs
198 (list(PayloadInformation)): all iovs valid for this globaltag
199 """
200 self._database.execute("INSERT OR REPLACE INTO globaltags VALUES (?,?,?)", (tag_id, name, state))
201 # remove existing iovs ... we want to append globaltags but we want all globaltags to be correct
202 self._database.execute("DELETE from iovs WHERE globalTagId=?", (tag_id,))
203
204 all_payloads = {}
205 all_iovs = []
206 for p in iovs:
207 if p.payload_id not in all_payloads:
208 base_url = self._resolve_id(p.base_url, 'baseUrl')
209 name = self._resolve_id(p.name, 'payloadName')
210 url = None
211 if p.payload_url.lstrip('/') != f"dbstore/{p.name}/dbstore_{p.name}_rev_{p.revision}.root":
212 url = p.payload_url
213 all_payloads[p.payload_id] = (p.payload_id, name, p.revision, p.checksum, url, base_url)
214 all_iovs.append((tag_id, p.payload_id) + p.iov)
215
216 self._database.executemany("INSERT OR REPLACE INTO payloads VALUES (?,?,?,?,?,?)", all_payloads.values())
217 self._database.executemany("INSERT INTO iovs VALUES (?,?,?,?,?,?)", all_iovs)
218 # make sure everything is committed
219 self._database.commit()
220 self._database.execute("VACUUM")
221

◆ get_all_iovs()

def get_all_iovs (   self,
  globalTag,
  exp = None,
  run = None,
  message = None 
)
Get all iovs for a given globaltag

Parameters:
  globalTag (str): name of the globaltag
  exp (int): experiment number to check (or None to return all iovs)
  run (int): run number to check (or None to return all iovs)
  message (str): ignored, just for compatibility with `ConditionsDB.get_all_iovs`

Returns:
  a sorted list of `PayloadInformation` objects

Definition at line 242 of file local_metadata.py.

242 def get_all_iovs(self, globalTag, exp=None, run=None, message=None):
243 """Get all iovs for a given globaltag
244
245 Parameters:
246 globalTag (str): name of the globaltag
247 exp (int): experiment number to check (or None to return all iovs)
248 run (int): run number to check (or None to return all iovs)
249 message (str): ignored, just for compatibility with `ConditionsDB.get_all_iovs`
250
251 Returns:
252 a sorted list of `PayloadInformation` objects
253 """
254 params = {"globalTag": globalTag}
255 query = """\
256 SELECT
257 payloadId, payloadName, revision, checksum, payloadUrl, baseUrl,
258 firstExp, firstRun, finalExp, finalRun
259 FROM iov_payloads
260 WHERE globalTagName=:globalTag"""
261 if exp is not None:
262 params.update({"exp": exp, "run": run})
263 query += """ AND\
264 ((firstExp==:exp AND firstRun<=:run) OR firstExp<:exp) AND
265 (finalExp<0 OR (finalRun<0 AND finalExp>=:exp) OR (finalExp>:exp) OR (finalExp==:exp AND finalRun>=:run))"""
266 iovs = sorted([PayloadInformation(*row[:6], iov_id=None, iov=row[6:]) for row in
267 self._database.execute(query, params)])
268 return iovs

◆ get_globaltags()

def get_globaltags (   self)
Return the list of globaltags stored in the file

Returns:
  a list of (id, name, state) tuples for all globaltags

Definition at line 222 of file local_metadata.py.

222 def get_globaltags(self):
223 """Return the list of globaltags stored in the file
224
225 Returns:
226 a list of (id, name, state) tuples for all globaltags
227 """
228 return [row for row in self._database.execute("SELECT globalTagId, globalTagName, globalTagStatus FROM globaltags "
229 "ORDER by globalTagName")]
230

◆ get_payload_count()

def get_payload_count (   self)
Get the number of distinct payloads known to this file

Definition at line 165 of file local_metadata.py.

165 def get_payload_count(self):
166 """Get the number of distinct payloads known to this file"""
167 cursor = self._database.execute("SELECT count(*) from full_payloads")
168 return cursor.fetchone()[0]
169

◆ get_payloads()

def get_payloads (   self)
Get all payloads existing in this file

Returns:
  a sorted list of `PayloadInformation` objects for all payloads defined in this file with the iov set to None

Definition at line 231 of file local_metadata.py.

231 def get_payloads(self):
232 """Get all payloads existing in this file
233
234 Returns:
235 a sorted list of `PayloadInformation` objects for all payloads defined in this file with the iov set to None
236 """
237 payloads = sorted([PayloadInformation(*row) for row in
238 self._database.execute("SELECT payloadId, payloadName, revision, checksum, "
239 "payloadUrl, baseUrl from full_payloads")])
240 return payloads
241

Member Data Documentation

◆ _cache

_cache
protected

Cache name->id mappings from the database.

Definition at line 137 of file local_metadata.py.

◆ _database

_database
protected

sqlite Database connection

Definition at line 139 of file local_metadata.py.

◆ APPLICATION_ID

int APPLICATION_ID = 0xb2cdb
static

Application ID to be stored int the sqlite file.

Definition at line 28 of file local_metadata.py.

◆ SCHEMA_SQL

str SCHEMA_SQL
static

SQL script to create all necessary tables and views.

Definition at line 33 of file local_metadata.py.

◆ SCHEMA_VERSION

int SCHEMA_VERSION = 1
static

Schema version, to be increased when the table definitions change so that we can check for safe append operation.

Definition at line 31 of file local_metadata.py.


The documentation for this class was generated from the following file: