<commit_before>// Copyright 2012 Joyent, Inc.  All rights reserved.

var assert = require('assert');

var ldap = require('ldapjs');


///--- Globals

var parseDN = ldap.parseDN;


///--- Functions


function operationsError(err) {
    var msg = err && err.message ? err.message : '';
    return new ldap.OperationsError('Moray failure: ' + msg,
                                    null, operationsError);
}


function _error(err, req) {
    switch (err.name) {
    case 'ObjectNotFoundError':
        return new ldap.NoSuchObjectError(req ? req.dn.toString() : '');
    case 'UniqueAttributeError':
        return new ldap.ConstraintViolationError(err.message);
    default:
        return operationsError(err);
    }
}


function _exists(req) {
    return function exists(bucket, key, callback) {
        var client = req.moray,
            log = req.log,
            opts = { req_id: req.req_id };

        log.debug({bucket: bucket, key: key}, 'exists entered');
        return client.getObject(bucket, key, opts, function (err, obj) {
            if (err) {
                if (err.name === 'ObjectNotFoundError') {
                    return callback(null, false);
                }

                return callback(operationsError(err));
            }
            return callback(null, true);
        });
    };
}


function _get(req) {
    return function get(bucket, key, callback) {
        var client = req.moray,
            log = req.log,
            opts = { req_id: req.req_id };

        log.debug({bucket: bucket, key: key}, 'get entered');
        return client.getObject(bucket, key, opts, function (err, obj) {
            if (err) {
                return callback(_error(err, req));
            }

            log.debug({bucket: bucket, key: key, val: obj}, 'get done');
            return callback(null, obj);
        });
    };
}


function _put(req) {
    return function put(bucket, key, value, meta, callback) {
        if (typeof (meta) === 'function') {
            callback = meta;
            meta = {};
        }

        var client = req.moray,
            log = req.log,
            opts = {
                match: meta.etag,
                req_id: req.req_id,
                headers: meta.headers || {}
            };

        opts.headers['x-ufds-changelog-bucket'] = req.config.changelog.bucket;

        log.debug({bucket: bucket, key: key, opts: opts}, 'put entered');
        return client.putObject(bucket, key, value, opts, function (err) {
            if (err) {
                return callback(_error(err, req));
            }

            log.debug({bucket: bucket, key: key, val: value}, 'put done');
            return callback(null);
        });
    };
}


function _del(req) {
    return function del(bucket, key, meta, callback) {
        if (typeof (meta) === 'function') {
            callback = meta;
            meta = {};
        }

        var client = req.moray,
            log = req.log,
            opts = {
                match: meta.etag,
                req_id: req.req_id,
                headers: meta.headers || {}
            };

        opts.headers['x-ufds-changelog-bucket'] = req.config.changelog.bucket;

        log.debug({bucket: bucket, key: key, opts: opts}, 'del entered');
        return client.delObject(bucket, key, opts, function (err) {
            if (err) {
                return _error(err, req);
            }

            log.debug({bucket: bucket, key: key}, 'del done');
            return callback(null);
        });
    };
}


function _search(req) {
    return function search(bucket, filter, callback) {
        // Changelog needs special massage:
        var clog = (req.config.changelog &&
                bucket === req.config.changelog.bucket);

        var client = req.moray;
        var log = req.log;
        var opts = { req_id: req.req_id };

        if (clog) {
            // If we are searching changelog by "changenumber", we need to
            // replace it with an "_id":
            if (/changenumber/.test(filter)) {
                filter = filter.replace(/changenumber/g, '_id');
            }

            // If we are searching changelog by "changetime", we need to go
            // for millisecs since epoch instead of ISO Date
            /* JSSTYLED */
            var RE = /changetime([<\=|>\=|\=|~\=]+)(\d{4}\-\d{2}\-\d{2}(T\d{2}\:\d{2}\:\d{2}(\.\d{1,3})*Z)*)/;
            var d;
            var rest;
            if ((rest = RE.exec(filter)) !== null) {
                d = new Date(rest[2]).getTime();
                filter = filter.replace(RE, 'changetime' + rest[1] + d);
            }
        }

        var r = client.findObjects(bucket, filter, opts);
        var results = {};

        log.debug({bucket: bucket, filter: filter}, 'search entered');

        r.once('error', function (err) {
            return callback(err);
        });

        r.on('record', function (obj) {
            if (clog) {<commit_msg>CAPI-277: Need explicit "ORDER BY _id ASC" when multiple indexes are given to changelog search queries<commit_after>// Copyright 2012 Joyent, Inc.  All rights reserved.

var assert = require('assert');

var ldap = require('ldapjs');


///--- Globals

var parseDN = ldap.parseDN;


///--- Functions


function operationsError(err) {
    var msg = err && err.message ? err.message : '';
    return new ldap.OperationsError('Moray failure: ' + msg,
                                    null, operationsError);
}


function _error(err, req) {
    switch (err.name) {
    case 'ObjectNotFoundError':
        return new ldap.NoSuchObjectError(req ? req.dn.toString() : '');
    case 'UniqueAttributeError':
        return new ldap.ConstraintViolationError(err.message);
    default:
        return operationsError(err);
    }
}


function _exists(req) {
    return function exists(bucket, key, callback) {
        var client = req.moray,
            log = req.log,
            opts = { req_id: req.req_id };

        log.debug({bucket: bucket, key: key}, 'exists entered');
        return client.getObject(bucket, key, opts, function (err, obj) {
            if (err) {
                if (err.name === 'ObjectNotFoundError') {
                    return callback(null, false);
                }

                return callback(operationsError(err));
            }
            return callback(null, true);
        });
    };
}


function _get(req) {
    return function get(bucket, key, callback) {
        var client = req.moray,
            log = req.log,
            opts = { req_id: req.req_id };

        log.debug({bucket: bucket, key: key}, 'get entered');
        return client.getObject(bucket, key, opts, function (err, obj) {
            if (err) {
                return callback(_error(err, req));
            }

            log.debug({bucket: bucket, key: key, val: obj}, 'get done');
            return callback(null, obj);
        });
    };
}


function _put(req) {
    return function put(bucket, key, value, meta, callback) {
        if (typeof (meta) === 'function') {
            callback = meta;
            meta = {};
        }

        var client = req.moray,
            log = req.log,
            opts = {
                match: meta.etag,
                req_id: req.req_id,
                headers: meta.headers || {}
            };

        opts.headers['x-ufds-changelog-bucket'] = req.config.changelog.bucket;

        log.debug({bucket: bucket, key: key, opts: opts}, 'put entered');
        return client.putObject(bucket, key, value, opts, function (err) {
            if (err) {
                return callback(_error(err, req));
            }

            log.debug({bucket: bucket, key: key, val: value}, 'put done');
            return callback(null);
        });
    };
}


function _del(req) {
    return function del(bucket, key, meta, callback) {
        if (typeof (meta) === 'function') {
            callback = meta;
            meta = {};
        }

        var client = req.moray,
            log = req.log,
            opts = {
                match: meta.etag,
                req_id: req.req_id,
                headers: meta.headers || {}
            };

        opts.headers['x-ufds-changelog-bucket'] = req.config.changelog.bucket;

        log.debug({bucket: bucket, key: key, opts: opts}, 'del entered');
        return client.delObject(bucket, key, opts, function (err) {
            if (err) {
                return _error(err, req);
            }

            log.debug({bucket: bucket, key: key}, 'del done');
            return callback(null);
        });
    };
}


function _search(req) {
    return function search(bucket, filter, callback) {
        // Changelog needs special massage:
        var clog = (req.config.changelog &&
                bucket === req.config.changelog.bucket);

        var client = req.moray;
        var log = req.log;
        var opts = { req_id: req.req_id };

        if (clog) {
            // If we are searching changelog by "changenumber", we need to
            // replace it with an "_id":
            if (/changenumber/.test(filter)) {
                filter = filter.replace(/changenumber/g, '_id');
            }

            // CAPI-277: Redundant sorting value, just to make sure PostgreSQL
            // is searching and sorting as it should if multiple search indexes
            // are given:
            opts.sort =  {
                attribute: '_id',
                order: 'ASC'
            };

            // If we are searching changelog by "changetime", we need to go
            // for millisecs since epoch instead of ISO Date
            /* JSSTYLED */
            var RE = /changetime([<\=|>\=|\=|~\=]+)(\d{4}\-\d{2}\-\d{2}(T\d{2}\:\d{2}\:\d{2}(\.\d{1,3})*Z)*)/;
            var d;
            var rest;
            if ((rest = RE.exec(filter)) !== null) {
                d = new Date(rest[2]).getTime();
                filter = filter.replace(RE, 'changetime' + rest[1] + d);
            }
        }

        var r = client.findObjects(bucket, filter, opts);
        var results = {};

        log.debug({bucket: bucket, filter: filter}, 'search entered');

        r.once('error', function (err) {
            return callback(err);
        });

        r.on('record', function (obj) {
            if (clog) {
{'fraction_non_alphanumeric': 0.13935383417672245, 'fraction_numerical': 0.0033086804203970414, 'mean_word_length': 1.6638154484188699, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '716489', 'n_tokens_mistral': 3041, 'n_tokens_neox': 2926, 'n_words': 768}