'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenDBIObject = void 0;
const envMngr = __importStar(require("../environment/environmentManager.js"));
const terms = __importStar(require("../../utility/hdbTerms.js"));
const RecordEncoder_ts_1 = require("../../resources/RecordEncoder.js");
envMngr.initSync();
const LMDB_CACHING = envMngr.get(terms.CONFIG_PARAMS.STORAGE_CACHING) !== false;
/**
 * Defines how a DBI will be created/opened
 */
class OpenDBIObject {
    dupSort;
    encoding;
    useVersions;
    sharedStructuresKey;
    compression;
    cache;
    freezeData;
    encoder;
    randomAccessStructure;
    /**
     * @param {Boolean} dupSort - if the dbi allows duplicate keys
     * @param {Boolean} [isPrimary] - if the dbi is the primary dbi
     */
    constructor(dupSort, isPrimary = false) {
        /** @type {boolean} */
        this.dupSort = dupSort === true;
        /** @type {"string" | "json" | "binary" | "msgpack" | "ordered-binary"} */
        this.encoding = dupSort ? 'ordered-binary' : 'msgpack';
        /** @type {boolean} */
        this.useVersions = isPrimary;
        /** @type {Symbol} */
        this.sharedStructuresKey = Symbol.for('structures');
        /** @type {any} */
        this.compression = undefined;
        this.encoder = { Encoder: RecordEncoder_ts_1.RecordEncoder };
        // Only enable struct (random-access) encoding on primary DBIs. Struct headers occupy
        // the 0x20-0x3f range, which readers without struct support (msgpackr v1 / Harper v4)
        // decode as positive fixints. v4 likewise gated struct mode behind is_primary, so
        // non-primary stores (e.g. the __dbis__ metadata DBI) stay in records mode and remain
        // decodable after a downgrade. RecordEncoder still reads struct data so existing v5
        // struct entries decode.
        // As of 5.1 the primary default is itself opt-in via storage.randomAccessFields (default
        // off; overridable per-table via databases.ts before the store opens). Typed structures key
        // on per-field value WIDTH, so wide/variably-typed schemas can mint an unbounded dictionary
        // (OOM) and diverge across replicas. Read the config here at construction — not at module
        // import — so an env/CLI override applied during startup is honored (DBIs open after config
        // is finalized).
        this.randomAccessStructure = isPrimary && envMngr.get(terms.CONFIG_PARAMS.STORAGE_RANDOMACCESSFIELDS) === true;
        if (isPrimary) {
            this.cache = LMDB_CACHING && { validated: true };
            this.freezeData = true;
        }
    }
}
exports.OpenDBIObject = OpenDBIObject;
//# sourceMappingURL=OpenDBIObject.js.map