add method

  1. @override
Future<void> add(
  1. Node node
)
override

Add a non existing node to the storage backend.

@param node the node to be added to the backend @throws StorageException if the node already exists, the parental node does not exist, or the backend storage encountered a problem

Implementation

@override
Future<void> add(Node node) async {
  // do not add skeleton nodes
  if (node.skeleton) {
    throw StorageException('Skeleton nodes cannot be added.');
  }
  checkPath(node.path);
  String? parentPath = node.parentPath != '' ? node.parentPath : ':';
  if (parentPath != null && (await get(parentPath)).tombstone) {
    throw StorageException(
        'Node parent "$parentPath" does not exist (but exited in the past; It is a tombstone now)');
  }
  try {
    Node n = await get(node.path);
    if (!n.tombstone) {
      throw Exception('Node "${node.path}" already exists');
    } else {
      await delete(node.path, deleteTombStone: true);
    }
  } on Exception catch (e, st) {
    if ((e is! StorageException)) {
      throw StorageException(
          'Node "${node.path}" already exists', null, e, st);
    }
  }

  // update children
  if (parentPath != null) {
    Node parent = await get(parentPath);
    await parent.addChild(node);
    await update(parent);
  }

  // add SQL table entry for node
  String sqlStatement = 'INSERT INTO '
      'storage_node(path, owner, name, visibility, children, tombstone,last_modified) '
      'VALUES (?,?,?,?,?,?,?)';
  try {
    var args = [
      node.path,
      node.owner,
      node.name,
      (node.visibility).toValueString(),
      await node.getChildNodesCsv(),
      node.tombstone.toString().toUpperCase(),
      node.lastModified
    ];
    _requestResult(sqlStatement, args);
  } on StorageException catch (e, s) {
    throw StorageException(
        'Could not add new node "${node.path}"', null, e, s);
  }

  // write node values
  Iterable<NodeValue> nvl = (await node.getValues()).values;
  for (NodeValue nv in nvl) {
    await addValue(node.path, nv);
  }
}