fromAnnotation static method

ModelHnswParams fromAnnotation(
  1. HnswIndex hnsw
)

Create with values from an HnswIndex annotation.

Implementation

static ModelHnswParams fromAnnotation(HnswIndex hnsw) {
  // Reject illegal configuration values during a generator run already,
  // otherwise would error at runtime (which might not be easily attributable,
  // see model.dart).
  // See allowed ranges in objectbox-c/src/model.cpp
  final dimensions = hnsw.dimensions;
  if (dimensions == null) {
    if (!(hnsw.flags?.lateInit ?? false)) {
      throw ArgumentError.value(
        dimensions,
        "dimensions",
        "is required, unless the index uses late init (HnswFlags.lateInit)",
      );
    }
  } else {
    _checkArgument(
      argument: dimensions,
      condition: dimensions > 0 && dimensions <= _maxUint32,
      name: "dimensions",
      message: "must be between 1 and $_maxUint32",
    );
  }
  final neighborsPerNode = hnsw.neighborsPerNode;
  if (neighborsPerNode != null) {
    _checkArgument(
      argument: neighborsPerNode,
      condition: neighborsPerNode > 0 && neighborsPerNode <= _maxUint32,
      name: "neighborsPerNode",
      message: "must be between 1 and $_maxUint32",
    );
  }
  final indexingSearchCount = hnsw.indexingSearchCount;
  if (indexingSearchCount != null) {
    _checkArgument(
      argument: indexingSearchCount,
      condition: indexingSearchCount > 0 && indexingSearchCount <= _maxUint32,
      name: "indexingSearchCount",
      message: "must be between 1 and $_maxUint32",
    );
  }
  final reparationBacklinkProbability = hnsw.reparationBacklinkProbability;
  if (reparationBacklinkProbability != null) {
    // The C API allows values bigger than 1.0, but internally everything
    // above 0.999 is just mapped to "always": so restrict to max 1.0.
    _checkArgument(
      argument: reparationBacklinkProbability,
      condition:
          reparationBacklinkProbability >= 0.0 &&
          reparationBacklinkProbability <= 1.0,
      name: "reparationBacklinkProbability",
      message: "must be between 0.0 or 1.0",
    );
  }
  final vectorCacheHintSizeKB = hnsw.vectorCacheHintSizeKB;
  if (vectorCacheHintSizeKB != null) {
    _checkArgument(
      argument: vectorCacheHintSizeKB,
      condition: vectorCacheHintSizeKB > 0,
      name: "vectorCacheHintSizeKB",
      message: "must be 1 or greater",
    );
  }
  final quantizationOversample = hnsw.quantizationOversample;
  if (quantizationOversample != null) {
    _checkArgument(
      argument: quantizationOversample,
      condition: quantizationOversample >= 1.0,
      name: "quantizationOversample",
      message: "must be 1.0 or greater",
    );
  }
  final quantizationRescoreFactor = hnsw.quantizationRescoreFactor;
  if (quantizationRescoreFactor != null) {
    _checkArgument(
      argument: quantizationRescoreFactor,
      condition: quantizationRescoreFactor >= 1.0,
      name: "quantizationRescoreFactor",
      message: "must be 1.0 or greater",
    );
  }
  final bruteForceMaxCandidates = hnsw.bruteForceMaxCandidates;
  if (bruteForceMaxCandidates != null) {
    _checkArgument(
      argument: bruteForceMaxCandidates,
      condition:
          bruteForceMaxCandidates > 0 &&
          bruteForceMaxCandidates <= _maxUint32,
      name: "bruteForceMaxCandidates",
      message: "must be between 1 and $_maxUint32",
    );
  }
  final filteredSearchMaxNodes = hnsw.filteredSearchMaxNodes;
  if (filteredSearchMaxNodes != null) {
    _checkArgument(
      argument: filteredSearchMaxNodes,
      condition:
          filteredSearchMaxNodes > 0 && filteredSearchMaxNodes <= _maxUint32,
      name: "filteredSearchMaxNodes",
      message: "must be between 1 and $_maxUint32",
    );
  }
  final randomSeed = hnsw.randomSeed;
  if (randomSeed != null) {
    _checkArgument(
      argument: randomSeed,
      condition: randomSeed >= 0,
      name: "randomSeed",
      message: "must be 0 or greater",
    );
  }
  return ModelHnswParams(
    dimensions: dimensions,
    neighborsPerNode: neighborsPerNode,
    indexingSearchCount: indexingSearchCount,
    flags: hnsw.flags?.toFlags(),
    distanceType: hnsw.distanceType?.toConstant(),
    reparationBacklinkProbability: reparationBacklinkProbability,
    vectorCacheHintSizeKB: vectorCacheHintSizeKB,
    quantization: hnsw.quantization?.toConstant(),
    quantizationOversample: quantizationOversample,
    quantizationRescoreFactor: quantizationRescoreFactor,
    searchLevel: hnsw.searchLevel?.toConstant(),
    buildLevel: hnsw.buildLevel?.toConstant(),
    bruteForceMaxCandidates: bruteForceMaxCandidates,
    filteredSearchMaxNodes: filteredSearchMaxNodes,
    randomSeed: randomSeed,
  );
}