<commit_before>/**
 * Created by cellis on 12/21/14.
 */
var ObjectID = require('mongodb').ObjectID;

module.exports = new dbutils;

function dbutils() {};


dbutils.prototype.createDocument = function(collection, newDocument, callback) {

    global.db.collection(collection)
        .insert(newDocument, function(err, result) {
            //return the assigned id or null if nothing was inserted
            if (result && result.length > 0) {
                result = {_id:result[0]._id};
            } else {
                result = null;
            }
            callback(err, result);
        });
}

dbutils.prototype.findDocumentById = function(collection, docid, queryOptions, callback) {

    var selector = { _id: new ObjectID(docid) };

    //lookup doc in db
    global.db.collection(collection)
        .find(selector, {}, queryOptions.options)
        .toArray(function(err, result) {

            if (err) {
                callback(err, result);
            } else if (!result || result.length == 0) {
                callback("No document found for id: " + docid, null);
            } else {
                //we have something from the db, prep it and send it
                var resultObj = result[0];

                //prune the object if caller only requested a specific object
                if (queryOptions.fieldsArray && queryOptions.fieldsArray.length > 0) {

                    // caching the length and index for loop
                    var len = queryOptions.fieldsArray.length,
                        i = 0;

                    for (i = 0; i < len; i += 1) {
                        resultObj = resultObj[queryOptions.fieldsArray[i]];
                    }
                }

                //return the results
                callback(err, resultObj);
            }
        });

}

dbutils.prototype.findDocument = function(collection, queryOptions, callback) {

    //go look in mongo
    db.collection(collection)
        .find(queryOptions.select, {}, queryOptions.options)
        .toArray(function(err, result) {
            callback(err, result);
        });

}

// Method to keep track of the number of calls to the .collection.find method in
// in the function below
var objToCollectCount = 0;
//function objCollectCB(err, objRemainingCount, result) {
//}

dbutils.prototype.findRandomDocument = function(collection, queryOptions, callback) {

    // Check if more than one random number of documents have been requested
    console.log("From dbutils findRandomDocument queryOptions =", queryOptions);

    // Let us see how many random objects are being requested (the 'limit' parameter
    // specifies that).
    randomObjCount = queryOptions.options.limit
    
    // get a count of docs
    global.db.collection(collection).count(function(err, result) {
        // Check if the number of requested objects is 1 or more
        if (randomObjCount == 1) {
            // Just a single random object is being requested
            // get random value to skip
            var skipCount = Math.floor(Math.random() * (result + 1) );
             
            global.db.collection(collection)
                .find(queryOptions.select, {}, {limit:1, skip:skipCount})
                .toArray(function(err, result) {
                    if (result && result.length>0) {
                        callback(err, result[0]);
                    } else {
                        callback(err, null);
                    }
                });
        } else {
            // If the requested count exceeds the number of records in the database,
            // return the entire collection in random order
            if (randomObjCount > result) {
                randomObjCount = result;
            }
            // Now, create a set of random values to skip for each query
            var skipCountArr = [];
            for (i = 0; i < randomObjCount; i++) {
                skipCount = Math.floor(Math.random() * (result + 1) );
                while (skipCountArr.indexOf(skipCount) != -1) {
                    skipCount = Math.floor(Math.random() * (result + 1) );
                }
                skipCountArr[i] = skipCount;
            }
            console.log("The indices being fetched are:", skipCountArr);
            // Now loop around once again to do a .find on the DB and collect the
            // requested number of records
            var objArr = [];
            objToCollectCount = randomObjCount;
            for (i = 0; i < randomObjCount; i++) {
                global.db.collection(collection)
                    .find(queryOptions.select, {}, {limit:1, skip:skipCountArr[i]})
                    .toArray(function(err, result) {
                        if (result && result.length>0) {
                            objArr[randomObjCount-objToCollectCount] = result[0];
                            objToCollectCount--;
                            console.log("Number of documents found so far:", randomObjCount-objToCollectCount);
                            console.log("Number of documents still pending:", objToCollectCount);
                            if (objToCollectCount == 0) {
                                console.log("All documents found. The objArr is:", objArr);
                                callback(err, objArr)
                            }
                        } else {
                            callback(err, null);
                        }
                    });
            }
        }
    });
}

/*
 * defaults to doing a "set" operation on the document or subdocument.  an unset operation
 * can be specified by setting queryOptions.mode = "unset"<commit_msg>fixed a couple of bugs<commit_after>/**
 * Created by cellis on 12/21/14.
 */
var ObjectID = require('mongodb').ObjectID;

module.exports = new dbutils;

function dbutils() {};


dbutils.prototype.createDocument = function(collection, newDocument, callback) {

    global.db.collection(collection)
        .insert(newDocument, function(err, result) {
            //return the assigned id or null if nothing was inserted
            if (result && result.length > 0) {
                result = {_id:result[0]._id};
            } else {
                result = null;
            }
            callback(err, result);
        });
}

dbutils.prototype.findDocumentById = function(collection, docid, queryOptions, callback) {

    var selector = { _id: new ObjectID(docid) };

    //lookup doc in db
    global.db.collection(collection)
        .find(selector, {}, queryOptions.options)
        .toArray(function(err, result) {

            if (err) {
                callback(err, result);
            } else if (!result || result.length == 0) {
                callback("No document found for id: " + docid, null);
            } else {
                //we have something from the db, prep it and send it
                var resultObj = result[0];

                //prune the object if caller only requested a specific object
                if (queryOptions.fieldsArray && queryOptions.fieldsArray.length > 0) {

                    // caching the length and index for loop
                    var len = queryOptions.fieldsArray.length,
                        i = 0;

                    for (i = 0; i < len; i += 1) {
                        resultObj = resultObj[queryOptions.fieldsArray[i]];
                    }
                }

                //return the results
                callback(err, resultObj);
            }
        });

}

dbutils.prototype.findDocument = function(collection, queryOptions, callback) {

    //go look in mongo
    db.collection(collection)
        .find(queryOptions.select, {}, queryOptions.options)
        .toArray(function(err, result) {
            callback(err, result);
        });

}

// Method to keep track of the number of calls to the .collection.find method in
// in the function below
var objToCollectCount = 0;
//function objCollectCB(err, objRemainingCount, result) {
//}

dbutils.prototype.findRandomDocument = function(collection, queryOptions, callback) {

    // Check if more than one random number of documents have been requested
    console.log("From dbutils findRandomDocument queryOptions =", queryOptions);

    // Let us see how many random objects are being requested (the 'limit' parameter
    // specifies that).
    randomObjCount = queryOptions.options.limit
    
    // get a count of docs
    global.db.collection(collection).count(function(err, result) {
        // Check if the number of requested objects is 1 or more
        if (randomObjCount == 1) {
            // Just a single random object is being requested
            // get random value to skip
            var skipCount = Math.floor(Math.random() * result);
             
            global.db.collection(collection)
                .find(queryOptions.select, {}, {limit:1, skip:skipCount})
                .toArray(function(err, result) {
                    if (result && result.length>0) {
                        callback(err, result[0]);
                    } else {
                        callback(err, null);
                    }
                });
        } else {
            // If the requested count exceeds the number of records in the database,
            // return the entire collection in random order
            if (randomObjCount > result) {
                randomObjCount = result;
            }
            console.log("The total number of random entries being fetched are:", randomObjCount);
            // Now, create a set of random values to skip for each query
            var skipCountArr = [];
            for (i = 0; i < randomObjCount; i++) {
                skipCount = Math.floor(Math.random() * result);
                while (skipCountArr.indexOf(skipCount) != -1) {
                    skipCount = Math.floor(Math.random() * result);
                }
                skipCountArr[i] = skipCount;
            }
            console.log("The indices being fetched are:", skipCountArr);
            // Now loop around once again to do a .find on the DB and collect the
            // requested number of records
            var objArr = [];
            objToCollectCount = randomObjCount;
            for (i = 0; i < randomObjCount; i++) {
                console.log("Fetching document with skipCount:", skipCountArr[i]);
                global.db.collection(collection)
                    .find(queryOptions.select, {}, {limit:1, skip:skipCountArr[i]})
                    .toArray(function(err, result) {
                        if (result && result.length>0) {
                            objArr[randomObjCount-objToCollectCount] = result[0];
                            objToCollectCount--;
//                            console.log("Number of documents found so far:", randomObjCount-objToCollectCount);
//                            console.log("Number of documents still pending:", objToCollectCount);
                            console.log("Document fetched successfully. ID:", result[0]._id);
                        } else {
                            objArr[randomObjCount-objToCollectCount] = err;
                            objToCollectCount--;
                            console.log("Error encountered in fetching object. Error:", err);
                        }
                        if (objToCollectCount == 0) {
                            console.log("All documents found. The objArr is:", objArr);
                            callback(err, objArr)
                        }
                    });
            }
        }
    });
}

/*
 * defaults to doing a "set" operation on the document or subdocument.  an unset operation
 * can be specified by setting queryOptions.mode = "unset"
{'fraction_non_alphanumeric': 0.09857836043245084, 'fraction_numerical': 0.004937430833404273, 'mean_word_length': 1.5916611515552614, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '3783399', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2821, 'n_words': 864}