Belle II Software  release-08-01-10
LocalMetadataProvider Class Reference
Collaboration diagram for LocalMetadataProvider:

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.
 
string SCHEMA_SQL
 SQL script to create all necessary tables and views.
 

Private Member Functions

def _resolve_id (self, name, entity)
 

Private 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 
)
private
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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.


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