splitCommands method

List<Map<String, Object>> splitCommands(
  1. Map<String, Object> command
)

Split the command if the number of documents exceed the maxWriteBatchSixe

Here we assume that the command is made this way: {

Implementation

List<Map<String, Object>> splitCommands(Map<String, Object> command) {
  var ret = <Map<String, Object>>[];
  if (command.isEmpty) {
    return ret;
  }
  var maxWriteBatchSize = MongoModernMessage.maxWriteBatchSize;
  var documentsNum = (command.values.toList()[1] as List).length;
  if (documentsNum <= maxWriteBatchSize) {
    ret.add(command);
  } else {
    var documents = command.values.toList()[1] as List;
    var offset = 0;
    var endSubList = maxWriteBatchSize;
    var rest = documentsNum;
    Map<String, Object> splittedDocument;
    while (rest > 0) {
      splittedDocument = Map.from(command);
      splittedDocument[command.keys.last] =
          documents.sublist(offset, endSubList);
      ret.add(splittedDocument);
      rest = documentsNum - endSubList;
      offset = endSubList;
      endSubList += min(rest, maxWriteBatchSize);
    }
  }
  return ret;
}