<commit_before>var util = require('util');
var utils = require('./utils');
var async = require('async');
var Transform = require('stream').Transform;
var AbstractIterator = require('abstract-leveldown').AbstractIterator;

function RiakIterator(db, options) {
    AbstractIterator.call(this, db);

    options = JSON.parse(JSON.stringify(options));

    this._bucket = options.bucket || db._bucket;
    this._reverse = !!options.reverse;
    this._keyAsBuffer = !!options.keyAsBuffer;
    this._valueAsBuffer = !!options.valueAsBuffer;

    var low, high;

    if (options.hasOwnProperty('lt') && options.lt !== null && options.lt !== '') {
        high = utils.decrement(options.lt);
    } else if (options.hasOwnProperty('lte') && options.lte !== null && options.lte !== '') {
        high = utils.roundDown(options.lte);
    }

    if (typeof high === 'undefined' || high === null || high === '') {
        high = utils.roundDown(this._reverse ? options.start : options.end);
    }

    if (typeof high === 'undefined' || high === null || high === '') {
        high = '~';
    }

    if (options.hasOwnProperty('gt') && options.gt !== null && options.gt !== '') {
        low = utils.increment(options.gt);
    } else if (options.hasOwnProperty('gte') && options.gte !== null && options.gte !== '') {
        low = utils.roundUp(options.gte);
    }

    if (typeof low === 'undefined' || low === null || low === '') {
        low = utils.roundUp(this._reverse ? options.end : options.start);
    }

    if (typeof low === 'undefined' || low === null || low === '') {
        low = '!';
    }

    if (this._reverse) {
        while (low.length < high.length) {
            low += '!';
        }

        this._start = utils.reverseString(high);
        this._end = utils.reverseString(low);
    } else {
        while (high.length < low.length) {
            high += '~';
        }

        this._end = high;
        this._start = low;
    }

    var query = {
        bucket: this._bucket,
        qtype: 1,
        range_min: this._start,
        range_max: this._end,
        pagination_sort: true
    };

    if (options.index) {
        query.index = this._reverse ? '_reverse_' + options.index : options.index;
    } else {
        query.index = this._reverse ? '_reverse_key_bin' : '$key';
    }

    if (options.limit > 0) {
        query.max_results = options.limit;
    }

    var keyTransform = new Transform({ objectMode: true });
    keyTransform._transform = function (chunk, encoding, next) {
        if (!chunk.keys) {
            return next();
        }<commit_msg>if key is an int, multiply by -1 instead of reverseString<commit_after>var util = require('util');
var utils = require('./utils');
var async = require('async');
var Transform = require('stream').Transform;
var AbstractIterator = require('abstract-leveldown').AbstractIterator;

function RiakIterator(db, options) {
    AbstractIterator.call(this, db);

    options = JSON.parse(JSON.stringify(options));

    this._bucket = options.bucket || db._bucket;
    this._reverse = !!options.reverse;
    this._keyAsBuffer = !!options.keyAsBuffer;
    this._valueAsBuffer = !!options.valueAsBuffer;

    var low, high;

    if (options.hasOwnProperty('lt') && options.lt !== null && options.lt !== '') {
        high = utils.decrement(options.lt);
    } else if (options.hasOwnProperty('lte') && options.lte !== null && options.lte !== '') {
        high = utils.roundDown(options.lte);
    }

    if (typeof high === 'undefined' || high === null || high === '') {
        high = utils.roundDown(this._reverse ? options.start : options.end);
    }

    if (typeof high === 'undefined' || high === null || high === '') {
        high = '~';
    }

    if (options.hasOwnProperty('gt') && options.gt !== null && options.gt !== '') {
        low = utils.increment(options.gt);
    } else if (options.hasOwnProperty('gte') && options.gte !== null && options.gte !== '') {
        low = utils.roundUp(options.gte);
    }

    if (typeof low === 'undefined' || low === null || low === '') {
        low = utils.roundUp(this._reverse ? options.end : options.start);
    }

    if (typeof low === 'undefined' || low === null || low === '') {
        low = '!';
    }

    var query = {
        bucket: this._bucket,
        qtype: 1,
        pagination_sort: true
    };

    if (options.index) {
        query.index = this._reverse ? '_reverse_' + options.index : options.index;
    } else {
        query.index = this._reverse ? '_reverse_key_bin' : '$key';
    }

    if (options.limit > 0) {
        query.max_results = options.limit;
    }

    var keyIsString = (/_bin$/.test(query.index) || query.index === '$key') ? true : false;

    if (this._reverse) {
        if (keyIsString) {
            while (low.length < high.length) {
                low += '!';
            }
        }

        query.range_min = keyIsString ? utils.reverseString(high) : -1 * high;
        query.range_max = keyIsString ? utils.reverseString(low) : -1 * low;
    } else {
        if (keyIsString) {
            while (high.length < low.length) {
                high += '~';
            }
        }

        query.range_max = high;
        query.range_min = low;
    }

    var keyTransform = new Transform({ objectMode: true });
    keyTransform._transform = function (chunk, encoding, next) {
        if (!chunk.keys) {
            return next();
        }
{'fraction_non_alphanumeric': 0.16912850812407682, 'fraction_numerical': 0.001292466765140325, 'mean_word_length': 2.381398252184769, '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': '1112353', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1613, 'n_words': 353}