<commit_before>var _ = require('lodash'),
  pg = require('pg');

module.exports = {
  infoTable: 'koopinfo', 
  timerTable: 'kooptimers', 

  connect: function( conn ){
    var self = this;
    this.client = new pg.Client( conn );
    this.client.connect(function(err) {
      if ( err ){
        console.log('Cannot connect to the given database', conn);
        process.exit();
      } else {
        console.log('Connected to postgres db for storage', conn);
        // creates table only if they dont exist
        self._createTable(self.infoTable, "( id varchar(255) PRIMARY KEY, info JSON)");
        self._createTable(self.timerTable, "( id varchar(255) PRIMARY KEY, expires timestamp)");
      }
    });
    return this; 
  },


  // get data out of the db
  select: function(key, options, callback){
    var self = this;
    //var layer = 0;
    var error = false,
      totalLayers,
      queryOpts = {}, 
      allLayers = [];

    // closure to check each layer and send back when done
    var collect = function(err, data){
      if (err) error = err;
      allLayers.push(data);
      if (allLayers.length == totalLayers){
        callback(error, allLayers);
      }
    };

    this._query('select info from "'+this.infoTable+'" where id=\''+(key+":info")+'\'', function(err, result){
      if ( err || !result || !result.rows || !result.rows.length ){
        callback('Not Found', []);
      } else {
        var info = result.rows[0].info;
        totalLayers = info.length;

        info.forEach(function(layer, i){
          var select = 'select feature from "' + key+':'+i+'"'; 

          if ( options.geometry ){

            if ( typeof(options.geometry) == 'string' ){
              options.geometry = JSON.parse( options.geometry );
            }
            var box = options.geometry;
            select += ' WHERE ST_Intersects(ST_GeomFromGeoJSON(feature->>\'geometry\'), ST_MakeEnvelope('+box.xmin+','+box.ymin+','+box.xmax+','+box.ymax+'))';
          }
          console.log(select);
          self._query( select, function (err, result) {
            if ( result && result.rows && result.rows.length ) {
              collect( null, {
                type: 'FeatureCollection', 
                features: _.pluck(result.rows, 'feature'),
                name: layer.name, 
                sha: layer.sha, 
                updated_at: layer.updated_at 
              });
            } else {
              collect( 'Not Found', {
                type: 'FeatureCollection',
                features: []
              });
            }
          });
        });
      }
    });
  },

  // create a collection and insert features
  // create a 2d index 
  insert: function( key, geojson, callback ){
    var self = this; 
    var info = [],
      count = 0;
      error = null;
    var check = function( err, success){
      if (err) error = err;
      count++;
      if (count == geojson.length){
        self._query( 'insert into "'+self.infoTable+'" values (\''+key+':info\',\''+JSON.stringify(info)+'\')', function(err, result){
          callback(error, true);
        });
      }
    };
    geojson.forEach(function( layer, i ){
      info[i] = { name: layer.name };
      info[i].updated_at = layer.updated_at;
      info[i].sha = layer.sha;
   
      var table = key+':'+i;
      self._createTable( table, self._buildSchemaFromFeature(layer.features[0]), function(err, result){
        layer.features.forEach(function(feature, i){
          self._insertFeature(table, feature, i);
        });
        check(err, true);
      });     
    });
    
  },

  // inserts geojson features into the feature column of the given table
  _insertFeature: function(table, feature, i){
    var sql = 'insert into "'+table+'" (feature) VALUES (\''+JSON.stringify(feature).replace(/'/g, "")+'\')' ;
    this._query(sql);
  },


  remove: function( key, callback){
    var self = this;
    var totalLayers, processedLayers = 0;
    var collect = function(){
      processedLayers++;
      if ( processedLayers == totalLayers ){
        //delete row from info table 
        self._query("delete from \""+self.infoTable+"\" where id='"+(key+':info')+"'", function(err, result){
          callback();
        })
      }
    };
  
    this._query('select info from "'+this.infoTable+'" where id=\''+(key+":info")+"'", function(err, result){
      if ( !result || !result.rows.length ){
        // nothing to remove
        callback( null, true );
      } else {
        var info = result.rows[0].info;
        totalLayers = info.length;
        info.forEach(function(layer, i){
          self.dropTable(key+':'+i, function(err, result){
            collect();
          });
        });
      }
    });
  },

  dropTable: function(table, callback){
    this._query('drop table "'+table+'"' , callback);
  },

  services: { 
    register: function( type, info, callback){
      PostGIS._createTable(type, '( id varchar(100), host varchar(100))', function(err, result){
        PostGIS._query('select * from "'+type+'" where id=\''+info.id+"\'", function(err, res){
          if ( !res.rows.length ) {<commit_msg>fix service registration<commit_after>var _ = require('lodash'),
  pg = require('pg');

module.exports = {
  infoTable: 'koopinfo', 
  timerTable: 'kooptimers', 

  connect: function( conn ){
    var self = this;
    this.client = new pg.Client( conn );
    this.client.connect(function(err) {
      if ( err ){
        console.log('Cannot connect to the given database', conn);
        process.exit();
      } else {
        console.log('Connected to postgres db for storage', conn);
        // creates table only if they dont exist
        self._createTable(self.infoTable, "( id varchar(255) PRIMARY KEY, info JSON)");
        self._createTable(self.timerTable, "( id varchar(255) PRIMARY KEY, expires timestamp)");
      }
    });
    return this; 
  },


  // get data out of the db
  select: function(key, options, callback){
    var self = this;
    //var layer = 0;
    var error = false,
      totalLayers,
      queryOpts = {}, 
      allLayers = [];

    // closure to check each layer and send back when done
    var collect = function(err, data){
      if (err) error = err;
      allLayers.push(data);
      if (allLayers.length == totalLayers){
        callback(error, allLayers);
      }
    };

    this._query('select info from "'+this.infoTable+'" where id=\''+(key+":info")+'\'', function(err, result){
      if ( err || !result || !result.rows || !result.rows.length ){
        callback('Not Found', []);
      } else {
        var info = result.rows[0].info;
        totalLayers = info.length;

        info.forEach(function(layer, i){
          var select = 'select feature from "' + key+':'+i+'"'; 

          if ( options.geometry ){

            if ( typeof(options.geometry) == 'string' ){
              options.geometry = JSON.parse( options.geometry );
            }
            var box = options.geometry;
            select += ' WHERE ST_Intersects(ST_GeomFromGeoJSON(feature->>\'geometry\'), ST_MakeEnvelope('+box.xmin+','+box.ymin+','+box.xmax+','+box.ymax+'))';
          }
          console.log(select);
          self._query( select, function (err, result) {
            if ( result && result.rows && result.rows.length ) {
              collect( null, {
                type: 'FeatureCollection', 
                features: _.pluck(result.rows, 'feature'),
                name: layer.name, 
                sha: layer.sha, 
                updated_at: layer.updated_at 
              });
            } else {
              collect( 'Not Found', {
                type: 'FeatureCollection',
                features: []
              });
            }
          });
        });
      }
    });
  },

  // create a collection and insert features
  // create a 2d index 
  insert: function( key, geojson, callback ){
    var self = this; 
    var info = [],
      count = 0;
      error = null;
    var check = function( err, success){
      if (err) error = err;
      count++;
      if (count == geojson.length){
        self._query( 'insert into "'+self.infoTable+'" values (\''+key+':info\',\''+JSON.stringify(info)+'\')', function(err, result){
          callback(error, true);
        });
      }
    };
    geojson.forEach(function( layer, i ){
      info[i] = { name: layer.name };
      info[i].updated_at = layer.updated_at;
      info[i].sha = layer.sha;
   
      var table = key+':'+i;
      self._createTable( table, self._buildSchemaFromFeature(layer.features[0]), function(err, result){
        layer.features.forEach(function(feature, i){
          self._insertFeature(table, feature, i);
        });
        check(err, true);
      });     
    });
    
  },

  // inserts geojson features into the feature column of the given table
  _insertFeature: function(table, feature, i){
    var sql = 'insert into "'+table+'" (feature) VALUES (\''+JSON.stringify(feature).replace(/'/g, "")+'\')' ;
    this._query(sql);
  },


  remove: function( key, callback){
    var self = this;
    var totalLayers, processedLayers = 0;
    var collect = function(){
      processedLayers++;
      if ( processedLayers == totalLayers ){
        //delete row from info table 
        self._query("delete from \""+self.infoTable+"\" where id='"+(key+':info')+"'", function(err, result){
          callback();
        })
      }
    };
  
    this._query('select info from "'+this.infoTable+'" where id=\''+(key+":info")+"'", function(err, result){
      if ( !result || !result.rows.length ){
        // nothing to remove
        callback( null, true );
      } else {
        var info = result.rows[0].info;
        totalLayers = info.length;
        info.forEach(function(layer, i){
          self.dropTable(key+':'+i, function(err, result){
            collect();
          });
        });
      }
    });
  },

  dropTable: function(table, callback){
    this._query('drop table "'+table+'"' , callback);
  },

  services: { 
    register: function( type, info, callback){
      PostGIS._createTable(type, '( id varchar(100), host varchar(100))', function(err, result){
        PostGIS._query('select * from "'+type+'" where id=\''+info.id+"\'", function(err, res){
          if ( !err || !res || !res.rows || !res.rows.length ) {
{'fraction_non_alphanumeric': 0.16145327375170002, 'fraction_numerical': 0.0036914707596658247, 'mean_word_length': 2.156039239730227, 'pattern_counts': {'":': 4, '<': 3, '<?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': '2185737', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3001, 'n_words': 737}