pruneCache method

  1. @override
void pruneCache(
  1. Path root,
  2. PruneForest pruneForest
)
override

Deletes the cached data below root that pruneForest prunes and does not keep.

Data stored above root, and data below it that pruneForest neither prunes nor keeps, is left alone with a warning through the engine's DatabaseLogger; it is never an error. Android: SqlPersistenceStorageEngine.java:657-681.

Implementation

@override
void pruneCache(Path root, PruneForest pruneForest) {
  _verifyInsideTransaction();
  for (final MapEntry<Path, Node> write in _serverCache.entries.toList()) {
    final Path absoluteDataPath = write.key;
    if (root != absoluteDataPath && absoluteDataPath.contains(root)) {
      logger?.call(
        DatabaseLogLevel.warning,
        'We are pruning at $root but we have data stored higher up at '
        '$absoluteDataPath. Ignoring.',
      );
      continue;
    }
    if (!root.contains(absoluteDataPath)) {
      continue;
    }
    final Path dataPath = Path.relative(root, absoluteDataPath);
    final Node dataNode = write.value;
    if (pruneForest.shouldPruneUnkeptDescendants(dataPath)) {
      final CompoundWrite kept = pruneForest
          .childAtPath(dataPath)
          .foldKeptNodes<CompoundWrite>(
            CompoundWrite.empty,
            (Path keepPath, CompoundWrite accumulator) =>
                accumulator.addWrite(keepPath, dataNode.getChild(keepPath)),
          );
      _serverCache = _serverCache
          .removeWrite(absoluteDataPath)
          .addWrites(absoluteDataPath, kept);
    } else if (!pruneForest.shouldKeep(dataPath)) {
      // Android: SqlPersistenceStorageEngine.java:672-680.
      logger?.call(
        DatabaseLogLevel.warning,
        'We are pruning at $root and have data at $absoluteDataPath that '
        "isn't marked for pruning or keeping. Ignoring.",
      );
    }
  }
}