executeBulk method

Future<List<Map<String, dynamic>>> executeBulk()

Implementation

Future<List<Map<String, dynamic>>> executeBulk() async {
  var retList = <Map<String, dynamic>>[];
  var isOrdered = options[keyOrdered] as bool? ?? true;
  final db = this.db;
  if (db.state != State.open) {
    throw MongoDartError('Db is in the wrong state: ${db.state}');
  }
  //final options = Map.from(this.options);

  // Todo implement topology
  // Did the user destroy the topology
  /*if (db?.serverConfig?.isDestroyed() ?? false) {
    return callback(MongoDartError('topology was destroyed'));
  }*/

  var commands = getBulkCommands();
  var origins = getBulkInputOrigins();
  var saveOptions = Map<String, Object>.from(options);

  var batchIndex = 0;
  for (var command in commands) {
    processOptions(command);
    command.addAll(options);

    if (readPreference != null) {
      // search for the right connection
    }

    var modernMessage = MongoModernMessage(command);

    var ret =
        await db.executeModernMessage(modernMessage, connection: connection);

    ret[keyCommandType] = command.keys.first;
    if (ret.containsKey(keyWriteErrors)) {
      var writeErrors = ret[keyWriteErrors] as List?;
      for (Map error in writeErrors ?? []) {
        var selectedKey = 0;
        var origin = origins[batchIndex];
        for (var key in origin.keys /* ?? <int>[] */) {
          if (key <= error[keyIndex] && key > selectedKey) {
            selectedKey = key;
          }
        }
        var opInputIndex = origins[batchIndex][selectedKey];
        error[keyOperationInputIndex] = opInputIndex;
      }
    }
    ret[keyBatchIndex] = batchIndex++;

    retList.add(ret);
    if (isOrdered) {
      if (ret[keyOk] == 0.0 ||
              ret.containsKey(
                  keyWriteErrors) /* ||
          ret.containsKey(keyWriteConcernError) */
          ) {
        return retList;
      }
    }

    options = Map<String, Object>.from(saveOptions);
  }
  return retList;
}