<commit_before>// (The MIT License)

// Copyright (c) 2012 Coradine Aviation Systems
// Copyright (c) 2012 <NAME>

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// 'Software'), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:

// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var events = require('events'),
    helpers = require('./helpers'),
    RObject = require('./robject'),
    async = require('async');

var Bucket = function Bucket(name, client) {
    this.name = name;
    this.props = {};
    this.resolver = Bucket.siblingLastWriteWins;
    this.getSiblingsSync = false;
    this.client = client;

    this.search = Object.create(Bucket.prototype.search, { // Establish namespace 'search'
        _this: { // used as a placeholder for namespaced functions, referenced using this._this in those functions.
            enumerable: false,
            configurable: true,
            writable: true,
            value: this
        }
    });

    this.objects = this.object = Object.create(Bucket.prototype.objects, { // Establish namespace 'objects' and 'object'
        _this: { // used as a placeholder for namespaced functions, referenced using this._this in those functions.
            enumerable: false,
            configurable: true,
            writable: true,
            value: this
        }
    });
};

Bucket.prototype.getProps = function(_return) {
    _return = _return !== undefined ? _return : function(){};
    var _this = this;

    _this.client._bucket.props(_this.name, function(err, props) {
        if(err) _return(err, props);
        else {
            _this.props = props;
            _return(err, _this.props);
        }
    });
    return this;
};

Bucket.prototype.saveProps = function(/* [merge], [_return] */) {
    var merge = typeof(arguments[0]) == 'boolean' ? arguments[0] : true;
    var _return = typeof(arguments[arguments.length - 1]) == 'function' ? arguments[arguments.length - 1] : function(){};

    var _this = this;

    if(merge === true) {
        _this.client._bucket.props(_this.name, function(err, props) {
            if(err) _return(err, props);
            else {
                _this.props = helpers.merge(props, _this.props);
                _this.client._bucket.save(_this.name, _this.props, function(err, response) {
                    if(err) _return(err, response);
                    else {
                        _return(err, _this);
                    }
                });
            }
        });
    }
    else {
        _this.client._bucket.save(_this.name, _this.props, function(err, response) {
            if(err) _return(err, response);
            else {
                _return(err, _this);
            }
        });
    }
    return this;
};


Bucket.prototype.search = {};
Bucket.prototype.search.solr = function(solr_query, _return) {
    var _this = this._this;

    if(_return) {
        _this.client._bucket.solr(_this.name, solr_query, function(err, results) {
            if(err) _return(err, results);
            else _return(err, results.data);
        });
    }
    else {
        return {
            stream: function(_return) {
                _this.client._bucket.solr(_this.name, solr_query, function(err, results) {
                    if(err) {
                        var emitter = new events.EventEmitter();
                        _return(emitter);
                        err.data = results;
                        emitter.emit('error', err);
                        emitter.removeAllListeners();
                    }
                    else {
                        var compiled_keys = [];
                        for(var i = 0, length = results.data.response.docs.length; i < length; i++) {
                            compiled_keys.push(results.data.response.docs[i].id);
                        }
                        _this.objects.get(compiled_keys).stream(_return);
                    }
                });
            }
        };
    }
    return _this;
};

Bucket.prototype.search.twoi = function(twoi_query, index, options, _return) {
    twoi_query = twoi_query instanceof Array ? twoi_query : [twoi_query];
    _return = typeof(options) === 'function' ? options : _return;
    options = typeof(options) === 'object' ? options : {};

    var _this = this._this;

    if(index !== '$key') { // handle $key special case. Issue #
        index = typeof(twoi_query[0]) === 'number' && twoi_query[0].toString().search(/[^0-9]/) === -1 ? index+'_int' : index+'_bin';
    }

    if(_return) {  // don't return stream
        _this.client._bucket.twoi(_this.name, twoi_query, index, options, function(err, results) {
            if(err) _return(err, results);
            else {
                _return(err, helpers.removeDuplicates(results));
            }
        });
    }
    else { // return stream
        return {
            stream: function(_return) {
                var loc_continuation = undefined;
                var emitter = new events.EventEmitter();<commit_msg>Fixed 2i continuation being missing.<commit_after>// (The MIT License)

// Copyright (c) 2012 Coradine Aviation Systems
// Copyright (c) 2012 <NAME>

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// 'Software'), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:

// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var events = require('events'),
    helpers = require('./helpers'),
    RObject = require('./robject'),
    async = require('async');

var Bucket = function Bucket(name, client) {
    this.name = name;
    this.props = {};
    this.resolver = Bucket.siblingLastWriteWins;
    this.getSiblingsSync = false;
    this.client = client;

    this.search = Object.create(Bucket.prototype.search, { // Establish namespace 'search'
        _this: { // used as a placeholder for namespaced functions, referenced using this._this in those functions.
            enumerable: false,
            configurable: true,
            writable: true,
            value: this
        }
    });

    this.objects = this.object = Object.create(Bucket.prototype.objects, { // Establish namespace 'objects' and 'object'
        _this: { // used as a placeholder for namespaced functions, referenced using this._this in those functions.
            enumerable: false,
            configurable: true,
            writable: true,
            value: this
        }
    });
};

Bucket.prototype.getProps = function(_return) {
    _return = _return !== undefined ? _return : function(){};
    var _this = this;

    _this.client._bucket.props(_this.name, function(err, props) {
        if(err) _return(err, props);
        else {
            _this.props = props;
            _return(err, _this.props);
        }
    });
    return this;
};

Bucket.prototype.saveProps = function(/* [merge], [_return] */) {
    var merge = typeof(arguments[0]) == 'boolean' ? arguments[0] : true;
    var _return = typeof(arguments[arguments.length - 1]) == 'function' ? arguments[arguments.length - 1] : function(){};

    var _this = this;

    if(merge === true) {
        _this.client._bucket.props(_this.name, function(err, props) {
            if(err) _return(err, props);
            else {
                _this.props = helpers.merge(props, _this.props);
                _this.client._bucket.save(_this.name, _this.props, function(err, response) {
                    if(err) _return(err, response);
                    else {
                        _return(err, _this);
                    }
                });
            }
        });
    }
    else {
        _this.client._bucket.save(_this.name, _this.props, function(err, response) {
            if(err) _return(err, response);
            else {
                _return(err, _this);
            }
        });
    }
    return this;
};


Bucket.prototype.search = {};
Bucket.prototype.search.solr = function(solr_query, _return) {
    var _this = this._this;

    if(_return) {
        _this.client._bucket.solr(_this.name, solr_query, function(err, results) {
            if(err) _return(err, results);
            else _return(err, results.data);
        });
    }
    else {
        return {
            stream: function(_return) {
                _this.client._bucket.solr(_this.name, solr_query, function(err, results) {
                    if(err) {
                        var emitter = new events.EventEmitter();
                        _return(emitter);
                        err.data = results;
                        emitter.emit('error', err);
                        emitter.removeAllListeners();
                    }
                    else {
                        var compiled_keys = [];
                        for(var i = 0, length = results.data.response.docs.length; i < length; i++) {
                            compiled_keys.push(results.data.response.docs[i].id);
                        }
                        _this.objects.get(compiled_keys).stream(_return);
                    }
                });
            }
        };
    }
    return _this;
};

Bucket.prototype.search.twoi = function(twoi_query, index, options, _return) {
    twoi_query = twoi_query instanceof Array ? twoi_query : [twoi_query];
    _return = typeof(options) === 'function' ? options : _return;
    options = typeof(options) === 'object' ? options : {};

    var _this = this._this;

    if(index !== '$key') { // handle $key special case. Issue #
        index = typeof(twoi_query[0]) === 'number' && twoi_query[0].toString().search(/[^0-9]/) === -1 ? index+'_int' : index+'_bin';
    }

    if(_return) {  // don't return stream
        _this.client._bucket.twoi(_this.name, twoi_query, index, options, function(err, results, continuation) {
            if(err) _return(err, results);
            else {
                _return(err, helpers.removeDuplicates(results), continuation);
            }
        });
    }
    else { // return stream
        return {
            stream: function(_return) {
                var emitter = new events.EventEmitter();
{'fraction_non_alphanumeric': 0.11631841637763303, 'fraction_numerical': 0.003130022840707216, 'mean_word_length': 2.045337454920144, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5848925', 'n_tokens_mistral': 3279, 'n_tokens_neox': 3129, 'n_words': 908}