@harperfast/rocksdb-js 2.7.1 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -369,7 +369,9 @@ interface StoreOptions extends Omit<NativeDatabaseOptions, "compression" | "comp
369
369
  randomAccessStructure?: boolean;
370
370
  /**
371
371
  * When `true`, the database is opened in read-only mode. Write operations
372
- * will throw an error with code `ERR_DATABASE_READONLY`.
372
+ * will throw an error with code `ERR_DATABASE_READONLY`, except for
373
+ * `flush()`/`flushSync()`/`compact()`/`compactSync()`, which succeed as
374
+ * no-ops.
373
375
  */
374
376
  readOnly?: boolean;
375
377
  sharedStructuresKey?: symbol;
@@ -550,7 +552,9 @@ declare class Store {
550
552
  pessimistic: boolean;
551
553
  /**
552
554
  * Whether the database is open in readonly mode. When `true`, write
553
- * operations will throw an error with code `ERR_DATABASE_READONLY`.
555
+ * operations will throw an error with code `ERR_DATABASE_READONLY`, except
556
+ * for `flush()`/`flushSync()`/`compact()`/`compactSync()`, which succeed as
557
+ * no-ops.
554
558
  */
555
559
  readOnly: boolean;
556
560
  /**
@@ -577,8 +581,8 @@ declare class Store {
577
581
  */
578
582
  transactionLogMaxAgeThreshold?: number;
579
583
  /**
580
- * The maximum size of a transaction log before it is rotated to the next
581
- * sequence number.
584
+ * The target maximum size of a transaction log before rotation. Transactions
585
+ * are never split across files, so one transaction may exceed this target.
582
586
  */
583
587
  transactionLogMaxSize?: number;
584
588
  /**
@@ -686,6 +690,13 @@ declare class Store {
686
690
  */
687
691
  encodeValue(value: any): BufferWithDataView | Uint8Array;
688
692
  get(context: StoreContext, key: Key, alwaysCreateNewBuffer?: boolean, options?: StoreGetOptions): any | undefined;
693
+ /**
694
+ * Estimates the number of keys in a range from RocksDB statistics
695
+ * (memtable stats + approximate SST sizes with range-local entry density)
696
+ * without iterating. Estimates always reflect committed state, so there is
697
+ * no transactional variant.
698
+ */
699
+ estimateCount(options?: CountEstimateOptions): CountEstimate;
689
700
  getCount(context: StoreContext, options?: StoreRangeOptions): number;
690
701
  getKeys(context: StoreContext, options?: StoreIteratorOptions): any | undefined;
691
702
  getKeysCount(context: StoreContext, options?: StoreRangeOptions): number;
@@ -930,6 +941,21 @@ interface RocksDBOptions {
930
941
  */
931
942
  tailing?: boolean;
932
943
  }
944
+ interface CountEstimate {
945
+ /**
946
+ * The estimated number of keys.
947
+ */
948
+ count: number;
949
+ /**
950
+ * Heuristic 0–1 indicator of how trustworthy `count` is; exactly 1 only
951
+ * when the count is exact. Derived from the estimate's resolution
952
+ * (SST data-block / memtable sampling granularity relative to the count),
953
+ * the tombstone fraction of the overlapping SSTs, and — for open-ended
954
+ * starts — the error compounded by complement subtraction. A heuristic
955
+ * ordering signal, not a statistical bound.
956
+ */
957
+ confidence: number;
958
+ }
933
959
  interface RangeOptions extends RocksDBOptions {
934
960
  /**
935
961
  * The range end key, otherwise known as the "upper bound". Defaults to
@@ -952,6 +978,13 @@ interface RangeOptions extends RocksDBOptions {
952
978
  */
953
979
  start?: Key | Uint8Array;
954
980
  }
981
+ interface CountEstimateOptions extends RangeOptions {
982
+ /**
983
+ * Interpret `start` as the upper bound and `end` as the lower bound, as
984
+ * `getRange()` does for reverse iteration. Defaults to `false`.
985
+ */
986
+ reverse?: boolean;
987
+ }
955
988
  interface IteratorOptions extends RangeOptions {
956
989
  /**
957
990
  * The maximum number of entries to yield. When set, iteration stops after
@@ -1232,6 +1265,7 @@ type StatsBasics = {
1232
1265
  "rocksdb.num-live-versions": number;
1233
1266
  "rocksdb.current-super-version-number": number;
1234
1267
  "rocksdb.oldest-snapshot-time": number;
1268
+ "rocksdb.num-snapshots": number;
1235
1269
  "rocksdb.num-blob-files": number;
1236
1270
  "rocksdb.total-blob-file-size": number;
1237
1271
  "rocksdb.live-blob-file-size": number;
@@ -1495,6 +1529,34 @@ type StatsAllExtras = {
1495
1529
  "rocksdb.prefetch.memory.bytes.granted": number;
1496
1530
  "rocksdb.prefetch.memory.bytes.released": number;
1497
1531
  "rocksdb.prefetch.memory.requests.blocked": number;
1532
+ "rocksdb.wal.precreate.hit": number;
1533
+ "rocksdb.wal.precreate.miss": number;
1534
+ "rocksdb.wal.precreate.waited": number;
1535
+ "rocksdb.wal.precreate.wait.micros": number;
1536
+ "rocksdb.wal.precreate.failed": number;
1537
+ "rocksdb.rate.limiter.bytes.read": number;
1538
+ "rocksdb.rate.limiter.bytes.write": number;
1539
+ "rocksdb.rate.limiter.requests.read": number;
1540
+ "rocksdb.rate.limiter.requests.write": number;
1541
+ "rocksdb.rate.limiter.delayed.requests.read": number;
1542
+ "rocksdb.rate.limiter.delayed.requests.write": number;
1543
+ "rocksdb.rate.limiter.total.wait.micros.read": number;
1544
+ "rocksdb.rate.limiter.total.wait.micros.write": number;
1545
+ "rocksdb.read.path.range.tombstones.inserted": number;
1546
+ "rocksdb.read.path.range.tombstones.discarded": number;
1547
+ "rocksdb.file.open.metadata.retrieved": number;
1548
+ "rocksdb.file.open.metadata.passed": number;
1549
+ "rocksdb.manifest.validation.failure.count": number;
1550
+ "rocksdb.flush.reason.write_buffer_full": number;
1551
+ "rocksdb.flush.reason.write_buffer_manager": number;
1552
+ "rocksdb.flush.reason.memtable_max_range_deletions": number;
1553
+ "rocksdb.atomic_flush.request.reason.write_buffer_full": number;
1554
+ "rocksdb.atomic_flush.request.reason.write_buffer_manager": number;
1555
+ "rocksdb.atomic_flush.request.reason.memtable_max_range_deletions": number;
1556
+ "rocksdb.atomic_flush.request.reason.other": number;
1557
+ "rocksdb.checkpoint.read.bytes": number;
1558
+ "rocksdb.checkpoint.write.bytes": number;
1559
+ "rocksdb.file.submit.async.read.fallback": number;
1498
1560
  "rocksdb.compaction.times.cpu_micros": StatsHistogramData;
1499
1561
  "rocksdb.subcompaction.setup.times.micros": StatsHistogramData;
1500
1562
  "rocksdb.table.sync.micros": StatsHistogramData;
@@ -1555,6 +1617,18 @@ type StatsAllExtras = {
1555
1617
  "rocksdb.multiscan.prepare.micros": StatsHistogramData;
1556
1618
  "rocksdb.multiscan.blocks.per.prepare": StatsHistogramData;
1557
1619
  "rocksdb.block.key.distribution.cv": StatsHistogramData;
1620
+ "rocksdb.ingest.external.file.prepare.micros": StatsHistogramData;
1621
+ "rocksdb.ingest.external.file.run.micros": StatsHistogramData;
1622
+ "rocksdb.iodispatcher.async.read.observed.completion.micros": StatsHistogramData;
1623
+ "rocksdb.iodispatcher.async.read.poll.wait.micros": StatsHistogramData;
1624
+ "rocksdb.iodispatcher.async.read.prefetch.lead.micros": StatsHistogramData;
1625
+ "rocksdb.rate.limiter.wait.micros.read": StatsHistogramData;
1626
+ "rocksdb.rate.limiter.wait.micros.write": StatsHistogramData;
1627
+ "rocksdb.flush.memtable.memory.bytes": StatsHistogramData;
1628
+ "rocksdb.flush.memtable.total.data.size.bytes": StatsHistogramData;
1629
+ "rocksdb.flush.write_buffer_full.memtable.memory.bytes": StatsHistogramData;
1630
+ "rocksdb.flush.write_buffer_manager.memtable.memory.bytes": StatsHistogramData;
1631
+ "rocksdb.open.and.compact.db.open.micros": StatsHistogramData;
1558
1632
  };
1559
1633
  type StatsCurated = StatsBasics & StatsCuratedExtras;
1560
1634
  type StatsAll = StatsCurated & StatsAllExtras;
@@ -1675,6 +1749,69 @@ interface ValidateTransactionLogStoreOptions {
1675
1749
  declare function validateTransactionLogStore(path: string, options?: ValidateTransactionLogStoreOptions): Promise<TransactionLogStoreValidation>;
1676
1750
  //#endregion
1677
1751
  //#region src/load-binding.d.ts
1752
+ /**
1753
+ * The `Error` subclass passed to `'error'` event listeners and returned by
1754
+ * `db.getLastError()` when RocksDB reports a background error (e.g. a write
1755
+ * failing at the filesystem level). A real `Error` instance (`instanceof Error`
1756
+ * and `instanceof BackgroundError` both hold) with the fields below. Only a
1757
+ * hard-or-worse error (`writesDisabled`) stops writes; a soft error is
1758
+ * auto-recoverable. When `writesDisabled` is `true` and the underlying condition
1759
+ * has cleared, call {@link RocksDatabase.resume}. See HarperFast/rocksdb-js#730.
1760
+ *
1761
+ * The runtime constructor is defined natively and exported as
1762
+ * {@link BackgroundError}; the same name is both a value (for `instanceof`) and
1763
+ * this instance type.
1764
+ *
1765
+ * @example
1766
+ * ```typescript
1767
+ * db.on('error', (err: BackgroundError) => {
1768
+ * if (err.writesDisabled) console.error(`writes disabled: ${err.message}`);
1769
+ * });
1770
+ * ```
1771
+ */
1772
+ interface BackgroundError extends Error {
1773
+ /** Always `'BackgroundError'`. */
1774
+ name: string;
1775
+ /** Discriminator for the error class; always `'background'` here. */
1776
+ type: string;
1777
+ /**
1778
+ * The RocksDB `Status::Severity` as a number: 1 soft, 2 hard, 3 fatal,
1779
+ * 4 unrecoverable.
1780
+ */
1781
+ severity: number;
1782
+ /** Human-readable severity: `'soft'`, `'hard'`, `'fatal'`, or `'unrecoverable'`. */
1783
+ severityName: string;
1784
+ /**
1785
+ * Whether RocksDB has disabled writes on the database in response to this
1786
+ * error (severity hard or worse, i.e. `severity >= 2`). Only when `true` is
1787
+ * {@link RocksDatabase.resume} warranted; a soft error auto-recovers and
1788
+ * leaves writes enabled. Distinct from opening the database in read-only
1789
+ * mode — this is RocksDB halting writes after a background failure.
1790
+ */
1791
+ writesDisabled: boolean;
1792
+ /**
1793
+ * The RocksDB `BackgroundErrorReason` as a number, present when the error
1794
+ * originated from a reason-bearing callback (flush, compaction, etc.).
1795
+ */
1796
+ reason?: number;
1797
+ /** Human-readable reason, e.g. `'flush'` or `'compaction'`. */
1798
+ reasonName?: string;
1799
+ }
1800
+ /**
1801
+ * The shape accepted by `db.setLastError(...)` to inject or reset a background
1802
+ * error. `message` is required; the rest default/omit as with a real error.
1803
+ * `type` defaults to `'background'`. Pass `null`/nothing to `setLastError` to
1804
+ * clear instead of an object of this shape.
1805
+ */
1806
+ type BackgroundErrorOptions = {
1807
+ message: string;
1808
+ severity?: number;
1809
+ severityName?: string;
1810
+ writesDisabled?: boolean;
1811
+ reason?: number;
1812
+ reasonName?: string;
1813
+ type?: string;
1814
+ };
1678
1815
  type NativeTransactionOptions = {
1679
1816
  /**
1680
1817
  * Whether to disable snapshots.
@@ -1708,7 +1845,7 @@ type NativeTransaction = {
1708
1845
  putSync(key: Key, value: Buffer | Uint8Array, txnId?: number): void;
1709
1846
  removeSync(key: Key): void;
1710
1847
  setTimestamp(timestamp?: number): void;
1711
- useLog(name: string | number): TransactionLog;
1848
+ useLog(name: string | number, jsDatabase: NativeDatabase): TransactionLog;
1712
1849
  };
1713
1850
  type LogBuffer = Buffer & {
1714
1851
  dataView: DataView;
@@ -1915,6 +2052,47 @@ type PurgedLog = {
1915
2052
  path: string;
1916
2053
  entries: number;
1917
2054
  };
2055
+ type FlushOptions = {
2056
+ /**
2057
+ * Whether the flush may proceed even though it will stall writes for its duration.
2058
+ *
2059
+ * Maps to `rocksdb::FlushOptions::allow_write_stall`. RocksDB's default is `false`, which
2060
+ * means the opposite of what the name suggests on first read: the flush **waits** until it
2061
+ * can run without causing a stall. That wait has no timeout, so a database sitting in a stall
2062
+ * condition — immutable-memtable backlog, L0 stop trigger, pending-compaction-bytes limit, an
2063
+ * exhausted WriteBufferManager budget — blocks the caller for as long as the condition lasts.
2064
+ * `flush()` runs on the libuv threadpool, so there the wait shows up as a promise that simply
2065
+ * never settles while the event loop stays alive — and it parks that whole worker, not just
2066
+ * the caller's promise: `UV_THREADPOOL_SIZE` defaults to 4, so a handful of concurrently
2067
+ * stalled flushes can exhaust the pool and stall every unrelated `fs`/`dns`/`crypto` call and
2068
+ * cold-cache `get()` in the process, not only this database's own operations. `flushSync()`
2069
+ * takes the same wait on the JS thread, so it is the worse of the two here rather than the
2070
+ * safer one: it freezes the event loop outright instead of parking a pool worker, and it holds
2071
+ * the in-flight operation claim that `close()` waits on for the duration.
2072
+ *
2073
+ * Pass `true` when the flush is a durability gate the caller is blocked on and stalling
2074
+ * writers is the acceptable cost of it completing — decide that up front, before issuing the
2075
+ * call, since there is no way to cancel an in-flight `flush()` and turn `true` into an escape
2076
+ * hatch after the fact. It is also not a free way out of an existing stall: forcing the
2077
+ * memtable switch adds another L0 file to the level whose file count may be *causing* the
2078
+ * stop trigger, so it can prolong the condition it is meant to clear. Weigh the cost
2079
+ * database-wide, not per-caller: a flush covers **every column family** on the database, and
2080
+ * the descriptor is process-global and shared across `worker_threads`, so the stall lands on
2081
+ * every other column family and every other handle that opened the same path — not just the
2082
+ * one you called. It also relocates the hang rather than removing it: a stalled `db->Write()`
2083
+ * blocks the database's single `CommitWorker` thread, which dispatches every
2084
+ * `Transaction.commit()` in order, so the stall queues up every commit behind it — including
2085
+ * ones from callers that never touched flush — until the stall clears.
2086
+ *
2087
+ * Note this is a *different* knob from the `writeBufferManagerAllowStall` config, and their
2088
+ * polarity is nearly opposite: that one decides whether the WriteBufferManager may stall
2089
+ * writers at all, this one decides whether a manual flush is willing to cause a stall rather
2090
+ * than wait one out.
2091
+ *
2092
+ * @default false
2093
+ */
2094
+ allowWriteStall?: boolean;
2095
+ };
1918
2096
  type NativeDatabase = {
1919
2097
  new (): NativeDatabase;
1920
2098
  addListener(event: string, callback: (...args: any[]) => void): void;
@@ -1933,15 +2111,21 @@ type NativeDatabase = {
1933
2111
  destroy(): void;
1934
2112
  drop(resolve: ResolveCallback<void>, reject: RejectCallback): void;
1935
2113
  dropSync(): void;
1936
- flush(resolve: ResolveCallback<void>, reject: RejectCallback): void;
1937
- flushSync(): void;
2114
+ flush(resolve: ResolveCallback<void>, reject: RejectCallback, options?: FlushOptions): void;
2115
+ flushSync(options?: FlushOptions): void;
1938
2116
  notify(event: string | BufferWithDataView, args?: any[]): boolean;
1939
2117
  get(keyLengthOrKeyBuffer: number | Buffer, resolve: ResolveCallback<Buffer | number>, reject: RejectCallback, txnId?: number, expectedVersion?: number): number;
2118
+ estimateCount(startKey?: Buffer, endKey?: Buffer): {
2119
+ count: number;
2120
+ confidence: number;
2121
+ };
1940
2122
  getCompression(): {
1941
2123
  algorithm: string;
1942
2124
  level?: number;
1943
2125
  };
1944
2126
  getCount(options?: RangeOptions, txnId?: number): number;
2127
+ getLastError(): BackgroundError | null;
2128
+ setLastError(error?: BackgroundErrorOptions | null): void;
1945
2129
  getDBIntProperty(propertyName: string): number | undefined;
1946
2130
  getDBProperty(propertyName: string): string | undefined;
1947
2131
  getLogOptions(): {
@@ -1971,12 +2155,13 @@ type NativeDatabase = {
1971
2155
  putSync(key: BufferWithDataView, value: any, txnId?: number): void;
1972
2156
  removeListener(event: string | BufferWithDataView, callback: () => void): boolean;
1973
2157
  removeSync(key: BufferWithDataView, txnId?: number): void;
2158
+ resume(): void;
1974
2159
  setDefaultKeyBuffer(buffer: Buffer | Uint8Array | null): void;
1975
2160
  setDefaultValueBuffer(buffer: Buffer | Uint8Array | null): void;
1976
2161
  setIteratorState(buffer: Buffer | Uint8Array): void;
1977
2162
  tryLock(key: BufferWithDataView, callback?: () => void): boolean;
1978
2163
  unlock(key: BufferWithDataView): void;
1979
- useLog(name: string): TransactionLog;
2164
+ useLog(name: string, jsDatabase?: NativeDatabase): TransactionLog;
1980
2165
  verifyVersion(keyLengthOrKeyBuffer: number | Buffer, version: number): boolean;
1981
2166
  withLock(key: BufferWithDataView, callback: () => void | Promise<void>): Promise<void>;
1982
2167
  };
@@ -2026,23 +2211,62 @@ type RocksDatabaseConfig = {
2026
2211
  */
2027
2212
  writeBufferManagerAllowStall?: boolean;
2028
2213
  };
2214
+ type RegistryStatusTransaction = {
2215
+ /** The transaction id assigned by the database descriptor. */
2216
+ id: number;
2217
+ /** Milliseconds since the transaction handle was created. */
2218
+ ageMs: number;
2219
+ };
2029
2220
  type RegistryStatusDB = {
2030
2221
  path: string;
2031
2222
  refCount: number;
2032
2223
  columnFamilies: string[];
2033
2224
  transactions: number;
2225
+ /**
2226
+ * One entry per live transaction handle. An `ageMs` beyond any plausible request lifetime,
2227
+ * against a nonzero `rocksdb.num-snapshots`, identifies a handle holding back reclamation for
2228
+ * its whole database.
2229
+ */
2230
+ transactionDetails: RegistryStatusTransaction[];
2034
2231
  closables: number;
2035
2232
  locks: number;
2036
2233
  userSharedBuffers: number;
2037
2234
  listenerCallbacks: number;
2038
2235
  };
2039
2236
  type RegistryStatus = RegistryStatusDB[];
2237
+ /**
2238
+ * The native `BackgroundError` constructor (a real `Error` subclass). Exported
2239
+ * as both a value — for `err instanceof BackgroundError` — and, via declaration
2240
+ * merging with the interface above, a type. Instances are produced by the
2241
+ * `'error'` event and `db.getLastError()`; consumers rarely construct their own.
2242
+ * The `details` param is required and typed to the instance fields so a bare
2243
+ * `new BackgroundError()` (which would omit `severity` / `writesDisabled` / …)
2244
+ * is a type error.
2245
+ */
2246
+ declare const BackgroundError: new (details: Pick<BackgroundError, "message" | "severity" | "severityName" | "writesDisabled" | "reason" | "reasonName"> & {
2247
+ type?: string;
2248
+ }) => BackgroundError;
2040
2249
  declare const constants: {
2041
2250
  ALWAYS_CREATE_NEW_BUFFER_FLAG: number;
2042
2251
  NOT_IN_MEMORY_CACHE_FLAG: number;
2043
2252
  ONLY_IF_IN_MEMORY_CACHE_FLAG: number;
2044
2253
  POPULATE_VERSION_FLAG: number;
2045
2254
  FRESH_VERSION_FLAG: number;
2255
+ /**
2256
+ * Producer flag in a value's metadata word (4 big-endian bytes at offset 8, top byte `0x0E`,
2257
+ * low 24 bits flags), declaring that this version does NOT uniquely identify the value —
2258
+ * the producer has stored more than one distinct value under it.
2259
+ *
2260
+ * Set it and the VerificationTable stops treating version equality as evidence for this value:
2261
+ * a read never answers `FRESH_VERSION_FLAG` for it and never publishes its version to a slot,
2262
+ * so a consumer holding a differing cached copy at that version cannot have it confirmed.
2263
+ * Clear it again on the next write that gives the value a version of its own.
2264
+ *
2265
+ * Read only from values in a column family that opted into the verification table. Do not call
2266
+ * `populateVersion()` for a marked version: that explicit primitive never sees the value and
2267
+ * cannot enforce this flag.
2268
+ */
2269
+ VERSION_NOT_UNIQUE_FLAG: number;
2046
2270
  /**
2047
2271
  * Sentinel value resolved (not rejected) by `commit()` when
2048
2272
  * `coordinatedRetry: true` and the transaction encountered an IsBusy
@@ -2153,6 +2377,57 @@ interface BackupStreamOptions {
2153
2377
  transactionLogs?: boolean;
2154
2378
  }
2155
2379
  //#endregion
2380
+ //#region src/count-estimator.d.ts
2381
+ interface CountEstimatorOptions extends CountEstimateOptions {}
2382
+ /**
2383
+ * Progressively refines a range key-count estimate as an iterator traverses
2384
+ * the range. The estimate starts as the pure statistical estimate
2385
+ * (`estimateCount`) and converges toward the exact count: the traversed
2386
+ * portion is exact, and the statistical estimate of the untraversed remainder
2387
+ * is calibrated by the observed ratio of actual-to-estimated entries over the
2388
+ * portion already traversed.
2389
+ *
2390
+ * The estimator never touches the iterator itself — the caller reports
2391
+ * progress with `advance(lastKey, count)` whenever it wants a checkpoint
2392
+ * (e.g. once per page), then reads `estimate()`. The caller owns the
2393
+ * progress contract: cursors must move monotonically through the range and
2394
+ * each entry must be reported once (a re-reported page inflates the count
2395
+ * undetectably). When traversal completes, call `finish()` so `estimate()`
2396
+ * returns the exact total instead of adding a block-granular remainder.
2397
+ *
2398
+ * Each `estimate()` checkpoint queries RocksDB statistics for the two range
2399
+ * segments (cost scales with the SSTs overlapping them, not with key count);
2400
+ * results are memoized per checkpoint, so repeated reads between `advance()`
2401
+ * calls are free.
2402
+ */
2403
+ declare class CountEstimator {
2404
+ #private;
2405
+ constructor(store: Store, options?: CountEstimatorOptions);
2406
+ /**
2407
+ * The number of entries reported traversed so far.
2408
+ */
2409
+ get traversed(): number;
2410
+ /**
2411
+ * Records that iteration has advanced through `count` more entries, ending
2412
+ * at `lastKey`. Pass `count: 0` when an empty page has no last key.
2413
+ */
2414
+ advance(lastKey: Key | Uint8Array | undefined, count?: number): void;
2415
+ /**
2416
+ * Marks traversal of the range as complete: `estimate()` becomes the exact
2417
+ * traversed count.
2418
+ */
2419
+ finish(): void;
2420
+ /**
2421
+ * Estimates the total number of entries in the full range: the exact
2422
+ * traversed count plus a calibrated statistical estimate of the remainder.
2423
+ * `confidence` is the exactness-weighted blend of the traversed portion
2424
+ * (exact) and the remainder's statistical confidence, so it approaches 1
2425
+ * as the exact portion grows (though a checkpoint can decrease when
2426
+ * calibration makes a large correction), and is exactly 1 after `finish()`.
2427
+ */
2428
+ estimate(): CountEstimate;
2429
+ }
2430
+ //#endregion
2156
2431
  //#region src/database.d.ts
2157
2432
  type TransactionCallback<T> = (txn: Transaction, attempt: number) => T | PromiseLike<T>;
2158
2433
  interface RocksDatabaseOptions extends StoreOptions {
@@ -2229,6 +2504,9 @@ declare class RocksDatabase extends DBI<DBITransactional> {
2229
2504
  * Compacts the entire key range of the database asynchronously.
2230
2505
  * This triggers manual compaction which removes tombstones and reclaims space.
2231
2506
  *
2507
+ * On a read-only database this is a no-op: arguments are still validated, but
2508
+ * the returned promise resolves without compacting rather than rejecting.
2509
+ *
2232
2510
  * @example
2233
2511
  * ```typescript
2234
2512
  * const db = RocksDatabase.open('/path/to/database');
@@ -2299,6 +2577,9 @@ declare class RocksDatabase extends DBI<DBITransactional> {
2299
2577
  * Compacts the entire key range of the database synchronously.
2300
2578
  * This triggers manual compaction which removes tombstones and reclaims space.
2301
2579
  *
2580
+ * On a read-only database this validates its arguments and then returns
2581
+ * without compacting, rather than throwing.
2582
+ *
2302
2583
  * @example
2303
2584
  * ```typescript
2304
2585
  * const db = RocksDatabase.open('/path/to/database');
@@ -2400,15 +2681,90 @@ declare class RocksDatabase extends DBI<DBITransactional> {
2400
2681
  /**
2401
2682
  * Flushes the underlying database by performing a commit or clearing any buffered operations.
2402
2683
  *
2684
+ * On a read-only database this is a no-op: the options bag is still validated, but the
2685
+ * returned promise resolves without flushing rather than rejecting.
2686
+ *
2687
+ * @param options - Flush options; see {@link FlushOptions.allowWriteStall} before relying on
2688
+ * this resolving promptly under write pressure.
2403
2689
  * @return {void} Does not return a value.
2404
2690
  */
2405
- flush(): Promise<void>;
2691
+ flush(options?: FlushOptions): Promise<void>;
2406
2692
  /**
2407
2693
  * Synchronously flushes the underlying database by performing a commit or clearing any buffered operations.
2408
2694
  *
2695
+ * The default `allowWriteStall: false` wait is taken on the JS thread here, so it freezes the
2696
+ * event loop rather than parking a libuv worker — a larger blast radius than {@link flush}, not
2697
+ * a smaller one.
2698
+ *
2699
+ * On a read-only database this validates its options and then returns without flushing, rather
2700
+ * than throwing.
2701
+ *
2702
+ * @param options - Flush options; see {@link FlushOptions.allowWriteStall}.
2409
2703
  * @return {void} Does not return a value.
2410
2704
  */
2411
- flushSync(): void;
2705
+ flushSync(options?: FlushOptions): void;
2706
+ /**
2707
+ * Returns the most recent {@link BackgroundError} observed on this database, or
2708
+ * `null` when none has occurred. The `'error'` event is the push counterpart;
2709
+ * this is the pull equivalent for on-demand checks (e.g. a health probe) and
2710
+ * for catching an error that fired after open but before a listener was
2711
+ * attached. (A background error that occurs *during* the initial open is a
2712
+ * narrow startup window not yet captured, addressed separately.)
2713
+ *
2714
+ * Purely historical — it is not cleared by a successful {@link RocksDatabase.resume}.
2715
+ * When the returned error's `writesDisabled` is `true`, writes are stopped
2716
+ * until recovery.
2717
+ *
2718
+ * @example
2719
+ * ```typescript
2720
+ * const err = db.getLastError();
2721
+ * if (err?.writesDisabled) {
2722
+ * // ...free disk space...
2723
+ * db.resume();
2724
+ * }
2725
+ * ```
2726
+ */
2727
+ getLastError(): BackgroundError | null;
2728
+ /**
2729
+ * Sets or clears the last background error, mirroring the Win32
2730
+ * `SetLastError`/`GetLastError` pair. Pass `null` (or no argument) to **reset**
2731
+ * it — {@link RocksDatabase.getLastError} then returns `null` and no event
2732
+ * fires; this is the intended way to clear the error after handling or
2733
+ * recovering it (e.g. after {@link RocksDatabase.resume}). Pass an object to
2734
+ * set it: it is stored and the `'error'` event fires with the reconstructed
2735
+ * {@link BackgroundError}. `type` defaults to `'background'`.
2736
+ *
2737
+ * @example
2738
+ * ```typescript
2739
+ * db.resume();
2740
+ * db.setLastError(null); // recovered — clear the historical error
2741
+ * ```
2742
+ */
2743
+ setLastError(error?: BackgroundErrorOptions | null): void;
2744
+ /**
2745
+ * Attempts to recover the database from a background error by calling
2746
+ * RocksDB's `DB::Resume()`. When a write fails at the filesystem level (e.g. a
2747
+ * full disk), RocksDB stops accepting writes and this database emits an
2748
+ * {@link BackgroundError} via the `'error'` event. Call `resume()` after the
2749
+ * underlying condition has cleared — e.g. once disk space has been freed. On
2750
+ * success writes are accepted again; on failure (the condition has not
2751
+ * actually cleared) it throws and the database stays read-only. A no-op on a
2752
+ * healthy database.
2753
+ *
2754
+ * Runs synchronously and may briefly block, since recovery can re-flush
2755
+ * memtables.
2756
+ *
2757
+ * @example
2758
+ * ```typescript
2759
+ * db.on('error', (err) => {
2760
+ * if (err.writesDisabled) {
2761
+ * // ...free disk space...
2762
+ * db.resume();
2763
+ * }
2764
+ * });
2765
+ * ```
2766
+ */
2767
+ resume(): void;
2412
2768
  /**
2413
2769
  * Gets a RocksDB database property as an integer.
2414
2770
  *
@@ -2438,7 +2794,9 @@ declare class RocksDatabase extends DBI<DBITransactional> {
2438
2794
  */
2439
2795
  getDBProperty(propertyName: string): string | undefined;
2440
2796
  /**
2441
- * Retrieves the estimated number of keys in the database.
2797
+ * Retrieves the estimated number of keys in the database. This is an alias
2798
+ * for `db.estimateCount().count`; use `estimateCount()` for range support
2799
+ * and a confidence indicator.
2442
2800
  *
2443
2801
  * @example
2444
2802
  * ```typescript
@@ -2448,6 +2806,54 @@ declare class RocksDatabase extends DBI<DBITransactional> {
2448
2806
  * ```
2449
2807
  */
2450
2808
  getEstimatedKeyCount(): number;
2809
+ /**
2810
+ * Estimates the number of keys in the database, or within a key range,
2811
+ * returning `{ count, confidence }`. Unlike `getKeysCount()`, this never
2812
+ * iterates: the estimate is derived from RocksDB statistics (memtable
2813
+ * stats plus approximate SST sizes converted through the entry density of
2814
+ * the SSTs overlapping the range), so its cost scales with the number of
2815
+ * SSTs overlapping the range rather than the number of keys — though
2816
+ * reading table properties for cold files can do I/O through the table
2817
+ * cache, and a start-only range does the work of its complement below
2818
+ * `start`. Accuracy improves with range size. Resolution is bounded by SST
2819
+ * data-block granularity, so tiny ranges may over-report or report zero for
2820
+ * present keys; low `confidence` is the signal. Recently deleted or
2821
+ * overwritten entries may be counted until compaction. An inverted range
2822
+ * (`start` >= `end`) returns `{ count: 0, confidence: 1 }`.
2823
+ *
2824
+ * `confidence` is a heuristic 0–1 trust indicator (1 only when exact) —
2825
+ * see `CountEstimate`. Estimates always reflect committed state; writes
2826
+ * pending in a transaction are not included.
2827
+ *
2828
+ * @example
2829
+ * ```typescript
2830
+ * const db = RocksDatabase.open('/path/to/database');
2831
+ * const { count, confidence } = db.estimateCount({ start: 'a', end: 'z' });
2832
+ * ```
2833
+ */
2834
+ estimateCount(options?: CountEstimateOptions): CountEstimate;
2835
+ /**
2836
+ * Creates a `CountEstimator` for progressively refining a range count
2837
+ * estimate while iterating the range: report progress with
2838
+ * `advance(lastKey, count)` (e.g. once per page of results) and
2839
+ * `estimate()` returns the exact traversed count plus a calibrated
2840
+ * estimate of the remainder, converging toward the exact total. Call
2841
+ * `finish()` when traversal completes to make `estimate()` exact.
2842
+ *
2843
+ * @example
2844
+ * ```typescript
2845
+ * const estimator = db.createCountEstimator({ start: 'a', end: 'z' });
2846
+ * let lastKey;
2847
+ * let pageSize = 0;
2848
+ * for (const { key } of db.getRange({ start: 'a', end: 'z', limit: 25 })) {
2849
+ * lastKey = key;
2850
+ * pageSize++;
2851
+ * }
2852
+ * estimator.advance(lastKey, pageSize);
2853
+ * const { count, confidence } = estimator.estimate();
2854
+ * ```
2855
+ */
2856
+ createCountEstimator(options?: CountEstimatorOptions): CountEstimator;
2451
2857
  /**
2452
2858
  * Returns the current timestamp as a monotonically increasing timestamp in
2453
2859
  * milliseconds represented as a decimal number.
@@ -2722,11 +3128,33 @@ declare function parseTransactionLog(path: string, options?: {
2722
3128
  skipData?: boolean;
2723
3129
  }): TransactionLog$1;
2724
3130
  //#endregion
3131
+ //#region src/transaction-log-reader.d.ts
3132
+ /**
3133
+ * Thrown when an entry's framing is broken. `resyncPosition` is the offset where valid framing
3134
+ * resumes, when one was found: the frames after a mid-log break are intact and reachable, and
3135
+ * only the broken span between `position` and `resyncPosition` is unreadable. It is `undefined`
3136
+ * for a torn tail, where nothing follows the break.
3137
+ *
3138
+ * The iterator is left positioned at `resyncPosition` (or at end-of-log when there is none), so a
3139
+ * caller that chooses to recover the entries past the break calls `next()` again.
3140
+ */
3141
+ declare class CorruptFrameError extends RangeError {
3142
+ /** Sequence number of the log file the break is in. */
3143
+ logId: number;
3144
+ /** Offset of the broken frame. */
3145
+ position: number;
3146
+ /** Offset where valid framing resumes, or `undefined` when nothing valid follows. */
3147
+ resyncPosition?: number;
3148
+ /** Bytes between the break and the resume point that cannot be read. */
3149
+ unreadableBytes: number;
3150
+ constructor(message: string, logId: number, position: number, resyncPosition: number | undefined);
3151
+ }
3152
+ //#endregion
2725
3153
  //#region src/index.d.ts
2726
3154
  declare const versions: {
2727
3155
  rocksdb: string;
2728
3156
  "rocksdb-js": string;
2729
3157
  };
2730
3158
  //#endregion
2731
- export { type BackupInfo, type BackupOptions, type BackupStreamOptions, type CompressionAlgorithm, type CompressionInfo, type CompressionOption, DBI, DBIterator, type GetStatsMethod, type IteratorOptions, type Key, type LogOptions, type RestoreMode, type RestoreOptions, type RocksDBStat, type RocksDBStats, RocksDatabase, type RocksDatabaseOptions, type StatsAll, type StatsAllExtras, type StatsBasics, type StatsCurated, type StatsCuratedExtras, type StatsDefault, type StatsHistogramData, type StatsValue, Store, type StoreContext, type StoreGetOptions, type StoreIteratorOptions, type StorePutOptions, type StoreRangeOptions, type StoreRemoveOptions, Transaction, type TransactionEntry, TransactionLog, type TransactionLogFileValidation, type TransactionLogPosition, type TransactionLogStats, type TransactionLogStoreValidation, type ValidateTransactionLogStoreOptions, backups, constants, coolTransactionLogs, currentThreadId, fileLockRelease, parseTransactionLog, registryStatus, shutdown, stats, supportedCompression, tryFileLock, validateTransactionLogStore, versions };
3159
+ export { BackgroundError, type BackgroundErrorOptions, type BackupInfo, type BackupOptions, type BackupStreamOptions, type CompressionAlgorithm, type CompressionInfo, type CompressionOption, CorruptFrameError, type CountEstimate, type CountEstimateOptions, CountEstimator, type CountEstimatorOptions, DBI, DBIterator, type FlushOptions, type GetStatsMethod, type IteratorOptions, type Key, type LogOptions, type RestoreMode, type RestoreOptions, type RocksDBStat, type RocksDBStats, RocksDatabase, type RocksDatabaseOptions, type StatsAll, type StatsAllExtras, type StatsBasics, type StatsCurated, type StatsCuratedExtras, type StatsDefault, type StatsHistogramData, type StatsValue, Store, type StoreContext, type StoreGetOptions, type StoreIteratorOptions, type StorePutOptions, type StoreRangeOptions, type StoreRemoveOptions, Transaction, type TransactionEntry, TransactionLog, type TransactionLogFileValidation, type TransactionLogPosition, type TransactionLogStats, type TransactionLogStoreValidation, type ValidateTransactionLogStoreOptions, backups, constants, coolTransactionLogs, currentThreadId, fileLockRelease, parseTransactionLog, registryStatus, shutdown, stats, supportedCompression, tryFileLock, validateTransactionLogStore, versions };
2732
3160
  //# sourceMappingURL=index.d.cts.map