fromJson<DataType extends Object> static method

SetAssociativeCacheSidecarImage<DataType> fromJson<DataType extends Object>(
  1. Map<String, Object?> json,
  2. StateValueDecoder<DataType> decodeData
)

Decodes a set-associative cache image.

Implementation

static SetAssociativeCacheSidecarImage<DataType>
    fromJson<DataType extends Object>(
  Map<String, Object?> json,
  StateValueDecoder<DataType> decodeData,
) {
  _validateHeader(json, kind, version);
  final setCount = _positiveInt(json, 'setCount');
  final wayCount = _positiveInt(json, 'wayCount');
  final capacity = setCount * wayCount;
  final tags = List<int?>.filled(capacity, null);
  final payloads = List<DataType?>.filled(capacity, null);
  final allocated = <int>{};
  for (final encoded in _entryList(json)) {
    final entry = _entryMap(encoded);
    final slot = entry['slot'];
    final tag = entry['tag'];
    if (slot is! int || slot < 0 || slot >= capacity || tag is! int) {
      throw const FormatException('Cache entry has an invalid slot or tag.');
    }
    if (!allocated.add(slot)) {
      throw FormatException('Duplicate cache entry slot $slot.');
    }
    tags[slot] = tag;
    payloads[slot] = decodeData(entry['data']);
  }
  return SetAssociativeCacheSidecarImage(
    setCount: setCount,
    wayCount: wayCount,
    tags: tags,
    payloads: payloads,
    allocatedSlots: allocated,
  );
}