<commit_before>(function (clique, $, _, Backbone, tangelo) {
    "use strict";

    tangelo.getPlugin("mongo").Mongo = function (cfg) {
        var findNodesService = "plugin/mongo/findNodes",
            mutators = {},
            mongoStore = {
                host: cfg.host || "localhost",
                db: cfg.database,
                coll: cfg.collection
            },
            addMutator,
            translateSpec;

        clique.util.require(cfg.database, "database");
        clique.util.require(cfg.collection, "collection");

        translateSpec = function (spec) {
            var result = {},
                value;

            if (_.has(spec, "queryOp")) {
                // If this is a "leaf node", translate it to a direct comparison
                // query and return.
                switch (spec.queryOp) {
                case "==": {
                    value = spec.value;
                    break;
                }

                case "!=":
                case ">":
                case ">=":
                case "<":
                case "<=":
                case "~~":
                case "|~":
                case "~|": {
                    throw new Error("unimplemented");
                    break;
                }

                default: {
                    throw new Error("illegal value for queryOp: " + spec.queryOp);
                    break;
                }
                }

                if (spec.field === "key") {
                    result["_id"] = {
                        $oid: value
                    };
                } else {
                    result["data." + spec.field] = value;
                }
            } else if (_.has(spec, "logicOp")) {
                // Otherwise, create a logic operator node and recurse down the
                // operands.
                result["$" + spec.logicOp] = [translateSpec(spec.left), translateSpec(spec.right)];
            }

            return result;
        };

        addMutator = function (mongoRec) {
            var target,
                mutator,
                key,
                type,
                source,
                targetNode;

            key = mongoRec._id.$oid;
            type = mongoRec.type;

            if (!_.has(mutators, key)) {
                target = {
                    key: key,
                    data: mongoRec.data || {}
                };

                if (type === "link") {
                    source = mongoRec.source;
                    if (_.isObject(source)) {
                        source = source.$oid;
                    }

                    target.source = source;

                    targetNode = mongoRec.target;
                    if (_.isObject(targetNode)) {
                        targetNode = targetNode.$oid;
                    }

                    target.target = targetNode;
                }

                mutator = new clique.util.Mutator(target);

                this.listenTo(mutator, "changed", function (mutator, prop, value) {
                    $.getJSON("plugin/mongo/update", _.extend({
                        key: mutator.key(),
                        prop: prop,
                        value: JSON.stringify(value)
                    }, mongoStore));
                });

                this.listenTo(mutator, "cleared", function (mutator, prop) {
                    $.getJSON("plugin/mongo/clear", _.extend({
                        key: mutator.key(),
                        prop: prop
                    }, mongoStore)).then(_.bind(function () {
                        this.trigger("cleared:" + mutator.key(), mutator, prop);
                    }, this));
                });

                mutators[key] = mutator;
            }

            return mutators[key];
        };

        return _.extend({
            findNodes: function (spec) {
                var targets,
                    data;

                data = _.extend({
                    spec: JSON.stringify(translateSpec(spec))
                }, mongoStore);

                return $.getJSON(findNodesService, data)
                    .then(_.partial(_.map, _, function (r) {
                        return _.bind(addMutator, this)(r);
                    }));
            },
<commit_msg>Fixing this-related error in mongo adapter library<commit_after>(function (clique, $, _, Backbone, tangelo) {
    "use strict";

    tangelo.getPlugin("mongo").Mongo = function (cfg) {
        var findNodesService = "plugin/mongo/findNodes",
            mutators = {},
            mongoStore = {
                host: cfg.host || "localhost",
                db: cfg.database,
                coll: cfg.collection
            },
            addMutator,
            translateSpec;

        clique.util.require(cfg.database, "database");
        clique.util.require(cfg.collection, "collection");

        translateSpec = function (spec) {
            var result = {},
                value;

            if (_.has(spec, "queryOp")) {
                // If this is a "leaf node", translate it to a direct comparison
                // query and return.
                switch (spec.queryOp) {
                case "==": {
                    value = spec.value;
                    break;
                }

                case "!=":
                case ">":
                case ">=":
                case "<":
                case "<=":
                case "~~":
                case "|~":
                case "~|": {
                    throw new Error("unimplemented");
                    break;
                }

                default: {
                    throw new Error("illegal value for queryOp: " + spec.queryOp);
                    break;
                }
                }

                if (spec.field === "key") {
                    result["_id"] = {
                        $oid: value
                    };
                } else {
                    result["data." + spec.field] = value;
                }
            } else if (_.has(spec, "logicOp")) {
                // Otherwise, create a logic operator node and recurse down the
                // operands.
                result["$" + spec.logicOp] = [translateSpec(spec.left), translateSpec(spec.right)];
            }

            return result;
        };

        addMutator = function (mongoRec) {
            var target,
                mutator,
                key,
                type,
                source,
                targetNode;

            key = mongoRec._id.$oid;
            type = mongoRec.type;

            if (!_.has(mutators, key)) {
                target = {
                    key: key,
                    data: mongoRec.data || {}
                };

                if (type === "link") {
                    source = mongoRec.source;
                    if (_.isObject(source)) {
                        source = source.$oid;
                    }

                    target.source = source;

                    targetNode = mongoRec.target;
                    if (_.isObject(targetNode)) {
                        targetNode = targetNode.$oid;
                    }

                    target.target = targetNode;
                }

                mutator = new clique.util.Mutator(target);

                this.listenTo(mutator, "changed", function (mutator, prop, value) {
                    $.getJSON("plugin/mongo/update", _.extend({
                        key: mutator.key(),
                        prop: prop,
                        value: JSON.stringify(value)
                    }, mongoStore));
                });

                this.listenTo(mutator, "cleared", function (mutator, prop) {
                    $.getJSON("plugin/mongo/clear", _.extend({
                        key: mutator.key(),
                        prop: prop
                    }, mongoStore)).then(_.bind(function () {
                        this.trigger("cleared:" + mutator.key(), mutator, prop);
                    }, this));
                });

                mutators[key] = mutator;
            }

            return mutators[key];
        };

        return _.extend({
            findNodes: function (spec) {
                var targets,
                    data;

                data = _.extend({
                    spec: JSON.stringify(translateSpec(spec))
                }, mongoStore);

                return $.getJSON(findNodesService, data)
                    .then(_.partial(_.map, _, _.bind(function (r) {
                        return _.bind(addMutator, this)(r);
                    }, this)));
            },

{'fraction_non_alphanumeric': 0.11310965063991699, 'fraction_numerical': 0.0, 'mean_word_length': 0.9378909740840036, 'pattern_counts': {'":': 18, '<': 7, '<?xml version=': 0, '>': 7, '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': '6314392', 'n_tokens_mistral': 2202, 'n_tokens_neox': 1974, 'n_words': 429}