putMany method

List<int> putMany(
  1. List<T> objects, {
  2. PutMode mode = PutMode.put,
})

Like put, but optimized to put many objects at once.

All objects are put in a single transaction (similar to using Store.runInTransaction), making this faster than calling put multiple times.

Returns the IDs of all put objects.

Implementation

List<int> putMany(List<T> objects, {PutMode mode = PutMode.put}) {
  if (objects.isEmpty) return [];

  final putIds = List<int>.filled(objects.length, 0);

  InternalStoreAccess.runInTransaction(_store, TxMode.write,
      (Transaction tx) {
    if (_hasToOneRelations) {
      for (var object in objects) {
        _putToOneRelFields(object, mode, tx);
      }
    }

    final cursor = tx.cursor(_entity);
    final cMode = _getOBXPutMode(mode);
    for (var i = 0; i < objects.length; i++) {
      final object = objects[i];
      _builder.fbb.reset();
      final id = _entity.objectToFB(object, _builder.fbb);
      final newId = C.cursor_put_object4(
          cursor.ptr, _builder.bufPtr, _builder.fbb.size(), cMode);
      putIds[i] = _handlePutObjectResult(object, id, newId);
    }

    if (_hasToManyRelations) {
      for (var object in objects) {
        _putToManyRelFields(object, mode, tx);
      }
    }
    _builder.resetIfLarge();
  });

  return putIds;
}