<commit_before>Datastore  = require('nedb')
logger = require('./logger.js').logger

articlesDb = new Datastore {filename: 'articles.db', nodeWebkitAppName: 'pocket-dashboard', autoload: true }
articlesDb.ensureIndex { fieldName: 'item_id' }, (err) ->
    if err?
        logger.error "Articles database index constraint error"
        logger.error err
    return

metadataDb = new Datastore {filename: 'metadata.db', nodeWebkitAppName: 'pocket-dashboard', autoload: true }

exports.upsertArticle = (doc) ->
    if doc.status is "2"
        articlesDb.remove {"item_id" : {$in : [doc.item_id]}}, (err, numRemove) ->
            if err?
                logger.error "Error removing document" + doc.item_id
                logger.error err
            else
                logger.info "Removed document: " + doc.item_id
            return
    else
        articlesDb.update {"item_id" : {$in : [doc.item_id]}}, doc,  { upsert: true }, (err, numReplaced, upsert) ->
            if err?
                logger.error "Error inserting document" + doc.item_id
                logger.error err
            else
                logger.info "Inserted document: " + doc.item_id
                logger.info "numReplaced : " + numReplaced + " - upsert : " + upsert
            return
    return

exports.updateLastSinceTimestamp = (time) ->
    metadataDb.update {"lastSince" : {$exists : true}}, {"lastSince" : time},  { upsert: true }, (err, numReplaced, upsert) ->
        if err?
            logger.error "Error updating the timestamp for last retrieving articles" + time
            logger.error err
        else
            logger.info "Updated last since timestamp for retrieving articles: " + time
        return
    return

exports.getLastSinceTimestamp = (callback) ->
    metadataDb.find {"lastSince" : {$exists : true}}, (findError, docs) ->
        if err?
            logger.error "Error find last since timestamp"<commit_msg>add number removed to logging message when deleting articles<commit_after>Datastore  = require('nedb')
logger = require('./logger.js').logger

articlesDb = new Datastore {filename: 'articles.db', nodeWebkitAppName: 'pocket-dashboard', autoload: true }
articlesDb.ensureIndex { fieldName: 'item_id' }, (err) ->
    if err?
        logger.error "Articles database index constraint error"
        logger.error err
    return

metadataDb = new Datastore {filename: 'metadata.db', nodeWebkitAppName: 'pocket-dashboard', autoload: true }

exports.upsertArticle = (doc) ->
    if doc.status is "2"
        articlesDb.remove {"item_id" : {$in : [doc.item_id]}}, (err, numRemove) ->
            if err?
                logger.error "Error removing document" + doc.item_id
                logger.error err
            else
                logger.info "Removed document: " + doc.item_id + " -  numRemove: " + numRemove
            return
    else
        articlesDb.update {"item_id" : {$in : [doc.item_id]}}, doc,  { upsert: true }, (err, numReplaced, upsert) ->
            if err?
                logger.error "Error inserting document" + doc.item_id
                logger.error err
            else
                logger.info "Inserted document: " + doc.item_id
                logger.info "numReplaced : " + numReplaced + " - upsert : " + upsert
            return
    return

exports.updateLastSinceTimestamp = (time) ->
    metadataDb.update {"lastSince" : {$exists : true}}, {"lastSince" : time},  { upsert: true }, (err, numReplaced, upsert) ->
        if err?
            logger.error "Error updating the timestamp for last retrieving articles" + time
            logger.error err
        else
            logger.info "Updated last since timestamp for retrieving articles: " + time
        return
    return

exports.getLastSinceTimestamp = (callback) ->
    metadataDb.find {"lastSince" : {$exists : true}}, (findError, docs) ->
        if err?
            logger.error "Error find last since timestamp"
{'fraction_non_alphanumeric': 0.11396591198168406, 'fraction_numerical': 0.0005087763927753752, 'mean_word_length': 2.3322033898305086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2796596', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1083, 'n_words': 311}