<commit_before>/*
  Copyright (c) 2012 All contributors as noted in the AUTHORS file

  This file is part of quarry.io

  quarry.io is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  quarry.io is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
  Module dependencies.
*/

if (typeof define !== 'function') { var define = require('amdefine')(module) }

define(['underscore', 'async', 'events', './utils', './selector'], function (_, async, events, utils, selector_parser) {

  var EventEmitter = events.EventEmitter;

    
/*
  Quarry.io Container
  -------------------

  Gives a standard container for raw JSON data returned by suppliers

  This is the base wrapper class for all QuarryDB data throughout the system

 */



/***********************************************************************************
 ***********************************************************************************
  Here is the  data structure:

  {
    
    name:"Bob",
    price:100,

    // this holds the flat value if that what it is
    _primitive:10,

    // the meta data storage
    _meta:{
      id:'bob',
      tagname:'product',
      classnames:{
        red:true,
        blue:true
      }
    },

    // the array of child container values
    _children:[

      {...} // a raw container

      OR

      [
          {...} // routing

          [ ... ] // raw children

      ]

    ]


  }

 */


/*
 * Container
 ****************************************************************
 ****************************************************************
 ****************************************************************
 ****************************************************************
 *
 * Here is the actual container code
 *
 *
 ***************************************************************
 ****************************************************************
 ****************************************************************
 ****************************************************************
 */

// make me a new container please - here is some data and the originating warehouse!
function factory(data, supply_chain){

  var force_tag = null;

  // we only have a supply chain
  if(_.isFunction(data)){
    supply_chain = data;
    data = {};
  }
  // this is for 'new' containers with no supply chain
  else if(_.isString(data) && _.isObject(supply_chain)){
    force_tag = data;
    data = supply_chain;
    supply_chain = null;
  }

  // ensure the data layout
  data = ensureContainerObject(data);

  if(force_tag){
    data._meta.tagname = force_tag;
  }

  // the base info for a container
  function get_skeleton(){
    return {
      _meta:data._meta,
      _data:data._data
    }
  }

  // a reference to the in-memory parent - this is passed on by descendent functions
  var parent = null;

  // trigger a 'contract' on the supply chain
  function container(selector, context){

    var self = this;

    // make sure the context is a string
    if(_.isFunction(context)){
      context = '=' + context.quarryid();
    }
    else if(_.isObject(context)){
      context = '=' + context._meta.quarryid;
    }

    // return a promise so they can set some options for the query
    // once the promise is triggered - we end up below with a callback
    // to which we pipe a results container
    return promiseFactory(function(promise_options, return_callback){

      // run the actual query through the supply chain
      route_packet(supply_chain, {
        action:'select',
        message:{
          previous:[get_skeleton()],
          selector:selector_parser(selector),
          context:selector_parser(context),
          options:promise_options
        }
      }, function(error, packet){

        // turn the results into an answer container
        //
        return_callback(factory(packet.answer.results, supply_chain));
        
      })<commit_msg>blank answer if none returned by supply chain<commit_after>/*
  Copyright (c) 2012 All contributors as noted in the AUTHORS file

  This file is part of quarry.io

  quarry.io is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  quarry.io is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
  Module dependencies.
*/

if (typeof define !== 'function') { var define = require('amdefine')(module) }

define(['underscore', 'async', 'events', './utils', './selector'], function (_, async, events, utils, selector_parser) {

  var EventEmitter = events.EventEmitter;

    
/*
  Quarry.io Container
  -------------------

  Gives a standard container for raw JSON data returned by suppliers

  This is the base wrapper class for all QuarryDB data throughout the system

 */



/***********************************************************************************
 ***********************************************************************************
  Here is the  data structure:

  {
    
    name:"Bob",
    price:100,

    // this holds the flat value if that what it is
    _primitive:10,

    // the meta data storage
    _meta:{
      id:'bob',
      tagname:'product',
      classnames:{
        red:true,
        blue:true
      }
    },

    // the array of child container values
    _children:[

      {...} // a raw container

      OR

      [
          {...} // routing

          [ ... ] // raw children

      ]

    ]


  }

 */


/*
 * Container
 ****************************************************************
 ****************************************************************
 ****************************************************************
 ****************************************************************
 *
 * Here is the actual container code
 *
 *
 ***************************************************************
 ****************************************************************
 ****************************************************************
 ****************************************************************
 */

// make me a new container please - here is some data and the originating warehouse!
function factory(data, supply_chain){

  var force_tag = null;

  // we only have a supply chain
  if(_.isFunction(data)){
    supply_chain = data;
    data = {};
  }
  // this is for 'new' containers with no supply chain
  else if(_.isString(data) && _.isObject(supply_chain)){
    force_tag = data;
    data = supply_chain;
    supply_chain = null;
  }

  // ensure the data layout
  data = ensureContainerObject(data);

  if(force_tag){
    data._meta.tagname = force_tag;
  }

  // the base info for a container
  function get_skeleton(){
    return {
      _meta:data._meta,
      _data:data._data
    }
  }

  // a reference to the in-memory parent - this is passed on by descendent functions
  var parent = null;

  // trigger a 'contract' on the supply chain
  function container(selector, context){

    var self = this;

    // make sure the context is a string
    if(_.isFunction(context)){
      context = '=' + context.quarryid();
    }
    else if(_.isObject(context)){
      context = '=' + context._meta.quarryid;
    }

    // return a promise so they can set some options for the query
    // once the promise is triggered - we end up below with a callback
    // to which we pipe a results container
    return promiseFactory(function(promise_options, return_callback){

      // run the actual query through the supply chain
      route_packet(supply_chain, {
        action:'select',
        message:{
          previous:[get_skeleton()],
          selector:selector_parser(selector),
          context:selector_parser(context),
          options:promise_options
        }
      }, function(error, packet){

        // turn the results into an answer container
        //
        packet.answer || (packet.answer = {
          ok:true,
          empty:true
        })
        return_callback(factory(packet.answer.results, supply_chain));
        
      })
{'fraction_non_alphanumeric': 0.23545240497813658, 'fraction_numerical': 0.002242403856934634, 'mean_word_length': 2.9892665474060824, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '458973', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2141, 'n_words': 783}