@fluidframework/container-runtime 2.0.0-dev.4.2.0.153917 → 2.0.0-dev.4.3.0.158678
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/dist/containerRuntime.d.ts +33 -3
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +187 -58
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStoreContext.d.ts +2 -1
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +3 -0
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStores.d.ts +5 -5
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +3 -6
- package/dist/dataStores.js.map +1 -1
- package/dist/gc/garbageCollection.d.ts.map +1 -1
- package/dist/gc/garbageCollection.js +5 -5
- package/dist/gc/garbageCollection.js.map +1 -1
- package/dist/gc/gcConfigs.d.ts.map +1 -1
- package/dist/gc/gcConfigs.js +1 -3
- package/dist/gc/gcConfigs.js.map +1 -1
- package/dist/gc/gcDefinitions.js +1 -1
- package/dist/gc/gcDefinitions.js.map +1 -1
- package/dist/id-compressor/appendOnlySortedMap.d.ts +146 -0
- package/dist/id-compressor/appendOnlySortedMap.d.ts.map +1 -0
- package/dist/id-compressor/appendOnlySortedMap.js +360 -0
- package/dist/id-compressor/appendOnlySortedMap.js.map +1 -0
- package/dist/id-compressor/idCompressor.d.ts +279 -0
- package/dist/id-compressor/idCompressor.d.ts.map +1 -0
- package/dist/id-compressor/idCompressor.js +1258 -0
- package/dist/id-compressor/idCompressor.js.map +1 -0
- package/dist/id-compressor/idRange.d.ts +11 -0
- package/dist/id-compressor/idRange.d.ts.map +1 -0
- package/dist/id-compressor/idRange.js +29 -0
- package/dist/id-compressor/idRange.js.map +1 -0
- package/dist/id-compressor/index.d.ts +14 -0
- package/dist/id-compressor/index.d.ts.map +1 -0
- package/dist/id-compressor/index.js +38 -0
- package/dist/id-compressor/index.js.map +1 -0
- package/dist/id-compressor/numericUuid.d.ts +59 -0
- package/dist/id-compressor/numericUuid.d.ts.map +1 -0
- package/dist/id-compressor/numericUuid.js +325 -0
- package/dist/id-compressor/numericUuid.js.map +1 -0
- package/dist/id-compressor/sessionIdNormalizer.d.ts +138 -0
- package/dist/id-compressor/sessionIdNormalizer.d.ts.map +1 -0
- package/dist/id-compressor/sessionIdNormalizer.js +488 -0
- package/dist/id-compressor/sessionIdNormalizer.js.map +1 -0
- package/dist/id-compressor/utils.d.ts +57 -0
- package/dist/id-compressor/utils.d.ts.map +1 -0
- package/dist/id-compressor/utils.js +90 -0
- package/dist/id-compressor/utils.js.map +1 -0
- package/dist/id-compressor/uuidUtilities.d.ts +30 -0
- package/dist/id-compressor/uuidUtilities.d.ts.map +1 -0
- package/dist/id-compressor/uuidUtilities.js +106 -0
- package/dist/id-compressor/uuidUtilities.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/opLifecycle/batchManager.d.ts +9 -2
- package/dist/opLifecycle/batchManager.d.ts.map +1 -1
- package/dist/opLifecycle/batchManager.js +21 -2
- package/dist/opLifecycle/batchManager.js.map +1 -1
- package/dist/opLifecycle/index.d.ts +1 -1
- package/dist/opLifecycle/index.d.ts.map +1 -1
- package/dist/opLifecycle/index.js.map +1 -1
- package/dist/opLifecycle/opGroupingManager.d.ts.map +1 -1
- package/dist/opLifecycle/opGroupingManager.js +5 -0
- package/dist/opLifecycle/opGroupingManager.js.map +1 -1
- package/dist/opLifecycle/outbox.d.ts +2 -2
- package/dist/opLifecycle/outbox.d.ts.map +1 -1
- package/dist/opLifecycle/outbox.js +34 -22
- package/dist/opLifecycle/outbox.js.map +1 -1
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.js +1 -1
- package/dist/packageVersion.js.map +1 -1
- package/dist/pendingStateManager.d.ts +1 -1
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +11 -3
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/summary/index.d.ts +1 -1
- package/dist/summary/index.d.ts.map +1 -1
- package/dist/summary/index.js +2 -1
- package/dist/summary/index.js.map +1 -1
- package/dist/summary/orderedClientElection.d.ts +1 -0
- package/dist/summary/orderedClientElection.d.ts.map +1 -1
- package/dist/summary/orderedClientElection.js +17 -1
- package/dist/summary/orderedClientElection.js.map +1 -1
- package/dist/summary/runningSummarizer.d.ts +0 -1
- package/dist/summary/runningSummarizer.d.ts.map +1 -1
- package/dist/summary/runningSummarizer.js +1 -17
- package/dist/summary/runningSummarizer.js.map +1 -1
- package/dist/summary/summaryFormat.d.ts +3 -0
- package/dist/summary/summaryFormat.d.ts.map +1 -1
- package/dist/summary/summaryFormat.js +3 -1
- package/dist/summary/summaryFormat.js.map +1 -1
- package/dist/summary/summaryManager.d.ts.map +1 -1
- package/dist/summary/summaryManager.js +2 -0
- package/dist/summary/summaryManager.js.map +1 -1
- package/lib/containerRuntime.d.ts +33 -3
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +170 -60
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStoreContext.d.ts +2 -1
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +3 -0
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStores.d.ts +5 -5
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +3 -6
- package/lib/dataStores.js.map +1 -1
- package/lib/gc/garbageCollection.d.ts.map +1 -1
- package/lib/gc/garbageCollection.js +5 -5
- package/lib/gc/garbageCollection.js.map +1 -1
- package/lib/gc/gcConfigs.d.ts.map +1 -1
- package/lib/gc/gcConfigs.js +1 -3
- package/lib/gc/gcConfigs.js.map +1 -1
- package/lib/gc/gcDefinitions.js +1 -1
- package/lib/gc/gcDefinitions.js.map +1 -1
- package/lib/id-compressor/appendOnlySortedMap.d.ts +146 -0
- package/lib/id-compressor/appendOnlySortedMap.d.ts.map +1 -0
- package/lib/id-compressor/appendOnlySortedMap.js +355 -0
- package/lib/id-compressor/appendOnlySortedMap.js.map +1 -0
- package/lib/id-compressor/idCompressor.d.ts +279 -0
- package/lib/id-compressor/idCompressor.d.ts.map +1 -0
- package/lib/id-compressor/idCompressor.js +1248 -0
- package/lib/id-compressor/idCompressor.js.map +1 -0
- package/lib/id-compressor/idRange.d.ts +11 -0
- package/lib/id-compressor/idRange.d.ts.map +1 -0
- package/lib/id-compressor/idRange.js +25 -0
- package/lib/id-compressor/idRange.js.map +1 -0
- package/lib/id-compressor/index.d.ts +14 -0
- package/lib/id-compressor/index.d.ts.map +1 -0
- package/lib/id-compressor/index.js +14 -0
- package/lib/id-compressor/index.js.map +1 -0
- package/lib/id-compressor/numericUuid.d.ts +59 -0
- package/lib/id-compressor/numericUuid.d.ts.map +1 -0
- package/lib/id-compressor/numericUuid.js +315 -0
- package/lib/id-compressor/numericUuid.js.map +1 -0
- package/lib/id-compressor/sessionIdNormalizer.d.ts +138 -0
- package/lib/id-compressor/sessionIdNormalizer.d.ts.map +1 -0
- package/lib/id-compressor/sessionIdNormalizer.js +484 -0
- package/lib/id-compressor/sessionIdNormalizer.js.map +1 -0
- package/lib/id-compressor/utils.d.ts +57 -0
- package/lib/id-compressor/utils.d.ts.map +1 -0
- package/lib/id-compressor/utils.js +79 -0
- package/lib/id-compressor/utils.js.map +1 -0
- package/lib/id-compressor/uuidUtilities.d.ts +30 -0
- package/lib/id-compressor/uuidUtilities.d.ts.map +1 -0
- package/lib/id-compressor/uuidUtilities.js +98 -0
- package/lib/id-compressor/uuidUtilities.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/opLifecycle/batchManager.d.ts +9 -2
- package/lib/opLifecycle/batchManager.d.ts.map +1 -1
- package/lib/opLifecycle/batchManager.js +19 -1
- package/lib/opLifecycle/batchManager.js.map +1 -1
- package/lib/opLifecycle/index.d.ts +1 -1
- package/lib/opLifecycle/index.d.ts.map +1 -1
- package/lib/opLifecycle/index.js.map +1 -1
- package/lib/opLifecycle/opGroupingManager.d.ts.map +1 -1
- package/lib/opLifecycle/opGroupingManager.js +5 -0
- package/lib/opLifecycle/opGroupingManager.js.map +1 -1
- package/lib/opLifecycle/outbox.d.ts +2 -2
- package/lib/opLifecycle/outbox.d.ts.map +1 -1
- package/lib/opLifecycle/outbox.js +35 -23
- package/lib/opLifecycle/outbox.js.map +1 -1
- package/lib/packageVersion.d.ts +1 -1
- package/lib/packageVersion.js +1 -1
- package/lib/packageVersion.js.map +1 -1
- package/lib/pendingStateManager.d.ts +1 -1
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +11 -3
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/summary/index.d.ts +1 -1
- package/lib/summary/index.d.ts.map +1 -1
- package/lib/summary/index.js +1 -1
- package/lib/summary/index.js.map +1 -1
- package/lib/summary/orderedClientElection.d.ts +1 -0
- package/lib/summary/orderedClientElection.d.ts.map +1 -1
- package/lib/summary/orderedClientElection.js +17 -1
- package/lib/summary/orderedClientElection.js.map +1 -1
- package/lib/summary/runningSummarizer.d.ts +0 -1
- package/lib/summary/runningSummarizer.d.ts.map +1 -1
- package/lib/summary/runningSummarizer.js +1 -17
- package/lib/summary/runningSummarizer.js.map +1 -1
- package/lib/summary/summaryFormat.d.ts +3 -0
- package/lib/summary/summaryFormat.d.ts.map +1 -1
- package/lib/summary/summaryFormat.js +2 -0
- package/lib/summary/summaryFormat.js.map +1 -1
- package/lib/summary/summaryManager.d.ts.map +1 -1
- package/lib/summary/summaryManager.js +2 -0
- package/lib/summary/summaryManager.js.map +1 -1
- package/package.json +27 -18
- package/src/containerRuntime.ts +256 -88
- package/src/dataStoreContext.ts +6 -0
- package/src/dataStores.ts +4 -7
- package/src/gc/garbageCollection.ts +7 -6
- package/src/gc/gcConfigs.ts +1 -3
- package/src/gc/gcDefinitions.ts +1 -1
- package/src/id-compressor/README.md +3 -0
- package/src/id-compressor/appendOnlySortedMap.ts +427 -0
- package/src/id-compressor/idCompressor.ts +1854 -0
- package/src/id-compressor/idRange.ts +35 -0
- package/src/id-compressor/index.ts +35 -0
- package/src/id-compressor/numericUuid.ts +383 -0
- package/src/id-compressor/sessionIdNormalizer.ts +609 -0
- package/src/id-compressor/utils.ts +114 -0
- package/src/id-compressor/uuidUtilities.ts +123 -0
- package/src/index.ts +1 -0
- package/src/opLifecycle/README.md +13 -0
- package/src/opLifecycle/batchManager.ts +35 -2
- package/src/opLifecycle/index.ts +1 -1
- package/src/opLifecycle/opGroupingManager.ts +5 -1
- package/src/opLifecycle/outbox.ts +57 -23
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +21 -7
- package/src/summary/index.ts +1 -0
- package/src/summary/orderedClientElection.ts +15 -2
- package/src/summary/runningSummarizer.ts +3 -24
- package/src/summary/summaryFormat.ts +4 -0
- package/src/summary/summaryManager.ts +2 -0
|
@@ -0,0 +1,1248 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { assert } from "@fluidframework/common-utils";
|
|
6
|
+
import BTree from "sorted-btree";
|
|
7
|
+
import { hasAtLeastLength, compareFiniteNumbers, compareFiniteNumbersReversed, compareMaps, compareStrings, fail, setPropertyIfDefined, } from "./utils";
|
|
8
|
+
import { assertIsStableId, isStableId } from "./uuidUtilities";
|
|
9
|
+
import { AppendOnlySortedMap } from "./appendOnlySortedMap";
|
|
10
|
+
import { getIds } from "./idRange";
|
|
11
|
+
import { numericUuidEquals, getPositiveDelta, incrementUuid, numericUuidFromStableId, stableIdFromNumericUuid, ensureSessionUuid, } from "./numericUuid";
|
|
12
|
+
import { SessionIdNormalizer } from "./sessionIdNormalizer";
|
|
13
|
+
/**
|
|
14
|
+
* Roughly equates to a minimum of 1M sessions before we start allocating 64 bit IDs.
|
|
15
|
+
* This value must *NOT* change without careful consideration to compatibility.
|
|
16
|
+
*/
|
|
17
|
+
export const defaultClusterCapacity = 512;
|
|
18
|
+
/**
|
|
19
|
+
* The base UUID for the reserved id cluster.
|
|
20
|
+
* This should not be changed without consideration to compatibility.
|
|
21
|
+
*/
|
|
22
|
+
const reservedSessionId = ensureSessionUuid(assertIsStableId("decaf40b-3c1a-47f8-a7a1-e8461ddb69ce"));
|
|
23
|
+
/**
|
|
24
|
+
* @returns true if the supplied ID is a final ID.
|
|
25
|
+
*/
|
|
26
|
+
export function isFinalId(id) {
|
|
27
|
+
return id >= 0;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @returns true if the supplied ID is a local ID.
|
|
31
|
+
*/
|
|
32
|
+
export function isLocalId(id) {
|
|
33
|
+
return id < 0;
|
|
34
|
+
}
|
|
35
|
+
/** Prepended to all keys in {@link IdCompressor.clustersAndOverridesInversion} that are override strings and not valid `StableIds` */
|
|
36
|
+
const nonStableOverridePrefix = "\ue15e"; // A character in the Private Use Area of the BMP (https://en.wikipedia.org/wiki/Private_Use_Areas)
|
|
37
|
+
/**
|
|
38
|
+
* See {@link IIdCompressor}
|
|
39
|
+
*/
|
|
40
|
+
export class IdCompressor {
|
|
41
|
+
/**
|
|
42
|
+
* @param localSessionId - the `IdCompressor`'s current local session ID.
|
|
43
|
+
* {@link generateStableId}.
|
|
44
|
+
*/
|
|
45
|
+
constructor(localSessionId, logger) {
|
|
46
|
+
this.localSessionId = localSessionId;
|
|
47
|
+
this.logger = logger;
|
|
48
|
+
/**
|
|
49
|
+
* Trivially reach consensus on default cluster size and reserved IDs.
|
|
50
|
+
* These initial values must *NOT* change without careful consideration to compatibility.
|
|
51
|
+
*/
|
|
52
|
+
this.newClusterCapacity = defaultClusterCapacity;
|
|
53
|
+
/**
|
|
54
|
+
* Session ID -\> data about the session's current cluster.
|
|
55
|
+
* Sessions are mutable, and thus should only be created via `createSession`.
|
|
56
|
+
*/
|
|
57
|
+
this.sessions = new Map();
|
|
58
|
+
/**
|
|
59
|
+
* The base final ID of the next cluster to be created.
|
|
60
|
+
*/
|
|
61
|
+
this.nextClusterBaseFinalId = 0;
|
|
62
|
+
/**
|
|
63
|
+
* Total number of IDs created locally during the current session.
|
|
64
|
+
*/
|
|
65
|
+
this.localIdCount = 0;
|
|
66
|
+
/**
|
|
67
|
+
* Maps local IDs to override strings. This will contain an entry for every override assigned to a local ID generated during
|
|
68
|
+
* the current session, and retains entries for the lifetime of this compressor.
|
|
69
|
+
*/
|
|
70
|
+
this.localOverrides = new AppendOnlySortedMap(compareFiniteNumbersReversed);
|
|
71
|
+
/**
|
|
72
|
+
* Maps local IDs to the final ID they are associated with (if any), and maps final IDs to the corresponding local ID (if any).
|
|
73
|
+
* This is used to efficiently compute normalization. This map can be thought of as mapping ranges of "optimistic uncertainty"
|
|
74
|
+
* (local IDs) to the result of consensus (reserved ranges of final IDs, a.k.a. clusters). Any given range of local IDs
|
|
75
|
+
* does not necessarily span an entire cluster, as some session-space IDs may be allocated *after* a cluster has been allocated
|
|
76
|
+
* but before it is full. In this case, there is no uncertainty, as the range of final IDs was reserved when the cluster was created.
|
|
77
|
+
* However, there is always a range of local IDs with size \>= 1 associated with the beginning of every cluster, as clusters are only
|
|
78
|
+
* created *after* they are needed and thus there is some period of uncertainty after local IDs have been handed out but before the
|
|
79
|
+
* range containing them has been finalized. There may also be ranges of local IDs that do not start at the beginning of a
|
|
80
|
+
* cluster; this happens when a cluster is expanded instead of allocating a new one.
|
|
81
|
+
* Additionally, session space IDs associated with an override string will also always be local IDs, because there is uncertainty as
|
|
82
|
+
* to whether another client simultaneously allocated the same override and could get sequenced first (a.k.a. unification) and its
|
|
83
|
+
* final ID would be associated with that override.
|
|
84
|
+
* See `SessionIdNormalizer` for more.
|
|
85
|
+
*/
|
|
86
|
+
this.sessionIdNormalizer = new SessionIdNormalizer();
|
|
87
|
+
/**
|
|
88
|
+
* Contains entries for cluster base UUIDs and override strings (both local and final).
|
|
89
|
+
* As a performance optimization, entries for finalized strings also include the containing cluster object.
|
|
90
|
+
* This can be viewed as three separate tables: the inverse table for `localOverrides`, the inverse table for the union of all
|
|
91
|
+
* the overrides of the clusters in `finalIdToCluster`, and the inverse lookup of cluster base UUIDs to their clusters.
|
|
92
|
+
* This is unified as a performance optimization, as the common case does not have overridden IDs. It is a btree due to the need
|
|
93
|
+
* to make range queries.
|
|
94
|
+
*/
|
|
95
|
+
this.clustersAndOverridesInversion = new BTree(undefined, compareStrings);
|
|
96
|
+
/**
|
|
97
|
+
* Maps the first final ID in a cluster to its owning cluster.
|
|
98
|
+
* Can be searched in O(log n) to determine clusters for any final ID.
|
|
99
|
+
*/
|
|
100
|
+
this.finalIdToCluster = new AppendOnlySortedMap(compareFiniteNumbers);
|
|
101
|
+
this.localSession = this.createSession(localSessionId);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The size of each newly created ID cluster.
|
|
105
|
+
*/
|
|
106
|
+
get clusterCapacity() {
|
|
107
|
+
return this.newClusterCapacity;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Must only be set with a value upon which consensus has been reached. Value must be greater than zero and less than
|
|
111
|
+
* `IdCompressor.maxClusterSize`.
|
|
112
|
+
*/
|
|
113
|
+
set clusterCapacity(value) {
|
|
114
|
+
assert(value > 0, 0x481 /* Clusters must have a positive capacity */);
|
|
115
|
+
assert(value <= IdCompressor.maxClusterSize, 0x482 /* Clusters must not exceed max cluster size */);
|
|
116
|
+
this.newClusterCapacity = value;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Creates a session object for the supplied ID.
|
|
120
|
+
* Must only be called once per ID.
|
|
121
|
+
* @param sessionId - the ID for the session
|
|
122
|
+
* @returns the session object for the supplied ID
|
|
123
|
+
*/
|
|
124
|
+
createSession(sessionId) {
|
|
125
|
+
assert(!this.clustersAndOverridesInversion.has(sessionId), 0x484 /* Attempted to create duplicate session */);
|
|
126
|
+
const existingSession = this.sessions.get(sessionId);
|
|
127
|
+
if (existingSession !== undefined) {
|
|
128
|
+
fail("createSession must only be called once for each session ID.");
|
|
129
|
+
}
|
|
130
|
+
const sessionUuid = numericUuidFromStableId(sessionId);
|
|
131
|
+
const session = {
|
|
132
|
+
sessionUuid,
|
|
133
|
+
currentClusterDetails: undefined,
|
|
134
|
+
lastFinalizedLocalId: undefined,
|
|
135
|
+
};
|
|
136
|
+
this.sessions.set(sessionId, session);
|
|
137
|
+
return session;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Returns a range of local IDs created by this session in a format for sending to the server for finalizing.
|
|
141
|
+
* The range will include all local IDs generated via calls to `generateCompressedId` since the last time this method was called.
|
|
142
|
+
* @returns the range of session-local IDs, which may be empty. This range must be sent to the server for ordering before
|
|
143
|
+
* it is finalized. Ranges must be sent to the server in the order that they are taken via calls to this method.
|
|
144
|
+
*/
|
|
145
|
+
takeNextCreationRange() {
|
|
146
|
+
var _a;
|
|
147
|
+
const lastLocalInRange = -this.localIdCount;
|
|
148
|
+
const lastTakenNormalized = (_a = this.lastTakenLocalId) !== null && _a !== void 0 ? _a : 0;
|
|
149
|
+
assert(lastLocalInRange <= lastTakenNormalized, 0x485 /* Inconsistent local ID state */);
|
|
150
|
+
let ids;
|
|
151
|
+
if (lastLocalInRange !== lastTakenNormalized) {
|
|
152
|
+
const firstLocalInRange = (lastTakenNormalized - 1);
|
|
153
|
+
const overrides = [
|
|
154
|
+
...this.localOverrides.getRange((lastTakenNormalized - 1), lastLocalInRange),
|
|
155
|
+
];
|
|
156
|
+
if (hasAtLeastLength(overrides, 1)) {
|
|
157
|
+
assert(overrides[0][0] <= firstLocalInRange, 0x486 /* Inconsistent override state */);
|
|
158
|
+
assert(overrides[overrides.length - 1][0] >= lastLocalInRange, 0x487 /* Inconsistent override state */);
|
|
159
|
+
ids = {
|
|
160
|
+
overrides,
|
|
161
|
+
};
|
|
162
|
+
const first = firstLocalInRange === overrides[0][0] ? undefined : firstLocalInRange;
|
|
163
|
+
const last = lastLocalInRange === overrides[overrides.length - 1][0]
|
|
164
|
+
? undefined
|
|
165
|
+
: lastLocalInRange;
|
|
166
|
+
setPropertyIfDefined(first, ids, "first");
|
|
167
|
+
setPropertyIfDefined(last, ids, "last");
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
ids = {
|
|
171
|
+
first: firstLocalInRange,
|
|
172
|
+
last: lastLocalInRange,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
this.lastTakenLocalId = lastLocalInRange;
|
|
176
|
+
}
|
|
177
|
+
const range = { sessionId: this.localSessionId };
|
|
178
|
+
if (ids === undefined) {
|
|
179
|
+
return range;
|
|
180
|
+
}
|
|
181
|
+
assert(this.lastTakenLocalId === -this.localIdCount &&
|
|
182
|
+
this.lastTakenLocalId !== lastTakenNormalized, 0x488 /* Non-empty range must properly consume local IDs */);
|
|
183
|
+
range.ids = ids;
|
|
184
|
+
return range;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Finalizes the supplied range of IDs (which may be from either a remote or local session).
|
|
188
|
+
* @param range - the range of session-local IDs to finalize.
|
|
189
|
+
*/
|
|
190
|
+
finalizeCreationRange(range) {
|
|
191
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
192
|
+
const { sessionId } = range;
|
|
193
|
+
const isLocal = sessionId === this.localSessionId;
|
|
194
|
+
const session = (_a = this.sessions.get(sessionId)) !== null && _a !== void 0 ? _a : this.createSession(sessionId);
|
|
195
|
+
const ids = getIds(range);
|
|
196
|
+
if (ids === undefined) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const { currentClusterDetails } = session;
|
|
200
|
+
const { cluster: currentCluster, clusterBase: currentBaseFinalId } = currentClusterDetails !== null && currentClusterDetails !== void 0 ? currentClusterDetails : {
|
|
201
|
+
cluster: undefined,
|
|
202
|
+
clusterBase: undefined,
|
|
203
|
+
};
|
|
204
|
+
const currentClusterExists = currentCluster !== undefined && currentBaseFinalId !== undefined;
|
|
205
|
+
const normalizedLastFinalizedLocal = (_b = session.lastFinalizedLocalId) !== null && _b !== void 0 ? _b : 0;
|
|
206
|
+
const { first: newFirstFinalizedLocal, last: newLastFinalizedLocal } = ids;
|
|
207
|
+
assert(newFirstFinalizedLocal === normalizedLastFinalizedLocal - 1, 0x489 /* Ranges finalized out of order. */);
|
|
208
|
+
// The total number of session-local IDs to finalize
|
|
209
|
+
const finalizeCount = normalizedLastFinalizedLocal - newLastFinalizedLocal;
|
|
210
|
+
assert(finalizeCount >= 1, 0x48a /* Cannot finalize an empty range. */);
|
|
211
|
+
let eagerFinalIdCount = 0;
|
|
212
|
+
let initialClusterCount = 0;
|
|
213
|
+
let remainingCount = finalizeCount;
|
|
214
|
+
let newBaseUuid;
|
|
215
|
+
if (currentClusterExists) {
|
|
216
|
+
if (isLocal) {
|
|
217
|
+
const lastKnownFinal = (_c = this.sessionIdNormalizer.getLastFinalId()) !== null && _c !== void 0 ? _c : fail("Cluster exists but normalizer does not have an entry for it.");
|
|
218
|
+
const lastAlignedFinalInCluster = (currentBaseFinalId +
|
|
219
|
+
Math.min(currentCluster.count + finalizeCount, currentCluster.capacity) -
|
|
220
|
+
1);
|
|
221
|
+
if (lastAlignedFinalInCluster > lastKnownFinal) {
|
|
222
|
+
this.sessionIdNormalizer.addFinalIds((lastKnownFinal + 1), lastAlignedFinalInCluster, currentCluster);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
initialClusterCount = currentCluster.count;
|
|
226
|
+
const remainingCapacity = currentCluster.capacity - initialClusterCount;
|
|
227
|
+
const overflow = remainingCount - remainingCapacity;
|
|
228
|
+
const hasRoom = overflow <= 0;
|
|
229
|
+
if (hasRoom || currentBaseFinalId === this.finalIdToCluster.maxKey()) {
|
|
230
|
+
currentCluster.count += remainingCount;
|
|
231
|
+
eagerFinalIdCount = remainingCount;
|
|
232
|
+
remainingCount = 0;
|
|
233
|
+
// The common case is that there is room in the cluster, and the new final IDs can simply be added to it
|
|
234
|
+
if (!hasRoom) {
|
|
235
|
+
// The cluster is full but is the last in the list of clusters.
|
|
236
|
+
// This allows it to be expanded instead of allocating a new one.
|
|
237
|
+
const expansionAmount = this.newClusterCapacity + overflow;
|
|
238
|
+
const previousCapacity = currentCluster.capacity;
|
|
239
|
+
currentCluster.capacity += expansionAmount;
|
|
240
|
+
this.nextClusterBaseFinalId = (this.nextClusterBaseFinalId +
|
|
241
|
+
expansionAmount);
|
|
242
|
+
assert(this.nextClusterBaseFinalId < Number.MAX_SAFE_INTEGER, 0x48b /* The number of allocated final IDs must not exceed the JS maximum safe integer. */);
|
|
243
|
+
this.checkClusterForCollision(currentCluster);
|
|
244
|
+
if (isLocal) {
|
|
245
|
+
// Example with cluster size of 3:
|
|
246
|
+
// Ids generated so far: -1 1 2 -4 -5 <-- note positive numbers are eager finals
|
|
247
|
+
// Cluster: [ 0 1 2 ]
|
|
248
|
+
// ~ finalizing happens, causing expansion of 2 (overflow) + 3 (cluster capacity) ~
|
|
249
|
+
// Cluster: [ 0 1 2 3 4 _ _ _ ]
|
|
250
|
+
// corresponding locals: -1 -4 -5
|
|
251
|
+
// lastFinalizedLocalId^ ^newLastFinalizedLocalId = -5
|
|
252
|
+
// overflow = 2: ----
|
|
253
|
+
// localIdPivot^
|
|
254
|
+
// lastFinalizedFinal^
|
|
255
|
+
const newLastFinalizedFinal = (currentBaseFinalId +
|
|
256
|
+
currentCluster.count -
|
|
257
|
+
1);
|
|
258
|
+
assert(session.lastFinalizedLocalId !== undefined, 0x48c /* Cluster already exists for session but there is no finalized local ID */);
|
|
259
|
+
const finalPivot = (newLastFinalizedFinal -
|
|
260
|
+
overflow +
|
|
261
|
+
1);
|
|
262
|
+
// Inform the normalizer of all IDs that we now know will end up being finalized into this cluster, including the ones
|
|
263
|
+
// that were given out as locals (non-eager) because they exceeded the bounds of the current cluster before it was expanded.
|
|
264
|
+
// It is safe to associate the unfinalized locals with their future final IDs even before the ranges for those locals are
|
|
265
|
+
// actually finalized, because total order broadcast guarantees that any usage of those final IDs will be observed after
|
|
266
|
+
// the finalization of the ranges.
|
|
267
|
+
this.sessionIdNormalizer.registerFinalIdBlock(finalPivot, expansionAmount, currentCluster);
|
|
268
|
+
(_d = this.logger) === null || _d === void 0 ? void 0 : _d.sendTelemetryEvent({
|
|
269
|
+
eventName: "RuntimeIdCompressor:ClusterExpansion",
|
|
270
|
+
sessionId: this.localSessionId,
|
|
271
|
+
previousCapacity,
|
|
272
|
+
newCapacity: currentCluster.capacity,
|
|
273
|
+
overflow,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
// The range cannot be fully allocated in the existing cluster, so allocate any space left in it and
|
|
280
|
+
// form a new one by incrementing the previous baseUuid
|
|
281
|
+
newBaseUuid = incrementUuid(currentCluster.baseUuid, currentCluster.capacity);
|
|
282
|
+
currentCluster.count += remainingCapacity;
|
|
283
|
+
remainingCount -= remainingCapacity;
|
|
284
|
+
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.sendTelemetryEvent({
|
|
285
|
+
eventName: "RuntimeIdCompressor:OverfilledCluster",
|
|
286
|
+
sessionId: this.localSessionId,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
// Session has never made a cluster, form a new one with the session UUID as the baseUuid
|
|
292
|
+
newBaseUuid = session.sessionUuid;
|
|
293
|
+
if (isLocal) {
|
|
294
|
+
(_f = this.logger) === null || _f === void 0 ? void 0 : _f.sendTelemetryEvent({
|
|
295
|
+
eventName: "RuntimeIdCompressor:FirstCluster",
|
|
296
|
+
sessionId: this.localSessionId,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// Finalizing a range results in one of three cases:
|
|
301
|
+
// 1. All local IDs are finalized into a new cluster (because there was either never a cluster for that session, or the current
|
|
302
|
+
// cluster for the session was full).
|
|
303
|
+
// 2. All local IDs are finalized into the existing (current) cluster for the session.
|
|
304
|
+
// 3. Local IDs are finalized into both the current cluster and a new one, as the current cluster did not have enough room.
|
|
305
|
+
let newCluster;
|
|
306
|
+
let newBaseFinalId;
|
|
307
|
+
// The first local ID that will be finalized into a new cluster, if there is one.
|
|
308
|
+
// This lets us quickly compare which cluster an override string will go into.
|
|
309
|
+
let localIdPivot;
|
|
310
|
+
// Need to make a new cluster
|
|
311
|
+
if (newBaseUuid !== undefined) {
|
|
312
|
+
if (remainingCount <= 0) {
|
|
313
|
+
fail("Should not create an empty cluster.");
|
|
314
|
+
}
|
|
315
|
+
if (currentCluster !== undefined && currentCluster.capacity !== currentCluster.count) {
|
|
316
|
+
fail("Cluster must be filled before another is allocated.");
|
|
317
|
+
}
|
|
318
|
+
newBaseFinalId = this.nextClusterBaseFinalId;
|
|
319
|
+
const newCapacity = Math.max(this.newClusterCapacity, remainingCount);
|
|
320
|
+
newCluster = {
|
|
321
|
+
baseUuid: newBaseUuid,
|
|
322
|
+
capacity: newCapacity,
|
|
323
|
+
count: remainingCount,
|
|
324
|
+
session,
|
|
325
|
+
};
|
|
326
|
+
const usedCapacity = finalizeCount - remainingCount;
|
|
327
|
+
localIdPivot = (newFirstFinalizedLocal - usedCapacity);
|
|
328
|
+
if (isLocal) {
|
|
329
|
+
(_g = this.logger) === null || _g === void 0 ? void 0 : _g.sendTelemetryEvent({
|
|
330
|
+
eventName: "RuntimeIdCompressor:NewCluster",
|
|
331
|
+
sessionId: this.localSessionId,
|
|
332
|
+
clusterCapacity: newCapacity,
|
|
333
|
+
clusterCount: remainingCount,
|
|
334
|
+
});
|
|
335
|
+
this.sessionIdNormalizer.registerFinalIdBlock(newBaseFinalId, newCluster.capacity, newCluster);
|
|
336
|
+
}
|
|
337
|
+
this.checkClusterForCollision(newCluster);
|
|
338
|
+
this.clustersAndOverridesInversion.set(stableIdFromNumericUuid(newCluster.baseUuid), {
|
|
339
|
+
clusterBase: newBaseFinalId,
|
|
340
|
+
cluster: newCluster,
|
|
341
|
+
});
|
|
342
|
+
session.currentClusterDetails = { cluster: newCluster, clusterBase: newBaseFinalId };
|
|
343
|
+
this.nextClusterBaseFinalId = (this.nextClusterBaseFinalId +
|
|
344
|
+
newCluster.capacity);
|
|
345
|
+
assert(this.nextClusterBaseFinalId < Number.MAX_SAFE_INTEGER, 0x48e /* The number of allocated final IDs must not exceed the JS maximum safe integer. */);
|
|
346
|
+
this.finalIdToCluster.append(newBaseFinalId, newCluster);
|
|
347
|
+
}
|
|
348
|
+
// If there are overrides, we must determine which cluster object (current or overflow) each belongs to and add it.
|
|
349
|
+
const overrides = ids.overrides;
|
|
350
|
+
if (overrides !== undefined) {
|
|
351
|
+
for (let i = 0; i < overrides.length; i++) {
|
|
352
|
+
const [overriddenLocal, override] = overrides[i];
|
|
353
|
+
// Note: recall that local IDs are negative
|
|
354
|
+
assert(i === 0 || overriddenLocal < overrides[i - 1][0], 0x48f /* Override IDs must be in sorted order. */);
|
|
355
|
+
assert(overriddenLocal < normalizedLastFinalizedLocal, 0x490 /* Ranges finalized out of order. */);
|
|
356
|
+
assert(overriddenLocal >= newLastFinalizedLocal, 0x491 /* Malformed range: override ID ahead of range start. */);
|
|
357
|
+
let cluster;
|
|
358
|
+
let overriddenFinal;
|
|
359
|
+
if (localIdPivot !== undefined && overriddenLocal <= localIdPivot) {
|
|
360
|
+
// Override is at or past the pivot, so it is in a new cluster.
|
|
361
|
+
assert(newCluster !== undefined && newBaseFinalId !== undefined, 0x492 /* No cluster was created when overflow occurred. */);
|
|
362
|
+
cluster = newCluster;
|
|
363
|
+
overriddenFinal = (newBaseFinalId +
|
|
364
|
+
(localIdPivot - overriddenLocal));
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
// Override was finalized into an existing cluster
|
|
368
|
+
assert(currentCluster !== undefined && currentBaseFinalId !== undefined, 0x493 /* No cluster exists but IDs were finalized. */);
|
|
369
|
+
cluster = currentCluster;
|
|
370
|
+
overriddenFinal = (currentBaseFinalId +
|
|
371
|
+
initialClusterCount +
|
|
372
|
+
(normalizedLastFinalizedLocal - overriddenLocal) -
|
|
373
|
+
1);
|
|
374
|
+
}
|
|
375
|
+
(_h = cluster.overrides) !== null && _h !== void 0 ? _h : (cluster.overrides = new Map());
|
|
376
|
+
const inversionKey = IdCompressor.createInversionKey(override);
|
|
377
|
+
const existingIds = this.getExistingIdsForNewOverride(inversionKey, true);
|
|
378
|
+
let overrideForCluster;
|
|
379
|
+
let associatedLocal;
|
|
380
|
+
if (existingIds !== undefined) {
|
|
381
|
+
let mostFinalExistingOverride;
|
|
382
|
+
if (typeof existingIds === "number") {
|
|
383
|
+
mostFinalExistingOverride = existingIds;
|
|
384
|
+
if (isLocalId(mostFinalExistingOverride)) {
|
|
385
|
+
associatedLocal = mostFinalExistingOverride;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
[associatedLocal, mostFinalExistingOverride] = existingIds;
|
|
390
|
+
}
|
|
391
|
+
if (isFinalId(mostFinalExistingOverride)) {
|
|
392
|
+
// A previous range already finalized an ID with this override. See `IdCluster` for more.
|
|
393
|
+
overrideForCluster = mostFinalExistingOverride;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
assert(!isLocal || mostFinalExistingOverride === overriddenLocal, 0x494 /* Cannot have multiple local IDs with identical overrides. */);
|
|
397
|
+
// This session has created an ID with this override before, but has not finalized it yet. The incoming
|
|
398
|
+
// range "wins" and will contain the final ID associated with that override, regardless of if that range was
|
|
399
|
+
// made by this session or not.
|
|
400
|
+
overrideForCluster = override;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
// This is the first time this override has been associated with any ID
|
|
405
|
+
overrideForCluster = override;
|
|
406
|
+
}
|
|
407
|
+
assert(!cluster.overrides.has(overriddenFinal), 0x495 /* Cannot add a second override for final id */);
|
|
408
|
+
if (typeof overrideForCluster === "string") {
|
|
409
|
+
if (isLocal || associatedLocal === undefined) {
|
|
410
|
+
cluster.overrides.set(overriddenFinal, override);
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
cluster.overrides.set(overriddenFinal, {
|
|
414
|
+
override,
|
|
415
|
+
originalOverridingFinal: overriddenFinal,
|
|
416
|
+
associatedLocalId: associatedLocal,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
const unifiedOverride = {
|
|
422
|
+
override,
|
|
423
|
+
originalOverridingFinal: overrideForCluster,
|
|
424
|
+
};
|
|
425
|
+
setPropertyIfDefined(associatedLocal, unifiedOverride, "associatedLocalId");
|
|
426
|
+
cluster.overrides.set(overriddenFinal, unifiedOverride);
|
|
427
|
+
}
|
|
428
|
+
const finalizedOverride = {
|
|
429
|
+
cluster,
|
|
430
|
+
originalOverridingFinal: overriddenFinal,
|
|
431
|
+
};
|
|
432
|
+
setPropertyIfDefined(associatedLocal, finalizedOverride, "associatedLocalId");
|
|
433
|
+
const currentOverride = this.clustersAndOverridesInversion.get(inversionKey);
|
|
434
|
+
if (currentOverride === undefined ||
|
|
435
|
+
IdCompressor.isUnfinalizedOverride(currentOverride)) {
|
|
436
|
+
// Update the map to contain a finalized override, but never update it with future finalized overrides with
|
|
437
|
+
// the same string; those should decompress to the first final ID with that override.
|
|
438
|
+
this.clustersAndOverridesInversion.set(inversionKey, finalizedOverride);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (isLocal) {
|
|
443
|
+
(_j = this.logger) === null || _j === void 0 ? void 0 : _j.sendTelemetryEvent({
|
|
444
|
+
eventName: "RuntimeIdCompressor:IdCompressorStatus",
|
|
445
|
+
eagerFinalIdCount: eagerFinalIdCount - ((_k = overrides === null || overrides === void 0 ? void 0 : overrides.length) !== null && _k !== void 0 ? _k : 0),
|
|
446
|
+
localIdCount: remainingCount + ((_l = overrides === null || overrides === void 0 ? void 0 : overrides.length) !== null && _l !== void 0 ? _l : 0),
|
|
447
|
+
overridesCount: (_m = overrides === null || overrides === void 0 ? void 0 : overrides.length) !== null && _m !== void 0 ? _m : 0,
|
|
448
|
+
sessionId: this.localSessionId,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
session.lastFinalizedLocalId = newLastFinalizedLocal;
|
|
452
|
+
}
|
|
453
|
+
checkClusterForCollision(cluster) {
|
|
454
|
+
const maxClusterUuid = incrementUuid(cluster.baseUuid, cluster.capacity - 1);
|
|
455
|
+
const maxClusterStableId = stableIdFromNumericUuid(maxClusterUuid);
|
|
456
|
+
const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(maxClusterStableId);
|
|
457
|
+
if (closestMatch !== undefined) {
|
|
458
|
+
const [inversionKey, compressionMapping] = closestMatch;
|
|
459
|
+
if (!IdCompressor.isClusterInfo(compressionMapping)) {
|
|
460
|
+
if (isStableId(inversionKey) &&
|
|
461
|
+
IdCompressor.uuidsMightCollide(inversionKey, maxClusterStableId, cluster.capacity)) {
|
|
462
|
+
const numericOverride = numericUuidFromStableId(inversionKey);
|
|
463
|
+
const delta = getPositiveDelta(maxClusterUuid, numericOverride, cluster.capacity - 1);
|
|
464
|
+
if (delta !== undefined) {
|
|
465
|
+
IdCompressor.failWithCollidingOverride(inversionKey);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
static failWithCollidingOverride(override) {
|
|
472
|
+
fail(`Override '${override}' collides with another allocated UUID.`);
|
|
473
|
+
}
|
|
474
|
+
static isClusterInfo(compressionMapping) {
|
|
475
|
+
return compressionMapping.clusterBase !== undefined;
|
|
476
|
+
}
|
|
477
|
+
static isUnfinalizedOverride(compressionMapping) {
|
|
478
|
+
return typeof compressionMapping === "number";
|
|
479
|
+
}
|
|
480
|
+
static createInversionKey(inversionKey) {
|
|
481
|
+
return isStableId(inversionKey)
|
|
482
|
+
? inversionKey
|
|
483
|
+
: `${nonStableOverridePrefix}${inversionKey}`;
|
|
484
|
+
}
|
|
485
|
+
static isStableInversionKey(inversionKey) {
|
|
486
|
+
return inversionKey.charAt(0) !== nonStableOverridePrefix;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Returns an existing ID associated with an override, or undefined if none exists.
|
|
490
|
+
*/
|
|
491
|
+
getExistingIdsForNewOverride(inversionKey, isFinalOverride) {
|
|
492
|
+
var _a;
|
|
493
|
+
const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(inversionKey, reusedArray);
|
|
494
|
+
let numericOverride;
|
|
495
|
+
let stableOverride;
|
|
496
|
+
if (closestMatch !== undefined) {
|
|
497
|
+
const [key, compressionMapping] = closestMatch;
|
|
498
|
+
if (!IdCompressor.isClusterInfo(compressionMapping)) {
|
|
499
|
+
if (key === inversionKey) {
|
|
500
|
+
if (IdCompressor.isUnfinalizedOverride(compressionMapping)) {
|
|
501
|
+
return compressionMapping;
|
|
502
|
+
}
|
|
503
|
+
const finalizedOverride = compressionMapping;
|
|
504
|
+
return finalizedOverride.associatedLocalId !== undefined
|
|
505
|
+
? [
|
|
506
|
+
finalizedOverride.associatedLocalId,
|
|
507
|
+
finalizedOverride.originalOverridingFinal,
|
|
508
|
+
]
|
|
509
|
+
: finalizedOverride.originalOverridingFinal;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else if (IdCompressor.isStableInversionKey(inversionKey)) {
|
|
513
|
+
stableOverride = inversionKey;
|
|
514
|
+
const cluster = compressionMapping.cluster;
|
|
515
|
+
if (IdCompressor.uuidsMightCollide(inversionKey, key, cluster.capacity)) {
|
|
516
|
+
numericOverride = numericUuidFromStableId(stableOverride);
|
|
517
|
+
const delta = getPositiveDelta(numericOverride, cluster.baseUuid, cluster.capacity - 1);
|
|
518
|
+
if (delta !== undefined) {
|
|
519
|
+
if (!isFinalOverride) {
|
|
520
|
+
if (delta >= cluster.count) {
|
|
521
|
+
// TODO:#283: Properly implement unification
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
return this.normalizeToSessionSpace((compressionMapping.clusterBase + delta));
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
const override = (_a = numericOverride !== null && numericOverride !== void 0 ? numericOverride : stableOverride) !== null && _a !== void 0 ? _a : (IdCompressor.isStableInversionKey(inversionKey) ? inversionKey : undefined);
|
|
531
|
+
if (override !== undefined) {
|
|
532
|
+
const sessionSpaceId = this.getCompressedIdForStableId(override);
|
|
533
|
+
if (sessionSpaceId !== undefined) {
|
|
534
|
+
return sessionSpaceId;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return undefined;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Check if `a` might be within `range` of `b`, where both are treated as hex numbers.
|
|
541
|
+
* @param range - an integer
|
|
542
|
+
*/
|
|
543
|
+
static uuidsMightCollide(a, b, range) {
|
|
544
|
+
// Check if any of the UUIDs in the cluster collide (i.e. any in [base, base + capacity)).
|
|
545
|
+
// Optimization: All UUIDs in a cluster are the same string up until the last few characters which encode the offset from
|
|
546
|
+
// the cluster base. So, first compute the length of that shared string, and early out if it is different from the override
|
|
547
|
+
// UUID. This way we usually need not do the more expensive check below.
|
|
548
|
+
const hexDigitsToCheck = 32 - Math.ceil(Math.log2(range) / 2);
|
|
549
|
+
if (a.startsWith(b.slice(0, hexDigitsToCheck))) {
|
|
550
|
+
return true;
|
|
551
|
+
}
|
|
552
|
+
return false;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Helper for retrieving an override.
|
|
556
|
+
*/
|
|
557
|
+
static tryGetOverride(cluster, finalId) {
|
|
558
|
+
var _a;
|
|
559
|
+
const override = (_a = cluster.overrides) === null || _a === void 0 ? void 0 : _a.get(finalId);
|
|
560
|
+
if (override === undefined) {
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
if (typeof override === "string") {
|
|
564
|
+
return override;
|
|
565
|
+
}
|
|
566
|
+
return override.override;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Generates a new compressed ID or returns an existing one.
|
|
570
|
+
* This should ONLY be called to generate IDs for local operations.
|
|
571
|
+
* @param override - Specifies a specific string to be associated with the returned compressed ID.
|
|
572
|
+
* Performance note: assigning override strings incurs a performance overhead.
|
|
573
|
+
* @returns an existing ID if one already exists for `override`, and a new local ID otherwise. The returned ID is in session space.
|
|
574
|
+
*/
|
|
575
|
+
generateCompressedId(override) {
|
|
576
|
+
let overrideInversionKey;
|
|
577
|
+
if (override !== undefined) {
|
|
578
|
+
overrideInversionKey = IdCompressor.createInversionKey(override);
|
|
579
|
+
const existingIds = this.getExistingIdsForNewOverride(overrideInversionKey, false);
|
|
580
|
+
if (existingIds !== undefined) {
|
|
581
|
+
return typeof existingIds === "number" ? existingIds : existingIds[0];
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
// Bump local counter regardless, then attempt to optimistically return a final ID.
|
|
585
|
+
// If the local session has reserved a cluster range via consensus, it is safe to hand out final IDs prior to
|
|
586
|
+
// finalizing the range that includes these locals.
|
|
587
|
+
const newLocalId = -++this.localIdCount;
|
|
588
|
+
const { currentClusterDetails } = this.localSession;
|
|
589
|
+
const { sessionIdNormalizer } = this;
|
|
590
|
+
let eagerFinalId;
|
|
591
|
+
let cluster;
|
|
592
|
+
if (currentClusterDetails !== undefined) {
|
|
593
|
+
cluster = currentClusterDetails.cluster;
|
|
594
|
+
const lastFinalKnown = sessionIdNormalizer.getLastFinalId();
|
|
595
|
+
if (lastFinalKnown !== undefined &&
|
|
596
|
+
lastFinalKnown - currentClusterDetails.clusterBase + 1 < cluster.capacity) {
|
|
597
|
+
eagerFinalId = (lastFinalKnown + 1);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (overrideInversionKey !== undefined) {
|
|
601
|
+
const registeredLocal = sessionIdNormalizer.addLocalId();
|
|
602
|
+
assert(registeredLocal === newLocalId, 0x496 /* Session ID Normalizer produced unexpected local ID */);
|
|
603
|
+
if (eagerFinalId !== undefined) {
|
|
604
|
+
sessionIdNormalizer.addFinalIds(eagerFinalId, eagerFinalId, cluster !== null && cluster !== void 0 ? cluster : fail("No cluster when generating compressed ID"));
|
|
605
|
+
}
|
|
606
|
+
this.localOverrides.append(newLocalId, override !== null && override !== void 0 ? override : fail("Override must be defined"));
|
|
607
|
+
// Since the local ID was just created, it is in both session and op space
|
|
608
|
+
const compressionMapping = newLocalId;
|
|
609
|
+
this.clustersAndOverridesInversion.set(overrideInversionKey, compressionMapping);
|
|
610
|
+
}
|
|
611
|
+
else if (eagerFinalId !== undefined) {
|
|
612
|
+
sessionIdNormalizer.addFinalIds(eagerFinalId, eagerFinalId, cluster !== null && cluster !== void 0 ? cluster : fail("No cluster when generating compressed ID"));
|
|
613
|
+
return eagerFinalId;
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
const registeredLocal = sessionIdNormalizer.addLocalId();
|
|
617
|
+
assert(registeredLocal === newLocalId, 0x497 /* Session ID Normalizer produced unexpected local ID */);
|
|
618
|
+
}
|
|
619
|
+
return newLocalId;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Decompresses a previously compressed ID into a UUID or override string.
|
|
623
|
+
* @param id - the compressed ID to be decompressed.
|
|
624
|
+
* @returns the UUID or override string associated with the compressed ID. Fails if the ID was not generated by this compressor.
|
|
625
|
+
*/
|
|
626
|
+
decompress(id) {
|
|
627
|
+
var _a;
|
|
628
|
+
return (_a = this.tryDecompress(id)) !== null && _a !== void 0 ? _a : fail("Compressed ID was not generated by this compressor");
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Attempts to decompress a previously compressed ID into a UUID or override string.
|
|
632
|
+
* @param id - the compressed ID to be decompressed.
|
|
633
|
+
* @returns the UUID or override string associated with the compressed ID, or undefined if the ID was not generated by this compressor.
|
|
634
|
+
*/
|
|
635
|
+
tryDecompress(id) {
|
|
636
|
+
var _a;
|
|
637
|
+
if (isFinalId(id)) {
|
|
638
|
+
const possibleCluster = this.getClusterForFinalId(id);
|
|
639
|
+
if (possibleCluster === undefined) {
|
|
640
|
+
// It may be an unfinalized eager final ID, so check with normalizer to get the offset from the session UUID
|
|
641
|
+
const creationIndex = this.sessionIdNormalizer.getCreationIndex(id);
|
|
642
|
+
if (creationIndex !== undefined) {
|
|
643
|
+
return stableIdFromNumericUuid(this.localSession.sessionUuid, creationIndex);
|
|
644
|
+
}
|
|
645
|
+
return undefined;
|
|
646
|
+
}
|
|
647
|
+
else {
|
|
648
|
+
const [baseFinalId, cluster] = possibleCluster;
|
|
649
|
+
const override = IdCompressor.tryGetOverride(cluster, id);
|
|
650
|
+
if (override !== undefined) {
|
|
651
|
+
return override;
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
const offsetInCluster = id - baseFinalId;
|
|
655
|
+
return stableIdFromNumericUuid(cluster.baseUuid, offsetInCluster);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
const idOffset = -id; // Convert to a positive number
|
|
661
|
+
if (idOffset > this.localIdCount) {
|
|
662
|
+
// This local ID was never allocated.
|
|
663
|
+
return undefined;
|
|
664
|
+
}
|
|
665
|
+
// If this is a local ID with an override, then it must have been allocated on this machine and will be contained in
|
|
666
|
+
// `localOverrides`s. Otherwise, it is a sequential allocation from the session UUID and can simply be negated and
|
|
667
|
+
// added to that UUID to obtain the stable ID associated with it.
|
|
668
|
+
const localOverride = (_a = this.localOverrides) === null || _a === void 0 ? void 0 : _a.get(id);
|
|
669
|
+
return localOverride !== undefined
|
|
670
|
+
? localOverride
|
|
671
|
+
: stableIdFromNumericUuid(this.localSession.sessionUuid, idOffset - 1);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Recompresses a decompressed ID, which could be a UUID or an override string.
|
|
676
|
+
* @param uncompressed - the UUID or override string to recompress.
|
|
677
|
+
* @returns the `CompressedId` associated with `uncompressed`. Fails if it has not been previously compressed by this compressor.
|
|
678
|
+
*/
|
|
679
|
+
recompress(uncompressed) {
|
|
680
|
+
var _a;
|
|
681
|
+
return (_a = this.tryRecompress(uncompressed)) !== null && _a !== void 0 ? _a : fail("No such string has ever been compressed");
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Attempts to recompresses a decompressed ID, which could be a UUID or an override string.
|
|
685
|
+
* @param uncompressed - the UUID or override string to recompress,
|
|
686
|
+
* @returns the `CompressedId` associated with `uncompressed` or undefined if it has not been previously compressed by this compressor.
|
|
687
|
+
*/
|
|
688
|
+
tryRecompress(uncompressed) {
|
|
689
|
+
return this.recompressInternal(uncompressed);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Helper to compress an uncompressed UUID. It can optionally be supplied with the numeric form of `uncompressedUuid` as a
|
|
693
|
+
* performance optimization.
|
|
694
|
+
*/
|
|
695
|
+
recompressInternal(uncompressed, uncompressedUuidNumeric) {
|
|
696
|
+
var _a, _b;
|
|
697
|
+
let numericUuid = uncompressedUuidNumeric;
|
|
698
|
+
const inversionKey = IdCompressor.createInversionKey(uncompressed);
|
|
699
|
+
const isStable = IdCompressor.isStableInversionKey(inversionKey);
|
|
700
|
+
const closestMatch = this.clustersAndOverridesInversion.getPairOrNextLower(inversionKey, reusedArray);
|
|
701
|
+
if (closestMatch !== undefined) {
|
|
702
|
+
const [key, compressionMapping] = closestMatch;
|
|
703
|
+
if (!IdCompressor.isClusterInfo(compressionMapping)) {
|
|
704
|
+
if (key === inversionKey) {
|
|
705
|
+
return IdCompressor.isUnfinalizedOverride(compressionMapping)
|
|
706
|
+
? compressionMapping
|
|
707
|
+
: (_a = compressionMapping.associatedLocalId) !== null && _a !== void 0 ? _a : compressionMapping.originalOverridingFinal;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
if (!isStable) {
|
|
712
|
+
return undefined;
|
|
713
|
+
}
|
|
714
|
+
const { clusterBase: closestBaseFinalId, cluster: closestCluster } = compressionMapping;
|
|
715
|
+
numericUuid !== null && numericUuid !== void 0 ? numericUuid : (numericUuid = numericUuidFromStableId(inversionKey));
|
|
716
|
+
const uuidOffset = getPositiveDelta(numericUuid, closestCluster.baseUuid, closestCluster.count - 1);
|
|
717
|
+
if (uuidOffset !== undefined) {
|
|
718
|
+
let targetFinalId = (closestBaseFinalId + uuidOffset);
|
|
719
|
+
const override = (_b = closestCluster.overrides) === null || _b === void 0 ? void 0 : _b.get(targetFinalId);
|
|
720
|
+
if (typeof override === "object") {
|
|
721
|
+
if (override.associatedLocalId !== undefined) {
|
|
722
|
+
return override.associatedLocalId;
|
|
723
|
+
}
|
|
724
|
+
// This may be a UUID that should actually compress into a different final ID that it aligns with, due to
|
|
725
|
+
// another session having an identical override (see `IdCluster` for more).
|
|
726
|
+
targetFinalId = override.originalOverridingFinal;
|
|
727
|
+
}
|
|
728
|
+
return this.normalizeToSessionSpace(targetFinalId);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (isStable) {
|
|
733
|
+
// May have already computed the numeric UUID, so avoid recomputing if possible
|
|
734
|
+
const sessionSpaceId = this.getCompressedIdForStableId(numericUuid !== null && numericUuid !== void 0 ? numericUuid : inversionKey);
|
|
735
|
+
if (sessionSpaceId !== undefined) {
|
|
736
|
+
return sessionSpaceId;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
return undefined;
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* Normalizes a session space ID into op space.
|
|
743
|
+
* @param id - the local ID to normalize.
|
|
744
|
+
* @returns the ID in op space.
|
|
745
|
+
*/
|
|
746
|
+
normalizeToOpSpace(id) {
|
|
747
|
+
var _a, _b, _c;
|
|
748
|
+
if (isFinalId(id)) {
|
|
749
|
+
return id;
|
|
750
|
+
}
|
|
751
|
+
// Check if this local ID has not been allocated yet
|
|
752
|
+
if (-id > this.localIdCount) {
|
|
753
|
+
fail("Supplied local ID was not created by this compressor.");
|
|
754
|
+
}
|
|
755
|
+
// Check if this local ID has not been finalized yet.
|
|
756
|
+
// Comparing lastFinalizedLocalId is a safe check for eager final IDs because the local IDs corresponding to them
|
|
757
|
+
// are never handed out to a consumer, and thus could not be passed into this method.
|
|
758
|
+
const { lastFinalizedLocalId } = this.localSession;
|
|
759
|
+
if (lastFinalizedLocalId === undefined || id < lastFinalizedLocalId) {
|
|
760
|
+
// Eager final IDs do not have overrides in the cluster until finalizing
|
|
761
|
+
// This means that using the normalizer to get the final/cluster associated would succeed but would not have the override,
|
|
762
|
+
// so checking localOverrides first is necessary.
|
|
763
|
+
const override = this.localOverrides.get(id);
|
|
764
|
+
if (override !== undefined) {
|
|
765
|
+
const inversionKey = IdCompressor.createInversionKey(override);
|
|
766
|
+
const compressionMapping = (_a = this.clustersAndOverridesInversion.get(inversionKey)) !== null && _a !== void 0 ? _a : fail("Bimap is malformed.");
|
|
767
|
+
return !IdCompressor.isClusterInfo(compressionMapping) &&
|
|
768
|
+
!IdCompressor.isUnfinalizedOverride(compressionMapping) &&
|
|
769
|
+
compressionMapping.associatedLocalId === id
|
|
770
|
+
? compressionMapping.originalOverridingFinal
|
|
771
|
+
: id;
|
|
772
|
+
}
|
|
773
|
+
const possibleFinal = this.sessionIdNormalizer.getFinalId(id);
|
|
774
|
+
return (_b = possibleFinal === null || possibleFinal === void 0 ? void 0 : possibleFinal[0]) !== null && _b !== void 0 ? _b : id;
|
|
775
|
+
}
|
|
776
|
+
const [correspondingFinal, cluster] = (_c = this.sessionIdNormalizer.getFinalId(id)) !== null && _c !== void 0 ? _c : fail("Locally created cluster should be added to the map when allocated");
|
|
777
|
+
if (cluster.overrides) {
|
|
778
|
+
const override = cluster.overrides.get(correspondingFinal);
|
|
779
|
+
if (typeof override === "object" && override.originalOverridingFinal !== undefined) {
|
|
780
|
+
// Rare case of two local IDs with same overrides are created concurrently. See `IdCluster` for more.
|
|
781
|
+
return override.originalOverridingFinal;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
return correspondingFinal;
|
|
785
|
+
}
|
|
786
|
+
normalizeToSessionSpace(id, sessionIdIfLocal) {
|
|
787
|
+
var _a, _b, _c, _d;
|
|
788
|
+
if (isLocalId(id)) {
|
|
789
|
+
if (sessionIdIfLocal === undefined || sessionIdIfLocal === this.localSessionId) {
|
|
790
|
+
const localIndex = -id;
|
|
791
|
+
if (localIndex > this.localIdCount) {
|
|
792
|
+
fail("Supplied local ID was not created by this compressor.");
|
|
793
|
+
}
|
|
794
|
+
return id;
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
const session = (_a = this.sessions.get(sessionIdIfLocal)) !== null && _a !== void 0 ? _a : fail("No IDs have ever been finalized by the supplied session.");
|
|
798
|
+
const localCount = -id;
|
|
799
|
+
const numericUuid = incrementUuid(session.sessionUuid, localCount - 1);
|
|
800
|
+
return ((_b = this.compressNumericUuid(numericUuid)) !== null && _b !== void 0 ? _b : fail("ID is not known to this compressor."));
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
const normalizedId = this.sessionIdNormalizer.getSessionSpaceId(id);
|
|
804
|
+
if (normalizedId !== undefined) {
|
|
805
|
+
return normalizedId;
|
|
806
|
+
}
|
|
807
|
+
// Check for a unified override finalized first by another session but to which the local session
|
|
808
|
+
// still has an associated local ID.
|
|
809
|
+
const [_, cluster] = (_c = this.getClusterForFinalId(id)) !== null && _c !== void 0 ? _c : fail("Supplied final ID was not finalized by this compressor.");
|
|
810
|
+
const override = (_d = cluster.overrides) === null || _d === void 0 ? void 0 : _d.get(id);
|
|
811
|
+
if (typeof override === "object" && override.associatedLocalId !== undefined) {
|
|
812
|
+
return override.associatedLocalId;
|
|
813
|
+
}
|
|
814
|
+
return id;
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Returns the session-space compressed ID corresponding to the numeric UUID, or undefined if it is not known to this compressor.
|
|
818
|
+
* Typically, it will return the session-space ID sequentially aligned with it (which will be local if `numericUuid` was made by
|
|
819
|
+
* the local session, or final otherwise). However, in the event that the aligned session-space ID was overridden with a UUID
|
|
820
|
+
* *and* that override UUID was concurrently used in an older ID (earlier, w.r.t. sequencing), this method can return the first
|
|
821
|
+
* ID to correspond to that override.
|
|
822
|
+
*
|
|
823
|
+
* As an example, consider the following two clients:
|
|
824
|
+
* ClientA, session UUID: A0000000-0000-0000-0000-000000000000
|
|
825
|
+
* ClientB, session UUID: B0000000-0000-0000-0000-000000000000
|
|
826
|
+
*
|
|
827
|
+
* If concurrently, two clients performed:
|
|
828
|
+
* ClientA: generateCompressedId(override: 'X0000000-0000-0000-0000-000000000000') // aligned with A0000000-0000-0000-0000-000000000000
|
|
829
|
+
*
|
|
830
|
+
* ClientB: generateCompressedId() // aligned with B0000000-0000-0000-0000-000000000000
|
|
831
|
+
* ClientB: generateCompressedId(override: 'X0000000-0000-0000-0000-000000000000') // aligned with B0000000-0000-0000-0000-000000000001
|
|
832
|
+
*
|
|
833
|
+
* After sequencing, calling this method and passing the numeric UUID for B0000000-0000-0000-0000-000000000001 would return the
|
|
834
|
+
* session-space ID corresponding to A0000000-0000-0000-0000-000000000000 (with override X0000000-0000-0000-0000-000000000000).
|
|
835
|
+
*/
|
|
836
|
+
compressNumericUuid(numericUuid) {
|
|
837
|
+
const stableId = stableIdFromNumericUuid(numericUuid);
|
|
838
|
+
const sessionSpaceId = this.recompressInternal(stableId, numericUuid);
|
|
839
|
+
if (sessionSpaceId === undefined) {
|
|
840
|
+
return undefined;
|
|
841
|
+
}
|
|
842
|
+
return sessionSpaceId;
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Returns a compressed ID for the supplied stable ID if it was created by the local session, and undefined otherwise.
|
|
846
|
+
*/
|
|
847
|
+
getCompressedIdForStableId(stableId) {
|
|
848
|
+
const numericUuid = typeof stableId === "string" ? numericUuidFromStableId(stableId) : stableId;
|
|
849
|
+
const creationIndex = getPositiveDelta(numericUuid, this.localSession.sessionUuid, this.localIdCount - 1);
|
|
850
|
+
if (creationIndex !== undefined) {
|
|
851
|
+
const sessionSpaceId = this.sessionIdNormalizer.getIdByCreationIndex(creationIndex);
|
|
852
|
+
if (sessionSpaceId !== undefined) {
|
|
853
|
+
return sessionSpaceId;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return undefined;
|
|
857
|
+
}
|
|
858
|
+
getClusterForFinalId(finalId) {
|
|
859
|
+
const possibleCluster = this.finalIdToCluster.getPairOrNextLower(finalId);
|
|
860
|
+
if (possibleCluster === undefined) {
|
|
861
|
+
return undefined;
|
|
862
|
+
}
|
|
863
|
+
const [clusterBase, cluster] = possibleCluster;
|
|
864
|
+
if (finalId - clusterBase >= cluster.count) {
|
|
865
|
+
return undefined;
|
|
866
|
+
}
|
|
867
|
+
return possibleCluster;
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* @returns if `other` is equal to this `IdCompressor`. The equality check includes local session state only if specified.
|
|
871
|
+
* \@testOnly
|
|
872
|
+
*/
|
|
873
|
+
equals(other, compareLocalState) {
|
|
874
|
+
if (compareLocalState) {
|
|
875
|
+
if (this.localIdCount !== other.localIdCount ||
|
|
876
|
+
this.localSessionId !== other.localSessionId ||
|
|
877
|
+
this.lastTakenLocalId !== other.lastTakenLocalId) {
|
|
878
|
+
return false;
|
|
879
|
+
}
|
|
880
|
+
if (!this.localOverrides.equals(other.localOverrides, (a, b) => a === b)) {
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
if (!compareMaps(this.sessions, other.sessions, (a, b) => IdCompressor.sessionDataEqual(a, b, true, compareLocalState))) {
|
|
884
|
+
return false;
|
|
885
|
+
}
|
|
886
|
+
if (!this.sessionIdNormalizer.equals(other.sessionIdNormalizer, (a, b) => IdCompressor.idClustersEqual(a, b, false, compareLocalState))) {
|
|
887
|
+
return false;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
else {
|
|
891
|
+
for (const [keyA, valueA] of this.sessions) {
|
|
892
|
+
const valueB = other.sessions.get(keyA);
|
|
893
|
+
if (valueB === undefined) {
|
|
894
|
+
if (valueA.lastFinalizedLocalId !== undefined) {
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
else if (!IdCompressor.sessionDataEqual(valueA, valueB, true, compareLocalState)) {
|
|
899
|
+
return false;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
for (const [keyB, valueB] of other.sessions) {
|
|
903
|
+
const valueA = this.sessions.get(keyB);
|
|
904
|
+
if (valueA === undefined) {
|
|
905
|
+
if (valueB.lastFinalizedLocalId !== undefined) {
|
|
906
|
+
return false;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
if (this.nextClusterBaseFinalId !== other.nextClusterBaseFinalId ||
|
|
912
|
+
this.newClusterCapacity !== other.newClusterCapacity) {
|
|
913
|
+
return false;
|
|
914
|
+
}
|
|
915
|
+
if (!this.finalIdToCluster.equals(other.finalIdToCluster, (a, b) => IdCompressor.idClustersEqual(a, b, true, compareLocalState))) {
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
const missingInOne = (_, value) => {
|
|
919
|
+
if (!compareLocalState && IdCompressor.isUnfinalizedOverride(value)) {
|
|
920
|
+
return undefined;
|
|
921
|
+
}
|
|
922
|
+
return { break: true };
|
|
923
|
+
};
|
|
924
|
+
const compareCompressionMappings = (a, b) => {
|
|
925
|
+
const unfinalizedA = IdCompressor.isUnfinalizedOverride(a);
|
|
926
|
+
const unfinalizedB = IdCompressor.isUnfinalizedOverride(b);
|
|
927
|
+
if (unfinalizedA) {
|
|
928
|
+
if (unfinalizedB) {
|
|
929
|
+
return a === b;
|
|
930
|
+
}
|
|
931
|
+
return false;
|
|
932
|
+
}
|
|
933
|
+
else if (unfinalizedB) {
|
|
934
|
+
return false;
|
|
935
|
+
}
|
|
936
|
+
if (IdCompressor.isClusterInfo(a)) {
|
|
937
|
+
if (!IdCompressor.isClusterInfo(b) || a.clusterBase !== b.clusterBase) {
|
|
938
|
+
return false;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
else {
|
|
942
|
+
if (IdCompressor.isClusterInfo(b) ||
|
|
943
|
+
(compareLocalState && a.associatedLocalId !== b.associatedLocalId) ||
|
|
944
|
+
a.originalOverridingFinal !== b.originalOverridingFinal) {
|
|
945
|
+
return false;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
if (!IdCompressor.idClustersEqual(a.cluster, b.cluster, true, compareLocalState)) {
|
|
949
|
+
return false;
|
|
950
|
+
}
|
|
951
|
+
return true;
|
|
952
|
+
};
|
|
953
|
+
const diff = this.clustersAndOverridesInversion.diffAgainst(other.clustersAndOverridesInversion, missingInOne, missingInOne, (_, valA, valB) => {
|
|
954
|
+
if (!compareCompressionMappings(valA, valB)) {
|
|
955
|
+
return { break: true };
|
|
956
|
+
}
|
|
957
|
+
return undefined;
|
|
958
|
+
});
|
|
959
|
+
return diff === undefined;
|
|
960
|
+
}
|
|
961
|
+
static sessionDataEqual(a, b, checkCluster = true, compareLocalState = true) {
|
|
962
|
+
if (!numericUuidEquals(a.sessionUuid, b.sessionUuid) ||
|
|
963
|
+
a.lastFinalizedLocalId !== b.lastFinalizedLocalId) {
|
|
964
|
+
return false;
|
|
965
|
+
}
|
|
966
|
+
if (a.currentClusterDetails === undefined || b.currentClusterDetails === undefined) {
|
|
967
|
+
if (a.currentClusterDetails !== b.currentClusterDetails) {
|
|
968
|
+
return false;
|
|
969
|
+
}
|
|
970
|
+
return true;
|
|
971
|
+
}
|
|
972
|
+
if (checkCluster &&
|
|
973
|
+
!IdCompressor.idClustersEqual(a.currentClusterDetails.cluster, b.currentClusterDetails.cluster, false, compareLocalState)) {
|
|
974
|
+
return false;
|
|
975
|
+
}
|
|
976
|
+
return true;
|
|
977
|
+
}
|
|
978
|
+
static idClustersEqual(a, b, checkSessionData = true, compareLocalState = true) {
|
|
979
|
+
var _a, _b;
|
|
980
|
+
const areEqual = numericUuidEquals(a.baseUuid, b.baseUuid) &&
|
|
981
|
+
a.capacity === b.capacity &&
|
|
982
|
+
a.count === b.count &&
|
|
983
|
+
(!checkSessionData ||
|
|
984
|
+
IdCompressor.sessionDataEqual(a.session, b.session, false, compareLocalState)) &&
|
|
985
|
+
(a.overrides === undefined) === (b.overrides === undefined) &&
|
|
986
|
+
(a.overrides === undefined ||
|
|
987
|
+
compareMaps((_a = a.overrides) !== null && _a !== void 0 ? _a : fail("Overrides must be defined"), (_b = b.overrides) !== null && _b !== void 0 ? _b : fail("Overrides must be defined"), (overrideA, overrideB) => {
|
|
988
|
+
if (compareLocalState) {
|
|
989
|
+
if (typeof overrideA === "string" || typeof overrideB === "string") {
|
|
990
|
+
return overrideA === overrideB;
|
|
991
|
+
}
|
|
992
|
+
const overridesEqual = overrideA.override === overrideB.override &&
|
|
993
|
+
overrideA.originalOverridingFinal ===
|
|
994
|
+
overrideB.originalOverridingFinal &&
|
|
995
|
+
(!compareLocalState ||
|
|
996
|
+
overrideA.associatedLocalId === overrideB.associatedLocalId);
|
|
997
|
+
return overridesEqual;
|
|
998
|
+
}
|
|
999
|
+
const uuidA = typeof overrideA === "string" ? overrideA : overrideA.override;
|
|
1000
|
+
const uuidB = typeof overrideB === "string" ? overrideB : overrideB.override;
|
|
1001
|
+
if (typeof overrideA !== "string" &&
|
|
1002
|
+
typeof overrideB !== "string" &&
|
|
1003
|
+
overrideA.originalOverridingFinal !== overrideB.originalOverridingFinal) {
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
return uuidA === uuidB;
|
|
1007
|
+
}));
|
|
1008
|
+
return areEqual;
|
|
1009
|
+
}
|
|
1010
|
+
serialize(withSession) {
|
|
1011
|
+
var _a, _b;
|
|
1012
|
+
const serializedSessions = [];
|
|
1013
|
+
const sessionIdToSessionIndex = new Map();
|
|
1014
|
+
for (const [sessionId, session] of this.sessions) {
|
|
1015
|
+
const isLocalSession = sessionId === this.localSessionId;
|
|
1016
|
+
const includeSession = sessionId !== reservedSessionId && // Ignore reserved clusters, but
|
|
1017
|
+
(session.lastFinalizedLocalId !== undefined || // always serialize sessions that made final IDs,
|
|
1018
|
+
(isLocalSession && withSession)); // include the un-acked local session if requested
|
|
1019
|
+
if (includeSession) {
|
|
1020
|
+
const sessionData = [sessionId];
|
|
1021
|
+
sessionIdToSessionIndex.set(sessionId, serializedSessions.length);
|
|
1022
|
+
serializedSessions.push(sessionData);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
const serializedClusters = [];
|
|
1026
|
+
for (const [baseFinalId, cluster] of this.finalIdToCluster.entries()) {
|
|
1027
|
+
const sessionId = stableIdFromNumericUuid(cluster.session.sessionUuid);
|
|
1028
|
+
if (sessionId !== reservedSessionId) {
|
|
1029
|
+
const sessionIndex = (_a = sessionIdToSessionIndex.get(sessionId)) !== null && _a !== void 0 ? _a : fail("Session object contains wrong session numeric UUID");
|
|
1030
|
+
const serializedCluster = [
|
|
1031
|
+
sessionIndex,
|
|
1032
|
+
cluster.capacity,
|
|
1033
|
+
];
|
|
1034
|
+
if (cluster.count !== cluster.capacity) {
|
|
1035
|
+
serializedCluster.push(cluster.count);
|
|
1036
|
+
}
|
|
1037
|
+
if (cluster.overrides !== undefined) {
|
|
1038
|
+
const serializedOverrides = [];
|
|
1039
|
+
for (const [finalId, override] of cluster.overrides) {
|
|
1040
|
+
const finalIdIndex = finalId - baseFinalId;
|
|
1041
|
+
if (typeof override === "string") {
|
|
1042
|
+
serializedOverrides.push([finalIdIndex, override]);
|
|
1043
|
+
}
|
|
1044
|
+
else if (override.originalOverridingFinal === finalId) {
|
|
1045
|
+
serializedOverrides.push([finalIdIndex, override.override]);
|
|
1046
|
+
}
|
|
1047
|
+
else {
|
|
1048
|
+
serializedOverrides.push([
|
|
1049
|
+
finalIdIndex,
|
|
1050
|
+
override.override,
|
|
1051
|
+
override.originalOverridingFinal,
|
|
1052
|
+
]);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
serializedCluster.push(serializedOverrides);
|
|
1056
|
+
}
|
|
1057
|
+
serializedClusters.push(serializedCluster);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
// Reserved session not serialized, and local session is present but may not make IDs
|
|
1061
|
+
assert(serializedSessions.length - this.sessions.size <= 2, 0x498 /* session not serialized */);
|
|
1062
|
+
const serializedIdCompressor = {
|
|
1063
|
+
version: currentWrittenVersion,
|
|
1064
|
+
clusterCapacity: this.clusterCapacity,
|
|
1065
|
+
sessions: serializedSessions,
|
|
1066
|
+
clusters: serializedClusters,
|
|
1067
|
+
};
|
|
1068
|
+
if (withSession) {
|
|
1069
|
+
const serializedWithSession = serializedIdCompressor;
|
|
1070
|
+
serializedWithSession.localSessionIndex = serializedWithSession.sessions.findIndex(([sessionId]) => sessionId === this.localSessionId);
|
|
1071
|
+
if (this.localIdCount > 0) {
|
|
1072
|
+
serializedWithSession.localState = {
|
|
1073
|
+
localIdCount: this.localIdCount,
|
|
1074
|
+
overrides: [...this.localOverrides.entries()].map((entry) => [...entry]),
|
|
1075
|
+
lastTakenLocalId: this.lastTakenLocalId,
|
|
1076
|
+
sessionNormalizer: this.sessionIdNormalizer.serialize(),
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
return serializedWithSession;
|
|
1080
|
+
}
|
|
1081
|
+
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.sendTelemetryEvent({
|
|
1082
|
+
eventName: "RuntimeIdCompressor:SerializedIdCompressorSize",
|
|
1083
|
+
size: JSON.stringify(serializedIdCompressor).length,
|
|
1084
|
+
clusterCount: serializedIdCompressor.clusters.length,
|
|
1085
|
+
sessionCount: serializedIdCompressor.sessions.length,
|
|
1086
|
+
});
|
|
1087
|
+
return serializedIdCompressor;
|
|
1088
|
+
}
|
|
1089
|
+
static deserialize(...args) {
|
|
1090
|
+
const [serialized, newSessionIdMaybe] = args;
|
|
1091
|
+
const { clusterCapacity, sessions: serializedSessions, clusters: serializedClusters, } = serialized;
|
|
1092
|
+
let localSessionId;
|
|
1093
|
+
let serializedLocalState;
|
|
1094
|
+
if (newSessionIdMaybe === undefined) {
|
|
1095
|
+
// Alias of serialized, but known to be a SerializedIdCompressorWithOngoingSession
|
|
1096
|
+
const [serializedWithSession] = args;
|
|
1097
|
+
const serializedSessionData = serializedSessions[serializedWithSession.localSessionIndex];
|
|
1098
|
+
localSessionId = serializedSessionData[0];
|
|
1099
|
+
serializedLocalState = serializedWithSession.localState;
|
|
1100
|
+
}
|
|
1101
|
+
else {
|
|
1102
|
+
localSessionId = newSessionIdMaybe;
|
|
1103
|
+
}
|
|
1104
|
+
const compressor = new IdCompressor(localSessionId);
|
|
1105
|
+
compressor.clusterCapacity = clusterCapacity;
|
|
1106
|
+
const localOverridesInverse = new Map();
|
|
1107
|
+
if (serializedLocalState !== undefined) {
|
|
1108
|
+
// Do this part of local rehydration first since the cluster map population needs to query to local overrides
|
|
1109
|
+
compressor.localIdCount = serializedLocalState.localIdCount;
|
|
1110
|
+
compressor.lastTakenLocalId = serializedLocalState.lastTakenLocalId;
|
|
1111
|
+
if (serializedLocalState.overrides !== undefined) {
|
|
1112
|
+
for (const [localId, override] of serializedLocalState.overrides) {
|
|
1113
|
+
compressor.localOverrides.append(localId, override);
|
|
1114
|
+
localOverridesInverse.set(override, localId);
|
|
1115
|
+
compressor.clustersAndOverridesInversion.set(IdCompressor.createInversionKey(override), localId);
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
const sessionInfos = [];
|
|
1120
|
+
for (const serializedSession of serializedSessions) {
|
|
1121
|
+
const [sessionId] = serializedSession;
|
|
1122
|
+
if (sessionId === localSessionId) {
|
|
1123
|
+
assert(hasOngoingSession(serialized), 0x499 /* Cannot resume existing session. */);
|
|
1124
|
+
sessionInfos.push({ session: compressor.localSession, sessionId });
|
|
1125
|
+
}
|
|
1126
|
+
else {
|
|
1127
|
+
const session = compressor.createSession(sessionId);
|
|
1128
|
+
sessionInfos.push({ session, sessionId });
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
for (const serializedCluster of serializedClusters) {
|
|
1132
|
+
const { sessionIndex, capacity, count, overrides } = deserializeCluster(serializedCluster);
|
|
1133
|
+
const { session, sessionId } = sessionInfos[sessionIndex];
|
|
1134
|
+
const { lastFinalizedLocalId, sessionUuid } = session;
|
|
1135
|
+
const currentIdCount = lastFinalizedLocalId === undefined ? 0 : -lastFinalizedLocalId;
|
|
1136
|
+
const cluster = {
|
|
1137
|
+
capacity,
|
|
1138
|
+
count,
|
|
1139
|
+
baseUuid: incrementUuid(sessionUuid, currentIdCount),
|
|
1140
|
+
session,
|
|
1141
|
+
};
|
|
1142
|
+
const lastFinalizedNormalized = lastFinalizedLocalId !== null && lastFinalizedLocalId !== void 0 ? lastFinalizedLocalId : 0;
|
|
1143
|
+
const clusterBase = compressor.nextClusterBaseFinalId;
|
|
1144
|
+
session.lastFinalizedLocalId = (lastFinalizedNormalized - count);
|
|
1145
|
+
session.currentClusterDetails = { clusterBase, cluster };
|
|
1146
|
+
compressor.nextClusterBaseFinalId = (compressor.nextClusterBaseFinalId +
|
|
1147
|
+
capacity);
|
|
1148
|
+
compressor.finalIdToCluster.append(clusterBase, cluster);
|
|
1149
|
+
compressor.clustersAndOverridesInversion.set(stableIdFromNumericUuid(cluster.baseUuid), {
|
|
1150
|
+
clusterBase,
|
|
1151
|
+
cluster,
|
|
1152
|
+
});
|
|
1153
|
+
if (overrides !== undefined) {
|
|
1154
|
+
cluster.overrides = new Map();
|
|
1155
|
+
for (const [finalIdIndex, override, originalOverridingFinal] of overrides) {
|
|
1156
|
+
const finalId = (clusterBase + finalIdIndex);
|
|
1157
|
+
if (originalOverridingFinal !== undefined) {
|
|
1158
|
+
const unifiedOverride = {
|
|
1159
|
+
override,
|
|
1160
|
+
originalOverridingFinal,
|
|
1161
|
+
};
|
|
1162
|
+
if (serializedLocalState !== undefined) {
|
|
1163
|
+
setPropertyIfDefined(localOverridesInverse.get(override), unifiedOverride, "associatedLocalId");
|
|
1164
|
+
}
|
|
1165
|
+
cluster.overrides.set(finalId, unifiedOverride);
|
|
1166
|
+
}
|
|
1167
|
+
else {
|
|
1168
|
+
const associatedLocal = localOverridesInverse.get(override);
|
|
1169
|
+
if (associatedLocal !== undefined && sessionId !== localSessionId) {
|
|
1170
|
+
// In this case, there is a local ID associated with this override, but this is the first cluster to contain
|
|
1171
|
+
// that override (because only the first cluster will have the string serialized). In this case, the override
|
|
1172
|
+
// needs to hold that local value.
|
|
1173
|
+
cluster.overrides.set(finalId, {
|
|
1174
|
+
override,
|
|
1175
|
+
originalOverridingFinal: finalId,
|
|
1176
|
+
associatedLocalId: associatedLocal,
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
else {
|
|
1180
|
+
cluster.overrides.set(finalId, override);
|
|
1181
|
+
}
|
|
1182
|
+
const finalizedOverride = {
|
|
1183
|
+
cluster,
|
|
1184
|
+
originalOverridingFinal: finalId,
|
|
1185
|
+
};
|
|
1186
|
+
if (serializedLocalState !== undefined) {
|
|
1187
|
+
setPropertyIfDefined(associatedLocal, finalizedOverride, "associatedLocalId");
|
|
1188
|
+
}
|
|
1189
|
+
compressor.clustersAndOverridesInversion.set(IdCompressor.createInversionKey(override), finalizedOverride);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
if (serializedLocalState !== undefined) {
|
|
1195
|
+
compressor.sessionIdNormalizer = SessionIdNormalizer.deserialize(serializedLocalState.sessionNormalizer, (finalId) => {
|
|
1196
|
+
var _a;
|
|
1197
|
+
const [_, cluster] = (_a = compressor.finalIdToCluster.getPairOrNextLower(finalId)) !== null && _a !== void 0 ? _a : fail("Final in serialized normalizer was never created.");
|
|
1198
|
+
return cluster;
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
assert(compressor.localSession.lastFinalizedLocalId === undefined ||
|
|
1202
|
+
compressor.localIdCount >= -compressor.localSession.lastFinalizedLocalId, 0x49a /* Inconsistent last finalized state when deserializing */);
|
|
1203
|
+
return compressor;
|
|
1204
|
+
}
|
|
1205
|
+
static convertToCurrentVersion(serializedCompressor, hasSession) {
|
|
1206
|
+
if (serializedCompressor.version !== currentWrittenVersion) {
|
|
1207
|
+
fail("Unknown SerializedIdCompressor version number");
|
|
1208
|
+
}
|
|
1209
|
+
const serialized = serializedCompressor;
|
|
1210
|
+
if (hasSession !== hasOngoingSession(serialized)) {
|
|
1211
|
+
return undefined;
|
|
1212
|
+
}
|
|
1213
|
+
return serialized;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Max allowed cluster size
|
|
1218
|
+
*/
|
|
1219
|
+
IdCompressor.maxClusterSize = 2 ** 20;
|
|
1220
|
+
/**
|
|
1221
|
+
* The version of `IdCompressor` that is currently persisted.
|
|
1222
|
+
*/
|
|
1223
|
+
const currentWrittenVersion = "0.0.1";
|
|
1224
|
+
/**
|
|
1225
|
+
* @returns whether or not the given serialized ID compressor has an ongoing session.
|
|
1226
|
+
*/
|
|
1227
|
+
export function hasOngoingSession(serialized) {
|
|
1228
|
+
return (serialized.localSessionIndex !==
|
|
1229
|
+
undefined);
|
|
1230
|
+
}
|
|
1231
|
+
function deserializeCluster(serializedCluster) {
|
|
1232
|
+
const [sessionIndex, capacity, countOrOverrides, overrides] = serializedCluster;
|
|
1233
|
+
const hasCount = typeof countOrOverrides === "number";
|
|
1234
|
+
return {
|
|
1235
|
+
sessionIndex,
|
|
1236
|
+
capacity,
|
|
1237
|
+
count: hasCount ? countOrOverrides : capacity,
|
|
1238
|
+
overrides: hasCount ? overrides : countOrOverrides,
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Optimization used by the sorted-btree library to avoid allocating tuples every time a lookup method is called.
|
|
1243
|
+
* Lookup methods on BTree accept a pre-allocated array that it populates with the result of the lookup and retains no ownership
|
|
1244
|
+
* of after the call, so this array may be supplied to any of them. References to this array should not be retained elsewhere and
|
|
1245
|
+
* lookup results should be extracted from the tuple immediately after invocation.
|
|
1246
|
+
*/
|
|
1247
|
+
const reusedArray = [];
|
|
1248
|
+
//# sourceMappingURL=idCompressor.js.map
|