findConflictByUniqueIndex method

Future<Object?> findConflictByUniqueIndex(
  1. String indexName,
  2. Map<String, Object?> row
)

Look up the (single) row that conflicts with row on the UNIQUE secondary index named indexName. Returns the existing row's PK value, or null when there is no conflict (including when any indexed component of row is NULL).

Implementation

Future<Object?> findConflictByUniqueIndex(
    String indexName, Map<String, Object?> row) async {
  final si = _secondary[indexName];
  if (si == null || !si.unique) return null;
  final prefix = _encodeSecondaryPrefix(si, row);
  if (prefix == null) return null;
  final upper = _bumpPrefix(prefix);
  await for (final e
      in si.btree.range(lower: prefix, lowerInclusive: true, upper: upper)) {
    final bytes = await _heap.get(e.value);
    if (bytes == null) continue;
    final r = _decodeRow(bytes);
    return r[primaryKey.name];
  }
  return null;
}