<commit_before>///<reference path='refs.ts'/>


// openDatabase("TDev", "1.0", "TouchDevelop database", 5 * 1024 * 1024).transaction(function (tx) { tx.executeSql("DELETE FROM TABLESessions;"); });

module TDev {
    export module Storage {
        function fatalError(e: any, op: string = undefined) {
            Util.navigateInWindow((<any>window).errorUrl + "#storageinit," + encodeURIComponent(op));
        }
        function versionError(e: any) {
            Util.navigateInWindow((<any>window).errorUrl + "#storageversion");
        }
        function tagError(e: any, origin: string, willRetry: boolean = false) {
            e.isDatabaseError = true;
            e.databaseOrigin = origin;
            Util.log("database error" + (willRetry ? " (will retry)" : " (no retry)") + ": " + Util.getErrorInfo(e));
            return e;
        }
        export interface Table {
            getValueAsync(key: string): Promise; // of string
            getItemsAsync(keys: string[]): Promise; // of Object
            getKeysAsync(): Promise; // of string[]
            setItemsAsync(items: any): Promise; // of void
        }

        // indicates if the database initialization failed and the changes are only stored in memory
        // this mode should only be allowed if no database was ever opened in this browser
        export var temporary = false;
        var temporaryRequestedSignin = false;
        export function showTemporaryWarning() {
            if (temporary) {
                if (Cloud.isAccessTokenExpired())
                    HTML.showWarningNotification(lf("Web site data not available, sign in to back up your scripts!"), lf("Your browser does not allow TouchDevelop to store data. This usually happens if run in Private Mode (Safari), in InPrivate mode (Internet Explorer) or your security settings prevent data storage. Sign in or change your browser settings to avoid loosing your work. All the changes not saved to the cloud will be lost when leaving this page."));
                else if (Cloud.isOffline())
                    HTML.showWarningNotification(lf("Web site data not available, connect to internet to back up your scripts!"), lf("Your browser does not allow TouchDevelop to store data. This usually happens if run in Private Mode (Safari), in InPrivate mode (Internet Explorer) or your security settings prevent data storage. Sign in or change your browser settings to avoid loosing your work. All the changes not saved to the cloud will be lost when leaving this page."));
                return true;
            } else {
                return false;
            }
        }

        // uses an in-memory object or localstorage
        var memoryStorage: any = {};
        class MemoryTable implements Table {
            public getValueAsync(key: string): Promise {
                var v = memoryStorage[this.tableName + "-" + key];
                if (typeof v !== "string") v = undefined;
                return Promise.as(v);
            }
            public getItemsAsync(keys: string[]): Promise {
                var items = {};
                keys.forEach((k) => {
                    var v = memoryStorage[this.tableName + "-" + k];
                    if (typeof v !== "string") v = undefined;
                    items[k] = v;
                });
                return Promise.as(items);
            }
            public getKeysAsync(): Promise {
                var prefix = this.tableName + "-";
                var results = []
                Object.keys(memoryStorage).forEach(k => {
                    if (k.length > prefix.length && k.slice(0, prefix.length) === prefix) results.push(k.slice(prefix.length));
                });
                return Promise.as(results);
            }
            public setItemsAsync(items: any): Promise {
                Object.keys(items).forEach((k) => {
                    var v = items[k];
                    if (typeof v === "string")
                        memoryStorage[this.tableName + "-" + k] = v;
                    else
                        memoryStorage.removeItem(this.tableName + "-" + k);
                });
                return Promise.as();
            }
            constructor(public tableName: string) {
                Storage.temporary = true;
            }
        }

        export function createMemoryTable(tableName: string): Table {
            return new MemoryTable(tableName);
        }

        /*
                class StorageTable implements Table {
                    static localStorage = window.localStorage;
                    public getValueAsync(key: string): Promise {
                        var v = localStorage[this.tableName + "-" + key];
                        if (typeof v !== "string") v = undefined;
                        return Promise.as(v);
                    }
                    public getItemsAsync(keys: string[]): Promise {
                        var items = {};
                        keys.forEach((k) => {
                            var v = localStorage[this.tableName + "-" + k];
                            if (typeof v !== "string") v = undefined;<commit_msg>fixed cloud crash<commit_after>///<reference path='refs.ts'/>


// openDatabase("TDev", "1.0", "TouchDevelop database", 5 * 1024 * 1024).transaction(function (tx) { tx.executeSql("DELETE FROM TABLESessions;"); });

module TDev {
    export module Storage {
        function fatalError(e: any, op: string = undefined) {
            Util.navigateInWindow((<any>window).errorUrl + "#storageinit," + encodeURIComponent(op));
        }
        function versionError(e: any) {
            Util.navigateInWindow((<any>window).errorUrl + "#storageversion");
        }
        function tagError(e: any, origin: string, willRetry: boolean = false) {
            e.isDatabaseError = true;
            e.databaseOrigin = origin;
            Util.log("database error" + (willRetry ? " (will retry)" : " (no retry)") + ": " + Util.getErrorInfo(e));
            return e;
        }
        export interface Table {
            getValueAsync(key: string): Promise; // of string
            getItemsAsync(keys: string[]): Promise; // of Object
            getKeysAsync(): Promise; // of string[]
            setItemsAsync(items: any): Promise; // of void
        }

        // indicates if the database initialization failed and the changes are only stored in memory
        // this mode should only be allowed if no database was ever opened in this browser
        export var temporary = false;
        var temporaryRequestedSignin = false;
        export function showTemporaryWarning() {
            if (temporary) {
                if (Cloud.isAccessTokenExpired())
                    HTML.showWarningNotification(lf("Web site data not available, sign in to back up your scripts!"), lf("Your browser does not allow TouchDevelop to store data. This usually happens if run in Private Mode (Safari), in InPrivate mode (Internet Explorer) or your security settings prevent data storage. Sign in or change your browser settings to avoid loosing your work. All the changes not saved to the cloud will be lost when leaving this page."));
                else if (Cloud.isOffline())
                    HTML.showWarningNotification(lf("Web site data not available, connect to internet to back up your scripts!"), lf("Your browser does not allow TouchDevelop to store data. This usually happens if run in Private Mode (Safari), in InPrivate mode (Internet Explorer) or your security settings prevent data storage. Sign in or change your browser settings to avoid loosing your work. All the changes not saved to the cloud will be lost when leaving this page."));
                return true;
            } else {
                return false;
            }
        }

        // uses an in-memory object or localstorage
        var memoryStorage: StringMap<string> = {};
        class MemoryTable implements Table {
            public getValueAsync(key: string): Promise {
                var v = memoryStorage[this.tableName + "-" + key];
                if (typeof v !== "string") v = undefined;
                return Promise.as(v);
            }
            public getItemsAsync(keys: string[]): Promise {
                var items = {};
                keys.forEach((k) => {
                    var v = memoryStorage[this.tableName + "-" + k];
                    if (typeof v !== "string") v = undefined;
                    items[k] = v;
                });
                return Promise.as(items);
            }
            public getKeysAsync(): Promise {
                var prefix = this.tableName + "-";
                var results = []
                Object.keys(memoryStorage).forEach(k => {
                    if (k.length > prefix.length && k.slice(0, prefix.length) === prefix) results.push(k.slice(prefix.length));
                });
                return Promise.as(results);
            }
            public setItemsAsync(items: any): Promise { // of string
                Object.keys(items).forEach((k) => {
                    var v = items[k];
                    if (typeof v === "string")
                        memoryStorage[this.tableName + "-" + k] = v;
                    else
                        delete memoryStorage[this.tableName + "-" + k];
                });
                return Promise.as();
            }
            constructor(public tableName: string) {
                Storage.temporary = true;
            }
        }

        export function createMemoryTable(tableName: string): Table {
            return new MemoryTable(tableName);
        }

        /*
                class StorageTable implements Table {
                    static localStorage = window.localStorage;
                    public getValueAsync(key: string): Promise {
                        var v = localStorage[this.tableName + "-" + key];
                        if (typeof v !== "string") v = undefined;
                        return Promise.as(v);
                    }
                    public getItemsAsync(keys: string[]): Promise {
                        var items = {};
                        keys.forEach((k) => {
                            var v = localStorage[this.tableName + "-" + k];
                            if (typeof v !== "string") v = undefined;
{'fraction_non_alphanumeric': 0.10244751862242431, 'fraction_numerical': 0.0023217567959756217, 'mean_word_length': 1.7148109243697478, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '599718', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2386, 'n_words': 818}