@harperfast/rocksdb-js 0.1.8 → 0.1.10

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.
@@ -1098,6 +1197,11 @@ either `glibc` (default) or `musl`.
1098
1197
  ROCKSDB_LIBC=musl pnpm rebuild
1099
1198
  ```
1100
1199
 
1200
+ ### Windows C runtime versions
1201
+
1202
+ By default on Windows, `rocksdb-js` is compiled with the `/MT` flag. This will statically link the
1203
+ C runtime making the binary self-contained and portable.
1204
+
1101
1205
  ### Building RocksDB from Source
1102
1206
 
1103
1207
  To build RocksDB from source, simply set the `ROCKSDB_PATH` environment variable to the path of the
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,
@@ -967,6 +980,7 @@ var Store = class {
967
980
  */
968
981
  useLog(context, name) {
969
982
  if (typeof name !== "string" && typeof name !== "number") throw new TypeError("Log name must be a string or number");
983
+ if (typeof name === "string" && /[\t\n\r\\/]/.test(name)) throw new Error(`Invalid transaction log name "${name}"`);
970
984
  return context.useLog(String(name));
971
985
  }
972
986
  /**
@@ -1170,22 +1184,36 @@ var RocksDatabase = class RocksDatabase extends DBI {
1170
1184
  return this.store.encoder;
1171
1185
  }
1172
1186
  /**
1173
- * Returns the current timestamp as a monotonically increasing timestamp in
1174
- * milliseconds represented as a decimal number.
1187
+ * Flushes the underlying database by performing a commit or clearing any buffered operations.
1175
1188
  *
1176
- * @returns The current monotonic timestamp in milliseconds.
1189
+ * @return {void} Does not return a value.
1177
1190
  */
1178
- getMonotonicTimestamp() {
1179
- return this.store.db.getMonotonicTimestamp();
1191
+ flush() {
1192
+ return new Promise((resolve, reject) => this.store.db.flush(resolve, reject));
1180
1193
  }
1181
1194
  /**
1182
- * Returns a number representing a unix timestamp of the oldest unreleased
1183
- * snapshot.
1195
+ * Synchronously flushes the underlying database by performing a commit or clearing any buffered operations.
1184
1196
  *
1185
- * @returns The oldest snapshot timestamp.
1197
+ * @return {void} Does not return a value.
1186
1198
  */
1187
- getOldestSnapshotTimestamp() {
1188
- return this.store.db.getOldestSnapshotTimestamp();
1199
+ flushSync() {
1200
+ return this.store.db.flushSync();
1201
+ }
1202
+ /**
1203
+ * Gets a RocksDB database property as an integer.
1204
+ *
1205
+ * @param propertyName - The name of the property to retrieve (e.g., 'rocksdb.num-blob-files').
1206
+ * @returns The property value as a number.
1207
+ *
1208
+ * @example
1209
+ * ```typescript
1210
+ * const db = RocksDatabase.open('/path/to/database');
1211
+ * const blobFiles = db.getDBIntProperty('rocksdb.num-blob-files');
1212
+ * const numKeys = db.getDBIntProperty('rocksdb.estimate-num-keys');
1213
+ * ```
1214
+ */
1215
+ getDBIntProperty(propertyName) {
1216
+ return this.store.db.getDBIntProperty(propertyName);
1189
1217
  }
1190
1218
  /**
1191
1219
  * Gets a RocksDB database property as a string.
@@ -1204,42 +1232,43 @@ var RocksDatabase = class RocksDatabase extends DBI {
1204
1232
  return this.store.db.getDBProperty(propertyName);
1205
1233
  }
1206
1234
  /**
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.
1235
+ * Returns the current timestamp as a monotonically increasing timestamp in
1236
+ * milliseconds represented as a decimal number.
1211
1237
  *
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
- * ```
1238
+ * @returns The current monotonic timestamp in milliseconds.
1218
1239
  */
1219
- getDBIntProperty(propertyName) {
1220
- return this.store.db.getDBIntProperty(propertyName);
1240
+ getMonotonicTimestamp() {
1241
+ return this.store.db.getMonotonicTimestamp();
1221
1242
  }
1222
1243
  /**
1223
- * Flushes the underlying database by performing a commit or clearing any buffered operations.
1244
+ * Returns a number representing a unix timestamp of the oldest unreleased
1245
+ * snapshot.
1224
1246
  *
1225
- * @return {void} Does not return a value.
1247
+ * @returns The oldest snapshot timestamp.
1226
1248
  */
1227
- flush() {
1228
- return new Promise((resolve, reject) => this.store.db.flush(resolve, reject));
1249
+ getOldestSnapshotTimestamp() {
1250
+ return this.store.db.getOldestSnapshotTimestamp();
1229
1251
  }
1230
1252
  /**
1231
- * Synchronously flushes the underlying database by performing a commit or clearing any buffered operations.
1253
+ * Gets a RocksDB statistic.
1232
1254
  *
1233
- * @return {void} Does not return a value.
1255
+ * @param statName - The name of the statistic to retrieve.
1256
+ * @returns The statistic value.
1234
1257
  */
1235
- flushSync() {
1236
- return this.store.db.flushSync();
1258
+ getStat(statName) {
1259
+ return this.store.db.getStat(statName);
1237
1260
  }
1238
- getStats() {
1239
- return {
1240
- free: {},
1241
- root: {}
1242
- };
1261
+ /**
1262
+ * Gets the RocksDB statistics. Requires statistics to be enabled.
1263
+ *
1264
+ * @example
1265
+ * ```typescript
1266
+ * const db = RocksDatabase.open('/path/to/database');
1267
+ * const stats = db.getStats();
1268
+ * ```
1269
+ */
1270
+ getStats(all = false) {
1271
+ return this.store.db.getStats(all);
1243
1272
  }
1244
1273
  /**
1245
1274
  * Gets or creates a buffer that can be shared across worker threads.
@@ -1816,7 +1845,7 @@ function loadLastPosition(transactionLog, readUncommitted) {
1816
1845
  //#region src/index.ts
1817
1846
  const versions = {
1818
1847
  rocksdb: version,
1819
- "rocksdb-js": "0.1.8"
1848
+ "rocksdb-js": "0.1.10"
1820
1849
  };
1821
1850
 
1822
1851
  //#endregion
@@ -1830,5 +1859,6 @@ exports.constants = constants;
1830
1859
  exports.parseTransactionLog = parseTransactionLog;
1831
1860
  exports.registryStatus = registryStatus;
1832
1861
  exports.shutdown = shutdown;
1862
+ exports.stats = stats;
1833
1863
  exports.versions = versions;
1834
1864
  //# sourceMappingURL=index.cjs.map