fromJson static method

PqIndex fromJson(
  1. Map<String, Object?> j
)

Reconstruct a PqIndex from toJson output.

Implementation

static PqIndex fromJson(Map<String, Object?> j) {
  final dim = (j['dim'] as num).toInt();
  final m = (j['m'] as num).toInt();
  final idx = PqIndex(dim, m: m);
  final centroids = _decodeF32Slice(j['centroids'] as String);
  final expected = idx._pq.m * _ProductQuantizer.ksub * idx._pq.dsub;
  if (centroids.length != expected) {
    throw FormatException(
      'PqIndex.fromJson: centroids length ${centroids.length} != $expected',
    );
  }
  idx._pq.centroids = centroids;
  idx._pq.trained = (j['trained'] as bool?) ?? true;
  final ids = (j['ids'] as List).cast<Object?>();
  final codes = base64.decode(j['codes'] as String);
  if (codes.length != ids.length * m) {
    throw FormatException(
      'PqIndex.fromJson: codes length ${codes.length} '
      '!= ids ${ids.length} * m $m',
    );
  }
  idx._codes = Uint8List.fromList(codes);
  idx._ids.addAll(ids);
  return idx;
}