'use strict';

process.on('unhandledRejection', function (e) {
    throw e;
});

const bluebird = require('bluebird');
const co = require('co');
const OCLE = require('openchemlib-extended');
const chemcalc = require('chemcalc');
const mfUtil = require('../src/node_modules/mf');

const mongo = require('../src/node_modules/mongo');

let db;
let done = 0;
co(function*() {
    db = yield mongo.connect();
    console.log('connected to MongoDB');
    const collection = db.collection('data');
    const cursor = collection.find();
    while (yield cursor.hasNext()) {
        if (done % 100000 === 0) {
            console.log(new Date(), done);
        }
        const doc = yield cursor.next();
        const mol = OCLE.Molecule.fromIDCode(doc.ocl.id, doc.ocl.coord);
        const mf = mol.getMF().parts.join('.');

        const toSet = {
            mf: mf
        };

        try {
            const chemcalcMF = chemcalc.analyseMF(mf);
            toSet.em = chemcalcMF.em;
            toSet.mw= chemcalcMF.mw;
            toSet.unsat= chemcalcMF.unsaturation;
            toSet.charge= chemcalcMF.charge;
            toSet.atom= mfUtil.getAtoms(chemcalcMF);
            yield collection.findOneAndUpdate({
                _id: doc._id
            }, {
                $set: toSet
            });
        } catch (e) {
            console.log('Remove fields for',doc._id);
            yield collection.findOneAndUpdate({
                _id: doc._id
            }, {
                $unset: {em:'', mw:'', unsat:'', charge:'', atom:''}
            });
        }


        done++;
    }
}).catch(function (e) {
    console.log('error');
    console.error(e);
}).then(function () {
    console.log('closing DB');
    if (db) db.close();
});

@@ -347,16 +347,77 @@
 one = 0;
+ // we may change this value and some records will be skipped
 %0Aco(func
@@ -581,16 +581,27 @@
 n.find()
+.skip(done)
 ;%0A    wh
@@ -1702,24 +1702,44 @@
 g('error');%0A
+    console.log(e);%0A
     console.

Allow to skip some records