<commit_before>var mongoskin = require('mongoskin')
var db = mongoskin.db('mongodb://@localhost:27017/db', {safe:true})
var cursors = {}

module.exports.usage = function(req, res, next) {
  res.send('please select a collection, e.g., /colls/msgs')
}

module.exports.getTop100Docs = function(req, res, next) {
  req.collection.find({} ,{limit:100, sort: [['_id',-1]]}).toArray(function(e, results){
    res.send(results)
    next()
  })
}

module.exports.findById = function(req, res, next) {
  req.collection.findById(req.params.id, function(e, result){
    res.send(result)
    next()
  })
}

module.exports.getCollections = function(req, res, next) {
  db.collectionNames(function(err, items) {
    res.send(items)
    next()
  })
}

module.exports.getAll = function(req, res, next) {
  getAllInternal(req, res, next, 1000)
}

module.exports.getAllWithLimit = function(req, res, next) {
  getAllInternal(req, res, next, req.params.limit)
}

function getAllInternal(req, res, next, max) {
  req.collection.find(req.body, {limit:max, sort: [['_id', -1]]}).toArray(function(e, results){
    res.send(results)
    next()
  })
}

module.exports.getNext = function(req, res, next) {
  getNextInternal(req, res, next, 1)
}

module.exports.getNextBatch = function(req, res, next) {
  getNextInternal(req, res, next, req.params.batchSize)
}

function getNextInternal(req, res, next, size) {
  console.log(size)
  var sess = req.session
  var cursor
  if (sess.previousQuery &&
    JSON.stringify(sess.previousQuery) === JSON.stringify(req.body)) {
      sess.docNumber++;
  }
  else {
    sess.previousQuery = req.body
    sess.docNumber = 0
  }

  cursor = req.collection.find(req.body, {limit: size, skip: sess.docNumber, sort: [['_id', -1]]}).toArray(function(e, results) {
    res.send(results)
    next()
  })
}<commit_msg>Bug fix for getNext<commit_after>var mongoskin = require('mongoskin')
var db = mongoskin.db('mongodb://@localhost:27017/db', {safe:true})
var cursors = {}

module.exports.usage = function(req, res, next) {
  res.send('please select a collection, e.g., /colls/msgs')
}

module.exports.getTop100Docs = function(req, res, next) {
  req.collection.find({} ,{limit:100, sort: [['_id',-1]]}).toArray(function(e, results){
    res.send(results)
    next()
  })
}

module.exports.findById = function(req, res, next) {
  req.collection.findById(req.params.id, function(e, result){
    res.send(result)
    next()
  })
}

module.exports.getCollections = function(req, res, next) {
  db.collectionNames(function(err, items) {
    res.send(items)
    next()
  })
}

module.exports.getAll = function(req, res, next) {
  getAllInternal(req, res, next, 1000)
}

module.exports.getAllWithLimit = function(req, res, next) {
  getAllInternal(req, res, next, req.params.limit)
}

function getAllInternal(req, res, next, max) {
  req.collection.find(req.body, {limit:max, sort: [['_id', -1]]}).toArray(function(e, results){
    res.send(results)
    next()
  })
}

module.exports.getNext = function(req, res, next) {
  getNextInternal(req, res, next, 1)
}

module.exports.getNextBatch = function(req, res, next) {
  getNextInternal(req, res, next, req.params.batchSize)
}

function getNextInternal(req, res, next, size) {
  var sess = req.session
  var cursor
  if (!sess.previousQuery ||
    JSON.stringify(sess.previousQuery) != JSON.stringify(req.body)) {
    sess.previousQuery = req.body
    sess.docNumber = 0
  }

  cursor = req.collection.find(req.body, {limit: parseInt(size), skip: sess.docNumber, sort: [['_id', -1]]}).toArray(function(e, results) {
    res.send(results)
    next()
  })

  sess.docNumber += parseInt(size)
}
{'fraction_non_alphanumeric': 0.1715699752543305, 'fraction_numerical': 0.010998075336816057, 'mean_word_length': 4.24207492795389, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2414090', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1312, 'n_words': 252}