<commit_before>/*
 * Copyright (C) 2013 salesforce.com, 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.
 */
/**
 * @description API to access action storage.
 *
 * This class is *the* API to access storable actions and action storage. All access to action storage
 * must use this API to ensure a consistent and reliable cache.
 *
 * By default a `filter` of storable action keys accessible to the current browser tab is built.
 * This is required because actions depend on defs, defs depend on labels, and the
 * label GVP is stored in the actions store. Labels and defs are loaded at framework boot so
 * the same must be done for actions. Failing to do this can result in storable action cache hits
 * that reference defs or labels the current tab does not have access to, which typically causes
 * a failure in the app. This commonly occurs in multi-tab browser scenarios because persistent
 * storage is shared across tabs but not synchronized into each tab's memory.
 *
 * Building the filter incurs a runtime cost at boot so is done only when persistent actions storage
 * is used. It may also selectively be disabled by the application (eg if the environment guarantees
 * a single tab).
 *
 * @constructor
 * @protected
 */
function ActionStorage() {
    this.actionKeysFilter = undefined;

    // allows the app to explicitly disable the filter
    this.actionsFilterEnabled = true;

    this.actionsFilterInited = false;
    this.actionsFilterPopulated = false;
}

ActionStorage.prototype.STORAGE_NAME = "actions";

ActionStorage.prototype.URI_DEFS_ENABLED_KEY = "_uri_defs_enabled";

/**
 * Enables or disables the persisted actions filter, required for multi-tab
 * environments that use storable actions.
 * @param {Boolean} enable - true to enable the filter, false to disable the filter.
 */
ActionStorage.prototype.enableActionsFilter = function(enable) {
    this.actionsFilterEnabled = !!enable;
};

/**
 * Initialize the persisted actions filter.
 * @return {Boolean} true if actions filter is set up
 */
ActionStorage.prototype.setupActionsFilter = function() {
    if (this.actionKeysFilter !== undefined) {
        return this.actionsFilterInited;
    }

    if (!this.actionsFilterEnabled) {
        this.actionKeysFilter = null;
        this.actionsFilterInited = false;
    } else {
        this.actionKeysFilter = {};
        this.actionsFilterInited = true;

        // Lazily populate actions filter after bootstrap.
        this.populateActionsFilter();
    }

    return this.actionsFilterInited;
};

/**
 * Populates the persisted actions filter if applicable.
 */
ActionStorage.prototype.populateActionsFilter = function() {
    if(this.actionsFilterPopulated) {
        return Promise["resolve"]();
    }
    this.actionsFilterPopulated = true;

    // if filter is not set up
    if (!this.isStoragePersistent() || !this.setupActionsFilter()) {
        return Promise["resolve"]();
    }

    var that = this;
    // if filter is enabled, getAll() populates all persisted actions to filter
    return this.getAll()["then"](function(items){

        var actionsStoredWithURIDefs = items[that.URI_DEFS_ENABLED_KEY];
        var uriEnabled = $A.getContext().uriAddressableDefsEnabled;
        
        // actions stored with uri defs enabled are backwards compatible with it disabled.
        // actions stored with uri defs disabled are not compatible with uri defs enabled.
        if ((uriEnabled !== !!actionsStoredWithURIDefs) &&
            (Object.keys(items).length > (actionsStoredWithURIDefs === undefined? 0: 1))) {

            $A.warning("Clearing actions db because uri addressable defs state was toggled");
            return that.clear()["then"](function(){
                return that.set(that.URI_DEFS_ENABLED_KEY, uriEnabled)["then"](function(){ return []; });
            });
        } else if (actionsStoredWithURIDefs === undefined){
            that.set(that.URI_DEFS_ENABLED_KEY, uriEnabled);
        }
        return items;
    });
};

/**
 * Reset the persisted actions filter.
 */
ActionStorage.prototype.clearActionsFilter = function() {
    this.actionKeysFilter = undefined;
    this.setupActionsFilter();
};

ActionStorage.prototype.isActionsFilterEnabled = function() {<commit_msg>when populating actions filter, include on the count for purging actions with uri defs enabled to include bootstrap entry (#3413)<commit_after>/*
 * Copyright (C) 2013 salesforce.com, 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.
 */
/**
 * @description API to access action storage.
 *
 * This class is *the* API to access storable actions and action storage. All access to action storage
 * must use this API to ensure a consistent and reliable cache.
 *
 * By default a `filter` of storable action keys accessible to the current browser tab is built.
 * This is required because actions depend on defs, defs depend on labels, and the
 * label GVP is stored in the actions store. Labels and defs are loaded at framework boot so
 * the same must be done for actions. Failing to do this can result in storable action cache hits
 * that reference defs or labels the current tab does not have access to, which typically causes
 * a failure in the app. This commonly occurs in multi-tab browser scenarios because persistent
 * storage is shared across tabs but not synchronized into each tab's memory.
 *
 * Building the filter incurs a runtime cost at boot so is done only when persistent actions storage
 * is used. It may also selectively be disabled by the application (eg if the environment guarantees
 * a single tab).
 *
 * @constructor
 * @protected
 */
function ActionStorage() {
    this.actionKeysFilter = undefined;

    // allows the app to explicitly disable the filter
    this.actionsFilterEnabled = true;

    this.actionsFilterInited = false;
    this.actionsFilterPopulated = false;
}

ActionStorage.prototype.STORAGE_NAME = "actions";

ActionStorage.prototype.URI_DEFS_ENABLED_KEY = "_uri_defs_enabled";

/**
 * Enables or disables the persisted actions filter, required for multi-tab
 * environments that use storable actions.
 * @param {Boolean} enable - true to enable the filter, false to disable the filter.
 */
ActionStorage.prototype.enableActionsFilter = function(enable) {
    this.actionsFilterEnabled = !!enable;
};

/**
 * Initialize the persisted actions filter.
 * @return {Boolean} true if actions filter is set up
 */
ActionStorage.prototype.setupActionsFilter = function() {
    if (this.actionKeysFilter !== undefined) {
        return this.actionsFilterInited;
    }

    if (!this.actionsFilterEnabled) {
        this.actionKeysFilter = null;
        this.actionsFilterInited = false;
    } else {
        this.actionKeysFilter = {};
        this.actionsFilterInited = true;

        // Lazily populate actions filter after bootstrap.
        this.populateActionsFilter();
    }

    return this.actionsFilterInited;
};

/**
 * Populates the persisted actions filter if applicable.
 */
ActionStorage.prototype.populateActionsFilter = function() {
    if(this.actionsFilterPopulated) {
        return Promise["resolve"]();
    }
    this.actionsFilterPopulated = true;

    // if filter is not set up
    if (!this.isStoragePersistent() || !this.setupActionsFilter()) {
        return Promise["resolve"]();
    }

    var that = this;
    // if filter is enabled, getAll() populates all persisted actions to filter
    return this.getAll()["then"](function(items){

        var actionsStoredWithURIDefs = items[that.URI_DEFS_ENABLED_KEY];
        var bootstrapEntry = items[AuraClientService.BOOTSTRAP_KEY];
        var uriEnabled = $A.getContext().uriAddressableDefsEnabled;
        var allowedStorage_size = actionsStoredWithURIDefs === undefined? 0: 1;
        allowedStorage_size += bootstrapEntry === undefined ? 0: 1;
        
        // actions stored with uri defs enabled are backwards compatible with it disabled.
        // actions stored with uri defs disabled are not compatible with uri defs enabled.
        if ((uriEnabled !== !!actionsStoredWithURIDefs) &&
            (Object.keys(items).length > allowedStorage_size) ) {

            $A.warning("Clearing actions db because uri addressable defs state was toggled");
            return that.clear()["then"](function(){
                return that.set(that.URI_DEFS_ENABLED_KEY, uriEnabled)["then"](function(){ return []; });
            });
        } else if (actionsStoredWithURIDefs === undefined){
            that.set(that.URI_DEFS_ENABLED_KEY, uriEnabled);
        }
        return items;
    });
};

/**
 * Reset the persisted actions filter.
 */
ActionStorage.prototype.clearActionsFilter = function() {
    this.actionKeysFilter = undefined;
    this.setupActionsFilter();
};

ActionStorage.prototype.isActionsFilterEnabled = function() {
{'fraction_non_alphanumeric': 0.08673106253177428, 'fraction_numerical': 0.0026436197254702592, 'mean_word_length': 3.4386281588447654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6645840', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2490, 'n_words': 976}