<commit_before>var EventEmitter = require('events').EventEmitter,
  util = require('util'),
  _ = require('underscore'),
  Schema = require('./schema'),
  random = Math.random;

var Model = module.exports = function Model(name, parent, schema, index, fields){
  this._name = name;
  this._parent = parent;
  this._store = parent._store[name];
  this._index = index;
  this._fields = fields;

  if (schema.constructor === Schema){
    this._schema = schema;

    var methods = schema.methods,
      _this = this;

    for (var i in methods){
      this.__proto__[i] = function(){
        return methods[i].call(_this);
      };
    }
  } else {
    this._schema = new Schema(schema);
  }

  this.__defineGetter__('length', function(){
    return this._index.length;
  });
};

util.inherits(Model, EventEmitter);

Model.prototype.forEach = Model.prototype.each = function(iterator){
  var index = this._index;
  for (var i=0, len=index.length; i<len; i++){
    var item = index[i];
    iterator.call(this, this._getOne(item), item);
  }
  return this;
};

Model.prototype.toArray = function(){
  var arr = [];
  this.each(function(item){
    arr.push(item);
  });
  return arr;
};

Model.prototype.count = function(){
  return this._index.length;
};

Model.prototype._getOne = function(id){
  id = parseInt(id, 10);
  var index = this._index,
    pos = index.indexOf(id);

  if (pos === -1) return undefined;

  var obj = this._store.get(id),
    data = this._schema.restore(obj),
    fields = this._fields;
  data._id = id;
  if (fields){
    return _.pick(data, fields);
  } else {
    return data;
  }
};

Model.prototype.get = function(){
  var args = _.toArray(arguments),
    options = _.isObject(args[args.length - 1]) ? args.pop() : {};

  if (args.length > 1){
    var id = _(args).flatten().uniq(),
      arr = [];
    for (var i=0, len=id.length; i<len; i++){
      var data = this._getOne(id[i]);
      if (typeof data !== 'undefined') arr.push(data);
    }
    return arr;
  } else {
    var id = args[0];
    return this._getOne(id);
  }
};

Model.prototype._insertOne = function(data, callback){
  var store = this._store,
    id = store.get('_primary');
  store.set(id, this._schema.save(data));
  store.set('_primary', id + 1);
  this._index.push(id);
  var item = this._getOne(id);
  this.emit('insert', item, id);
  callback.call(this, item, id);
};

Model.prototype.insert = function(data, callback){
  if (!_.isFunction(callback)) callback = function(){};

  if (_.isArray(data)){
    var arr = [];
    for (var i=0, len=obj.length; i<len; i++){
      this._insertOne(obj[i], function(item){
        arr.push(item);
      });
    }
    callback.call(this, arr);
  } else {
    this._insertOne(data, callback);
  }
  return this;
};

Model.prototype._updateOne = function(id, data){
  id = parseInt(id, 10);
  var obj = this._getOne(id);

  for (var i in data){
    var val = data[i];
    if (_.isObject(val)){
      var target = obj[i];
      if (_.isArray(target)){
        for (var j in val){
          var prop = val[j];
          switch (j){<commit_msg>Fixed array insertion<commit_after>var EventEmitter = require('events').EventEmitter,
  util = require('util'),
  _ = require('underscore'),
  Schema = require('./schema'),
  random = Math.random;

var Model = module.exports = function Model(name, parent, schema, index, fields){
  this._name = name;
  this._parent = parent;
  this._store = parent._store[name];
  this._index = index;
  this._fields = fields;

  if (schema.constructor === Schema){
    this._schema = schema;

    var methods = schema.methods,
      _this = this;

    for (var i in methods){
      this.__proto__[i] = function(){
        return methods[i].call(_this);
      };
    }
  } else {
    this._schema = new Schema(schema);
  }

  this.__defineGetter__('length', function(){
    return this._index.length;
  });
};

util.inherits(Model, EventEmitter);

Model.prototype.forEach = Model.prototype.each = function(iterator){
  var index = this._index;
  for (var i=0, len=index.length; i<len; i++){
    var item = index[i];
    iterator.call(this, this._getOne(item), item);
  }
  return this;
};

Model.prototype.toArray = function(){
  var arr = [];
  this.each(function(item){
    arr.push(item);
  });
  return arr;
};

Model.prototype.count = function(){
  return this._index.length;
};

Model.prototype._getOne = function(id){
  id = parseInt(id, 10);
  var index = this._index,
    pos = index.indexOf(id);

  if (pos === -1) return undefined;

  var obj = this._store.get(id),
    data = this._schema.restore(obj),
    fields = this._fields;
  data._id = id;
  if (fields){
    return _.pick(data, fields);
  } else {
    return data;
  }
};

Model.prototype.get = function(){
  var args = _.toArray(arguments),
    options = _.isObject(args[args.length - 1]) ? args.pop() : {};

  if (args.length > 1){
    var id = _(args).flatten().uniq(),
      arr = [];
    for (var i=0, len=id.length; i<len; i++){
      var data = this._getOne(id[i]);
      if (typeof data !== 'undefined') arr.push(data);
    }
    return arr;
  } else {
    var id = args[0];
    return this._getOne(id);
  }
};

Model.prototype._insertOne = function(data, callback){
  var store = this._store,
    id = store.get('_primary');
  store.set(id, this._schema.save(data));
  store.set('_primary', id + 1);
  this._index.push(id);
  var item = this._getOne(id);
  this.emit('insert', item, id);
  callback.call(this, item, id);
};

Model.prototype.insert = function(data, callback){
  if (!_.isFunction(callback)) callback = function(){};

  if (_.isArray(data)){
    var arr = [];
    for (var i=0, len=data.length; i<len; i++){
      this._insertOne(data[i], function(item){
        arr.push(item);
      });
    }
    callback.call(this, arr);
  } else {
    this._insertOne(data, callback);
  }
  return this;
};

Model.prototype._updateOne = function(id, data){
  id = parseInt(id, 10);
  var obj = this._getOne(id);

  for (var i in data){
    var val = data[i];
    if (_.isObject(val)){
      var target = obj[i];
      if (_.isArray(target)){
        for (var j in val){
          var prop = val[j];
          switch (j){
{'fraction_non_alphanumeric': 0.1729957805907173, 'fraction_numerical': 0.0038948393378773127, 'mean_word_length': 3.020221787345075, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '1296046', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2084, 'n_words': 489}