<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

var util = require('util');

var deepEqual = require('deep-equal');
var once = require('once');
var vasync = require('vasync');
var clone = require('clone');

var common = require('./common');
var control = require('../control');
var objCommon = require('../objects/common');
require('../errors');



///--- Helpers

/**
 * Consolidate existing reindex data with new fields.
 *
 * @param {Object} old Existing reindex data
 * @param {Integer} ver New bucket version number
 * @param {Object} fields Added index fields, name->type map
 * @return {Object} Consolidated reindex data
 */
function consolidateReindex(old, ver, fields) {
    var result = clone(old);
    var cur = result[ver] || [];
    result[ver] = cur.concat(fields.filter(function (field) {
        return (cur.indexOf(field) === -1);
    }));
    return result;
}

///--- Handlers

function loadBucket(req, cb) {
    var b = req.bucket;
    var log = req.log;
    var pg = req.pg;
    var q;
    var row;
    var sql = util.format('SELECT * FROM buckets_config WHERE name=\'%s\'',
                          b.name);

    cb = once(cb);

    log.debug({
        bucket: b.name
    }, 'loadBucket: entered');

    q = pg.query(sql);
    q.once('error', function (err) {
        log.debug({
            bucket: b.name,
            err: err
        }, 'loadIndexes: failed');
        cb(err);
    });

    q.once('row', function (r) {
        row = r;
    });

    q.once('end', function (r) {
        if (!row) {
            cb(new BucketNotFoundError(req.bucket.name));
            return;
        }

        var v = parseInt(((JSON.parse(row.options) || {}).version || 0),
                         10);
        var v2 = req.bucket.options.version || 0;
        req.index = JSON.parse(row.index);

        // Needed for reindexing details
        req.raw_bucket = row;

        // Note v=0 is a legacy hack here that you get when you make a
        // bucket with no version. If you have the version set to zero
        // in the DB, we go ahead and overwrite regardless.
        if (v !== 0 && v >= v2) {
            log.error({
                bucket: b.name,
                oldVersion: v,
                newVersion: v2
            }, 'updateBucket: new version <= existing version');
            cb(new BucketVersionError(req.bucket.name, v, v2));
            return;
        }

        log.debug({<commit_msg>MORAY-307 moray message about updateBucket should not be an error<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

var util = require('util');

var deepEqual = require('deep-equal');
var once = require('once');
var vasync = require('vasync');
var clone = require('clone');

var common = require('./common');
var control = require('../control');
var objCommon = require('../objects/common');
require('../errors');



///--- Helpers

/**
 * Consolidate existing reindex data with new fields.
 *
 * @param {Object} old Existing reindex data
 * @param {Integer} ver New bucket version number
 * @param {Object} fields Added index fields, name->type map
 * @return {Object} Consolidated reindex data
 */
function consolidateReindex(old, ver, fields) {
    var result = clone(old);
    var cur = result[ver] || [];
    result[ver] = cur.concat(fields.filter(function (field) {
        return (cur.indexOf(field) === -1);
    }));
    return result;
}

///--- Handlers

function loadBucket(req, cb) {
    var b = req.bucket;
    var log = req.log;
    var pg = req.pg;
    var q;
    var row;
    var sql = util.format('SELECT * FROM buckets_config WHERE name=\'%s\'',
                          b.name);

    cb = once(cb);

    log.debug({
        bucket: b.name
    }, 'loadBucket: entered');

    q = pg.query(sql);
    q.once('error', function (err) {
        log.debug({
            bucket: b.name,
            err: err
        }, 'loadIndexes: failed');
        cb(err);
    });

    q.once('row', function (r) {
        row = r;
    });

    q.once('end', function (r) {
        if (!row) {
            cb(new BucketNotFoundError(req.bucket.name));
            return;
        }

        var v = parseInt(((JSON.parse(row.options) || {}).version || 0),
                         10);
        var v2 = req.bucket.options.version || 0;
        req.index = JSON.parse(row.index);

        // Needed for reindexing details
        req.raw_bucket = row;

        // Note v=0 is a legacy hack here that you get when you make a
        // bucket with no version. If you have the version set to zero
        // in the DB, we go ahead and overwrite regardless.
        if (v !== 0 && v >= v2) {
            log.warn({
                bucket: b.name,
                oldVersion: v,
                newVersion: v2
            }, 'updateBucket: new version <= existing version');
            cb(new BucketVersionError(req.bucket.name, v, v2));
            return;
        }

        log.debug({
{'fraction_non_alphanumeric': 0.14494108846081916, 'fraction_numerical': 0.007667851131475594, 'mean_word_length': 2.0507701083856245, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1032478', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1581, 'n_words': 510}