<commit_before>"use strict";

const events = require("events");
const Model = require("../inc/Model");
const superagent = require("superagent");

let cache = new Map();

var refToPath= function(ref) {
    // Todo support when ref is a Model and not a string
    return ref.replace("#", "/");
};

// Use a callback, to call again the caller setState() when the model has been updated

class Storage extends events.EventEmitter {

    get(ref) {
        if (ref instanceof Model) {
            return Promise.resolve(ref);
        } else if (cache.has(ref)) {
            return Promise.resolve(cache.get(ref));
        } else {
            return new Promise((resolve, reject) => {
                superagent.get(`/api/${refToPath(ref)}`).end((err, res) => {
                    if (err) reject(err);
                    resolve(Model.decode(res.body));
                });
            }).then((model) => {
                cache.set(model.ref, model);
                return model;
            });
        }
    }

    add(model) {
        return new Promise((resolve, reject) => {
            superagent.post(`/api/${model.constructor.name}`)
                .set("Content-Type", "application/json")
                .send(model.encode())
                .end((err, res) => {
                    if (err) reject(err);
                    let newModel = Model.decode(res.body);
                    cache.set(newModel.ref, newModel);
                    resolve(newModel);
                });
        });
    }

    addParent(ref, refToParent) {
        return new Promise((resolve, reject) => {
            superagent.post(`/api/${refToPath(ref)}/parent/${refToPath(refToParent)}`)
                .end((err, res) => {
                    if (err) reject(err);
                    let newModel = Model.decode(res.body);
                    cache.set(newModel.ref, newModel);
                    resolve(newModel);
                });
        });
    }

    addDerivator(ref, refToDerivator) {
        return new Promise((resolve, reject) => {
            superagent.post(`/api/${refToPath(ref)}/derivator/${refToPath(refToDerivator)}`)
                .end((err, res) => {
                    if (err) reject(err);
                    let newModel = Model.decode(res.body);
                    cache.set(newModel.ref, newModel);
                    resolve(newModel);
                });
        });
    }

    delete(ref) {
        return new Promise((resolve, reject) => {
            superagent.del(`/api/${refToPath(ref)}`).end((err, res) => {
                if (err) reject(err);
                resolve(res.body);
            });
        });
    }

    search(query, sort, offset, size) {
        query = encodeURIComponent(query);
        sort = encodeURIComponent(sort || "");
        offset = parseInt(offset || 0, 10);
        size = parseInt(size || 50, 10);

        return new Promise((resolve, reject) => {
            superagent.get(`/api/search/${query}/${sort}/${offset}/${size}`).end((err, res) => {
                if (err) reject(err);
                resolve(res.body);
            });

        }).then((response) => {
            if (response.prefetch) {
                response.prefetch.forEach((data) => {
                    let model = Model.decode(data);<commit_msg>Remove the deleted model from the cache<commit_after>"use strict";

const events = require("events");
const Model = require("../inc/Model");
const superagent = require("superagent");

let cache = new Map();

var refToPath= function(ref) {
    // Todo support when ref is a Model and not a string
    return ref.replace("#", "/");
};

// Use a callback, to call again the caller setState() when the model has been updated

class Storage extends events.EventEmitter {

    get(ref) {
        if (ref instanceof Model) {
            return Promise.resolve(ref);
        } else if (cache.has(ref)) {
            return Promise.resolve(cache.get(ref));
        } else {
            return new Promise((resolve, reject) => {
                superagent.get(`/api/${refToPath(ref)}`).end((err, res) => {
                    if (err) reject(err);
                    resolve(Model.decode(res.body));
                });
            }).then((model) => {
                cache.set(model.ref, model);
                return model;
            });
        }
    }

    add(model) {
        return new Promise((resolve, reject) => {
            superagent.post(`/api/${model.constructor.name}`)
                .set("Content-Type", "application/json")
                .send(model.encode())
                .end((err, res) => {
                    if (err) reject(err);
                    let newModel = Model.decode(res.body);
                    cache.set(newModel.ref, newModel);
                    resolve(newModel);
                });
        });
    }

    addParent(ref, refToParent) {
        return new Promise((resolve, reject) => {
            superagent.post(`/api/${refToPath(ref)}/parent/${refToPath(refToParent)}`)
                .end((err, res) => {
                    if (err) reject(err);
                    let newModel = Model.decode(res.body);
                    cache.set(newModel.ref, newModel);
                    resolve(newModel);
                });
        });
    }

    addDerivator(ref, refToDerivator) {
        return new Promise((resolve, reject) => {
            superagent.post(`/api/${refToPath(ref)}/derivator/${refToPath(refToDerivator)}`)
                .end((err, res) => {
                    if (err) reject(err);
                    let newModel = Model.decode(res.body);
                    cache.set(newModel.ref, newModel);
                    resolve(newModel);
                });
        });
    }

    delete(ref) {
        return new Promise((resolve, reject) => {
            superagent.del(`/api/${refToPath(ref)}`).end((err, res) => {
                if (err) reject(err);
                cache.delete(ref);
                resolve(res.body);
            });
        });
    }

    search(query, sort, offset, size) {
        query = encodeURIComponent(query);
        sort = encodeURIComponent(sort || "");
        offset = parseInt(offset || 0, 10);
        size = parseInt(size || 50, 10);

        return new Promise((resolve, reject) => {
            superagent.get(`/api/search/${query}/${sort}/${offset}/${size}`).end((err, res) => {
                if (err) reject(err);
                resolve(res.body);
            });

        }).then((response) => {
            if (response.prefetch) {
                response.prefetch.forEach((data) => {
                    let model = Model.decode(data);
{'fraction_non_alphanumeric': 0.1522491349480969, 'fraction_numerical': 0.0021062133293214985, 'mean_word_length': 1.5857642940490082, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '7099520', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1800, 'n_words': 362}