<commit_before>sys = require 'sys'
url = require 'url'
require '../public/javascripts/underscore'
MongoDb = require('../lib/node-mongodb-native/lib/mongodb/db').Db
MongoServer = require('../lib/node-mongodb-native/lib/mongodb/connection').Server
MongoId = require('../lib/node-mongodb-native/lib/mongodb/bson/bson').ObjectID

class Mongo
  localUrl: 'http://localhost:27017/nodeknockout'
  constructor: ->
    @parseUrl process.env['MONGOHQ_URL'] or @localUrl
    @server = new MongoServer @host, @port
    @db = new MongoDb @dbname, @server
    if @user?
      @db.open =>
        @db.authenticate @user, @password, -> # no callback
    else @db.open -> # no callback

  parseUrl: (urlString)->
    sys.log "Connecting to MongoDB at #{urlString}"
    uri = url.parse urlString
    @host = uri.hostname
    @port = parseInt(uri.port)
    @dbname = uri.pathname.replace(/^\//,'')
    [@user, @password] = uri.auth.split(':') if uri.auth?
exports.Mongo = Mongo

_.extend Mongo,
  db: (new Mongo()).db

  bless: (klass) ->
    Serializer.bless klass
    _.extend klass.prototype, Mongo.InstanceMethods
    _.extend klass, Mongo.ClassMethods
    klass

  blessAll: (namespace) ->
    for name, klass of namespace
      klass::serialize ||= name
      @bless klass

  queryify: (query) ->
    return {} unless query?
    if _.isString query
      _id: MongoId.createFromHexString(query)
    else query

  instantiate: (data, fn) ->
    unpacked = Serializer.unpack data
    if unpacked?.beforeInstantiate?
      unpacked.beforeInstantiate ->
        fn null, unpacked
    else
      fn null, unpacked

  ClassMethods:
    count: (query, options, fn) ->
      unless fn?
        fn = options
        options = {}
      unless fn?
        fn = query
        query = null
      @prototype.collection (error, collection) ->
        return fn error if error?
        collection.count query, options, fn<commit_msg>fix count<commit_after>sys = require 'sys'
url = require 'url'
require '../public/javascripts/underscore'
MongoDb = require('../lib/node-mongodb-native/lib/mongodb/db').Db
MongoServer = require('../lib/node-mongodb-native/lib/mongodb/connection').Server
MongoId = require('../lib/node-mongodb-native/lib/mongodb/bson/bson').ObjectID

class Mongo
  localUrl: 'http://localhost:27017/nodeknockout'
  constructor: ->
    @parseUrl process.env['MONGOHQ_URL'] or @localUrl
    @server = new MongoServer @host, @port
    @db = new MongoDb @dbname, @server
    if @user?
      @db.open =>
        @db.authenticate @user, @password, -> # no callback
    else @db.open -> # no callback

  parseUrl: (urlString)->
    sys.log "Connecting to MongoDB at #{urlString}"
    uri = url.parse urlString
    @host = uri.hostname
    @port = parseInt(uri.port)
    @dbname = uri.pathname.replace(/^\//,'')
    [@user, @password] = uri.auth.split(':') if uri.auth?
exports.Mongo = Mongo

_.extend Mongo,
  db: (new Mongo()).db

  bless: (klass) ->
    Serializer.bless klass
    _.extend klass.prototype, Mongo.InstanceMethods
    _.extend klass, Mongo.ClassMethods
    klass

  blessAll: (namespace) ->
    for name, klass of namespace
      klass::serialize ||= name
      @bless klass

  queryify: (query) ->
    return {} unless query?
    if _.isString query
      _id: MongoId.createFromHexString(query)
    else query

  instantiate: (data, fn) ->
    unpacked = Serializer.unpack data
    if unpacked?.beforeInstantiate?
      unpacked.beforeInstantiate ->
        fn null, unpacked
    else
      fn null, unpacked

  ClassMethods:
    count: (query, fn) ->
      unless fn?
        fn = query
        query = null
      @prototype.collection (error, collection) ->
        return fn error if error?
        collection.count query, fn
{'fraction_non_alphanumeric': 0.1328145961899651, 'fraction_numerical': 0.002683123155352831, 'mean_word_length': 2.8118609406952966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '5961256', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1186, 'n_words': 297}