<commit_before>var assert = require('assert');
var stringify = require('json-stable-stringify');

var CHECK_STORED_DATA = true;

var cassandraTypes = require('cassandra-driver').types;
var Joi = require('joi');
var Q = require("q");

var IoC = require('electrolyte');
var log = IoC.create('logger').createLogger("Model");

var Model = function (parameters) {
    this._name = parameters.name;
    this._schema = parameters.schema;
    this._joiSchema = parameters.joiSchema;
    this._queryBuilder = parameters.queryBuilder;
    this._preWriteTransform = parameters.preWriteTransform;
    //this._postReadTransform = parameters.postReadTransform;
    log.info("MODEL READY!");
};

Model.prototype.getName = function () {
    return this._name;
};

Model.prototype.save = function (o) {
    if (this._preWriteTransform) {
        o = this._preWriteTransform(o);
    }
    o = tranformCommon(o);
    this.validate(o);
    o = addUnderscore(o);

    var cql = this._queryBuilder().insert(o).into(this._name);
    var res = this.exec(cql).then(function (r) {
        o = removeUnderscore(o);
        r.insertedObject = o;
        return r;
    });

    if (CHECK_STORED_DATA) {
        var self = this;
        var queryResponse;
        return res
            .then(function (response) {
                queryResponse = response;
                return self.find({timeuuid: o.timeuuid});
            })
            .then(function (v) {
                assert.equal(stringify(v[0]), stringify(o));
                return queryResponse;
            })
            .catch(function (e) {
                if (e instanceof assert.AssertionError) {
                    log.info(stringify(e.actual));
                    log.info(stringify(e.expected));
                }
                throw e;
            });
    }

    return res;
};

Model.prototype.find = function (params) {
    var cql = this._queryBuilder().select('*').from(this._name);
    var i = 0;
    Object.keys(params).forEach(function (k) {
        if (i == 0) {
            cql.where(k, "=", params[k]);
        } else {
            cql.andWhere(k, "=", params[k]);
        }
        i++;
    });
    return this.exec(cql)
        .then(function (res) {
            return res.rows
                .map(mapToJs)
                .map(removeUnderscore)
                .map(tranformCommon);
        });
};

var mapToJs = function (obj) {
    if (obj instanceof cassandraTypes.Long) {
        return parseInt(obj.toString());
    } else if (obj instanceof cassandraTypes.Uuid) {
        return obj.toString();
    } else if (obj instanceof Date) {
        return obj.getTime();
    } else if (obj instanceof Buffer) {
        return obj;
    } else if (Array.isArray(obj)) {
        return obj.map(function (v) {
            return mapToJs(v);
        });
    } else if (obj === null) {
        return obj;
    } else if (typeof obj === "object") {
        var res = {};
        Object.keys(obj).forEach(function (k) {
            res[k] = mapToJs(obj[k]);
        });
        return res;
    } else {
        return obj;
    }
};

Model.prototype.validate = function (obj) {
    Joi.assert(obj, this._joiSchema);
};

Model.prototype.exec = function (cql) {
    return Q.nbind(cql.exec, cql, {prepare: true})();
};

function tranformCommon(obj) {
    if (obj == null) {
        return null;
    }

    if (typeof obj !== "object") {
        return obj;
    }

    if (obj instanceof Buffer) {
        return obj;
    }

    return Object.keys(obj).reduce(function (acc, k) {<commit_msg>empty strings bugfix<commit_after>var assert = require('assert');
var stringify = require('json-stable-stringify');

var CHECK_STORED_DATA = true;

var cassandraTypes = require('cassandra-driver').types;
var Joi = require('joi');
var Q = require("q");

var IoC = require('electrolyte');
var log = IoC.create('logger').createLogger("Model");

var Model = function (parameters) {
    this._name = parameters.name;
    this._schema = parameters.schema;
    this._joiSchema = parameters.joiSchema;
    this._queryBuilder = parameters.queryBuilder;
    this._preWriteTransform = parameters.preWriteTransform;
    //this._postReadTransform = parameters.postReadTransform;
    log.info("MODEL READY!");
};

Model.prototype.getName = function () {
    return this._name;
};

Model.prototype.save = function (o) {
    if (this._preWriteTransform) {
        o = this._preWriteTransform(o);
    }
    o = tranformCommon(o);
    this.validate(o);
    o = addUnderscore(o);

    var cql = this._queryBuilder().insert(o).into(this._name);
    var res = this.exec(cql).then(function (r) {
        o = removeUnderscore(o);
        r.insertedObject = o;
        return r;
    });

    if (CHECK_STORED_DATA) {
        var self = this;
        var queryResponse;
        return res
            .then(function (response) {
                queryResponse = response;
                return self.find({timeuuid: o.timeuuid});
            })
            .then(function (v) {
                assert.equal(stringify(v[0]), stringify(o));
                return queryResponse;
            })
            .catch(function (e) {
                if (e instanceof assert.AssertionError) {
                    log.info(stringify(e.actual));
                    log.info(stringify(e.expected));
                }
                throw e;
            });
    }

    return res;
};

Model.prototype.find = function (params) {
    var cql = this._queryBuilder().select('*').from(this._name);
    var i = 0;
    Object.keys(params).forEach(function (k) {
        if (i == 0) {
            cql.where(k, "=", params[k]);
        } else {
            cql.andWhere(k, "=", params[k]);
        }
        i++;
    });
    return this.exec(cql)
        .then(function (res) {
            return res.rows
                .map(mapToJs)
                .map(removeUnderscore)
                .map(tranformCommon);
        });
};

var mapToJs = function (obj) {
    if (obj instanceof cassandraTypes.Long) {
        return parseInt(obj.toString());
    } else if (obj instanceof cassandraTypes.Uuid) {
        return obj.toString();
    } else if (obj instanceof Date) {
        return obj.getTime();
    } else if (obj instanceof Buffer) {
        return obj;
    } else if (Array.isArray(obj)) {
        return obj.map(function (v) {
            return mapToJs(v);
        });
    } else if (obj === null) {
        return obj;
    } else if (typeof obj === "object") {
        var res = {};
        Object.keys(obj).forEach(function (k) {
            res[k] = mapToJs(obj[k]);
        });
        return res;
    } else {
        return obj;
    }
};

Model.prototype.validate = function (obj) {
    Joi.assert(obj, this._joiSchema);
};

Model.prototype.exec = function (cql) {
    return Q.nbind(cql.exec, cql, {prepare: true})();
};

function tranformCommon(obj) {
    if (obj == null) {
        return null;
    }

    if (obj == "") {
        return null;
    }

    if (typeof obj !== "object") {
        return obj;
    }

    if (obj instanceof Buffer) {
        return obj;
    }

    return Object.keys(obj).reduce(function (acc, k) {
{'fraction_non_alphanumeric': 0.14131652661064426, 'fraction_numerical': 0.0008403361344537816, 'mean_word_length': 2.041311754684838, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6905639', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2157, 'n_words': 468}