<commit_before>'use strict';
/**
 * The Pad Manager is a Factory for pad Objects
 */

/*
 * 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
 *
 * 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.
 */

const CustomError = require('../utils/customError');
const Pad = require('../db/Pad');
const db = require('./DB');
const hooks = require('../../static/js/pluginfw/hooks');

/**
 * A cache of all loaded Pads.
 *
 * Provides "get" and "set" functions,
 * which should be used instead of indexing with brackets. These prepend a
 * colon to the key, to avoid conflicting with built-in Object methods or with
 * these functions themselves.
 *
 * If this is needed in other places, it would be wise to make this a prototype
 * that's defined somewhere more sensible.
 */
const globalPads = {
  get(name) { return this[`:${name}`]; },
  set(name, value) {
    this[`:${name}`] = value;
  },
  remove(name) {
    delete this[`:${name}`];
  },
};

/**
 * A cache of the list of all pads.
 *
 * Updated without db access as new pads are created/old ones removed.
 */
const padList = {
  list: new Set(),
  cachedList: undefined,
  initiated: false,
  async init() {
    const dbData = await db.findKeys('pad:*', '*:*:*');

    if (dbData != null) {
      this.initiated = true;

      for (const val of dbData) {
        this.addPad(val.replace(/^pad:/, ''), false);
      }
    }

    return this;
  },
  async load() {
    if (!this.initiated) {
      return this.init();
    }

    return this;
  },
  /**
   * Returns all pads in alphabetical order as array.
   */
  async getPads() {
    await this.load();

    if (!this.cachedList) {
      this.cachedList = Array.from(this.list).sort();
    }

    return this.cachedList;
  },
  addPad(name) {
    if (!this.initiated) return;

    if (!this.list.has(name)) {
      this.list.add(name);
      this.cachedList = undefined;
    }
  },
  removePad(name) {
    if (!this.initiated) return;

    if (this.list.has(name)) {
      this.list.delete(name);
      this.cachedList = undefined;
    }
  },
};

// initialises the all-knowing data structure

/**
 * Returns a Pad Object with the callback
 * @param id A String with the id of the pad
 * @param {Function} callback
 */
exports.getPad = async (id, text) => {
  // check if this is a valid padId<commit_msg>PadManager: Refactor `padList` to avoid duplicate loads<commit_after>'use strict';
/**
 * The Pad Manager is a Factory for pad Objects
 */

/*
 * 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
 *
 * 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.
 */

const CustomError = require('../utils/customError');
const Pad = require('../db/Pad');
const db = require('./DB');
const hooks = require('../../static/js/pluginfw/hooks');

/**
 * A cache of all loaded Pads.
 *
 * Provides "get" and "set" functions,
 * which should be used instead of indexing with brackets. These prepend a
 * colon to the key, to avoid conflicting with built-in Object methods or with
 * these functions themselves.
 *
 * If this is needed in other places, it would be wise to make this a prototype
 * that's defined somewhere more sensible.
 */
const globalPads = {
  get(name) { return this[`:${name}`]; },
  set(name, value) {
    this[`:${name}`] = value;
  },
  remove(name) {
    delete this[`:${name}`];
  },
};

/**
 * A cache of the list of all pads.
 *
 * Updated without db access as new pads are created/old ones removed.
 */
const padList = new class {
  constructor() {
    this._cachedList = null;
    this._list = new Set();
    this._loaded = null;
  }

  /**
   * Returns all pads in alphabetical order as array.
   */
  async getPads() {
    if (!this._loaded) {
      this._loaded = (async () => {
        const dbData = await db.findKeys('pad:*', '*:*:*');
        if (dbData == null) return;
        for (const val of dbData) this.addPad(val.replace(/^pad:/, ''));
      })();
    }
    await this._loaded;
    if (!this._cachedList) this._cachedList = [...this._list].sort();
    return this._cachedList;
  }

  addPad(name) {
    if (this._list.has(name)) return;
    this._list.add(name);
    this._cachedList = null;
  }

  removePad(name) {
    if (!this._list.has(name)) return;
    this._list.delete(name);
    this._cachedList = null;
  }
}();

// initialises the all-knowing data structure

/**
 * Returns a Pad Object with the callback
 * @param id A String with the id of the pad
 * @param {Function} callback
 */
exports.getPad = async (id, text) => {
  // check if this is a valid padId
{'fraction_non_alphanumeric': 0.13428778542950345, 'fraction_numerical': 0.002899601304820587, 'mean_word_length': 2.981962481962482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '6783227', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1654, 'n_words': 608}