isReclaimableSuiteDatabase function

bool isReclaimableSuiteDatabase({
  1. required String database,
  2. required DateTime now,
  3. required File marker,
  4. Duration staleAfter = defaultStaleAfter,
  5. Duration markerStaleAfter = defaultMarkerStaleAfter,
})

Whether database is a suite database that can be taken back.

Three things have to hold. The name has to be one suiteDatabaseName produced — anything else belongs to whoever made it. The database has to be older than staleAfter, judged by the latest moment its name allows rather than the earliest, because the stamp is floored to the minute and being wrong the other way drops a database whose own suite has not connected yet. And marker must have stopped claiming it.

Age alone is not evidence and "nobody connected" alone is not either: a suite holding a lease may simply be between connections. The marker is what actually distinguishes a live suite, which is why it is the last word here.

Implementation

bool isReclaimableSuiteDatabase({
  required String database,
  required DateTime now,
  required File marker,
  Duration staleAfter = defaultStaleAfter,
  Duration markerStaleAfter = defaultMarkerStaleAfter,
}) {
  final createdAt = createdAtOf(database);
  if (createdAt == null) return false;

  final createdNoLaterThan = createdAt.add(const Duration(minutes: 1));
  if (!createdNoLaterThan.isBefore(now.toUtc().subtract(staleAfter))) {
    return false;
  }

  return !markerStillClaims(
    marker,
    now: now,
    markerStaleAfter: markerStaleAfter,
  );
}