<commit_before>'use strict';

var Registry = require('..').Registry
  , MongoClient = require('mongodb').MongoClient
  , ObjectId = require('mongodb').ObjectId
  , errors = require('../lib/errors');

function MongoRegistry(url) {
  Registry.call(this);

  var _this = this;
  MongoClient.connect(url, function(err, db) {
    if (err) {
      _this.emit('error', err); // FIXME server should check this
      db.close();
    } else {
      _this.clients = db.collection('clients');
    }
  });
}

module.exports = MongoRegistry;
MongoRegistry.prototype = Object.create(Registry.prototype);
MongoRegistry.prototype.constructor = MongoRegistry;

MongoRegistry.prototype._find = function(endpoint, callback) {
  this.clients.find({ ep: endpoint }).limit(1).next(function(err, result) {
    if (err) {
      callback(err);
    } else if (result === null) {
      callback(new errors.DeviceNotFound());
    } else {
      callback(null, Object.assign({ location: result._id }, result));
    }
  });
};

MongoRegistry.prototype._get = function(location, callback) {
  this.clients.find({ _id: ObjectId(location) })
    .limit(1).next(function(err, result) {
      if (err) {
        callback(err);
      } else if (result === null) {
        callback(new errors.DeviceNotFound());
      } else {
        callback(null, Object.assign({ location: result._id }, result));
      }
    });
};

MongoRegistry.prototype._save = function(client, callback) {
  this.clients.insertOne(client, function(err, result) {
    if (err) {
      callback(err);
    } else {
      callback(null, result.insertedId);
    }
  });
};

MongoRegistry.prototype._update = function(location, params, callback) {
  this.clients.findOneAndUpdate({ 
    _id: ObjectId(location), 
  }, { 
    $set: params, 
  }, function(err, result) {
    if (err) {
      callback(err);
    } else if (result.value === null) {
      callback(new errors.DeviceNotFound());
    } else {
      callback(null, result.value._id);
    }
  });
};

MongoRegistry.prototype._delete = function(location, callback) {
  this.clients.findOneAndDelete({ 
    _id: ObjectId(location), <commit_msg>expire clients from registry after lifetime<commit_after>'use strict';

var Registry = require('..').Registry
  , MongoClient = require('mongodb').MongoClient
  , ObjectId = require('mongodb').ObjectId
  , errors = require('../lib/errors');

function MongoRegistry(url) {
  Registry.call(this);

  var _this = this;
  MongoClient.connect(url, function(err, db) {
    if (err) {
      _this.emit('error', err); // FIXME server should check this
      db.close();
    } else {
      _this.clients = db.collection('clients');
      _this.clients.createIndex({ expires: 1 }, { expireAfterSeconds: 30 });
    }
  });
}

module.exports = MongoRegistry;
MongoRegistry.prototype = Object.create(Registry.prototype);
MongoRegistry.prototype.constructor = MongoRegistry;

MongoRegistry.prototype._find = function(endpoint, callback) {
  this.clients.find({ ep: endpoint }).limit(1).next(function(err, result) {
    if (err) {
      callback(err);
    } else if (result === null) {
      callback(new errors.DeviceNotFound());
    } else {
      callback(null, Object.assign({ location: result._id }, result));
    }
  });
};

MongoRegistry.prototype._get = function(location, callback) {
  this.clients.find({ _id: ObjectId(location) })
    .limit(1).next(function(err, result) {
      if (err) {
        callback(err);
      } else if (result === null) {
        callback(new errors.DeviceNotFound());
      } else {
        callback(null, Object.assign({ location: result._id }, result));
      }
    });
};

MongoRegistry.prototype._save = function(params, callback) {
  var client = Object.assign({
    expires: new Date(Date.now() + (params.lt || 86400) * 1e3),
  }, params);

  this.clients.insertOne(client, function(err, result) {
    if (err) {
      callback(err);
    } else {
      callback(null, result.insertedId);
    }
  });
};

MongoRegistry.prototype._update = function(location, params, callback) {
  var _this = this;

  this.get(location)
    .then(function(result) {
      var client = Object.assign(result, params, {
        expires: new Date(Date.now() + (params.lt || result.lt) * 1e3),
      });

      return _this.clients.updateOne({ 
        _id: ObjectId(location), 
      }, { 
        $set: client,
      });
    })
    .then(function() {
      callback(null, location);
    })
    .catch(callback);
};

MongoRegistry.prototype._delete = function(location, callback) {
  this.clients.findOneAndDelete({ 
    _id: ObjectId(location), 
{'fraction_non_alphanumeric': 0.15787178368948976, 'fraction_numerical': 0.0034888791975577847, 'mean_word_length': 3.2159926470588234, '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': '6410374', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1417, 'n_words': 278}