@keyv/bigmap 1.0.3 → 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 +591 -8
- package/dist/index.cjs +18 -13
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +14 -11
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# @keyv/
|
|
1
|
+
# @keyv/bigmap [<img width="100" align="right" src="https://jaredwray.com/images/keyv-symbol.svg" alt="keyv">](https://github.com/jaredwra/keyv)
|
|
2
2
|
|
|
3
3
|
> Bigmap for Keyv
|
|
4
4
|
|
|
5
5
|
[](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
|
|
6
6
|
[](https://codecov.io/gh/jaredwray/keyv)
|
|
7
|
-
[](https://www.npmjs.com/package/@keyv/bigmap)
|
|
8
|
+
[](https://npmjs.com/package/@keyv/bigmap)
|
|
9
9
|
|
|
10
10
|
# Features
|
|
11
11
|
* Based on the Map interface and uses the same API.
|
|
@@ -14,13 +14,50 @@
|
|
|
14
14
|
* Uses a hash `djb2Hash` for fast key lookups.
|
|
15
15
|
* Ability to use your own hash function.
|
|
16
16
|
* Built in Typescript and Generics for type safety.
|
|
17
|
-
* Used as default in-memory store for `Keyv`.
|
|
18
|
-
* Used as in-memory store for `@keyv/fs`.
|
|
19
17
|
* Used in `@cacheable/memory` for scalable in-memory caching.
|
|
20
18
|
* Maintained regularly with a focus on performance and reliability.
|
|
21
19
|
|
|
22
20
|
# Table of Contents
|
|
23
|
-
|
|
21
|
+
- [Features](#features)
|
|
22
|
+
- [Installation](#installation)
|
|
23
|
+
- [Overview](#overview)
|
|
24
|
+
- [Basic Usage](#basic-usage)
|
|
25
|
+
- [Custom Store Size](#custom-store-size)
|
|
26
|
+
- [Custom Hash Function](#custom-hash-function)
|
|
27
|
+
- [Iteration](#iteration)
|
|
28
|
+
- [For...of Loop](#forof-loop)
|
|
29
|
+
- [forEach](#foreach)
|
|
30
|
+
- [Keys, Values, and Entries](#keys-values-and-entries)
|
|
31
|
+
- [Advanced Features](#advanced-features)
|
|
32
|
+
- [Type Safety with Generics](#type-safety-with-generics)
|
|
33
|
+
- [Large-Scale Data](#large-scale-data)
|
|
34
|
+
- [Using with Keyv](#using-with-keyv)
|
|
35
|
+
- [createKeyv](#createkeyv)
|
|
36
|
+
- [With Custom Options](#with-custom-options)
|
|
37
|
+
- [Type Safety](#type-safety)
|
|
38
|
+
- [Integration with Keyv Ecosystem](#integration-with-keyv-ecosystem)
|
|
39
|
+
- [API](#api)
|
|
40
|
+
- [Constructor](#constructor)
|
|
41
|
+
- [Properties](#properties)
|
|
42
|
+
- [Methods](#methods)
|
|
43
|
+
- [set](#set)
|
|
44
|
+
- [get](#get)
|
|
45
|
+
- [has](#has)
|
|
46
|
+
- [delete](#delete)
|
|
47
|
+
- [clear](#clear)
|
|
48
|
+
- [forEach](#foreach)
|
|
49
|
+
- [keys](#keys)
|
|
50
|
+
- [values](#values)
|
|
51
|
+
- [entries](#entries)
|
|
52
|
+
- [Symbol.iterator](#symboliterator)
|
|
53
|
+
- [getStore](#getstorekey)
|
|
54
|
+
- [getStoreMap](#getstoremapindex)
|
|
55
|
+
- [initStore](#initstore)
|
|
56
|
+
- [Types](#types)
|
|
57
|
+
- [StoreHashFunction](#storehashfunction)
|
|
58
|
+
- [defaultHashFunction(key, storeSize)](#defaulthashfunctionkey-storesize)
|
|
59
|
+
- [Contributing](#contributing)
|
|
60
|
+
- [License](#license)
|
|
24
61
|
|
|
25
62
|
# Installation
|
|
26
63
|
|
|
@@ -28,9 +65,555 @@
|
|
|
28
65
|
npm install --save keyv @keyv/bigmap
|
|
29
66
|
```
|
|
30
67
|
|
|
31
|
-
#
|
|
68
|
+
# Overview
|
|
69
|
+
|
|
70
|
+
BigMap is a scalable Map implementation that overcomes JavaScript's built-in Map limit of approximately 17 million entries. It uses a distributed hash approach with multiple internal Map instances.
|
|
71
|
+
|
|
72
|
+
# Basic Usage
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { BigMap } from '@keyv/bigmap';
|
|
76
|
+
|
|
77
|
+
// Create a new BigMap
|
|
78
|
+
const bigMap = new BigMap<string, number>();
|
|
79
|
+
|
|
80
|
+
// Set values
|
|
81
|
+
bigMap.set('key1', 100);
|
|
82
|
+
bigMap.set('key2', 200);
|
|
83
|
+
|
|
84
|
+
// Get values
|
|
85
|
+
const value = bigMap.get('key1'); // 100
|
|
86
|
+
|
|
87
|
+
// Check if key exists
|
|
88
|
+
bigMap.has('key1'); // true
|
|
89
|
+
|
|
90
|
+
// Delete a key
|
|
91
|
+
bigMap.delete('key1'); // true
|
|
92
|
+
|
|
93
|
+
// Get size
|
|
94
|
+
console.log(bigMap.size); // 1
|
|
95
|
+
|
|
96
|
+
// Clear all entries
|
|
97
|
+
bigMap.clear();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
# Custom Store Size
|
|
101
|
+
|
|
102
|
+
By default, BigMap uses 4 internal Map instances. You can configure this:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const bigMap = new BigMap<string, number>({ storeSize: 10 });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Note:** Changing the `storeSize` after initialization will clear all entries.
|
|
109
|
+
|
|
110
|
+
# Custom Hash Function
|
|
111
|
+
|
|
112
|
+
Provide your own hash function for key distribution:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const customHashFunction = (key: string, storeSize: number) => {
|
|
116
|
+
return key.length % storeSize;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const bigMap = new BigMap<string, string>({
|
|
120
|
+
storeHashFunction: customHashFunction
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Using Hashery for Hash Functions
|
|
125
|
+
|
|
126
|
+
[Hashery](https://github.com/jaredwray/hashery) is a powerful hashing library that provides multiple hash algorithms. You can use it for better key distribution and it is available as an export:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { BigMap, Hashery } from '@keyv/bigmap';
|
|
130
|
+
|
|
131
|
+
const hashery = new Hashery();
|
|
132
|
+
|
|
133
|
+
// Using Hashery's toNumberSync for deterministic key distribution
|
|
134
|
+
const bigMap = new BigMap<string, string>({
|
|
135
|
+
storeHashFunction: (key: string, storeSize: number) => {
|
|
136
|
+
return hashery.toNumberSync(key, { min: 0, max: storeSize - 1 });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// You can also use different algorithms
|
|
141
|
+
const hasheryFnv1 = new Hashery({ defaultAlgorithmSync: 'fnv1' });
|
|
142
|
+
|
|
143
|
+
const bigMapWithFnv1 = new BigMap<string, string>({
|
|
144
|
+
storeHashFunction: (key: string, storeSize: number) => {
|
|
145
|
+
return hasheryFnv1.toNumberSync(key, { min: 0, max: storeSize - 1 });
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Hashery supports multiple synchronous hash algorithms:
|
|
151
|
+
- **djb2** - Fast hash function (default)
|
|
152
|
+
- **fnv1** - Excellent distribution for hash tables
|
|
153
|
+
- **murmer** - MurmurHash algorithm
|
|
154
|
+
- **crc32** - Cyclic Redundancy Check
|
|
155
|
+
|
|
156
|
+
# Iteration
|
|
157
|
+
|
|
158
|
+
BigMap supports all standard Map iteration methods:
|
|
159
|
+
|
|
160
|
+
## For...of Loop
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
const bigMap = new BigMap<string, number>();
|
|
164
|
+
bigMap.set('a', 1);
|
|
165
|
+
bigMap.set('b', 2);
|
|
166
|
+
|
|
167
|
+
for (const [key, value] of bigMap) {
|
|
168
|
+
console.log(key, value);
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## forEach
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
bigMap.forEach((value, key) => {
|
|
176
|
+
console.log(key, value);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// With custom context
|
|
180
|
+
const context = { sum: 0 };
|
|
181
|
+
bigMap.forEach(function(value) {
|
|
182
|
+
this.sum += value;
|
|
183
|
+
}, context);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Keys, Values, and Entries
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
// Iterate over keys
|
|
190
|
+
for (const key of bigMap.keys()) {
|
|
191
|
+
console.log(key);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Iterate over values
|
|
195
|
+
for (const value of bigMap.values()) {
|
|
196
|
+
console.log(value);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Iterate over entries
|
|
200
|
+
for (const [key, value] of bigMap.entries()) {
|
|
201
|
+
console.log(key, value);
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
# Advanced Features
|
|
206
|
+
|
|
207
|
+
## Type Safety with Generics
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
interface User {
|
|
211
|
+
id: number;
|
|
212
|
+
name: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const userMap = new BigMap<string, User>();
|
|
216
|
+
userMap.set('user1', { id: 1, name: 'Alice' });
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Large-Scale Data
|
|
220
|
+
|
|
221
|
+
BigMap is designed to handle millions of entries:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const bigMap = new BigMap<string, number>({ storeSize: 16 });
|
|
225
|
+
|
|
226
|
+
// Add 20+ million entries without hitting Map limits
|
|
227
|
+
for (let i = 0; i < 20000000; i++) {
|
|
228
|
+
bigMap.set(`key${i}`, i);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
console.log(bigMap.size); // 20000000
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
# Using with Keyv
|
|
235
|
+
|
|
236
|
+
BigMap can be used as a storage adapter for [Keyv](https://github.com/jaredwray/keyv), providing a scalable in-memory store with TTL support.
|
|
237
|
+
|
|
238
|
+
## createKeyv
|
|
239
|
+
|
|
240
|
+
The `createKeyv` function creates a Keyv instance with BigMap as the storage adapter.
|
|
241
|
+
|
|
242
|
+
**Parameters:**
|
|
243
|
+
- `options` (optional): BigMap configuration options
|
|
244
|
+
- `storeSize` (number): Number of internal Map instances. Default: `4`
|
|
245
|
+
- `storeHashFunction` (StoreHashFunction): Custom hash function for key distribution
|
|
246
|
+
|
|
247
|
+
**Returns:** `Keyv` instance with BigMap adapter
|
|
248
|
+
|
|
249
|
+
**Example:**
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { createKeyv } from '@keyv/bigmap';
|
|
253
|
+
|
|
254
|
+
// Basic usage
|
|
255
|
+
const keyv = createKeyv();
|
|
256
|
+
|
|
257
|
+
// Set with TTL (in milliseconds)
|
|
258
|
+
await keyv.set('user:123', { name: 'Alice', age: 30 }, 60000); // Expires in 60 seconds
|
|
259
|
+
|
|
260
|
+
// Get value
|
|
261
|
+
const user = await keyv.get('user:123');
|
|
262
|
+
console.log(user); // { name: 'Alice', age: 30 }
|
|
263
|
+
|
|
264
|
+
// Check if key exists
|
|
265
|
+
const exists = await keyv.has('user:123');
|
|
266
|
+
|
|
267
|
+
// Delete key
|
|
268
|
+
await keyv.delete('user:123');
|
|
269
|
+
|
|
270
|
+
// Clear all keys
|
|
271
|
+
await keyv.clear();
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## With Custom Options
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
import { createKeyv } from '@keyv/bigmap';
|
|
278
|
+
|
|
279
|
+
// Create with custom store size for better performance with millions of keys
|
|
280
|
+
const keyv = createKeyv({ storeSize: 16 });
|
|
281
|
+
|
|
282
|
+
// With custom hash function
|
|
283
|
+
const keyv = createKeyv({
|
|
284
|
+
storeSize: 8,
|
|
285
|
+
storeHashFunction: (key, storeSize) => {
|
|
286
|
+
// Custom distribution logic
|
|
287
|
+
return key.length % storeSize;
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## Type Safety
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
import { createKeyv } from '@keyv/bigmap';
|
|
296
|
+
|
|
297
|
+
interface Product {
|
|
298
|
+
id: string;
|
|
299
|
+
name: string;
|
|
300
|
+
price: number;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const keyv = createKeyv<string, Product>();
|
|
304
|
+
|
|
305
|
+
await keyv.set('product:1', {
|
|
306
|
+
id: '1',
|
|
307
|
+
name: 'Laptop',
|
|
308
|
+
price: 999
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const product = await keyv.get<Product>('product:1');
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
# Integration with Keyv Ecosystem
|
|
315
|
+
|
|
316
|
+
BigMap works seamlessly with the Keyv ecosystem:
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
import { createKeyv } from '@keyv/bigmap';
|
|
320
|
+
|
|
321
|
+
const cache = createKeyv({ storeSize: 16 });
|
|
322
|
+
|
|
323
|
+
// Use with namespaces
|
|
324
|
+
const users = cache.namespace('users');
|
|
325
|
+
const products = cache.namespace('products');
|
|
326
|
+
|
|
327
|
+
await users.set('123', { name: 'Alice' });
|
|
328
|
+
await products.set('456', { name: 'Laptop' });
|
|
329
|
+
|
|
330
|
+
// Iterate over keys
|
|
331
|
+
for await (const [key, value] of cache.iterator()) {
|
|
332
|
+
console.log(key, value);
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
# API
|
|
337
|
+
|
|
338
|
+
## Constructor
|
|
339
|
+
|
|
340
|
+
`new BigMap<K, V>(options?)`
|
|
341
|
+
|
|
342
|
+
Creates a new BigMap instance.
|
|
343
|
+
|
|
344
|
+
**Parameters:**
|
|
345
|
+
- `options` (optional): Configuration options
|
|
346
|
+
- `storeSize` (number): Number of internal Map instances to use. Default: `4`. Must be at least 1.
|
|
347
|
+
- `storeHashFunction` (StoreHashFunction): Custom hash function for key distribution. Default: `defaultHashFunction`
|
|
348
|
+
|
|
349
|
+
**Example:**
|
|
350
|
+
```typescript
|
|
351
|
+
const bigMap = new BigMap<string, number>();
|
|
352
|
+
const customBigMap = new BigMap<string, number>({
|
|
353
|
+
storeSize: 10,
|
|
354
|
+
storeHashFunction: (key, storeSize) => key.length % storeSize
|
|
355
|
+
});
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Properties
|
|
359
|
+
|
|
360
|
+
| Property | Type | Access | Description |
|
|
361
|
+
|----------|------|--------|-------------|
|
|
362
|
+
| `size` | `number` | Read-only | Gets the total number of entries in the BigMap. |
|
|
363
|
+
| `storeSize` | `number` | Read/Write | Gets or sets the number of internal Map instances. **Note:** Setting this will clear all entries. Default: `4` |
|
|
364
|
+
| `storeHashFunction` | `StoreHashFunction \| undefined` | Read/Write | Gets or sets the hash function used for key distribution. |
|
|
365
|
+
| `store` | `Array<Map<K, V>>` | Read-only | Gets the internal array of Map instances. |
|
|
366
|
+
|
|
367
|
+
**Examples:**
|
|
368
|
+
```typescript
|
|
369
|
+
const bigMap = new BigMap<string, number>();
|
|
370
|
+
|
|
371
|
+
// size property
|
|
372
|
+
bigMap.set('key1', 100);
|
|
373
|
+
console.log(bigMap.size); // 1
|
|
374
|
+
|
|
375
|
+
// storeSize property
|
|
376
|
+
console.log(bigMap.storeSize); // 4 (default)
|
|
377
|
+
bigMap.storeSize = 8; // Changes size and clears all entries
|
|
378
|
+
|
|
379
|
+
// storeHashFunction property
|
|
380
|
+
bigMap.storeHashFunction = (key, storeSize) => key.length % storeSize;
|
|
381
|
+
|
|
382
|
+
// store property
|
|
383
|
+
console.log(bigMap.store.length); // 8
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
## Methods
|
|
387
|
+
|
|
388
|
+
### set
|
|
389
|
+
|
|
390
|
+
Sets the value for a key in the map.
|
|
391
|
+
|
|
392
|
+
**Parameters:**
|
|
393
|
+
- `key` (K): The key to set
|
|
394
|
+
- `value` (V): The value to associate with the key
|
|
395
|
+
|
|
396
|
+
**Returns:** `Map<K, V>` - The internal Map instance where the key was stored
|
|
397
|
+
|
|
398
|
+
**Example:**
|
|
399
|
+
```typescript
|
|
400
|
+
bigMap.set('user123', { name: 'Alice' });
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### get
|
|
404
|
+
|
|
405
|
+
Gets the value associated with a key.
|
|
406
|
+
|
|
407
|
+
**Parameters:**
|
|
408
|
+
- `key` (K): The key to retrieve
|
|
409
|
+
|
|
410
|
+
**Returns:** `V | undefined` - The value, or undefined if not found
|
|
411
|
+
|
|
412
|
+
**Example:**
|
|
413
|
+
```typescript
|
|
414
|
+
const value = bigMap.get('user123');
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### has
|
|
418
|
+
|
|
419
|
+
Checks if a key exists in the map.
|
|
420
|
+
|
|
421
|
+
**Parameters:**
|
|
422
|
+
- `key` (K): The key to check
|
|
423
|
+
|
|
424
|
+
**Returns:** `boolean` - True if the key exists, false otherwise
|
|
425
|
+
|
|
426
|
+
**Example:**
|
|
427
|
+
```typescript
|
|
428
|
+
if (bigMap.has('user123')) {
|
|
429
|
+
console.log('User exists');
|
|
430
|
+
}
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
### delete
|
|
434
|
+
|
|
435
|
+
Deletes a key-value pair from the map.
|
|
436
|
+
|
|
437
|
+
**Parameters:**
|
|
438
|
+
- `key` (K): The key to delete
|
|
439
|
+
|
|
440
|
+
**Returns:** `boolean` - True if the entry was deleted, false if the key was not found
|
|
441
|
+
|
|
442
|
+
**Example:**
|
|
443
|
+
```typescript
|
|
444
|
+
const deleted = bigMap.delete('user123');
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### clear
|
|
448
|
+
|
|
449
|
+
Removes all entries from the map.
|
|
450
|
+
|
|
451
|
+
**Returns:** `void`
|
|
452
|
+
|
|
453
|
+
**Example:**
|
|
454
|
+
```typescript
|
|
455
|
+
bigMap.clear();
|
|
456
|
+
console.log(bigMap.size); // 0
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### forEach
|
|
460
|
+
|
|
461
|
+
Executes a provided function once for each key-value pair.
|
|
462
|
+
|
|
463
|
+
**Parameters:**
|
|
464
|
+
- `callbackfn` (function): Function to execute for each entry
|
|
465
|
+
- `value` (V): The value of the current entry
|
|
466
|
+
- `key` (K): The key of the current entry
|
|
467
|
+
- `map` (`Map<K, V>`): The BigMap instance
|
|
468
|
+
- `thisArg` (optional): Value to use as `this` when executing the callback
|
|
469
|
+
|
|
470
|
+
**Returns:** `void`
|
|
471
|
+
|
|
472
|
+
**Example:**
|
|
473
|
+
```typescript
|
|
474
|
+
bigMap.forEach((value, key) => {
|
|
475
|
+
console.log(`${key}: ${value}`);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
// With custom context
|
|
479
|
+
const context = { total: 0 };
|
|
480
|
+
bigMap.forEach(function(value) {
|
|
481
|
+
this.total += value;
|
|
482
|
+
}, context);
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### keys
|
|
486
|
+
|
|
487
|
+
Returns an iterator of all keys in the map.
|
|
488
|
+
|
|
489
|
+
**Returns:** `IterableIterator<K>`
|
|
490
|
+
|
|
491
|
+
**Example:**
|
|
492
|
+
```typescript
|
|
493
|
+
for (const key of bigMap.keys()) {
|
|
494
|
+
console.log(key);
|
|
495
|
+
}
|
|
496
|
+
```
|
|
32
497
|
|
|
33
|
-
|
|
498
|
+
### values
|
|
499
|
+
|
|
500
|
+
Returns an iterator of all values in the map.
|
|
501
|
+
|
|
502
|
+
**Returns:** `IterableIterator<V>`
|
|
503
|
+
|
|
504
|
+
**Example:**
|
|
505
|
+
```typescript
|
|
506
|
+
for (const value of bigMap.values()) {
|
|
507
|
+
console.log(value);
|
|
508
|
+
}
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### entries
|
|
512
|
+
|
|
513
|
+
Returns an iterator of all key-value pairs in the map.
|
|
514
|
+
|
|
515
|
+
**Returns:** `IterableIterator<[K, V]>`
|
|
516
|
+
|
|
517
|
+
**Example:**
|
|
518
|
+
```typescript
|
|
519
|
+
for (const [key, value] of bigMap.entries()) {
|
|
520
|
+
console.log(key, value);
|
|
521
|
+
}
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
### Symbol.iterator
|
|
525
|
+
|
|
526
|
+
Returns an iterator for the map (same as `entries()`). Enables `for...of` loops.
|
|
527
|
+
|
|
528
|
+
**Returns:** `IterableIterator<[K, V]>`
|
|
529
|
+
|
|
530
|
+
**Example:**
|
|
531
|
+
```typescript
|
|
532
|
+
for (const [key, value] of bigMap) {
|
|
533
|
+
console.log(key, value);
|
|
534
|
+
}
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
### getStore
|
|
538
|
+
|
|
539
|
+
Gets the internal Map instance for a specific key.
|
|
540
|
+
|
|
541
|
+
**Parameters:**
|
|
542
|
+
- `key` (K): The key to find the store for
|
|
543
|
+
|
|
544
|
+
**Returns:** `Map<K, V>` - The internal Map instance
|
|
545
|
+
|
|
546
|
+
**Example:**
|
|
547
|
+
```typescript
|
|
548
|
+
const store = bigMap.getStore('user123');
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
### getStoreMap
|
|
552
|
+
|
|
553
|
+
Gets the internal Map instance at a specific index.
|
|
554
|
+
|
|
555
|
+
**Parameters:**
|
|
556
|
+
- `index` (number): The index of the Map to retrieve (0 to storeSize - 1)
|
|
557
|
+
|
|
558
|
+
**Returns:** `Map<K, V>` - The Map at the specified index
|
|
559
|
+
|
|
560
|
+
**Throws:** Error if index is out of bounds
|
|
561
|
+
|
|
562
|
+
**Example:**
|
|
563
|
+
```typescript
|
|
564
|
+
const firstMap = bigMap.getStoreMap(0);
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
### initStore
|
|
568
|
+
|
|
569
|
+
Initializes the internal store with empty Map instances. Called automatically during construction.
|
|
570
|
+
|
|
571
|
+
**Returns:** `void`
|
|
572
|
+
|
|
573
|
+
## Types
|
|
574
|
+
|
|
575
|
+
### StoreHashFunction
|
|
576
|
+
|
|
577
|
+
Type definition for custom hash functions.
|
|
578
|
+
|
|
579
|
+
```typescript
|
|
580
|
+
type StoreHashFunction = (key: string, storeSize: number) => number;
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
**Parameters:**
|
|
584
|
+
- `key` (string): The key to hash (converted to string)
|
|
585
|
+
- `storeSize` (number): The number of stores (adjusted for zero-based index)
|
|
586
|
+
|
|
587
|
+
**Returns:** `number` - The index of the store to use (0 to storeSize - 1)
|
|
588
|
+
|
|
589
|
+
### defaultHashFunction
|
|
590
|
+
|
|
591
|
+
The default hash function using DJB2 algorithm from [Hashery](https://npmjs.com/package/hashery):
|
|
592
|
+
|
|
593
|
+
**Example:**
|
|
594
|
+
```typescript
|
|
595
|
+
import { defaultHashFunction } from '@keyv/bigmap';
|
|
596
|
+
|
|
597
|
+
const index = defaultHashFunction('myKey', 4);
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### djb2Hash
|
|
601
|
+
|
|
602
|
+
DJB2 hash algorithm implementation.
|
|
603
|
+
|
|
604
|
+
**Parameters:**
|
|
605
|
+
- `string` (string): The string to hash
|
|
606
|
+
- `min` (number): Minimum value. Default: `0`
|
|
607
|
+
- `max` (number): Maximum value. Default: `10`
|
|
608
|
+
|
|
609
|
+
**Returns:** `number` - Hash value within the specified range
|
|
610
|
+
|
|
611
|
+
**Example:**
|
|
612
|
+
```typescript
|
|
613
|
+
import { djb2Hash } from '@keyv/bigmap';
|
|
614
|
+
|
|
615
|
+
const hash = djb2Hash('myKey', 0, 10);
|
|
616
|
+
```
|
|
34
617
|
|
|
35
618
|
# Contributing
|
|
36
619
|
|
package/dist/index.cjs
CHANGED
|
@@ -21,21 +21,19 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
BigMap: () => BigMap,
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
Hashery: () => import_hashery2.Hashery,
|
|
25
|
+
Keyv: () => import_keyv2.Keyv,
|
|
26
|
+
createKeyv: () => createKeyv,
|
|
27
|
+
defaultHashFunction: () => defaultHashFunction
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
var import_hashery = require("hashery");
|
|
28
31
|
var import_hookified = require("hookified");
|
|
32
|
+
var import_keyv = require("keyv");
|
|
33
|
+
var import_hashery2 = require("hashery");
|
|
34
|
+
var import_keyv2 = require("keyv");
|
|
29
35
|
function defaultHashFunction(key, storeSize) {
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
function djb2Hash(string_, min = 0, max = 10) {
|
|
33
|
-
let hash = 5381;
|
|
34
|
-
for (let i = 0; i < string_.length; i++) {
|
|
35
|
-
hash = hash * 33 ^ string_.charCodeAt(i);
|
|
36
|
-
}
|
|
37
|
-
const range = max - min + 1;
|
|
38
|
-
return min + Math.abs(hash) % range;
|
|
36
|
+
return new import_hashery.Hashery().toNumberSync(key, { min: 0, max: storeSize - 1 });
|
|
39
37
|
}
|
|
40
38
|
var BigMap = class extends import_hookified.Hookified {
|
|
41
39
|
_storeSize = 4;
|
|
@@ -255,9 +253,16 @@ var BigMap = class extends import_hookified.Hookified {
|
|
|
255
253
|
return size;
|
|
256
254
|
}
|
|
257
255
|
};
|
|
256
|
+
function createKeyv(options) {
|
|
257
|
+
const adapter = new BigMap(options);
|
|
258
|
+
const keyv = new import_keyv.Keyv({ store: adapter });
|
|
259
|
+
return keyv;
|
|
260
|
+
}
|
|
258
261
|
// Annotate the CommonJS export names for ESM import in node:
|
|
259
262
|
0 && (module.exports = {
|
|
260
263
|
BigMap,
|
|
261
|
-
|
|
262
|
-
|
|
264
|
+
Hashery,
|
|
265
|
+
Keyv,
|
|
266
|
+
createKeyv,
|
|
267
|
+
defaultHashFunction
|
|
263
268
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { HookifiedOptions, Hookified } from 'hookified';
|
|
2
|
+
import { Keyv } from 'keyv';
|
|
3
|
+
export { Keyv } from 'keyv';
|
|
4
|
+
export { Hashery } from 'hashery';
|
|
2
5
|
|
|
3
6
|
type MapInterfacee<K, V> = {
|
|
4
7
|
readonly size: number;
|
|
@@ -15,7 +18,6 @@ type MapInterfacee<K, V> = {
|
|
|
15
18
|
};
|
|
16
19
|
type StoreHashFunction = (key: string, storeSize: number) => number;
|
|
17
20
|
declare function defaultHashFunction(key: string, storeSize: number): number;
|
|
18
|
-
declare function djb2Hash(string_: string, min?: number, max?: number): number;
|
|
19
21
|
type BigMapOptions = {
|
|
20
22
|
/**
|
|
21
23
|
* Optional size of the store. The default is 4 maps objects.
|
|
@@ -145,5 +147,11 @@ declare class BigMap<K, V> extends Hookified implements MapInterfacee<K, V> {
|
|
|
145
147
|
*/
|
|
146
148
|
get size(): number;
|
|
147
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Will create a Keyv instance with the BigMap adapter. This will also set the namespace and useKeyPrefix to false.
|
|
152
|
+
* @param {BigMapOptions} options - Options for the BigMap adapter such as storeSize and storeHashFunction.
|
|
153
|
+
* @returns {Keyv} - Keyv instance with the BigMap adapter
|
|
154
|
+
*/
|
|
155
|
+
declare function createKeyv<K = string, V = unknown>(options?: BigMapOptions): Keyv;
|
|
148
156
|
|
|
149
|
-
export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction,
|
|
157
|
+
export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction, createKeyv, defaultHashFunction };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { HookifiedOptions, Hookified } from 'hookified';
|
|
2
|
+
import { Keyv } from 'keyv';
|
|
3
|
+
export { Keyv } from 'keyv';
|
|
4
|
+
export { Hashery } from 'hashery';
|
|
2
5
|
|
|
3
6
|
type MapInterfacee<K, V> = {
|
|
4
7
|
readonly size: number;
|
|
@@ -15,7 +18,6 @@ type MapInterfacee<K, V> = {
|
|
|
15
18
|
};
|
|
16
19
|
type StoreHashFunction = (key: string, storeSize: number) => number;
|
|
17
20
|
declare function defaultHashFunction(key: string, storeSize: number): number;
|
|
18
|
-
declare function djb2Hash(string_: string, min?: number, max?: number): number;
|
|
19
21
|
type BigMapOptions = {
|
|
20
22
|
/**
|
|
21
23
|
* Optional size of the store. The default is 4 maps objects.
|
|
@@ -145,5 +147,11 @@ declare class BigMap<K, V> extends Hookified implements MapInterfacee<K, V> {
|
|
|
145
147
|
*/
|
|
146
148
|
get size(): number;
|
|
147
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Will create a Keyv instance with the BigMap adapter. This will also set the namespace and useKeyPrefix to false.
|
|
152
|
+
* @param {BigMapOptions} options - Options for the BigMap adapter such as storeSize and storeHashFunction.
|
|
153
|
+
* @returns {Keyv} - Keyv instance with the BigMap adapter
|
|
154
|
+
*/
|
|
155
|
+
declare function createKeyv<K = string, V = unknown>(options?: BigMapOptions): Keyv;
|
|
148
156
|
|
|
149
|
-
export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction,
|
|
157
|
+
export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction, createKeyv, defaultHashFunction };
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { Hashery } from "hashery";
|
|
2
3
|
import { Hookified } from "hookified";
|
|
4
|
+
import { Keyv } from "keyv";
|
|
5
|
+
import { Hashery as Hashery2 } from "hashery";
|
|
6
|
+
import { Keyv as Keyv2 } from "keyv";
|
|
3
7
|
function defaultHashFunction(key, storeSize) {
|
|
4
|
-
return
|
|
5
|
-
}
|
|
6
|
-
function djb2Hash(string_, min = 0, max = 10) {
|
|
7
|
-
let hash = 5381;
|
|
8
|
-
for (let i = 0; i < string_.length; i++) {
|
|
9
|
-
hash = hash * 33 ^ string_.charCodeAt(i);
|
|
10
|
-
}
|
|
11
|
-
const range = max - min + 1;
|
|
12
|
-
return min + Math.abs(hash) % range;
|
|
8
|
+
return new Hashery().toNumberSync(key, { min: 0, max: storeSize - 1 });
|
|
13
9
|
}
|
|
14
10
|
var BigMap = class extends Hookified {
|
|
15
11
|
_storeSize = 4;
|
|
@@ -229,8 +225,15 @@ var BigMap = class extends Hookified {
|
|
|
229
225
|
return size;
|
|
230
226
|
}
|
|
231
227
|
};
|
|
228
|
+
function createKeyv(options) {
|
|
229
|
+
const adapter = new BigMap(options);
|
|
230
|
+
const keyv = new Keyv({ store: adapter });
|
|
231
|
+
return keyv;
|
|
232
|
+
}
|
|
232
233
|
export {
|
|
233
234
|
BigMap,
|
|
234
|
-
|
|
235
|
-
|
|
235
|
+
Hashery2 as Hashery,
|
|
236
|
+
Keyv2 as Keyv,
|
|
237
|
+
createKeyv,
|
|
238
|
+
defaultHashFunction
|
|
236
239
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keyv/bigmap",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Bigmap for Keyv",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -32,16 +32,20 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/jaredwray/keyv",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"
|
|
35
|
+
"hashery": "^1.2.0",
|
|
36
|
+
"hookified": "^1.12.2"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@biomejs/biome": "^2.2.
|
|
39
|
-
"@faker-js/faker": "^10.
|
|
39
|
+
"@biomejs/biome": "^2.2.6",
|
|
40
|
+
"@faker-js/faker": "^10.1.0",
|
|
40
41
|
"@vitest/coverage-v8": "^3.2.4",
|
|
41
42
|
"rimraf": "^6.0.1",
|
|
42
43
|
"tsd": "^0.33.0",
|
|
43
44
|
"vitest": "^3.2.4"
|
|
44
45
|
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"keyv": "^5.5.4"
|
|
48
|
+
},
|
|
45
49
|
"tsd": {
|
|
46
50
|
"directory": "test"
|
|
47
51
|
},
|