@harperfast/rocksdb-js 1.1.0 → 1.2.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/README.md CHANGED
@@ -85,6 +85,18 @@ const db = RocksDatabase.open('foo');
85
85
  db.close();
86
86
  ```
87
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
+
88
100
  ### `db.config(options)`
89
101
 
90
102
  Sets global database settings.
@@ -93,10 +105,12 @@ Sets global database settings.
93
105
  - `blockCacheSize: number` The amount of memory in bytes to use to cache uncompressed blocks.
94
106
  Defaults to 32MB. Set to `0` (zero) disables block cache for future opened databases. Existing
95
107
  block cache for any opened databases is resized immediately. Negative values throw an error.
108
+ - `compactOnClose: boolean` When `true`, compacts the database on close. Defaults to `false`.
96
109
 
97
110
  ```typescript
98
111
  RocksDatabase.config({
99
112
  blockCacheSize: 100 * 1024 * 1024, // 100MB
113
+ compactOnClose: true,
100
114
  });
101
115
  ```
102
116
 
@@ -181,6 +195,33 @@ const entriesRemoved = db.clearSync();
181
195
  console.log(entriesRemoved); // 10
182
196
  ```
183
197
 
198
+ ### `db.compact(options?): Promise<void>`
199
+
200
+ Compacts a range of keys in the database. In RocksDB, deleted keys are not immediately removed from
201
+ the database. Instead, they are marked as deleted and a tombstone is written. This function
202
+ triggers a manual compaction which removes the tombstones and reclaims space. Only one compaction
203
+ per database path can be performed at a time.
204
+
205
+ - `options: object`
206
+ - `start?: Key` The start key of the range to compact.
207
+ - `end?: Key` The end key of the range to compact.
208
+
209
+ ```typescript
210
+ await db.compact();
211
+
212
+ await db.compact({ start: 'a', end: 'z' });
213
+ ```
214
+
215
+ ### `db.compactSync(options?): void`
216
+
217
+ Synchronous version of `compact()`.
218
+
219
+ ```typescript
220
+ db.compactSync();
221
+
222
+ db.compactSync({ start: 'a', end: 'z' });
223
+ ```
224
+
184
225
  ### `db.destroy(): void`
185
226
 
186
227
  Completely removes a database based on the `db` instance's path including all data, column families,
@@ -212,6 +253,23 @@ db.dropSync();
212
253
  db.close();
213
254
  ```
214
255
 
256
+ ### `db.flush(): Promise<void>`
257
+
258
+ Flushes all in-memory data to disk asynchronously.
259
+
260
+ ```typescript
261
+ await db.flush();
262
+ ```
263
+
264
+ ### `db.flushSync(): void`
265
+
266
+ Flushes all in-memory data to disk synchronously. Note that this can be an expensive operation, so
267
+ it is recommended to use `flush()` if you want to keep the event loop free.
268
+
269
+ ```typescript
270
+ db.flushSync();
271
+ ```
272
+
215
273
  ### `db.get(key: Key, options?: GetOptions): MaybePromise<any>`
216
274
 
217
275
  Retreives the value for a given key. If the key does not exist, it will resolve `undefined`.
@@ -236,6 +294,16 @@ Note that all errors are returned as rejected promises.
236
294
 
237
295
  Synchronous version of `get()`.
238
296
 
297
+ ### `db.getEstimatedKeyCount(): number`
298
+
299
+ Retrieves the estimated number of keys in the database. This is an alias for
300
+ `db.getDBIntProperty('rocksdb.estimate-num-keys')`.
301
+
302
+ ```typescript
303
+ const estimated = db.getEstimatedKeyCount();
304
+ console.log(estimated);
305
+ ```
306
+
239
307
  ### `db.getKeys(options?: IteratorOptions): ExtendedIterable`
240
308
 
241
309
  Retrieves all keys within a range.
@@ -248,11 +316,11 @@ for (const key of db.getKeys()) {
248
316
 
249
317
  ### `db.getKeysCount(options?: RangeOptions): number`
250
318
 
251
- Retrieves the number of keys within a range.
319
+ Retrieves the exact number of keys in a database or a range.
252
320
 
253
321
  ```typescript
254
- const total = db.getKeysCount();
255
- const range = db.getKeysCount({ start: 'a', end: 'z' });
322
+ const count = db.getKeysCount(); // estimated number of keys
323
+ const range = db.getKeysCount({ start: 'a', end: 'z' }); // exact number of keys in the range
256
324
  ```
257
325
 
258
326
  ### `db.getMonotonicTimestamp(): number`
@@ -290,24 +358,28 @@ await promise;
290
358
  console.log(db.getOldestSnapshotTimestamp()); // returns `0`, no snapshots
291
359
  ```
292
360
 
293
- ### `db.getDBProperty(propertyName: string): string`
361
+ ### `db.getDBProperty(propertyName: string): string | undefined`
294
362
 
295
363
  Gets a RocksDB database property as a string.
296
364
 
297
365
  - `propertyName: string` The name of the property to retrieve (e.g., ) `'rocksdb.levelstats'`.
298
366
 
367
+ Returns `undefined` if the property is not found.
368
+
299
369
  ```typescript
300
370
  const db = RocksDatabase.open('/path/to/database');
301
371
  const levelStats = db.getDBProperty('rocksdb.levelstats');
302
372
  const stats = db.getDBProperty('rocksdb.stats');
303
373
  ```
304
374
 
305
- ### `db.getDBIntProperty(propertyName: string): number`
375
+ ### `db.getDBIntProperty(propertyName: string): number | undefined`
306
376
 
307
377
  Gets a RocksDB database property as an integer.
308
378
 
309
379
  - `propertyName: string` The name of the property to retrieve (e.g., ) `'rocksdb.num-blob-files'`.
310
380
 
381
+ Returns `undefined` if the property is not found.
382
+
311
383
  ```typescript
312
384
  const db = RocksDatabase.open('/path/to/database');
313
385
  const blobFiles = db.getDBIntProperty('rocksdb.num-blob-files');
@@ -399,7 +471,7 @@ Synchronous version of `remove()`.
399
471
 
400
472
  ## Transactions
401
473
 
402
- ### `db.transaction<T>(callback: TransactionCallback<T>, options?: DBTransactionOptions): Promise<T>`
474
+ ### `db.transaction<T>(callback: TransactionCallback<T>, options?: TransactionOptions): Promise<T>`
403
475
 
404
476
  Executes all database operations within the specified callback within a single transaction. If the
405
477
  callback completes without error, the database operations are automatically committed. However, if
@@ -860,23 +932,6 @@ Note: If the `callback` throws an error, Node.js suppress the error. Node.js 18.
860
932
  `--force-node-api-uncaught-exceptions-policy` flag which will cause errors to emit the
861
933
  `'uncaughtException'` event. Future Node.js releases will enable this flag by default.
862
934
 
863
- ### `db.flush(): Promise<void>`
864
-
865
- Flushes all in-memory data to disk asynchronously.
866
-
867
- ```typescript
868
- await db.flush();
869
- ```
870
-
871
- ### `db.flushSync(): void`
872
-
873
- Flushes all in-memory data to disk synchronously. Note that this can be an expensive operation, so
874
- it is recommended to use `flush()` if you want to keep the event loop free.
875
-
876
- ```typescript
877
- db.flushSync();
878
- ```
879
-
880
935
  ## Transaction Log
881
936
 
882
937
  A user controlled API for logging transactions. This API is designed to be generic so that you can
@@ -1113,6 +1168,8 @@ The default `Store` contains the following methods which can be overridden:
1113
1168
 
1114
1169
  - `constructor(path, options?)`
1115
1170
  - `close()`
1171
+ - `compact(options?)`
1172
+ - `compactSync(options?)`
1116
1173
  - `decodeKey(key)`
1117
1174
  - `decodeValue(value)`
1118
1175
  - `encodeKey(key)`
@@ -1219,6 +1276,40 @@ Extends `RangeOptions`.
1219
1276
  - `options: object`
1220
1277
  - `reverse: boolean` When `true`, the iterator will iterate in reverse order. Defaults to `false`.
1221
1278
 
1279
+ ## CLI
1280
+
1281
+ The `rocksdb-js` CLI is a command line interface for interacting with RocksDB databases.
1282
+
1283
+ ```bash
1284
+ rocksdb-js [dbpath]
1285
+ ```
1286
+
1287
+ Options:
1288
+
1289
+ - `-h, --help` Show this help message
1290
+ - `-r, --readonly` Open the database in read-only mode
1291
+ - `-v, --version` Show the version information
1292
+
1293
+ Available commands:
1294
+
1295
+ - `clear` Clear all data in the current column family
1296
+ - `columns` List column families
1297
+ - `compact` Compact the current column family
1298
+ - `count` Count the number of keys in the current column family
1299
+ - `drop <column>` Permanently drop a column family
1300
+ - `exit` Exit the REPL
1301
+ - `get <key>` Get the value of a key
1302
+ - `help` Show this help message
1303
+ - `log [name] [file] [entry]` List the transaction log store names and log store files
1304
+ - `prop <key>` Get a RocksDB property (try "rocksdb.stats")
1305
+ - `purge-logs <name>` Purge transaction log files older than 3 days
1306
+ - `put <key> <value>` Set the value of a key
1307
+ - `query [start] [end]` Query a range of keys
1308
+ - `remove <key>` Delete a key
1309
+ - `repl` Open a JS sub-REPL; "db" refers to the current column family
1310
+ - `stats` Show the statistics for the current column family
1311
+ - `use [column]` Create a new column family or switch to an existing one
1312
+
1222
1313
  ## Development
1223
1314
 
1224
1315
  This package requires Node.js 18 or higher, pnpm, and a C++ compiler.