@harperfast/rocksdb-js 1.0.1 → 1.1.1

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/README.md CHANGED
@@ -54,6 +54,9 @@ Creates a new database instance.
54
54
  Defaults to `1`.
55
55
  - `pessimistic: boolean` When `true`, throws conflict errors when they occur instead of waiting
56
56
  until commit. Defaults to `false`.
57
+ - `readOnly: boolean` When `true`, the database is opened in read-only mode. Read operations are
58
+ permitted. Write operations will throw an error with code `ERR_DATABASE_READONLY`. Transactions
59
+ are a no-op in read-only mode.
57
60
  - `statsLevel: StatsLevel` Controls which type of statistics to skip and reduce statistic
58
61
  overhead. Defaults to `StatsLevel.ExceptDetailedTimers`.
59
62
  - `store: Store` A custom store that handles all interaction between the `RocksDatabase` or
@@ -82,6 +85,18 @@ const db = RocksDatabase.open('foo');
82
85
  db.close();
83
86
  ```
84
87
 
88
+ ### `db.columns: string[]`
89
+
90
+ Returns the list of column families in the RocksDB database.
91
+
92
+ ```typescript
93
+ const db = RocksDatabase.open('path/to/db');
94
+ console.log(db.columns); // ['default']
95
+
96
+ const db2 = new RocksDatabase('path/to/db', { name: 'users' });
97
+ console.log(db.columns); // ['default', 'users']
98
+ ```
99
+
85
100
  ### `db.config(options)`
86
101
 
87
102
  Sets global database settings.
@@ -287,24 +302,28 @@ await promise;
287
302
  console.log(db.getOldestSnapshotTimestamp()); // returns `0`, no snapshots
288
303
  ```
289
304
 
290
- ### `db.getDBProperty(propertyName: string): string`
305
+ ### `db.getDBProperty(propertyName: string): string | undefined`
291
306
 
292
307
  Gets a RocksDB database property as a string.
293
308
 
294
309
  - `propertyName: string` The name of the property to retrieve (e.g., ) `'rocksdb.levelstats'`.
295
310
 
311
+ Returns `undefined` if the property is not found.
312
+
296
313
  ```typescript
297
314
  const db = RocksDatabase.open('/path/to/database');
298
315
  const levelStats = db.getDBProperty('rocksdb.levelstats');
299
316
  const stats = db.getDBProperty('rocksdb.stats');
300
317
  ```
301
318
 
302
- ### `db.getDBIntProperty(propertyName: string): number`
319
+ ### `db.getDBIntProperty(propertyName: string): number | undefined`
303
320
 
304
321
  Gets a RocksDB database property as an integer.
305
322
 
306
323
  - `propertyName: string` The name of the property to retrieve (e.g., ) `'rocksdb.num-blob-files'`.
307
324
 
325
+ Returns `undefined` if the property is not found.
326
+
308
327
  ```typescript
309
328
  const db = RocksDatabase.open('/path/to/database');
310
329
  const blobFiles = db.getDBIntProperty('rocksdb.num-blob-files');
@@ -396,7 +415,7 @@ Synchronous version of `remove()`.
396
415
 
397
416
  ## Transactions
398
417
 
399
- ### `db.transaction<T>(callback: TransactionCallback<T>, options?: DBTransactionOptions): Promise<T>`
418
+ ### `db.transaction<T>(callback: TransactionCallback<T>, options?: TransactionOptions): Promise<T>`
400
419
 
401
420
  Executes all database operations within the specified callback within a single transaction. If the
402
421
  callback completes without error, the database operations are automatically committed. However, if
@@ -441,6 +460,24 @@ db.transactionSync((txn: Transaction) => {
441
460
  });
442
461
  ```
443
462
 
463
+ ### Optimistic and Pessimistic Modes
464
+
465
+ `rocksdb-js` supports two different transaction modes: optimistic and pessimistic. The default mode
466
+ is optimistic.
467
+
468
+ - Optimistic: Conflicts detected at commit time.
469
+ - Pessimistic: Conflicts throw immediately on detection.
470
+
471
+ When a database is opened in optimistic mode, transactions are not locked and can be retried if
472
+ they fail with a conflict. When a database is opened in pessimistic mode, transactions are aborted
473
+ and cannot be retried if they fail with a conflict.
474
+
475
+ Optimistic mode is the default mode and is recommended for most use cases. Pessimistic mode is
476
+ recommended for use cases where you need to know immediately if a conflict occurs.
477
+
478
+ If a database is opened in one mode, it cannot be opened in a different mode. An error will be
479
+ thrown when trying to open it in a different mode without closing the database first.
480
+
444
481
  ### `TransactionCallback<T>`
445
482
 
446
483
  `(txn: Transaction, attempt: number) => T | PromiseLike<T>`
@@ -486,7 +523,7 @@ operations methods as the `RocksDatabase` instance plus:
486
523
  - `txn.commitSync()` Synchronously commits and closes the transaction.
487
524
  - `txn.getTimestamp(): number` Retrieves the transaction start timestamp in seconds as a decimal. It
488
525
  defaults to the time at which the transaction was created.
489
- - `txn.id: number` The readonly transaction ID. Transaction IDs are unique to the RocksDB database
526
+ - `txn.id: number` The read-only transaction ID. Transaction IDs are unique to the RocksDB database
490
527
  path, regardless the database name/column family.
491
528
  - `txn.setTimestamp(ts?: number): void` Overrides the transaction start timestamp. If called without
492
529
  a timestamp, it will set the timestamp to the current time. The value must be in seconds with
@@ -662,18 +699,20 @@ const db = RocksDatabase.open('/path/to/db', {
662
699
  console.log(db.getStats());
663
700
  ```
664
701
 
665
- ### `db.getStat(statName: string): number`
702
+ ### `db.getStat(statName: string): RocksDBStat`
666
703
 
667
- Retrieves a single statistic value.
704
+ Retrieves a single statistic value. Return value is either a `number` or `StatsHistogramData`
705
+ object.
668
706
 
669
707
  ```typescript
670
708
  console.log(db.getStat('rocksdb.block.cache.miss'));
671
709
  ```
672
710
 
673
- ### `db.getStats(all?: boolean): Object<string, number | StatsHistogramData>`
711
+ ### `db.getStats(all?: boolean): RocksDBStats`
674
712
 
675
713
  Returns an object containing a curated list of column family-level properties, internal tickers
676
- stats, and internal histogram stats.
714
+ stats, and internal histogram stats. Return value is an object with the stat name as the key and
715
+ a `RocksDBStat` as the value.
677
716
 
678
717
  By default, it only returns the most meaningful internal stats. When `all = true`, it returns the
679
718
  same column family-level properties, but includes all internal tickers and histogram stats.
@@ -723,6 +762,14 @@ The `stats.StatsLevel` contains constants used to set which types of skip and re
723
762
  - `StatsLevel.ExceptTimeForMutex` Skip time waiting for mutex locks.
724
763
  - `StatsLevel.All` Collects all stats.
725
764
 
765
+ ### `type RocksDBStat = number | StatsHistogramData`
766
+
767
+ A `RocksDBStat` is either a `number` or `StatsHistogramData` object.
768
+
769
+ ### `type RocksDBStats = Record<string, RocksDBStat>`
770
+
771
+ A `RocksDBStats` is an object with the stat name as the key and a `RocksDBStat` as the value.
772
+
726
773
  ### `type StatsHistogramData`
727
774
 
728
775
  An object is a record with the following properties:
package/dist/index.cjs CHANGED
@@ -654,6 +654,11 @@ var Store = class {
654
654
  */
655
655
  pessimistic;
656
656
  /**
657
+ * Whether the database is open in readonly mode. When `true`, write
658
+ * operations will throw an error with code `ERR_DATABASE_READONLY`.
659
+ */
660
+ readOnly;
661
+ /**
657
662
  * Encoder specific flag used to signal that the encoder should use a random
658
663
  * access structure.
659
664
  */
@@ -722,6 +727,7 @@ var Store = class {
722
727
  this.parallelismThreads = options?.parallelismThreads ?? 1;
723
728
  this.path = path;
724
729
  this.pessimistic = options?.pessimistic ?? false;
730
+ this.readOnly = options?.readOnly ?? false;
725
731
  this.randomAccessStructure = options?.randomAccessStructure ?? false;
726
732
  this.readKey = readKey;
727
733
  this.sharedStructuresKey = options?.sharedStructuresKey;
@@ -792,6 +798,7 @@ var Store = class {
792
798
  const keyParam = getKeyParam(this.encodeKey(key));
793
799
  let flags = 0;
794
800
  if (alwaysCreateNewBuffer) flags |= ALWAYS_CREATE_NEW_BUFFER_FLAG;
801
+ if (this.readOnly) txnId = void 0;
795
802
  const result = context.getSync(keyParam, flags | ONLY_IF_IN_MEMORY_CACHE_FLAG, txnId);
796
803
  if (typeof result === "number") {
797
804
  if (result === NOT_IN_MEMORY_CACHE_FLAG) return new Promise((resolve, reject) => {
@@ -864,7 +871,7 @@ var Store = class {
864
871
  */
865
872
  getTxnId(options) {
866
873
  let txnId;
867
- if (options?.transaction) {
874
+ if (!this.readOnly && options?.transaction) {
868
875
  txnId = options.transaction.id;
869
876
  if (txnId === void 0) throw new TypeError("Invalid transaction");
870
877
  }
@@ -932,6 +939,7 @@ var Store = class {
932
939
  name: this.name,
933
940
  noBlockCache: this.noBlockCache,
934
941
  parallelismThreads: this.parallelismThreads,
942
+ readOnly: this.readOnly,
935
943
  statsLevel: this.statsLevel,
936
944
  transactionLogMaxAgeThreshold: this.transactionLogMaxAgeThreshold,
937
945
  transactionLogMaxSize: this.transactionLogMaxSize,
@@ -1047,9 +1055,17 @@ var Transaction = class extends DBI {
1047
1055
  * @param options - The options for the transaction.
1048
1056
  */
1049
1057
  constructor(store, options) {
1050
- const txn = new NativeTransaction(store.db, options);
1051
- super(store, txn);
1052
- this.#txn = txn;
1058
+ if (store.readOnly) {
1059
+ super(store);
1060
+ this.#txn = { id: 0 };
1061
+ this.abort = this.commitSync = this.setTimestamp = () => {};
1062
+ this.commit = async () => {};
1063
+ this.getTimestamp = () => 0;
1064
+ } else {
1065
+ const txn = new NativeTransaction(store.db, options);
1066
+ super(store, txn);
1067
+ this.#txn = txn;
1068
+ }
1053
1069
  }
1054
1070
  /**
1055
1071
  * Abort the transaction.
@@ -1203,6 +1219,12 @@ var RocksDatabase = class RocksDatabase extends DBI {
1203
1219
  this.store.close();
1204
1220
  }
1205
1221
  /**
1222
+ * Returns the list of column families in the RocksDB database.
1223
+ */
1224
+ get columns() {
1225
+ return this.store.db.columns;
1226
+ }
1227
+ /**
1206
1228
  * Set global database settings.
1207
1229
  *
1208
1230
  * @param options - The options for the database.
@@ -1379,6 +1401,12 @@ var RocksDatabase = class RocksDatabase extends DBI {
1379
1401
  return this.#name;
1380
1402
  }
1381
1403
  /**
1404
+ * Whether the database is open in readonly mode.
1405
+ */
1406
+ get readOnly() {
1407
+ return this.store.readOnly;
1408
+ }
1409
+ /**
1382
1410
  * Sugar method for opening a database.
1383
1411
  *
1384
1412
  * @param pathOrStore - The filesystem path to the database or a custom store.
@@ -1440,14 +1468,14 @@ var RocksDatabase = class RocksDatabase extends DBI {
1440
1468
  return buffer && store.decoder?.decode ? store.decoder.decode(buffer) : void 0;
1441
1469
  };
1442
1470
  opts.saveStructures = (structures, isCompatible) => {
1443
- return this.transactionSync((txn) => {
1471
+ return this.transactionSync((txn, _attempt) => {
1444
1472
  const existingStructuresBuffer = this.getBinarySync(sharedStructuresKey);
1445
1473
  const existingStructures = existingStructuresBuffer && store.decoder?.decode ? store.decoder.decode(existingStructuresBuffer) : void 0;
1446
1474
  if (typeof isCompatible == "function") {
1447
1475
  if (!isCompatible(existingStructures)) return false;
1448
1476
  } else if (existingStructures && existingStructures.length !== isCompatible) return false;
1449
1477
  txn.putSync(sharedStructuresKey, structures);
1450
- });
1478
+ }, { retryOnBusy: true });
1451
1479
  };
1452
1480
  }
1453
1481
  store.encoder = new EncoderClass({
@@ -1464,16 +1492,15 @@ var RocksDatabase = class RocksDatabase extends DBI {
1464
1492
  };
1465
1493
  store.decoder = store.encoder;
1466
1494
  }
1467
- if (typeof store.encoder?.writeKey === "function" && !store.encoder?.encode) {
1468
- store.encoder = {
1469
- ...store.encoder,
1470
- encode: (value, _mode) => {
1471
- const bytesWritten = store.writeKey(value, store.encodeBuffer, 0);
1472
- return store.encodeBuffer.subarray(0, bytesWritten);
1473
- }
1474
- };
1475
- store.encoder.copyBuffers = true;
1476
- }
1495
+ if (typeof store.encoder?.writeKey === "function" && !store.encoder?.encode) store.encoder = {
1496
+ ...store.encoder,
1497
+ encode: (value, _mode) => {
1498
+ const bytesWritten = store.writeKey(value, store.encodeBuffer, 0);
1499
+ return store.encodeBuffer.subarray(0, bytesWritten);
1500
+ },
1501
+ copyBuffers: true
1502
+ };
1503
+ if (store.encoder) store.encoder.name = this.#name;
1477
1504
  if (store.decoder && store.decoder.needsStableBuffer !== true) store.decoderCopies = true;
1478
1505
  if (store.decoder?.readKey && !store.decoder.decode) {
1479
1506
  store.decoder.decode = (buffer) => {
@@ -1909,7 +1936,7 @@ function loadLastPosition(transactionLog, readUncommitted) {
1909
1936
  //#region src/index.ts
1910
1937
  const versions = {
1911
1938
  rocksdb: version,
1912
- "rocksdb-js": "1.0.1"
1939
+ "rocksdb-js": "1.1.1"
1913
1940
  };
1914
1941
 
1915
1942
  //#endregion