<commit_before>var Keys = require('./Keys');
var parseTree = require('./parse-tree');
var matcher = require('./operations/matcher');
var normalizePathSets = require('./operations/ranges/normalizePathSets');
var recurseMatchAndExecute = require('./run/recurseMatchAndExecute');
var optimizePathSets = require('./cache/optimizePathSets');
var pathValueMerge = require('./cache/pathValueMerge');
var runGetAction = require('./run/get/runGetAction');
var runSetAction = require('./run/set/runSetAction');
var runCallAction = require('./run/call/runCallAction');
var $atom = require('./support/types').$atom;
var get = 'get';
var set = 'set';
var call = 'call';
var pathUtils = require('falcor-path-utils');
var collapse = pathUtils.collapse;
var JSONGraphError = require('./JSONGraphError');
var MAX_REF_FOLLOW = 50;

var Router = function(routes, options) {
    var opts = options || {};

    this._routes = routes;
    this._rst = parseTree(routes);
    this._matcher = matcher(this._rst);
    this._debug = opts.debug;
    this.maxRefFollow = opts.maxRefFollow || MAX_REF_FOLLOW;
};

Router.createClass = function(routes) {
  function C(options) {
    var opts = options || {};
    this._debug = opts.debug;
  }

  C.prototype = new Router(routes);
  C.prototype.constructor = C;

  return C;
};

Router.prototype = {
    get: function(paths) {
        var jsongCache = {};
        var action = runGetAction(this, jsongCache);
        var router = this;
        var normPS = normalizePathSets(paths);
        return run(this._matcher, action, normPS, get, this, jsongCache).
            map(function(jsongEnv) {
                return materializeMissing(router, paths, jsongEnv);
            });
    },

    set: function(jsong) {
        // TODO: Remove the modelContext and replace with just jsongEnv
        // when http://github.com/Netflix/falcor-router/issues/24 is addressed
        var jsongCache = {};
        var action = runSetAction(this, jsong, jsongCache);
        var router = this;
        return run(this._matcher, action, jsong.paths, set, this, jsongCache).
            map(function(jsongEnv) {
                return materializeMissing(router, jsong.paths, jsongEnv);
            });
    },

    call: function(callPath, args, suffixes, paths) {
        var jsongCache = {};
        var action = runCallAction(this, callPath, args,
                                   suffixes, paths, jsongCache);
        var callPaths = [callPath];
        var router = this;
        return run(this._matcher, action, callPaths, call, this, jsongCache).
            map(function(jsongResult) {
                var reportedPaths = jsongResult.reportedPaths;
                var jsongEnv = materializeMissing(
                    router,
                    callPaths,
                    jsongResult, {
                        $type: $atom,
                        $expires: 0
                    });
                materializeMissing(
                    router,
                    reportedPaths,
                    jsongResult);


                if (reportedPaths.length) {
                    jsongEnv.paths = reportedPaths;
                }
                else {
                    jsongEnv.paths = [];
                }

                jsongEnv.paths.push(callPath);
                var invalidated = jsongResult.invalidated;
                if (invalidated && invalidated.length) {
                    jsongEnv.invalidations = invalidated;
                }
                jsongEnv.paths = collapse(jsongEnv.paths);
                return jsongEnv;
            });
    }
};

function run(matcherFn, actionRunner, paths, method,<commit_msg>invalidations to invalidated.<commit_after>var Keys = require('./Keys');
var parseTree = require('./parse-tree');
var matcher = require('./operations/matcher');
var normalizePathSets = require('./operations/ranges/normalizePathSets');
var recurseMatchAndExecute = require('./run/recurseMatchAndExecute');
var optimizePathSets = require('./cache/optimizePathSets');
var pathValueMerge = require('./cache/pathValueMerge');
var runGetAction = require('./run/get/runGetAction');
var runSetAction = require('./run/set/runSetAction');
var runCallAction = require('./run/call/runCallAction');
var $atom = require('./support/types').$atom;
var get = 'get';
var set = 'set';
var call = 'call';
var pathUtils = require('falcor-path-utils');
var collapse = pathUtils.collapse;
var JSONGraphError = require('./JSONGraphError');
var MAX_REF_FOLLOW = 50;

var Router = function(routes, options) {
    var opts = options || {};

    this._routes = routes;
    this._rst = parseTree(routes);
    this._matcher = matcher(this._rst);
    this._debug = opts.debug;
    this.maxRefFollow = opts.maxRefFollow || MAX_REF_FOLLOW;
};

Router.createClass = function(routes) {
  function C(options) {
    var opts = options || {};
    this._debug = opts.debug;
  }

  C.prototype = new Router(routes);
  C.prototype.constructor = C;

  return C;
};

Router.prototype = {
    get: function(paths) {
        var jsongCache = {};
        var action = runGetAction(this, jsongCache);
        var router = this;
        var normPS = normalizePathSets(paths);
        return run(this._matcher, action, normPS, get, this, jsongCache).
            map(function(jsongEnv) {
                return materializeMissing(router, paths, jsongEnv);
            });
    },

    set: function(jsong) {
        // TODO: Remove the modelContext and replace with just jsongEnv
        // when http://github.com/Netflix/falcor-router/issues/24 is addressed
        var jsongCache = {};
        var action = runSetAction(this, jsong, jsongCache);
        var router = this;
        return run(this._matcher, action, jsong.paths, set, this, jsongCache).
            map(function(jsongEnv) {
                return materializeMissing(router, jsong.paths, jsongEnv);
            });
    },

    call: function(callPath, args, suffixes, paths) {
        var jsongCache = {};
        var action = runCallAction(this, callPath, args,
                                   suffixes, paths, jsongCache);
        var callPaths = [callPath];
        var router = this;
        return run(this._matcher, action, callPaths, call, this, jsongCache).
            map(function(jsongResult) {
                var reportedPaths = jsongResult.reportedPaths;
                var jsongEnv = materializeMissing(
                    router,
                    callPaths,
                    jsongResult, {
                        $type: $atom,
                        $expires: 0
                    });
                materializeMissing(
                    router,
                    reportedPaths,
                    jsongResult);


                if (reportedPaths.length) {
                    jsongEnv.paths = reportedPaths;
                }
                else {
                    jsongEnv.paths = [];
                }

                jsongEnv.paths.push(callPath);
                var invalidated = jsongResult.invalidated;
                if (invalidated && invalidated.length) {
                    jsongEnv.invalidated = invalidated;
                }
                jsongEnv.paths = collapse(jsongEnv.paths);
                return jsongEnv;
            });
    }
};

function run(matcherFn, actionRunner, paths, method,
{'fraction_non_alphanumeric': 0.11534261534261535, 'fraction_numerical': 0.001365001365001365, 'mean_word_length': 2.0503746877602, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5952925', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1952, 'n_words': 445}