@karmaniverous/entity-manager 6.14.1 → 6.14.2
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var radash = require('radash');
|
|
4
|
+
var stringHash = require('string-hash');
|
|
4
5
|
var encodeGeneratedProperty = require('./encodeGeneratedProperty.js');
|
|
5
6
|
var validateGeneratedProperty = require('./validateGeneratedProperty.js');
|
|
6
7
|
|
|
@@ -24,6 +25,9 @@ function getHashKeySpace(entityManager, entityToken, hashKeyToken, item, timesta
|
|
|
24
25
|
if (hashKeyToken !== entityManager.config.hashKey)
|
|
25
26
|
validateGeneratedProperty.validateGeneratedProperty(entityManager, hashKeyToken, true);
|
|
26
27
|
const { shardBumps } = entityManager.config.entities[entityToken];
|
|
28
|
+
// Detect presence of the entity's unique property on the item.
|
|
29
|
+
const uniqueProp = entityManager.config.entities[entityToken].uniqueProperty;
|
|
30
|
+
const uniqueValue = item[uniqueProp];
|
|
27
31
|
const hashKeySpace = shardBumps
|
|
28
32
|
// Filter shard bumps by timestamp range.
|
|
29
33
|
.filter((bump, i) => (i === shardBumps.length - 1 ||
|
|
@@ -32,9 +36,18 @@ function getHashKeySpace(entityManager, entityToken, hashKeyToken, item, timesta
|
|
|
32
36
|
// Generate shard key space.
|
|
33
37
|
.flatMap(({ charBits, chars }) => {
|
|
34
38
|
const radix = 2 ** charBits;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
// If the item's unique property is present, deterministically
|
|
40
|
+
// compute exactly one shard suffix for this bump. Otherwise,
|
|
41
|
+
// enumerate the full shard space for the bump.
|
|
42
|
+
if (chars) {
|
|
43
|
+
if (uniqueValue) {
|
|
44
|
+
const space = radix ** chars;
|
|
45
|
+
const mod = stringHash(uniqueValue) % space;
|
|
46
|
+
return mod.toString(radix).padStart(chars, '0');
|
|
47
|
+
}
|
|
48
|
+
return [...radash.range(0, radix ** chars - 1)].map((char) => char.toString(radix).padStart(chars, '0'));
|
|
49
|
+
}
|
|
50
|
+
return '';
|
|
38
51
|
})
|
|
39
52
|
// Map shard keys to hash keys.
|
|
40
53
|
.map((shardKey) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { range } from 'radash';
|
|
2
|
+
import stringHash from 'string-hash';
|
|
2
3
|
import { encodeGeneratedProperty } from './encodeGeneratedProperty.js';
|
|
3
4
|
import { validateGeneratedProperty } from './validateGeneratedProperty.js';
|
|
4
5
|
|
|
@@ -22,6 +23,9 @@ function getHashKeySpace(entityManager, entityToken, hashKeyToken, item, timesta
|
|
|
22
23
|
if (hashKeyToken !== entityManager.config.hashKey)
|
|
23
24
|
validateGeneratedProperty(entityManager, hashKeyToken, true);
|
|
24
25
|
const { shardBumps } = entityManager.config.entities[entityToken];
|
|
26
|
+
// Detect presence of the entity's unique property on the item.
|
|
27
|
+
const uniqueProp = entityManager.config.entities[entityToken].uniqueProperty;
|
|
28
|
+
const uniqueValue = item[uniqueProp];
|
|
25
29
|
const hashKeySpace = shardBumps
|
|
26
30
|
// Filter shard bumps by timestamp range.
|
|
27
31
|
.filter((bump, i) => (i === shardBumps.length - 1 ||
|
|
@@ -30,9 +34,18 @@ function getHashKeySpace(entityManager, entityToken, hashKeyToken, item, timesta
|
|
|
30
34
|
// Generate shard key space.
|
|
31
35
|
.flatMap(({ charBits, chars }) => {
|
|
32
36
|
const radix = 2 ** charBits;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
// If the item's unique property is present, deterministically
|
|
38
|
+
// compute exactly one shard suffix for this bump. Otherwise,
|
|
39
|
+
// enumerate the full shard space for the bump.
|
|
40
|
+
if (chars) {
|
|
41
|
+
if (uniqueValue) {
|
|
42
|
+
const space = radix ** chars;
|
|
43
|
+
const mod = stringHash(uniqueValue) % space;
|
|
44
|
+
return mod.toString(radix).padStart(chars, '0');
|
|
45
|
+
}
|
|
46
|
+
return [...range(0, radix ** chars - 1)].map((char) => char.toString(radix).padStart(chars, '0'));
|
|
47
|
+
}
|
|
48
|
+
return '';
|
|
36
49
|
})
|
|
37
50
|
// Map shard keys to hash keys.
|
|
38
51
|
.map((shardKey) => {
|
package/package.json
CHANGED