'Create a Database for Spack installations under ``root``.
A Database is a cache of Specs data from ``$prefix/spec.yaml``
files in Spack installation directories.
By default, Database files (data and lock files) are stored
under ``root/.spack-db``, which is created if it does not
exist.  This is the ``db_dir``.
The Database will attempt to read an ``index.json`` file in
``db_dir``.  If it does not find one, it will fall back to read
an ``index.yaml`` if one is present.  If that does not exist, it
will create a database when needed by scanning the entire
Database root for ``spec.yaml`` files according to Spack\'s
``DirectoryLayout``.
Caller may optionally provide a custom ``db_dir`` parameter
where data will be stored.  This is intended to be used for
testing the Database class.'

def __init__(self, root, db_dir=None):

    self.root = root
    if (db_dir is None):
        self._db_dir = join_path(self.root, _db_dirname)
    else:
        self._db_dir = db_dir
    self._old_yaml_index_path = join_path(self._db_dir, 'index.yaml')
    self._index_path = join_path(self._db_dir, 'index.json')
    self._lock_path = join_path(self._db_dir, 'lock')
    self.prefix_lock_path = join_path(self._db_dir, 'prefix_lock')
    if (not os.path.exists(self._db_dir)):
        mkdirp(self._db_dir)
    self.lock = Lock(self._lock_path)
    self._data = {}
    self._error = None
