@harperfast/rocksdb-js 0.1.8 → 0.1.9

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
@@ -10,6 +10,7 @@ A Node.js binding for the RocksDB library.
10
10
  - Transaction log system for recording transaction related data
11
11
  - Custom stores provide ability to override default database interactions
12
12
  - Efficient binary key and value encoding
13
+ - Access to internal RocksDB statistics
13
14
  - Designed for Node.js and Bun on Linux, macOS, and Windows
14
15
 
15
16
  ## Example
@@ -42,7 +43,10 @@ Creates a new database instance.
42
43
  - `path: string` The path to write the database files to. This path does not need to exist, but the
43
44
  parent directories do.
44
45
  - `options: object` [optional]
45
- - `disableWAL: boolean` Whether to disable the RocksDB write ahead log.
46
+ - `disableWAL: boolean` Whether to disable the RocksDB write ahead log. Defaults to `false`.
47
+ - `enableStats: boolean` When `true` and the database is open, RocksDB will captures stats that
48
+ are retrieved by calling `db.getStats()`. Enabling statistics imposes 5-10% in overhead.
49
+ Defaults to `false`.
46
50
  - `name: string` The column family name. Defaults to `"default"`.
47
51
  - `noBlockCache: boolean` When `true`, disables the block cache. Block caching is enabled by
48
52
  default and the cache is shared across all database instances.
@@ -50,6 +54,8 @@ Creates a new database instance.
50
54
  Defaults to `1`.
51
55
  - `pessimistic: boolean` When `true`, throws conflict errors when they occur instead of waiting
52
56
  until commit. Defaults to `false`.
57
+ - `statsLevel: StatsLevel` Controls which type of statistics to skip and reduce statistic
58
+ overhead. Defaults to `StatsLevel.ExceptDetailedTimers`.
53
59
  - `store: Store` A custom store that handles all interaction between the `RocksDatabase` or
54
60
  `Transaction` instances and the native database interface. See [Custom Store](#custom-store) for
55
61
  more information.
@@ -599,6 +605,99 @@ db.notify(1234);
599
605
  db.notify({ key: 'bar' }, { value: 'baz' });
600
606
  ```
601
607
 
608
+ ## Statistics
609
+
610
+ Retrieve RocksDB statistics at runtime. You must set `enableStats: true` when calling `db.open()`.
611
+ Statistics are captured at the database level and include all column families.
612
+
613
+ RocksDB has two types of statistics: tickers and histograms. Tickers are 64-bit unsigned integers
614
+ that measure counters. Histograms are objects containing various measurements of statistic
615
+ distribution across all operations.
616
+
617
+ ```typescript
618
+ import { RocksDatabase, stats } from '@harperfast/rocksdb-js';
619
+ const db = RocksDatabase.open('/path/to/db', {
620
+ enableStats: true,
621
+ statsLevel: stats.StatsLevel.ExceptDetailedTimers, // default
622
+ });
623
+ console.log(db.getStats());
624
+ ```
625
+
626
+ ### `db.getStat(statName: string): number`
627
+
628
+ Retrieves a single statistic value.
629
+
630
+ ```typescript
631
+ console.log(db.getStat('rocksdb.block.cache.miss'));
632
+ ```
633
+
634
+ ### `db.getStats(all?: boolean): Object<string, number | StatsHistogramData>`
635
+
636
+ Returns an object containing a curated list of column family-level properties, internal tickers
637
+ stats, and internal histogram stats.
638
+
639
+ By default, it only returns the most meaningful internal stats. When `all = true`, it returns the
640
+ same column family-level properties, but includes all internal tickers and histogram stats.
641
+
642
+ Column family and ticker stat values are 64-bit unsigned integers and histogram values are
643
+ `StatsHistogramData` objects.
644
+
645
+ ```typescript
646
+ // get essential stats
647
+ console.log(db.getStats());
648
+
649
+ // get all stats
650
+ console.log(db.getStats(true));
651
+ ```
652
+
653
+ ### `stats`
654
+
655
+ An object containing stat specific constants including ticker and histogram names.
656
+
657
+ #### `stats.histograms`
658
+
659
+ An array of internal histogram stat names.
660
+
661
+ ```typescript
662
+ import { stats } from '@harperfast/rocksdb-js';
663
+ console.log(stats.histograms);
664
+ ```
665
+
666
+ #### `stats.tickers`
667
+
668
+ An array of internal ticker stat names.
669
+
670
+ ```typescript
671
+ import { stats } from '@harperfast/rocksdb-js';
672
+ console.log(stats.tickers);
673
+ ```
674
+
675
+ #### `stats.StatsLevel`
676
+
677
+ The `stats.StatsLevel` contains constants used to set which types of skip and reduce statistic overhead.
678
+
679
+ - `StatsLevel.DisableAll` Disable all metrics.
680
+ - `StatsLevel.ExceptTickers` Disable all tickers.
681
+ - `StatsLevel.ExceptHistogramOrTimers` Disable timer stats and skip histogram stats.
682
+ - `StatsLevel.ExceptTimers` Skip timer stats
683
+ - `StatsLevel.ExceptDetailedTimers` Skip time waiting for mutex locks and compression.
684
+ - `StatsLevel.ExceptTimeForMutex` Skip time waiting for mutex locks.
685
+ - `StatsLevel.All` Collects all stats.
686
+
687
+ ### `type StatsHistogramData`
688
+
689
+ An object is a record with the following properties:
690
+
691
+ - `average: number` A double containing the average value.
692
+ - `count: number` An unsigned 64-bit integer containing the number of values.
693
+ - `max: number` A double containing the maximum value.
694
+ - `median: number` A double containing the median value.
695
+ - `min: number` A double containing the minimum value.
696
+ - `percentile95: number` A double containing the 95th percentile value.
697
+ - `percentile99: number` A double containing the 99th percentile value.
698
+ - `standardDeviation: number` A double containing the standard deviation.
699
+ - `sum: number` An unsigned 64-bit integer containing the sum of all values.
700
+
602
701
  ## Exclusive Locking
603
702
 
604
703
  `rocksdb-js` includes a handful of functions for executing thread-safe mutually exclusive functions.
package/dist/index.cjs CHANGED
@@ -87,6 +87,7 @@ const NativeTransaction = binding.Transaction;
87
87
  const TransactionLog = binding.TransactionLog;
88
88
  const registryStatus = binding.registryStatus;
89
89
  const shutdown = binding.shutdown;
90
+ const stats = binding.stats;
90
91
  const version = binding.version;
91
92
 
92
93
  //#endregion
@@ -595,6 +596,10 @@ var Store = class {
595
596
  */
596
597
  disableWAL;
597
598
  /**
599
+ * Whether to enable RocksDB statistics.
600
+ */
601
+ enableStats;
602
+ /**
598
603
  * Reusable buffer for encoding values using `writeKey()` when the custom
599
604
  * encoder does not provide a `encode()` method.
600
605
  */
@@ -661,6 +666,10 @@ var Store = class {
661
666
  */
662
667
  sharedStructuresKey;
663
668
  /**
669
+ * The level of statistics to capture.
670
+ */
671
+ statsLevel;
672
+ /**
664
673
  * The threshold for the transaction log file's last modified time to be
665
674
  * older than the retention period before it is rotated to the next sequence
666
675
  * number. A threshold of 0 means ignore age check.
@@ -699,6 +708,7 @@ var Store = class {
699
708
  this.db = new NativeDatabase();
700
709
  this.decoder = options?.decoder ?? null;
701
710
  this.disableWAL = options?.disableWAL ?? false;
711
+ this.enableStats = options?.enableStats ?? false;
702
712
  this.encodeBuffer = createFixedBuffer(SAVE_BUFFER_SIZE);
703
713
  this.encoder = options?.encoder ?? null;
704
714
  this.encoding = options?.encoding ?? null;
@@ -714,6 +724,7 @@ var Store = class {
714
724
  this.randomAccessStructure = options?.randomAccessStructure ?? false;
715
725
  this.readKey = readKey;
716
726
  this.sharedStructuresKey = options?.sharedStructuresKey;
727
+ this.statsLevel = options?.statsLevel;
717
728
  this.transactionLogMaxAgeThreshold = options?.transactionLogMaxAgeThreshold;
718
729
  this.transactionLogMaxSize = options?.transactionLogMaxSize;
719
730
  this.transactionLogRetention = options?.transactionLogRetention;
@@ -915,10 +926,12 @@ var Store = class {
915
926
  if (this.db.opened) return true;
916
927
  this.db.open(this.path, {
917
928
  disableWAL: this.disableWAL,
929
+ enableStats: this.enableStats,
918
930
  mode: this.pessimistic ? "pessimistic" : "optimistic",
919
931
  name: this.name,
920
932
  noBlockCache: this.noBlockCache,
921
933
  parallelismThreads: this.parallelismThreads,
934
+ statsLevel: this.statsLevel,
922
935
  transactionLogMaxAgeThreshold: this.transactionLogMaxAgeThreshold,
923
936
  transactionLogMaxSize: this.transactionLogMaxSize,
924
937
  transactionLogRetentionMs: this.transactionLogRetention ? parseDuration(this.transactionLogRetention) : void 0,
@@ -1170,22 +1183,36 @@ var RocksDatabase = class RocksDatabase extends DBI {
1170
1183
  return this.store.encoder;
1171
1184
  }
1172
1185
  /**
1173
- * Returns the current timestamp as a monotonically increasing timestamp in
1174
- * milliseconds represented as a decimal number.
1186
+ * Flushes the underlying database by performing a commit or clearing any buffered operations.
1175
1187
  *
1176
- * @returns The current monotonic timestamp in milliseconds.
1188
+ * @return {void} Does not return a value.
1177
1189
  */
1178
- getMonotonicTimestamp() {
1179
- return this.store.db.getMonotonicTimestamp();
1190
+ flush() {
1191
+ return new Promise((resolve, reject) => this.store.db.flush(resolve, reject));
1180
1192
  }
1181
1193
  /**
1182
- * Returns a number representing a unix timestamp of the oldest unreleased
1183
- * snapshot.
1194
+ * Synchronously flushes the underlying database by performing a commit or clearing any buffered operations.
1184
1195
  *
1185
- * @returns The oldest snapshot timestamp.
1196
+ * @return {void} Does not return a value.
1186
1197
  */
1187
- getOldestSnapshotTimestamp() {
1188
- return this.store.db.getOldestSnapshotTimestamp();
1198
+ flushSync() {
1199
+ return this.store.db.flushSync();
1200
+ }
1201
+ /**
1202
+ * Gets a RocksDB database property as an integer.
1203
+ *
1204
+ * @param propertyName - The name of the property to retrieve (e.g., 'rocksdb.num-blob-files').
1205
+ * @returns The property value as a number.
1206
+ *
1207
+ * @example
1208
+ * ```typescript
1209
+ * const db = RocksDatabase.open('/path/to/database');
1210
+ * const blobFiles = db.getDBIntProperty('rocksdb.num-blob-files');
1211
+ * const numKeys = db.getDBIntProperty('rocksdb.estimate-num-keys');
1212
+ * ```
1213
+ */
1214
+ getDBIntProperty(propertyName) {
1215
+ return this.store.db.getDBIntProperty(propertyName);
1189
1216
  }
1190
1217
  /**
1191
1218
  * Gets a RocksDB database property as a string.
@@ -1204,42 +1231,43 @@ var RocksDatabase = class RocksDatabase extends DBI {
1204
1231
  return this.store.db.getDBProperty(propertyName);
1205
1232
  }
1206
1233
  /**
1207
- * Gets a RocksDB database property as an integer.
1208
- *
1209
- * @param propertyName - The name of the property to retrieve (e.g., 'rocksdb.num-blob-files').
1210
- * @returns The property value as a number.
1234
+ * Returns the current timestamp as a monotonically increasing timestamp in
1235
+ * milliseconds represented as a decimal number.
1211
1236
  *
1212
- * @example
1213
- * ```typescript
1214
- * const db = RocksDatabase.open('/path/to/database');
1215
- * const blobFiles = db.getDBIntProperty('rocksdb.num-blob-files');
1216
- * const numKeys = db.getDBIntProperty('rocksdb.estimate-num-keys');
1217
- * ```
1237
+ * @returns The current monotonic timestamp in milliseconds.
1218
1238
  */
1219
- getDBIntProperty(propertyName) {
1220
- return this.store.db.getDBIntProperty(propertyName);
1239
+ getMonotonicTimestamp() {
1240
+ return this.store.db.getMonotonicTimestamp();
1221
1241
  }
1222
1242
  /**
1223
- * Flushes the underlying database by performing a commit or clearing any buffered operations.
1243
+ * Returns a number representing a unix timestamp of the oldest unreleased
1244
+ * snapshot.
1224
1245
  *
1225
- * @return {void} Does not return a value.
1246
+ * @returns The oldest snapshot timestamp.
1226
1247
  */
1227
- flush() {
1228
- return new Promise((resolve, reject) => this.store.db.flush(resolve, reject));
1248
+ getOldestSnapshotTimestamp() {
1249
+ return this.store.db.getOldestSnapshotTimestamp();
1229
1250
  }
1230
1251
  /**
1231
- * Synchronously flushes the underlying database by performing a commit or clearing any buffered operations.
1252
+ * Gets a RocksDB statistic.
1232
1253
  *
1233
- * @return {void} Does not return a value.
1254
+ * @param statName - The name of the statistic to retrieve.
1255
+ * @returns The statistic value.
1234
1256
  */
1235
- flushSync() {
1236
- return this.store.db.flushSync();
1257
+ getStat(statName) {
1258
+ return this.store.db.getStat(statName);
1237
1259
  }
1238
- getStats() {
1239
- return {
1240
- free: {},
1241
- root: {}
1242
- };
1260
+ /**
1261
+ * Gets the RocksDB statistics. Requires statistics to be enabled.
1262
+ *
1263
+ * @example
1264
+ * ```typescript
1265
+ * const db = RocksDatabase.open('/path/to/database');
1266
+ * const stats = db.getStats();
1267
+ * ```
1268
+ */
1269
+ getStats(all = false) {
1270
+ return this.store.db.getStats(all);
1243
1271
  }
1244
1272
  /**
1245
1273
  * Gets or creates a buffer that can be shared across worker threads.
@@ -1816,7 +1844,7 @@ function loadLastPosition(transactionLog, readUncommitted) {
1816
1844
  //#region src/index.ts
1817
1845
  const versions = {
1818
1846
  rocksdb: version,
1819
- "rocksdb-js": "0.1.8"
1847
+ "rocksdb-js": "0.1.9"
1820
1848
  };
1821
1849
 
1822
1850
  //#endregion
@@ -1830,5 +1858,6 @@ exports.constants = constants;
1830
1858
  exports.parseTransactionLog = parseTransactionLog;
1831
1859
  exports.registryStatus = registryStatus;
1832
1860
  exports.shutdown = shutdown;
1861
+ exports.stats = stats;
1833
1862
  exports.versions = versions;
1834
1863
  //# sourceMappingURL=index.cjs.map