bulkSet method

Future<void> bulkSet(
  1. Map<String, Map<String, dynamic>> docs, {
  2. int chunkSize = 500,
})

Implementation

Future<void> bulkSet(
  Map<String, Map<String, dynamic>> docs, {
  int chunkSize = 500,
}) async {
  if (_indexedFields.isNotEmpty) {
    for (final entry in docs.entries) {
      await _put(entry.key, entry.value);
    }
    return;
  }

  final entries = <MapEntry<String, Uint8List>>[];
  for (final entry in docs.entries) {
    final blockId = '$_name/${entry.key}';
    _pendingTags.remove(blockId);
    _invalidateQueryCacheFor(blockId);
    Uint8List bytes = _serializer.serialize(entry.value);
    final crypto = _crypto;
    if (crypto != null && _secretKey != null) {
      bytes = await crypto.encrypt(bytes, _secretKey);
      final marked = Uint8List(bytes.length + 1);
      marked[0] = 0xAE;
      marked.setRange(1, marked.length, bytes);
      bytes = marked;
    }
    if (_local) {
      final tainted = Uint8List(bytes.length + 1);
      tainted[0] = 0xAA;
      tainted.setRange(1, tainted.length, bytes);
      bytes = tainted;
    }
    entries.add(MapEntry(blockId, bytes));
  }

  for (final e in entries) {
    await _cachePut(e.key, e.value);
  }

  if (_local) return;

  if (!_ws.isConnected && _smartCache != null) {
    for (final e in entries) {
      await _smartCache.enqueuePut(e.key, e.value);
    }
    return;
  }

  try {
    for (var i = 0; i < entries.length; i += chunkSize) {
      final end = math.min(i + chunkSize, entries.length);
      await _ws.bulkPut(entries.sublist(i, end));
    }
  } catch (e) {
    if (e is WsDeniedException) rethrow;
    if (_smartCache != null) {
      for (final entry in entries) {
        await _smartCache.enqueuePut(entry.key, entry.value);
      }
    }
  }
}