"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RocksIndexStore = void 0;
const rocksdb_js_1 = require("@harperfast/rocksdb-js");
const ordered_binary_1 = require("ordered-binary");
/**
 * A specialized RocksDB-based index store that maintains indexed references to primary keys.
 * This store uses composite keys consisting of indexed values and primary keys, enabling
 * efficient range queries over indexed data. The actual data values are stored as null since
 * this is purely an index structure pointing to primary records elsewhere. This extends
 * RocksDatabase rather than a store because it actually alters the interface
 */
class RocksIndexStore extends rocksdb_js_1.RocksDatabase {
    /**
     * Get all entries matching the range
     * @param options
     */
    getRange(options) {
        let { start, end, exclusiveStart, inclusiveEnd, reverse } = options;
        if ((reverse ? !exclusiveStart : exclusiveStart) && start !== undefined) {
            start = [start, ordered_binary_1.MAXIMUM_KEY];
        }
        if ((reverse ? !inclusiveEnd : inclusiveEnd) && end !== undefined) {
            end = [end, ordered_binary_1.MAXIMUM_KEY];
        }
        const translatedOptions = { ...options, start, end };
        return super.getRange(translatedOptions).map(({ key }) => {
            return { key: key[0], value: key.length > 2 ? key.slice(1) : key[1] };
        });
    }
    /**
     * Translate a put with indexed value and primary key to an underlying put
     * @param indexedValue - ignored, only used by LMDB
     * @param primaryKey
     * @param txnId
     */
    put(indexedValue, primaryKey, options) {
        return super.putSync([indexedValue, primaryKey], null, options);
    }
    putSync(indexedValue, primaryKey, options) {
        return super.putSync([indexedValue, primaryKey], null, options);
    }
    remove(indexedValue, primaryKey, options) {
        return super.removeSync([indexedValue, primaryKey], options);
    }
    removeSync(indexedValue, primaryKey, options) {
        super.removeSync([indexedValue, primaryKey], options);
    }
}
exports.RocksIndexStore = RocksIndexStore;
/**
 * Add `getValuesCount` to the DBI prototype which is used by the `RocksDatabase` and `Transaction`
 * classes.
 */
rocksdb_js_1.DBI.prototype.getValuesCount = function getValuesCount(indexedValue) {
    if (this instanceof RocksIndexStore) {
        return this.store.getCount(this._context, { start: indexedValue, end: [indexedValue, ordered_binary_1.MAXIMUM_KEY] });
    }
    throw new Error('getValuesCount is only supported if dupSort=true');
};
//# sourceMappingURL=RocksIndexStore.js.map