Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

MongoDB internally defines a database as a set of collections, with each collection consisting of a set of documents.  These documents are essentially JSON objects.  In order to use MongoDB to save configuration information, additional python classes have been written on top of pymongo., 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:

Code Block
languagepy
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, the ":types:" dictionary does not contain lists at that level.

A few top-level keys have predefined types, reflecting their use as 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:

  • set(name, value, type="INT32", override=False, append=False)
  • setInfo(detType=None, detName=None, detId=None, doc=None)
  • setAlg(alg, version=[0,0,0], doc="")
  • typed_json()
  • get(name, withtype=False)
  • getenumdict(name, reverse=False)
  • define_enum(name, value)