<commit_before>// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';

import 'package:build/build.dart';
import 'package:glob/glob.dart';

import 'in_memory_writer.dart';

/// An implementation of [AssetReader] with primed in-memory assets.
class InMemoryAssetReader implements MultiPackageAssetReader {
  final Map<AssetId, DatedValue> assets;
  final String rootPackage;

  /// Create a new asset reader that contains [sourceAssets].
  ///
  /// May optionally define a [rootPackage], which is required for some APIs.
  InMemoryAssetReader({Map<AssetId, DatedValue> sourceAssets, this.rootPackage})
      : assets = sourceAssets ?? <AssetId, DatedValue>{};

  @override
  Future<bool> canRead(AssetId id) async => assets.containsKey(id);

  @override
  Future<List<int>> readAsBytes(AssetId id) async {
    if (!await canRead(id)) throw new AssetNotFoundException(id);
    return assets[id].bytesValue;
  }

  @override
  Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) async {
    if (!await canRead(id)) throw new AssetNotFoundException(id);
    return assets[id].stringValue;
  }

  @override
  Iterable<AssetId> findAssets(Glob glob, {String package}) {
    package ??= rootPackage;
    if (package == null) {
      throw new UnsupportedError(
          'Root package is required to use findAssets without providing an '
          'explicit package.');
    }
    return assets.keys
        .where((id) => id.package == package && glob.matches(id.path));
  }

  void cacheBytesAsset(AssetId id, List<int> bytes) {
    assets[id] = new DatedBytes(bytes);
  }
<commit_msg>Add recording of read assets to InMemoryAssetReader (#446)<commit_after>// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';

import 'package:build/build.dart';
import 'package:glob/glob.dart';

import 'in_memory_writer.dart';

/// An [AssetReader] that records which assets have been read to [assetsRead].
abstract class RecordingAssetReader implements AssetReader {
  Iterable<AssetId> get assetsRead;
}

/// An implementation of [AssetReader] with primed in-memory assets.
class InMemoryAssetReader
    implements MultiPackageAssetReader, RecordingAssetReader {
  final Map<AssetId, DatedValue> assets;
  final String rootPackage;
  final Set<AssetId> assetsRead;

  /// Create a new asset reader that contains [sourceAssets].
  ///
  /// May optionally define a [rootPackage], which is required for some APIs.
  InMemoryAssetReader({Map<AssetId, DatedValue> sourceAssets, this.rootPackage})
      : assets = sourceAssets ?? <AssetId, DatedValue>{},
        assetsRead = new Set<AssetId>();

  @override
  Future<bool> canRead(AssetId id) async {
    assetsRead.add(id);
    return assets.containsKey(id);
  }

  @override
  Future<List<int>> readAsBytes(AssetId id) async {
    if (!await canRead(id)) throw new AssetNotFoundException(id);
    assetsRead.add(id);
    return assets[id].bytesValue;
  }

  @override
  Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) async {
    if (!await canRead(id)) throw new AssetNotFoundException(id);
    assetsRead.add(id);
    return assets[id].stringValue;
  }

  @override
  Iterable<AssetId> findAssets(Glob glob, {String package}) {
    package ??= rootPackage;
    if (package == null) {
      throw new UnsupportedError(
          'Root package is required to use findAssets without providing an '
          'explicit package.');
    }
    return assets.keys
        .where((id) => id.package == package && glob.matches(id.path));
  }

  void cacheBytesAsset(AssetId id, List<int> bytes) {
    assets[id] = new DatedBytes(bytes);
  }

{'fraction_non_alphanumeric': 0.11072319201995012, 'fraction_numerical': 0.0032418952618453864, 'mean_word_length': 3.957972805933251, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280935', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1173, 'n_words': 373}