You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

MongoDB internally defines a database as a set of collections, with each collection consisting of a set of documents, each of which is essentially a JSON object.  Additional python code has been written to use MongoDB as a configuration database.  These classes and routines can be imported as:

from psalg.configdb.typed_json import *
import psalg.configdb.configdb as configdb

Typed JSON

JSON does not provide any type information for the values it contains.  For a configuration database, this can be problematic, as some numeric values might be limited to integers or even a small set of integers.  Therefore, additional information is added to the JSON object to provide type information.  Any field name ending in ":RO" is considered to be read-only and will not be displayed by the graphical editor.  An additional key, ":types:" will be added to the top level dictionary.  This key maps to a dictionary with roughly the same structure as the JSON object, except that instead of containing values, the dictionary contains type information.  This information is either:

  • A basic type: one of "UINT8", "UINT16", "UINT32", "UINT64", "INT8", "INT16", "INT32", "INT64", "FLOAT", "DOUBLE" or "CHARSTR".
  • A string denoting an enumeration type.
  • A list, the first element is either a basic or enumeration type, and the other elements are integers denoting array sizes.

The ":types:" dictionary also has an ":enum:" entry defining all of the enumeration types.  The keys of the ":enum:" dictionary are the enumeration types, and the value is a dictionary mapping enumeration names to integer values.

It is also assumed that any lists in the JSON object contain objects of the same type, so the ":types:" dictionary does not contain a list at that level but the type information for every element of the list.

A few top-level keys have predefined types, reflecting their use in configuration objects.  These reserved keys are:

  • "detType:RO", a mandatory CHARSTR.
  • "detName:RO", a mandatory CHARSTR.
  • "detId:RO", a mandatory CHARSTR.
  • "doc:RO", an optional CHARSTR.
  • "alg:RO", an optional dictionary containing:
    • "alg:RO", a mandatory CHARSTR.
    • "doc:RO", an optional CHARSTR.
    • "version:RO", a mandatory ["INT32", 3].

The cdict Class

In order to simplify the creation of typed JSON objects, the typed_json module defines the cdict class.  The constructor for this class takes an optional argument which is either an instance of another cdict or a dictionary representing a typed JSON object that is used to initialize the new cdict.  The main methods for this class are:

  • setInfo(detType=None, detName=None, detId=None, doc=None)
    Set the top-level reserved keys to the specified values.
  • setAlg(alg, version=[0,0,0], doc="")
    Set the top-level "alg" key to a dictionary containing the arguments.
  • set(name, value, type="INT32", override=False, append=False)
    The "name" parameter is a "flattened" name with dot-separated fields.  XXX
  • typed_json()
    Return a dictionary representing a typed JSON object (with the ":types:" key, etc.)
  • get(name, withtype=False)
  • getenumdict(name, reverse=False)
  • define_enum(name, value)

 

  • No labels