<commit_before>/**
 * @license
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

goog.provide('shaka.offline.StorageCellPath');
goog.provide('shaka.offline.StorageMuxer');

goog.require('shaka.util.MapUtils');


shaka.offline.StorageCellPath = class {
  /**
   * @param {string} mechanism
   * @param {string} cell
   */
  constructor(mechanism, cell) {
    this.mechanism_ = mechanism;
    this.cell_ = cell;
  }

  /** @return {string} */
  cell() { return this.cell_; }

  /** @return {string} */
  mechanism() { return this.mechanism_; }
};


/**
 * StorageMuxer is responsible for managing StorageMechanisms and addressing
 * cells. The primary purpose of the muxer is to give the caller the correct
 * cell for the operations they want to perform.
 *
 * |findActive| will be used when the caller wants a cell that supports
 * add-operations. This will be used when saving new content to storage.
 *
 * |findAll| will be used when the caller want to look at all the content
 * in storage.
 *
 * |resolvePath| will be used to convert a path (from |findActive| and
 * |findAll|) into a cell, which it then returns.
 */
shaka.offline.StorageMuxer = class {
  constructor() {
    /**
     * A key in this map is the name given when registering a StorageMechanism.
     *
     * @private {!Object.<string, !shaka.extern.StorageMechanism>}
     */
    this.mechanisms_ = {};
  }

  /**
   * Free all resources used by the muxer, mechanisms, and cells. This should
   * not affect the stored content.
   *
   * @return {!Promise}
   */
  destroy() {
    /** @type {!Array.<!shaka.extern.StorageMechanism>} */
    let destroys = shaka.util.MapUtils
        .values(this.mechanisms_)
        .map((mechanism) => mechanism.destroy());

    // Empty each map so that subsequent calls will be no-ops.
    this.mechanisms_ = {};

    return Promise.all(destroys);
  }

  /**
   * Initialize the storage muxer. This must be called before any other calls.
   * This will initialize the muxer to use all mechanisms that have been
   * registered with |StorageMuxer.register|.
   *
   * @return {!Promise}
   */
  init() {
    const MapUtils = shaka.util.MapUtils;
    const StorageMuxer = shaka.offline.StorageMuxer;

    // Add the new instance of each mechanism to the muxer.
    MapUtils.forEach(StorageMuxer.registry_, (name, factory) => {
      this.mechanisms_[name] = factory();
    });

    let initPromises = MapUtils
        .values(this.mechanisms_)
        .map((mechanism) => mechanism.init());

    return Promise.all(initPromises);
  }

  /**
   * Get paths to all cells that support add-operations. All paths can be used
   * with |resolvePath|.
   *
   * @return {!Array.<!shaka.offline.StorageCellPath>}
   */
  findActive() {
    const MapUtils = shaka.util.MapUtils;

    /** @type {!Array.<!shaka.offline.StorageCellPath>} */
    let active = [];

    MapUtils.forEach(this.mechanisms_, (mechanismName, mechanism) => {
      MapUtils.forEach(mechanism.getCells(), (cellName, cell) => {
        if (!cell.hasFixedKeySpace()) {
          let path = new shaka.offline.StorageCellPath(mechanismName, cellName);
          active.push(path);
        }
      });
    });

    return active;
  }

  /**
   * Get paths to all cells. All paths can be used with |resolvePath|.
   *
   * @return {!Array.<!shaka.offline.StorageCellPath>}
   */
  findAll() {
    const MapUtils = shaka.util.MapUtils;

    /** @type {!Array.<!shaka.offline.StorageCellPath>} */
    let all = [];

    MapUtils.forEach(this.mechanisms_, (mechanismName, mechanism) => {
      MapUtils.forEach(mechanism.getCells(), (cellName, cell) => {
        let path = new shaka.offline.StorageCellPath(mechanismName, cellName);
        all.push(path);
      });
    });

    return all;
  }

  /**
   * Find the cell that the path points to. A path is made up of a mount point
   * and a cell id. If a cell can be found, the cell will be returned. If no
   * cell is found, null will be returned.
   *
   * @param {!shaka.offline.StorageCellPath} path
   * @return {shaka.extern.StorageCell}
   */
  resolvePath(path) {
    let mechanism = this.mechanisms_[path.mechanism()];

    if (!mechanism) { return null; }

    return mechanism.getCells()[path.cell()];
  }

  /**
   * This will erase all previous content from storage. Using paths obtained
   * before calling |erase| is discouraged, as cells may have changed during a
   * erase.<commit_msg>Made Storage Muxer implement IDestroyable<commit_after>/**
 * @license
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

goog.provide('shaka.offline.StorageCellPath');
goog.provide('shaka.offline.StorageMuxer');

goog.require('shaka.util.IDestroyable');
goog.require('shaka.util.MapUtils');


shaka.offline.StorageCellPath = class {
  /**
   * @param {string} mechanism
   * @param {string} cell
   */
  constructor(mechanism, cell) {
    this.mechanism_ = mechanism;
    this.cell_ = cell;
  }

  /** @return {string} */
  cell() { return this.cell_; }

  /** @return {string} */
  mechanism() { return this.mechanism_; }
};


/**
 * StorageMuxer is responsible for managing StorageMechanisms and addressing
 * cells. The primary purpose of the muxer is to give the caller the correct
 * cell for the operations they want to perform.
 *
 * |findActive| will be used when the caller wants a cell that supports
 * add-operations. This will be used when saving new content to storage.
 *
 * |findAll| will be used when the caller want to look at all the content
 * in storage.
 *
 * |resolvePath| will be used to convert a path (from |findActive| and
 * |findAll|) into a cell, which it then returns.
 *
 * @implements {shaka.util.IDestroyable}
 */
shaka.offline.StorageMuxer = class {
  constructor() {
    /**
     * A key in this map is the name given when registering a StorageMechanism.
     *
     * @private {!Object.<string, !shaka.extern.StorageMechanism>}
     */
    this.mechanisms_ = {};
  }

  /**
   * Free all resources used by the muxer, mechanisms, and cells. This should
   * not affect the stored content.
   *
   * @override
   */
  destroy() {
    /** @type {!Array.<!shaka.extern.StorageMechanism>} */
    let destroys = shaka.util.MapUtils
        .values(this.mechanisms_)
        .map((mechanism) => mechanism.destroy());

    // Empty each map so that subsequent calls will be no-ops.
    this.mechanisms_ = {};

    return Promise.all(destroys);
  }

  /**
   * Initialize the storage muxer. This must be called before any other calls.
   * This will initialize the muxer to use all mechanisms that have been
   * registered with |StorageMuxer.register|.
   *
   * @return {!Promise}
   */
  init() {
    const MapUtils = shaka.util.MapUtils;
    const StorageMuxer = shaka.offline.StorageMuxer;

    // Add the new instance of each mechanism to the muxer.
    MapUtils.forEach(StorageMuxer.registry_, (name, factory) => {
      this.mechanisms_[name] = factory();
    });

    let initPromises = MapUtils
        .values(this.mechanisms_)
        .map((mechanism) => mechanism.init());

    return Promise.all(initPromises);
  }

  /**
   * Get paths to all cells that support add-operations. All paths can be used
   * with |resolvePath|.
   *
   * @return {!Array.<!shaka.offline.StorageCellPath>}
   */
  findActive() {
    const MapUtils = shaka.util.MapUtils;

    /** @type {!Array.<!shaka.offline.StorageCellPath>} */
    let active = [];

    MapUtils.forEach(this.mechanisms_, (mechanismName, mechanism) => {
      MapUtils.forEach(mechanism.getCells(), (cellName, cell) => {
        if (!cell.hasFixedKeySpace()) {
          let path = new shaka.offline.StorageCellPath(mechanismName, cellName);
          active.push(path);
        }
      });
    });

    return active;
  }

  /**
   * Get paths to all cells. All paths can be used with |resolvePath|.
   *
   * @return {!Array.<!shaka.offline.StorageCellPath>}
   */
  findAll() {
    const MapUtils = shaka.util.MapUtils;

    /** @type {!Array.<!shaka.offline.StorageCellPath>} */
    let all = [];

    MapUtils.forEach(this.mechanisms_, (mechanismName, mechanism) => {
      MapUtils.forEach(mechanism.getCells(), (cellName, cell) => {
        let path = new shaka.offline.StorageCellPath(mechanismName, cellName);
        all.push(path);
      });
    });

    return all;
  }

  /**
   * Find the cell that the path points to. A path is made up of a mount point
   * and a cell id. If a cell can be found, the cell will be returned. If no
   * cell is found, null will be returned.
   *
   * @param {shaka.offline.StorageCellPath} path
   * @return {shaka.extern.StorageCell}
   */
  resolvePath(path) {
    let mechanism = this.mechanisms_[path.mechanism()];

    if (!mechanism) { return null; }

    return mechanism.getCells()[path.cell()];
  }

  /**
   * This will erase all previous content from storage. Using paths obtained
   * before calling |erase| is discouraged, as cells may have changed during a
   * erase.
{'fraction_non_alphanumeric': 0.12002394492666867, 'fraction_numerical': 0.0015963284445774718, 'mean_word_length': 2.9904458598726116, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3839055', 'n_tokens_mistral': 3133, 'n_tokens_neox': 3046, 'n_words': 957}