@fluidframework/container-runtime 2.0.0-internal.6.1.0 → 2.0.0-internal.6.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/CHANGELOG.md +35 -0
- package/README.md +4 -3
- package/dist/batchTracker.d.ts +1 -1
- package/dist/batchTracker.js +1 -1
- package/dist/batchTracker.js.map +1 -1
- package/dist/blobManager.d.ts +4 -20
- package/dist/blobManager.d.ts.map +1 -1
- package/dist/blobManager.js +47 -125
- package/dist/blobManager.js.map +1 -1
- package/dist/containerRuntime.d.ts +82 -14
- package/dist/containerRuntime.d.ts.map +1 -1
- package/dist/containerRuntime.js +236 -138
- package/dist/containerRuntime.js.map +1 -1
- package/dist/dataStore.d.ts.map +1 -1
- package/dist/dataStore.js +1 -2
- package/dist/dataStore.js.map +1 -1
- package/dist/dataStoreContext.d.ts.map +1 -1
- package/dist/dataStoreContext.js +4 -5
- package/dist/dataStoreContext.js.map +1 -1
- package/dist/dataStoreContexts.d.ts +1 -2
- package/dist/dataStoreContexts.d.ts.map +1 -1
- package/dist/dataStoreContexts.js.map +1 -1
- package/dist/dataStoreRegistry.js +2 -2
- package/dist/dataStoreRegistry.js.map +1 -1
- package/dist/dataStores.d.ts.map +1 -1
- package/dist/dataStores.js +4 -5
- package/dist/dataStores.js.map +1 -1
- package/dist/error.d.ts +14 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +21 -0
- package/dist/error.js.map +1 -0
- package/dist/gc/garbageCollection.d.ts +1 -1
- package/dist/gc/garbageCollection.d.ts.map +1 -1
- package/dist/gc/garbageCollection.js +23 -5
- package/dist/gc/garbageCollection.js.map +1 -1
- package/dist/gc/gcConfigs.d.ts.map +1 -1
- package/dist/gc/gcConfigs.js +5 -3
- package/dist/gc/gcConfigs.js.map +1 -1
- package/dist/gc/gcDefinitions.d.ts +2 -0
- package/dist/gc/gcDefinitions.d.ts.map +1 -1
- package/dist/gc/gcDefinitions.js.map +1 -1
- package/dist/gc/gcTelemetry.d.ts.map +1 -1
- package/dist/gc/gcTelemetry.js +2 -0
- package/dist/gc/gcTelemetry.js.map +1 -1
- package/dist/id-compressor/appendOnlySortedMap.d.ts +8 -30
- package/dist/id-compressor/appendOnlySortedMap.d.ts.map +1 -1
- package/dist/id-compressor/appendOnlySortedMap.js +25 -67
- package/dist/id-compressor/appendOnlySortedMap.js.map +1 -1
- package/dist/id-compressor/finalSpace.d.ts +29 -0
- package/dist/id-compressor/finalSpace.d.ts.map +1 -0
- package/dist/id-compressor/finalSpace.js +62 -0
- package/dist/id-compressor/finalSpace.js.map +1 -0
- package/dist/id-compressor/idCompressor.d.ts +25 -250
- package/dist/id-compressor/idCompressor.d.ts.map +1 -1
- package/dist/id-compressor/idCompressor.js +385 -1149
- package/dist/id-compressor/idCompressor.js.map +1 -1
- package/dist/id-compressor/identifiers.d.ts +32 -0
- package/dist/id-compressor/identifiers.d.ts.map +1 -0
- package/dist/id-compressor/identifiers.js +15 -0
- package/dist/id-compressor/identifiers.js.map +1 -0
- package/dist/id-compressor/index.d.ts +5 -6
- package/dist/id-compressor/index.d.ts.map +1 -1
- package/dist/id-compressor/index.js +20 -26
- package/dist/id-compressor/index.js.map +1 -1
- package/dist/id-compressor/persistanceUtilities.d.ts +22 -0
- package/dist/id-compressor/persistanceUtilities.d.ts.map +1 -0
- package/dist/id-compressor/persistanceUtilities.js +43 -0
- package/dist/id-compressor/persistanceUtilities.js.map +1 -0
- package/dist/id-compressor/sessionSpaceNormalizer.d.ts +46 -0
- package/dist/id-compressor/sessionSpaceNormalizer.d.ts.map +1 -0
- package/dist/id-compressor/sessionSpaceNormalizer.js +80 -0
- package/dist/id-compressor/sessionSpaceNormalizer.js.map +1 -0
- package/dist/id-compressor/sessions.d.ts +115 -0
- package/dist/id-compressor/sessions.d.ts.map +1 -0
- package/dist/id-compressor/sessions.js +305 -0
- package/dist/id-compressor/sessions.js.map +1 -0
- package/dist/id-compressor/utilities.d.ts +49 -0
- package/dist/id-compressor/utilities.d.ts.map +1 -0
- package/dist/id-compressor/utilities.js +166 -0
- package/dist/id-compressor/utilities.js.map +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
- package/dist/opLifecycle/opCompressor.js +1 -2
- package/dist/opLifecycle/opCompressor.js.map +1 -1
- package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
- package/dist/opLifecycle/opSplitter.js +2 -3
- package/dist/opLifecycle/opSplitter.js.map +1 -1
- package/dist/opLifecycle/outbox.d.ts +1 -0
- package/dist/opLifecycle/outbox.d.ts.map +1 -1
- package/dist/opLifecycle/outbox.js +10 -11
- package/dist/opLifecycle/outbox.js.map +1 -1
- package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
- package/dist/opLifecycle/remoteMessageProcessor.js +11 -5
- package/dist/opLifecycle/remoteMessageProcessor.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 +12 -5
- package/dist/pendingStateManager.d.ts.map +1 -1
- package/dist/pendingStateManager.js +24 -10
- package/dist/pendingStateManager.js.map +1 -1
- package/dist/scheduleManager.d.ts.map +1 -1
- package/dist/scheduleManager.js +4 -5
- package/dist/scheduleManager.js.map +1 -1
- package/dist/summary/index.d.ts +2 -2
- 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 -2
- package/dist/summary/orderedClientElection.d.ts.map +1 -1
- package/dist/summary/orderedClientElection.js +2 -3
- package/dist/summary/orderedClientElection.js.map +1 -1
- package/dist/summary/runningSummarizer.d.ts +27 -4
- package/dist/summary/runningSummarizer.d.ts.map +1 -1
- package/dist/summary/runningSummarizer.js +237 -66
- package/dist/summary/runningSummarizer.js.map +1 -1
- package/dist/summary/summarizer.d.ts +6 -5
- package/dist/summary/summarizer.d.ts.map +1 -1
- package/dist/summary/summarizer.js +70 -67
- package/dist/summary/summarizer.js.map +1 -1
- package/dist/summary/summarizerClientElection.d.ts +1 -1
- package/dist/summary/summarizerClientElection.d.ts.map +1 -1
- package/dist/summary/summarizerClientElection.js.map +1 -1
- package/dist/summary/summarizerTypes.d.ts +38 -25
- package/dist/summary/summarizerTypes.d.ts.map +1 -1
- package/dist/summary/summarizerTypes.js.map +1 -1
- package/dist/summary/summaryCollection.d.ts +1 -2
- package/dist/summary/summaryCollection.d.ts.map +1 -1
- package/dist/summary/summaryCollection.js.map +1 -1
- package/dist/summary/summaryGenerator.d.ts +9 -3
- package/dist/summary/summaryGenerator.d.ts.map +1 -1
- package/dist/summary/summaryGenerator.js +42 -38
- package/dist/summary/summaryGenerator.js.map +1 -1
- package/dist/summary/summaryManager.d.ts +7 -6
- package/dist/summary/summaryManager.d.ts.map +1 -1
- package/dist/summary/summaryManager.js +22 -15
- package/dist/summary/summaryManager.js.map +1 -1
- package/lib/batchTracker.d.ts +1 -1
- package/lib/batchTracker.js +1 -1
- package/lib/batchTracker.js.map +1 -1
- package/lib/blobManager.d.ts +4 -20
- package/lib/blobManager.d.ts.map +1 -1
- package/lib/blobManager.js +46 -124
- package/lib/blobManager.js.map +1 -1
- package/lib/containerRuntime.d.ts +82 -14
- package/lib/containerRuntime.d.ts.map +1 -1
- package/lib/containerRuntime.js +223 -123
- package/lib/containerRuntime.js.map +1 -1
- package/lib/dataStore.d.ts.map +1 -1
- package/lib/dataStore.js +1 -2
- package/lib/dataStore.js.map +1 -1
- package/lib/dataStoreContext.d.ts.map +1 -1
- package/lib/dataStoreContext.js +1 -2
- package/lib/dataStoreContext.js.map +1 -1
- package/lib/dataStoreContexts.d.ts +1 -2
- package/lib/dataStoreContexts.d.ts.map +1 -1
- package/lib/dataStoreContexts.js.map +1 -1
- package/lib/dataStoreRegistry.js +1 -1
- package/lib/dataStoreRegistry.js.map +1 -1
- package/lib/dataStores.d.ts.map +1 -1
- package/lib/dataStores.js +1 -2
- package/lib/dataStores.js.map +1 -1
- package/lib/error.d.ts +14 -0
- package/lib/error.d.ts.map +1 -0
- package/lib/error.js +17 -0
- package/lib/error.js.map +1 -0
- package/lib/gc/garbageCollection.d.ts +1 -1
- package/lib/gc/garbageCollection.d.ts.map +1 -1
- package/lib/gc/garbageCollection.js +22 -4
- package/lib/gc/garbageCollection.js.map +1 -1
- package/lib/gc/gcConfigs.d.ts.map +1 -1
- package/lib/gc/gcConfigs.js +3 -1
- package/lib/gc/gcConfigs.js.map +1 -1
- package/lib/gc/gcDefinitions.d.ts +2 -0
- package/lib/gc/gcDefinitions.d.ts.map +1 -1
- package/lib/gc/gcDefinitions.js.map +1 -1
- package/lib/gc/gcTelemetry.d.ts.map +1 -1
- package/lib/gc/gcTelemetry.js +2 -0
- package/lib/gc/gcTelemetry.js.map +1 -1
- package/lib/id-compressor/appendOnlySortedMap.d.ts +8 -30
- package/lib/id-compressor/appendOnlySortedMap.d.ts.map +1 -1
- package/lib/id-compressor/appendOnlySortedMap.js +24 -65
- package/lib/id-compressor/appendOnlySortedMap.js.map +1 -1
- package/lib/id-compressor/finalSpace.d.ts +29 -0
- package/lib/id-compressor/finalSpace.d.ts.map +1 -0
- package/lib/id-compressor/finalSpace.js +58 -0
- package/lib/id-compressor/finalSpace.js.map +1 -0
- package/lib/id-compressor/idCompressor.d.ts +25 -250
- package/lib/id-compressor/idCompressor.d.ts.map +1 -1
- package/lib/id-compressor/idCompressor.js +381 -1139
- package/lib/id-compressor/idCompressor.js.map +1 -1
- package/lib/id-compressor/identifiers.d.ts +32 -0
- package/lib/id-compressor/identifiers.d.ts.map +1 -0
- package/lib/id-compressor/identifiers.js +11 -0
- package/lib/id-compressor/identifiers.js.map +1 -0
- package/lib/id-compressor/index.d.ts +5 -6
- package/lib/id-compressor/index.d.ts.map +1 -1
- package/lib/id-compressor/index.js +5 -6
- package/lib/id-compressor/index.js.map +1 -1
- package/lib/id-compressor/persistanceUtilities.d.ts +22 -0
- package/lib/id-compressor/persistanceUtilities.d.ts.map +1 -0
- package/lib/id-compressor/persistanceUtilities.js +34 -0
- package/lib/id-compressor/persistanceUtilities.js.map +1 -0
- package/lib/id-compressor/sessionSpaceNormalizer.d.ts +46 -0
- package/lib/id-compressor/sessionSpaceNormalizer.d.ts.map +1 -0
- package/lib/id-compressor/sessionSpaceNormalizer.js +76 -0
- package/lib/id-compressor/sessionSpaceNormalizer.js.map +1 -0
- package/lib/id-compressor/sessions.d.ts +115 -0
- package/lib/id-compressor/sessions.d.ts.map +1 -0
- package/lib/id-compressor/sessions.js +290 -0
- package/lib/id-compressor/sessions.js.map +1 -0
- package/lib/id-compressor/utilities.d.ts +49 -0
- package/lib/id-compressor/utilities.d.ts.map +1 -0
- package/lib/id-compressor/utilities.js +148 -0
- package/lib/id-compressor/utilities.js.map +1 -0
- package/lib/index.d.ts +3 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
- package/lib/opLifecycle/opCompressor.js +1 -2
- package/lib/opLifecycle/opCompressor.js.map +1 -1
- package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
- package/lib/opLifecycle/opSplitter.js +1 -2
- package/lib/opLifecycle/opSplitter.js.map +1 -1
- package/lib/opLifecycle/outbox.d.ts +1 -0
- package/lib/opLifecycle/outbox.d.ts.map +1 -1
- package/lib/opLifecycle/outbox.js +6 -7
- package/lib/opLifecycle/outbox.js.map +1 -1
- package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
- package/lib/opLifecycle/remoteMessageProcessor.js +12 -6
- package/lib/opLifecycle/remoteMessageProcessor.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 +12 -5
- package/lib/pendingStateManager.d.ts.map +1 -1
- package/lib/pendingStateManager.js +21 -7
- package/lib/pendingStateManager.js.map +1 -1
- package/lib/scheduleManager.d.ts.map +1 -1
- package/lib/scheduleManager.js +1 -2
- package/lib/scheduleManager.js.map +1 -1
- package/lib/summary/index.d.ts +2 -2
- 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 -2
- package/lib/summary/orderedClientElection.d.ts.map +1 -1
- package/lib/summary/orderedClientElection.js +1 -2
- package/lib/summary/orderedClientElection.js.map +1 -1
- package/lib/summary/runningSummarizer.d.ts +27 -4
- package/lib/summary/runningSummarizer.d.ts.map +1 -1
- package/lib/summary/runningSummarizer.js +237 -66
- package/lib/summary/runningSummarizer.js.map +1 -1
- package/lib/summary/summarizer.d.ts +6 -5
- package/lib/summary/summarizer.d.ts.map +1 -1
- package/lib/summary/summarizer.js +68 -65
- package/lib/summary/summarizer.js.map +1 -1
- package/lib/summary/summarizerClientElection.d.ts +1 -1
- package/lib/summary/summarizerClientElection.d.ts.map +1 -1
- package/lib/summary/summarizerClientElection.js.map +1 -1
- package/lib/summary/summarizerTypes.d.ts +38 -25
- package/lib/summary/summarizerTypes.d.ts.map +1 -1
- package/lib/summary/summarizerTypes.js.map +1 -1
- package/lib/summary/summaryCollection.d.ts +1 -2
- package/lib/summary/summaryCollection.d.ts.map +1 -1
- package/lib/summary/summaryCollection.js.map +1 -1
- package/lib/summary/summaryGenerator.d.ts +9 -3
- package/lib/summary/summaryGenerator.d.ts.map +1 -1
- package/lib/summary/summaryGenerator.js +43 -39
- package/lib/summary/summaryGenerator.js.map +1 -1
- package/lib/summary/summaryManager.d.ts +7 -6
- package/lib/summary/summaryManager.d.ts.map +1 -1
- package/lib/summary/summaryManager.js +23 -16
- package/lib/summary/summaryManager.js.map +1 -1
- package/package.json +27 -24
- package/src/batchTracker.ts +1 -1
- package/src/blobManager.ts +57 -146
- package/src/containerRuntime.ts +331 -158
- package/src/dataStore.ts +1 -2
- package/src/dataStoreContext.ts +3 -6
- package/src/dataStoreContexts.ts +1 -2
- package/src/dataStoreRegistry.ts +1 -1
- package/src/dataStores.ts +3 -5
- package/src/error.ts +18 -0
- package/src/gc/garbageCollection.ts +38 -5
- package/src/gc/gcConfigs.ts +4 -2
- package/src/gc/gcDefinitions.ts +2 -0
- package/src/gc/gcTelemetry.ts +2 -0
- package/src/id-compressor/appendOnlySortedMap.ts +25 -86
- package/src/id-compressor/finalSpace.ts +67 -0
- package/src/id-compressor/idCompressor.ts +455 -1681
- package/src/id-compressor/identifiers.ts +42 -0
- package/src/id-compressor/index.ts +11 -20
- package/src/id-compressor/persistanceUtilities.ts +58 -0
- package/src/id-compressor/sessionSpaceNormalizer.ts +83 -0
- package/src/id-compressor/sessions.ts +405 -0
- package/src/id-compressor/utilities.ts +187 -0
- package/src/index.ts +7 -1
- package/src/opLifecycle/opCompressor.ts +1 -2
- package/src/opLifecycle/opSplitter.ts +4 -4
- package/src/opLifecycle/outbox.ts +13 -10
- package/src/opLifecycle/remoteMessageProcessor.ts +19 -6
- package/src/packageVersion.ts +1 -1
- package/src/pendingStateManager.ts +49 -27
- package/src/scheduleManager.ts +5 -4
- package/src/summary/index.ts +3 -1
- package/src/summary/orderedClientElection.ts +6 -4
- package/src/summary/runningSummarizer.ts +276 -95
- package/src/summary/summarizer.ts +22 -12
- package/src/summary/summarizerClientElection.ts +1 -1
- package/src/summary/summarizerTypes.ts +40 -25
- package/src/summary/summaryCollection.ts +1 -2
- package/src/summary/summaryGenerator.ts +49 -52
- package/src/summary/summaryManager.ts +33 -11
- package/dist/id-compressor/idRange.d.ts +0 -11
- package/dist/id-compressor/idRange.d.ts.map +0 -1
- package/dist/id-compressor/idRange.js +0 -29
- package/dist/id-compressor/idRange.js.map +0 -1
- package/dist/id-compressor/numericUuid.d.ts +0 -59
- package/dist/id-compressor/numericUuid.d.ts.map +0 -1
- package/dist/id-compressor/numericUuid.js +0 -325
- package/dist/id-compressor/numericUuid.js.map +0 -1
- package/dist/id-compressor/sessionIdNormalizer.d.ts +0 -138
- package/dist/id-compressor/sessionIdNormalizer.d.ts.map +0 -1
- package/dist/id-compressor/sessionIdNormalizer.js +0 -483
- package/dist/id-compressor/sessionIdNormalizer.js.map +0 -1
- package/dist/id-compressor/utils.d.ts +0 -57
- package/dist/id-compressor/utils.d.ts.map +0 -1
- package/dist/id-compressor/utils.js +0 -90
- package/dist/id-compressor/utils.js.map +0 -1
- package/dist/id-compressor/uuidUtilities.d.ts +0 -28
- package/dist/id-compressor/uuidUtilities.d.ts.map +0 -1
- package/dist/id-compressor/uuidUtilities.js +0 -104
- package/dist/id-compressor/uuidUtilities.js.map +0 -1
- package/lib/id-compressor/idRange.d.ts +0 -11
- package/lib/id-compressor/idRange.d.ts.map +0 -1
- package/lib/id-compressor/idRange.js +0 -25
- package/lib/id-compressor/idRange.js.map +0 -1
- package/lib/id-compressor/numericUuid.d.ts +0 -59
- package/lib/id-compressor/numericUuid.d.ts.map +0 -1
- package/lib/id-compressor/numericUuid.js +0 -315
- package/lib/id-compressor/numericUuid.js.map +0 -1
- package/lib/id-compressor/sessionIdNormalizer.d.ts +0 -138
- package/lib/id-compressor/sessionIdNormalizer.d.ts.map +0 -1
- package/lib/id-compressor/sessionIdNormalizer.js +0 -479
- package/lib/id-compressor/sessionIdNormalizer.js.map +0 -1
- package/lib/id-compressor/utils.d.ts +0 -57
- package/lib/id-compressor/utils.d.ts.map +0 -1
- package/lib/id-compressor/utils.js +0 -79
- package/lib/id-compressor/utils.js.map +0 -1
- package/lib/id-compressor/uuidUtilities.d.ts +0 -28
- package/lib/id-compressor/uuidUtilities.d.ts.map +0 -1
- package/lib/id-compressor/uuidUtilities.js +0 -96
- package/lib/id-compressor/uuidUtilities.js.map +0 -1
- package/src/id-compressor/idRange.ts +0 -35
- package/src/id-compressor/numericUuid.ts +0 -383
- package/src/id-compressor/sessionIdNormalizer.ts +0 -609
- package/src/id-compressor/utils.ts +0 -114
- package/src/id-compressor/uuidUtilities.ts +0 -120
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { AppendOnlySortedMap } from "./appendOnlySortedMap";
|
|
6
|
+
import { compareFiniteNumbers, genCountFromLocalId } from "./utilities";
|
|
7
|
+
/**
|
|
8
|
+
* The `SessionSpaceNormalizer` tracks the form of the IDs created by the local session.
|
|
9
|
+
* More precisely, it acts as a set of all `LocalCompressedId`s created by the local session and allows querying whether a specific
|
|
10
|
+
* ID was produced by the local session. This information can be used to determine whether the nth ID created by the local session
|
|
11
|
+
* was produced as a local ID (negative) or a final ID (positive).
|
|
12
|
+
*
|
|
13
|
+
* The local and final forms of IDs made by a session can be thought of as two equal-length sparse arrays, aligned such
|
|
14
|
+
* that normalizeLocalToFinal(locals[i]) === finals[i] and vice versa.
|
|
15
|
+
* Below is an example to illustrate how various mappings can arise:
|
|
16
|
+
*
|
|
17
|
+
* ```
|
|
18
|
+
* +- Creation Index
|
|
19
|
+
* / +- Locals
|
|
20
|
+
* / / +- Finals
|
|
21
|
+
* / / /
|
|
22
|
+
* ---+-----+----
|
|
23
|
+
* 0 | -1 | 0 -|___ Two IDs are allocated as locals since no cluster exists. A new cluster is created when acked.
|
|
24
|
+
* 1 | -2 | 1 -|
|
|
25
|
+
* 2 | | 2 -|
|
|
26
|
+
* 3 | | 3 --|-- Three more IDs are allocated as finals eagerly since a cluster exists with available capacity.
|
|
27
|
+
* 4 | | 4 -|
|
|
28
|
+
* 5 | -6 | 10 ----- One ID is allocated as a local (it overflows the existing cluster) and a new cluster is created after ack.
|
|
29
|
+
* 6 | | 11 ----- This ID (and the subsequent few) is allocated eagerly as a final ID into the existing cluster.
|
|
30
|
+
* 7 | | 12
|
|
31
|
+
* 8 | | 13
|
|
32
|
+
* 9 | | 14
|
|
33
|
+
* 10 | -11 | ----- The cluster is out of room, so a local ID is allocated. It has no corresponding final ID since it has not been acked.
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* This class stores the set of local IDs and thus their indices, as local IDs are essentially negative offsets from the first ID.
|
|
37
|
+
* The form (local or final) of a given ID index (for example, the 5th ID made by the local session) can be deduced from this information.
|
|
38
|
+
*/
|
|
39
|
+
export class SessionSpaceNormalizer {
|
|
40
|
+
constructor() {
|
|
41
|
+
// Run-length encoding of IDs that were generated as local IDs. They are stored as a list of tuples (genCount, count)
|
|
42
|
+
// that are sorted on the genCount so that contains checks can use a binary search.
|
|
43
|
+
this.localIdRanges = new AppendOnlySortedMap(compareFiniteNumbers);
|
|
44
|
+
}
|
|
45
|
+
get idRanges() {
|
|
46
|
+
return this.localIdRanges;
|
|
47
|
+
}
|
|
48
|
+
addLocalRange(baseGenCount, count) {
|
|
49
|
+
const last = this.localIdRanges.last();
|
|
50
|
+
if (last !== undefined) {
|
|
51
|
+
const [lastGenCount, lastCount] = last;
|
|
52
|
+
// Check to see if the added run of local IDs is contiguous with the last range added.
|
|
53
|
+
// If it is, simply merge them (this is the common case).
|
|
54
|
+
if (lastGenCount + lastCount === baseGenCount) {
|
|
55
|
+
this.localIdRanges.replaceLast(lastGenCount, lastCount + count);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
this.localIdRanges.append(baseGenCount, count);
|
|
60
|
+
}
|
|
61
|
+
contains(query) {
|
|
62
|
+
const genCount = genCountFromLocalId(query);
|
|
63
|
+
const containingBlock = this.localIdRanges.getPairOrNextLower(genCount);
|
|
64
|
+
if (containingBlock !== undefined) {
|
|
65
|
+
const [baseGenCount, count] = containingBlock;
|
|
66
|
+
if (genCount <= baseGenCount + (count - 1)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
equals(other) {
|
|
73
|
+
return this.localIdRanges.equals(other.localIdRanges, (countA, countB) => countA === countB);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=sessionSpaceNormalizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessionSpaceNormalizer.js","sourceRoot":"","sources":["../../src/id-compressor/sessionSpaceNormalizer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,OAAO,sBAAsB;IAAnC;QACC,qHAAqH;QACrH,mFAAmF;QAClE,kBAAa,GAAG,IAAI,mBAAmB,CAAiB,oBAAoB,CAAC,CAAC;IAsChG,CAAC;IApCA,IAAW,QAAQ;QAClB,OAAO,IAAI,CAAC,aAAa,CAAC;IAC3B,CAAC;IAEM,aAAa,CAAC,YAAoB,EAAE,KAAa;QACvD,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;QACvC,IAAI,IAAI,KAAK,SAAS,EAAE;YACvB,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC,GAAG,IAAI,CAAC;YACvC,sFAAsF;YACtF,yDAAyD;YACzD,IAAI,YAAY,GAAG,SAAS,KAAK,YAAY,EAAE;gBAC9C,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,YAAY,EAAE,SAAS,GAAG,KAAK,CAAC,CAAC;gBAChE,OAAO;aACP;SACD;QACD,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC;IAEM,QAAQ,CAAC,KAAwB;QACvC,MAAM,QAAQ,GAAG,mBAAmB,CAAC,KAAK,CAAC,CAAC;QAC5C,MAAM,eAAe,GAAG,IAAI,CAAC,aAAa,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC;QACxE,IAAI,eAAe,KAAK,SAAS,EAAE;YAClC,MAAM,CAAC,YAAY,EAAE,KAAK,CAAC,GAAG,eAAe,CAAC;YAC9C,IAAI,QAAQ,IAAI,YAAY,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE;gBAC3C,OAAO,IAAI,CAAC;aACZ;SACD;QACD,OAAO,KAAK,CAAC;IACd,CAAC;IAEM,MAAM,CAAC,KAA6B;QAC1C,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAC/B,KAAK,CAAC,aAAa,EACnB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,MAAM,CACrC,CAAC;IACH,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { AppendOnlySortedMap } from \"./appendOnlySortedMap\";\nimport { LocalCompressedId } from \"./identifiers\";\nimport { compareFiniteNumbers, genCountFromLocalId } from \"./utilities\";\n\n/**\n * The `SessionSpaceNormalizer` tracks the form of the IDs created by the local session.\n * More precisely, it acts as a set of all `LocalCompressedId`s created by the local session and allows querying whether a specific\n * ID was produced by the local session. This information can be used to determine whether the nth ID created by the local session\n * was produced as a local ID (negative) or a final ID (positive).\n *\n * The local and final forms of IDs made by a session can be thought of as two equal-length sparse arrays, aligned such\n * that normalizeLocalToFinal(locals[i]) === finals[i] and vice versa.\n * Below is an example to illustrate how various mappings can arise:\n *\n * ```\n * +- Creation Index\n * / +- Locals\n * / / +- Finals\n * / / /\n * ---+-----+----\n * 0 | -1 | 0 -|___ Two IDs are allocated as locals since no cluster exists. A new cluster is created when acked.\n * 1 | -2 | 1 -|\n * 2 | | 2 -|\n * 3 | | 3 --|-- Three more IDs are allocated as finals eagerly since a cluster exists with available capacity.\n * 4 | | 4 -|\n * 5 | -6 | 10 ----- One ID is allocated as a local (it overflows the existing cluster) and a new cluster is created after ack.\n * 6 | | 11 ----- This ID (and the subsequent few) is allocated eagerly as a final ID into the existing cluster.\n * 7 | | 12\n * 8 | | 13\n * 9 | | 14\n * 10 | -11 | ----- The cluster is out of room, so a local ID is allocated. It has no corresponding final ID since it has not been acked.\n * ```\n *\n * This class stores the set of local IDs and thus their indices, as local IDs are essentially negative offsets from the first ID.\n * The form (local or final) of a given ID index (for example, the 5th ID made by the local session) can be deduced from this information.\n */\nexport class SessionSpaceNormalizer {\n\t// Run-length encoding of IDs that were generated as local IDs. They are stored as a list of tuples (genCount, count)\n\t// that are sorted on the genCount so that contains checks can use a binary search.\n\tprivate readonly localIdRanges = new AppendOnlySortedMap<number, number>(compareFiniteNumbers);\n\n\tpublic get idRanges(): Pick<AppendOnlySortedMap<number, number>, \"size\" | \"entries\"> {\n\t\treturn this.localIdRanges;\n\t}\n\n\tpublic addLocalRange(baseGenCount: number, count: number): void {\n\t\tconst last = this.localIdRanges.last();\n\t\tif (last !== undefined) {\n\t\t\tconst [lastGenCount, lastCount] = last;\n\t\t\t// Check to see if the added run of local IDs is contiguous with the last range added.\n\t\t\t// If it is, simply merge them (this is the common case).\n\t\t\tif (lastGenCount + lastCount === baseGenCount) {\n\t\t\t\tthis.localIdRanges.replaceLast(lastGenCount, lastCount + count);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tthis.localIdRanges.append(baseGenCount, count);\n\t}\n\n\tpublic contains(query: LocalCompressedId): boolean {\n\t\tconst genCount = genCountFromLocalId(query);\n\t\tconst containingBlock = this.localIdRanges.getPairOrNextLower(genCount);\n\t\tif (containingBlock !== undefined) {\n\t\t\tconst [baseGenCount, count] = containingBlock;\n\t\t\tif (genCount <= baseGenCount + (count - 1)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\tpublic equals(other: SessionSpaceNormalizer): boolean {\n\t\treturn this.localIdRanges.equals(\n\t\t\tother.localIdRanges,\n\t\t\t(countA, countB) => countA === countB,\n\t\t);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { SessionId, StableId } from "@fluidframework/runtime-definitions";
|
|
6
|
+
import { FinalCompressedId, LocalCompressedId, NumericUuid } from "./identifiers";
|
|
7
|
+
/**
|
|
8
|
+
* A collection of all sessions known to the compressor (i.e. all finalized/acked allocated UUIDs and their corresponding local and final forms).
|
|
9
|
+
* This collection of all sessions comprises a distributed document's IDs.
|
|
10
|
+
*/
|
|
11
|
+
export declare class Sessions {
|
|
12
|
+
private readonly uuidSpace;
|
|
13
|
+
private readonly sessionCache;
|
|
14
|
+
constructor(sessions?: [sessionBase: NumericUuid, session: Session][]);
|
|
15
|
+
sessions(): IterableIterator<Session>;
|
|
16
|
+
getOrCreate(sessionId: SessionId): Session;
|
|
17
|
+
get(sessionId: SessionId): Session | undefined;
|
|
18
|
+
getContainingCluster(query: StableId): [cluster: IdCluster, alignedLocal: LocalCompressedId] | undefined;
|
|
19
|
+
clusterCollides(cluster: IdCluster): boolean;
|
|
20
|
+
equals(other: Sessions, includeLocalState: boolean): boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The IDs created by a specific session, stored as a cluster chain to allow for fast conversions.
|
|
24
|
+
*/
|
|
25
|
+
export declare class Session {
|
|
26
|
+
private readonly clusterChain;
|
|
27
|
+
readonly sessionUuid: NumericUuid;
|
|
28
|
+
constructor(sessionId: SessionId | NumericUuid);
|
|
29
|
+
/**
|
|
30
|
+
* Adds a new empty cluster to the cluster chain of this session.
|
|
31
|
+
*/
|
|
32
|
+
addNewCluster(baseFinalId: FinalCompressedId, capacity: number, count: number): IdCluster;
|
|
33
|
+
isEmpty(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the last cluster in this session's cluster chain, if any.
|
|
36
|
+
*/
|
|
37
|
+
getLastCluster(): IdCluster | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Converts the local ID from this session to a final ID, if possible.
|
|
40
|
+
* @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
|
|
41
|
+
*/
|
|
42
|
+
tryConvertToFinal(searchLocal: LocalCompressedId, includeAllocated: boolean): FinalCompressedId | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the cluster containing the supplied local ID, if possible.
|
|
45
|
+
* @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
|
|
46
|
+
*/
|
|
47
|
+
getClusterByLocal(localId: LocalCompressedId, includeAllocated: boolean): IdCluster | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Returns the cluster containing the supplied final ID, if possible.
|
|
50
|
+
*/
|
|
51
|
+
getClusterByAllocatedFinal(final: FinalCompressedId): IdCluster | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Returns the cluster from the supplied cluster chain containing the supplied final ID, if possible.
|
|
54
|
+
* `clusterChain` must be sorted by final/local base ID.
|
|
55
|
+
*/
|
|
56
|
+
static getContainingCluster(finalId: FinalCompressedId, clusterChain: readonly IdCluster[]): IdCluster | undefined;
|
|
57
|
+
static binarySearch<S, T>(search: S, arr: readonly T[], comparator: (a: S, b: T) => number): T | undefined;
|
|
58
|
+
equals(other: Session): boolean;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A cluster of final (sequenced via consensus), sequentially allocated compressed IDs.
|
|
62
|
+
* A final ID in a cluster decompresses to a sequentially allocated UUID that is the result of adding its offset within
|
|
63
|
+
* the cluster to base UUID for the session that created it.
|
|
64
|
+
*/
|
|
65
|
+
export interface IdCluster {
|
|
66
|
+
/**
|
|
67
|
+
* The session that created this cluster.
|
|
68
|
+
*/
|
|
69
|
+
readonly session: Session;
|
|
70
|
+
/**
|
|
71
|
+
* The first final ID in the cluster.
|
|
72
|
+
*/
|
|
73
|
+
readonly baseFinalId: FinalCompressedId;
|
|
74
|
+
/**
|
|
75
|
+
* The local ID aligned with `baseFinalId`.
|
|
76
|
+
*/
|
|
77
|
+
readonly baseLocalId: LocalCompressedId;
|
|
78
|
+
/**
|
|
79
|
+
* The total number of final IDs reserved for allocation in the cluster.
|
|
80
|
+
* Clusters are reserved in blocks as a performance optimization.
|
|
81
|
+
*/
|
|
82
|
+
capacity: number;
|
|
83
|
+
/**
|
|
84
|
+
* The number of final IDs currently allocated in the cluster.
|
|
85
|
+
*/
|
|
86
|
+
count: number;
|
|
87
|
+
}
|
|
88
|
+
export declare function clustersEqual(a: IdCluster, b: IdCluster): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Returns the final ID that is aligned with the supplied local ID within a cluster.
|
|
91
|
+
* Includes allocated IDs.
|
|
92
|
+
*/
|
|
93
|
+
export declare function getAlignedFinal(cluster: IdCluster, localWithin: LocalCompressedId): FinalCompressedId | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the local ID that is aligned with the supplied final ID within a cluster.
|
|
96
|
+
* Fails if the supplied ID does not fall within the cluster bounds.
|
|
97
|
+
*/
|
|
98
|
+
export declare function getAlignedLocal(cluster: IdCluster, finalWithin: FinalCompressedId): LocalCompressedId;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the last allocated final ID (i.e. any ID between base final and base final + capacity) within a cluster
|
|
101
|
+
*/
|
|
102
|
+
export declare function lastAllocatedFinal(cluster: IdCluster): FinalCompressedId;
|
|
103
|
+
/**
|
|
104
|
+
* Returns the last allocated final ID (i.e. any ID between base final and base final + count) within a cluster
|
|
105
|
+
*/
|
|
106
|
+
export declare function lastFinalizedFinal(cluster: IdCluster): FinalCompressedId;
|
|
107
|
+
/**
|
|
108
|
+
* Returns the last allocated local ID (i.e. any ID between base local and base local + capacity) within a cluster
|
|
109
|
+
*/
|
|
110
|
+
export declare function lastAllocatedLocal(cluster: IdCluster): LocalCompressedId;
|
|
111
|
+
/**
|
|
112
|
+
* Returns the last allocated local ID (i.e. any ID between base local and base local + count) within a cluster
|
|
113
|
+
*/
|
|
114
|
+
export declare function lastFinalizedLocal(cluster: IdCluster): LocalCompressedId;
|
|
115
|
+
//# sourceMappingURL=sessions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../../src/id-compressor/sessions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,qCAAqC,CAAC;AAW1E,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAElF;;;GAGG;AACH,qBAAa,QAAQ;IAGpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8D;IAExF,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAiC;gBAE3C,QAAQ,CAAC,EAAE,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE;IAgBrE,QAAQ,IAAI,gBAAgB,CAAC,OAAO,CAAC;IAIrC,WAAW,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO;IAc1C,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,GAAG,SAAS;IAI9C,oBAAoB,CAC1B,KAAK,EAAE,QAAQ,GACb,CAAC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,iBAAiB,CAAC,GAAG,SAAS;IAmB7D,eAAe,CAAC,OAAO,EAAE,SAAS,GAAG,OAAO;IA0C5C,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,iBAAiB,EAAE,OAAO,GAAG,OAAO;CAsBnE;AAED;;GAEG;AACH,qBAAa,OAAO;IAEnB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAmB;IAEhD,SAAgB,WAAW,EAAE,WAAW,CAAC;gBAEtB,SAAS,EAAE,SAAS,GAAG,WAAW;IAKrD;;OAEG;IACI,aAAa,CACnB,WAAW,EAAE,iBAAiB,EAC9B,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MA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iBAAiB,GAC5B,iBAAiB,GAAG,SAAS,CAO/B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,OAAO,EAAE,SAAS,EAClB,WAAW,EAAE,iBAAiB,GAC5B,iBAAiB,CAOnB;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,GAAG,iBAAiB,CAExE"}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import BTree from "sorted-btree";
|
|
6
|
+
import { assert } from "@fluidframework/common-utils";
|
|
7
|
+
import { compareBigints, localIdFromGenCount, genCountFromLocalId, numericUuidFromStableId, stableIdFromNumericUuid, subtractNumericUuids, offsetNumericUuid, } from "./utilities";
|
|
8
|
+
/**
|
|
9
|
+
* A collection of all sessions known to the compressor (i.e. all finalized/acked allocated UUIDs and their corresponding local and final forms).
|
|
10
|
+
* This collection of all sessions comprises a distributed document's IDs.
|
|
11
|
+
*/
|
|
12
|
+
export class Sessions {
|
|
13
|
+
constructor(sessions) {
|
|
14
|
+
// A range-queryable store of all sessions. A btree is used as it solves the predecessor problem for any given UUID, allowing
|
|
15
|
+
// us to quickly find the session that may have produced it.
|
|
16
|
+
this.uuidSpace = new BTree(undefined, compareBigints);
|
|
17
|
+
// A fast lookup table from session ID to the session object, used to avoid accessing the slower btree
|
|
18
|
+
this.sessionCache = new Map();
|
|
19
|
+
if (sessions !== undefined) {
|
|
20
|
+
// bulk load path
|
|
21
|
+
for (const [numeric, session] of sessions) {
|
|
22
|
+
this.sessionCache.set(stableIdFromNumericUuid(numeric), session);
|
|
23
|
+
}
|
|
24
|
+
this.uuidSpace = new BTree(sessions, compareBigints);
|
|
25
|
+
if (this.sessionCache.size !== sessions.length ||
|
|
26
|
+
sessions.length !== this.uuidSpace.size) {
|
|
27
|
+
throw new Error("Cannot resume existing session.");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
sessions() {
|
|
32
|
+
return this.sessionCache.values();
|
|
33
|
+
}
|
|
34
|
+
getOrCreate(sessionId) {
|
|
35
|
+
const existing = this.sessionCache.get(sessionId);
|
|
36
|
+
if (existing !== undefined) {
|
|
37
|
+
return existing;
|
|
38
|
+
}
|
|
39
|
+
const session = new Session(sessionId);
|
|
40
|
+
assert(this.uuidSpace.set(session.sessionUuid, session), 0x760 /* Duplicate session in map. */);
|
|
41
|
+
this.sessionCache.set(sessionId, session);
|
|
42
|
+
return session;
|
|
43
|
+
}
|
|
44
|
+
get(sessionId) {
|
|
45
|
+
return this.sessionCache.get(sessionId);
|
|
46
|
+
}
|
|
47
|
+
getContainingCluster(query) {
|
|
48
|
+
const numericStable = numericUuidFromStableId(query);
|
|
49
|
+
const possibleMatch = this.uuidSpace.getPairOrNextLower(numericStable);
|
|
50
|
+
if (possibleMatch === undefined) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const [_, session] = possibleMatch;
|
|
54
|
+
const numericDelta = subtractNumericUuids(numericStable, session.sessionUuid);
|
|
55
|
+
if (numericDelta > Number.MAX_SAFE_INTEGER) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const alignedLocal = localIdFromGenCount(Number(numericDelta) + 1);
|
|
59
|
+
const containingCluster = session.getClusterByLocal(alignedLocal, true);
|
|
60
|
+
if (containingCluster === undefined) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return [containingCluster, alignedLocal];
|
|
64
|
+
}
|
|
65
|
+
clusterCollides(cluster) {
|
|
66
|
+
const { session: owningSession, baseLocalId, capacity } = cluster;
|
|
67
|
+
const clusterBaseNumeric = offsetNumericUuid(owningSession.sessionUuid, genCountFromLocalId(baseLocalId) - 1);
|
|
68
|
+
const clusterMaxNumeric = offsetNumericUuid(clusterBaseNumeric, capacity - 1);
|
|
69
|
+
let closestMatch = this.uuidSpace.getPairOrNextLower(clusterMaxNumeric);
|
|
70
|
+
// Find the first session that is not the owner of this new cluster.
|
|
71
|
+
// Once we have that, check to see if its cluster chain overlaps with the new cluster.
|
|
72
|
+
// Consider the following diagram of UUID space:
|
|
73
|
+
// Cluster chain A: |----------------------|
|
|
74
|
+
// Cluster chain B: |----------|
|
|
75
|
+
// Cluster chain C: |-------|
|
|
76
|
+
// While it is true that when adding a cluster to chain C, we would find
|
|
77
|
+
// the next lower session (which is B) and erroneously determine we do not collide
|
|
78
|
+
// with any other session, but this situation is impossible to get into as B would
|
|
79
|
+
// have detected that it collided with A (or the other way around, depending on ordering).
|
|
80
|
+
while (closestMatch !== undefined && closestMatch[1] === owningSession) {
|
|
81
|
+
closestMatch = this.uuidSpace.nextLowerPair(closestMatch[0]);
|
|
82
|
+
}
|
|
83
|
+
if (closestMatch === undefined) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const [_, session] = closestMatch;
|
|
87
|
+
assert(session !== owningSession, 0x761 /* Failed to attempt to detect collisions. */);
|
|
88
|
+
const lastCluster = session.getLastCluster();
|
|
89
|
+
if (lastCluster === undefined) {
|
|
90
|
+
// If the closest session is empty (the local session), then it is guaranteed (probabilistically) that there are no
|
|
91
|
+
// non-empty sessions that have a cluster chain that starts prior to the empty session and collides with the cluster
|
|
92
|
+
// we are checking, so we can return false.
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
const lastAllocatedNumeric = offsetNumericUuid(session.sessionUuid, genCountFromLocalId(lastAllocatedLocal(lastCluster)) - 1);
|
|
96
|
+
return lastAllocatedNumeric >= clusterBaseNumeric;
|
|
97
|
+
}
|
|
98
|
+
equals(other, includeLocalState) {
|
|
99
|
+
const checkIsSubset = (sessionsA, sessionsB) => {
|
|
100
|
+
const first = sessionsA.sessions().next();
|
|
101
|
+
const firstSessionThis = first.done ? undefined : first.value;
|
|
102
|
+
for (const [stableId, session] of sessionsA.sessionCache.entries()) {
|
|
103
|
+
const otherSession = sessionsB.sessionCache.get(stableId);
|
|
104
|
+
if (otherSession === undefined) {
|
|
105
|
+
if (!session.isEmpty() || includeLocalState) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
assert(session === firstSessionThis, 0x762 /* The only non-empty session must be the local session. */);
|
|
109
|
+
}
|
|
110
|
+
else if (!session.equals(otherSession)) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
};
|
|
116
|
+
return checkIsSubset(this, other) && checkIsSubset(other, this);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The IDs created by a specific session, stored as a cluster chain to allow for fast conversions.
|
|
121
|
+
*/
|
|
122
|
+
export class Session {
|
|
123
|
+
constructor(sessionId) {
|
|
124
|
+
// All clusters created by this session, in creation order (thus sorted by base final and local ID).
|
|
125
|
+
this.clusterChain = [];
|
|
126
|
+
this.sessionUuid =
|
|
127
|
+
typeof sessionId === "string" ? numericUuidFromStableId(sessionId) : sessionId;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Adds a new empty cluster to the cluster chain of this session.
|
|
131
|
+
*/
|
|
132
|
+
addNewCluster(baseFinalId, capacity, count) {
|
|
133
|
+
const lastCluster = this.getLastCluster();
|
|
134
|
+
const newCluster = {
|
|
135
|
+
session: this,
|
|
136
|
+
baseFinalId,
|
|
137
|
+
baseLocalId: (lastCluster === undefined
|
|
138
|
+
? -1
|
|
139
|
+
: lastAllocatedLocal(lastCluster) - 1),
|
|
140
|
+
capacity,
|
|
141
|
+
count,
|
|
142
|
+
};
|
|
143
|
+
this.clusterChain.push(newCluster);
|
|
144
|
+
return newCluster;
|
|
145
|
+
}
|
|
146
|
+
isEmpty() {
|
|
147
|
+
return this.clusterChain.length === 0;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns the last cluster in this session's cluster chain, if any.
|
|
151
|
+
*/
|
|
152
|
+
getLastCluster() {
|
|
153
|
+
return this.clusterChain[this.clusterChain.length - 1];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Converts the local ID from this session to a final ID, if possible.
|
|
157
|
+
* @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
|
|
158
|
+
*/
|
|
159
|
+
tryConvertToFinal(searchLocal, includeAllocated) {
|
|
160
|
+
const containingCluster = this.getClusterByLocal(searchLocal, includeAllocated);
|
|
161
|
+
if (containingCluster === undefined) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
return getAlignedFinal(containingCluster, searchLocal);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns the cluster containing the supplied local ID, if possible.
|
|
168
|
+
* @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.
|
|
169
|
+
*/
|
|
170
|
+
getClusterByLocal(localId, includeAllocated) {
|
|
171
|
+
const lastValidLocal = includeAllocated
|
|
172
|
+
? lastAllocatedLocal
|
|
173
|
+
: lastFinalizedLocal;
|
|
174
|
+
const matchedCluster = Session.binarySearch(localId, this.clusterChain, (local, cluster) => {
|
|
175
|
+
const lastLocal = lastValidLocal(cluster);
|
|
176
|
+
if (local < lastLocal) {
|
|
177
|
+
return 1;
|
|
178
|
+
}
|
|
179
|
+
else if (local > cluster.baseLocalId) {
|
|
180
|
+
return -1;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
return matchedCluster;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns the cluster containing the supplied final ID, if possible.
|
|
190
|
+
*/
|
|
191
|
+
getClusterByAllocatedFinal(final) {
|
|
192
|
+
return Session.getContainingCluster(final, this.clusterChain);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns the cluster from the supplied cluster chain containing the supplied final ID, if possible.
|
|
196
|
+
* `clusterChain` must be sorted by final/local base ID.
|
|
197
|
+
*/
|
|
198
|
+
static getContainingCluster(finalId, clusterChain) {
|
|
199
|
+
return Session.binarySearch(finalId, clusterChain, (final, cluster) => {
|
|
200
|
+
const lastFinal = lastAllocatedFinal(cluster);
|
|
201
|
+
if (final < cluster.baseFinalId) {
|
|
202
|
+
return -1;
|
|
203
|
+
}
|
|
204
|
+
else if (final > lastFinal) {
|
|
205
|
+
return 1;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
return 0;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
static binarySearch(search, arr, comparator) {
|
|
213
|
+
let left = 0;
|
|
214
|
+
let right = arr.length - 1;
|
|
215
|
+
while (left <= right) {
|
|
216
|
+
const mid = Math.floor((left + right) / 2);
|
|
217
|
+
const c = comparator(search, arr[mid]);
|
|
218
|
+
if (c === 0) {
|
|
219
|
+
return arr[mid]; // Found the target, return its index.
|
|
220
|
+
}
|
|
221
|
+
else if (c > 0) {
|
|
222
|
+
left = mid + 1; // Continue search on right half.
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
right = mid - 1; // Continue search on left half.
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return undefined; // If we reach here, target is not in array.
|
|
229
|
+
}
|
|
230
|
+
equals(other) {
|
|
231
|
+
for (let i = 0; i < this.clusterChain.length; i++) {
|
|
232
|
+
if (!clustersEqual(this.clusterChain[i], other.clusterChain[i])) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return this.sessionUuid === other.sessionUuid;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
export function clustersEqual(a, b) {
|
|
240
|
+
return (a.session.sessionUuid === b.session.sessionUuid &&
|
|
241
|
+
a.baseFinalId === b.baseFinalId &&
|
|
242
|
+
a.baseLocalId === b.baseLocalId &&
|
|
243
|
+
a.capacity === b.capacity &&
|
|
244
|
+
a.count === b.count);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Returns the final ID that is aligned with the supplied local ID within a cluster.
|
|
248
|
+
* Includes allocated IDs.
|
|
249
|
+
*/
|
|
250
|
+
export function getAlignedFinal(cluster, localWithin) {
|
|
251
|
+
const clusterOffset = genCountFromLocalId(localWithin) - genCountFromLocalId(cluster.baseLocalId);
|
|
252
|
+
if (clusterOffset < cluster.capacity) {
|
|
253
|
+
return (cluster.baseFinalId + clusterOffset);
|
|
254
|
+
}
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Returns the local ID that is aligned with the supplied final ID within a cluster.
|
|
259
|
+
* Fails if the supplied ID does not fall within the cluster bounds.
|
|
260
|
+
*/
|
|
261
|
+
export function getAlignedLocal(cluster, finalWithin) {
|
|
262
|
+
assert(finalWithin >= cluster.baseFinalId && finalWithin <= lastAllocatedFinal(cluster), 0x763 /* Supplied ID is not within the cluster. */);
|
|
263
|
+
const finalDelta = finalWithin - cluster.baseFinalId;
|
|
264
|
+
return (cluster.baseLocalId - finalDelta);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Returns the last allocated final ID (i.e. any ID between base final and base final + capacity) within a cluster
|
|
268
|
+
*/
|
|
269
|
+
export function lastAllocatedFinal(cluster) {
|
|
270
|
+
return (cluster.baseFinalId + (cluster.capacity - 1));
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Returns the last allocated final ID (i.e. any ID between base final and base final + count) within a cluster
|
|
274
|
+
*/
|
|
275
|
+
export function lastFinalizedFinal(cluster) {
|
|
276
|
+
return (cluster.baseFinalId + (cluster.count - 1));
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Returns the last allocated local ID (i.e. any ID between base local and base local + capacity) within a cluster
|
|
280
|
+
*/
|
|
281
|
+
export function lastAllocatedLocal(cluster) {
|
|
282
|
+
return (cluster.baseLocalId - (cluster.capacity - 1));
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Returns the last allocated local ID (i.e. any ID between base local and base local + count) within a cluster
|
|
286
|
+
*/
|
|
287
|
+
export function lastFinalizedLocal(cluster) {
|
|
288
|
+
return (cluster.baseLocalId - (cluster.count - 1));
|
|
289
|
+
}
|
|
290
|
+
//# sourceMappingURL=sessions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessions.js","sourceRoot":"","sources":["../../src/id-compressor/sessions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,cAAc,CAAC;AAEjC,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AACtD,OAAO,EACN,cAAc,EACd,mBAAmB,EACnB,mBAAmB,EACnB,uBAAuB,EACvB,uBAAuB,EACvB,oBAAoB,EACpB,iBAAiB,GACjB,MAAM,aAAa,CAAC;AAGrB;;;GAGG;AACH,MAAM,OAAO,QAAQ;IAOpB,YAAmB,QAAyD;QAN5E,6HAA6H;QAC7H,4DAA4D;QAC3C,cAAS,GAAG,IAAI,KAAK,CAAuB,SAAS,EAAE,cAAc,CAAC,CAAC;QACxF,sGAAsG;QACrF,iBAAY,GAAG,IAAI,GAAG,EAAsB,CAAC;QAG7D,IAAI,QAAQ,KAAK,SAAS,EAAE;YAC3B,iBAAiB;YACjB,KAAK,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,QAAQ,EAAE;gBAC1C,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,uBAAuB,CAAC,OAAO,CAAc,EAAE,OAAO,CAAC,CAAC;aAC9E;YACD,IAA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H,MAAM,UAAU,kBAAkB,CAAC,OAAkB;IACpD,OAAO,CAAE,OAAO,CAAC,WAAsB,GAAG,CAAC,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAsB,CAAC;AACxF,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAkB;IACpD,OAAO,CAAE,OAAO,CAAC,WAAsB,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,CAAsB,CAAC;AACrF,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport BTree from \"sorted-btree\";\nimport { SessionId, StableId } from \"@fluidframework/runtime-definitions\";\nimport { assert } from \"@fluidframework/common-utils\";\nimport {\n\tcompareBigints,\n\tlocalIdFromGenCount,\n\tgenCountFromLocalId,\n\tnumericUuidFromStableId,\n\tstableIdFromNumericUuid,\n\tsubtractNumericUuids,\n\toffsetNumericUuid,\n} from \"./utilities\";\nimport { FinalCompressedId, LocalCompressedId, NumericUuid } from \"./identifiers\";\n\n/**\n * A collection of all sessions known to the compressor (i.e. all finalized/acked allocated UUIDs and their corresponding local and final forms).\n * This collection of all sessions comprises a distributed document's IDs.\n */\nexport class Sessions {\n\t// A range-queryable store of all sessions. A btree is used as it solves the predecessor problem for any given UUID, allowing\n\t// us to quickly find the session that may have produced it.\n\tprivate readonly uuidSpace = new BTree<NumericUuid, Session>(undefined, compareBigints);\n\t// A fast lookup table from session ID to the session object, used to avoid accessing the slower btree\n\tprivate readonly sessionCache = new Map<SessionId, Session>();\n\n\tpublic constructor(sessions?: [sessionBase: NumericUuid, session: Session][]) {\n\t\tif (sessions !== undefined) {\n\t\t\t// bulk load path\n\t\t\tfor (const [numeric, session] of sessions) {\n\t\t\t\tthis.sessionCache.set(stableIdFromNumericUuid(numeric) as SessionId, session);\n\t\t\t}\n\t\t\tthis.uuidSpace = new BTree(sessions, compareBigints);\n\t\t\tif (\n\t\t\t\tthis.sessionCache.size !== sessions.length ||\n\t\t\t\tsessions.length !== this.uuidSpace.size\n\t\t\t) {\n\t\t\t\tthrow new Error(\"Cannot resume existing session.\");\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic sessions(): IterableIterator<Session> {\n\t\treturn this.sessionCache.values();\n\t}\n\n\tpublic getOrCreate(sessionId: SessionId): Session {\n\t\tconst existing = this.sessionCache.get(sessionId);\n\t\tif (existing !== undefined) {\n\t\t\treturn existing;\n\t\t}\n\t\tconst session = new Session(sessionId);\n\t\tassert(\n\t\t\tthis.uuidSpace.set(session.sessionUuid, session),\n\t\t\t0x760 /* Duplicate session in map. */,\n\t\t);\n\t\tthis.sessionCache.set(sessionId, session);\n\t\treturn session;\n\t}\n\n\tpublic get(sessionId: SessionId): Session | undefined {\n\t\treturn this.sessionCache.get(sessionId);\n\t}\n\n\tpublic getContainingCluster(\n\t\tquery: StableId,\n\t): [cluster: IdCluster, alignedLocal: LocalCompressedId] | undefined {\n\t\tconst numericStable = numericUuidFromStableId(query);\n\t\tconst possibleMatch = this.uuidSpace.getPairOrNextLower(numericStable);\n\t\tif (possibleMatch === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst [_, session] = possibleMatch;\n\t\tconst numericDelta = subtractNumericUuids(numericStable, session.sessionUuid);\n\t\tif (numericDelta > Number.MAX_SAFE_INTEGER) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst alignedLocal = localIdFromGenCount(Number(numericDelta) + 1);\n\t\tconst containingCluster = session.getClusterByLocal(alignedLocal, true);\n\t\tif (containingCluster === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn [containingCluster, alignedLocal];\n\t}\n\n\tpublic clusterCollides(cluster: IdCluster): boolean {\n\t\tconst { session: owningSession, baseLocalId, capacity } = cluster;\n\t\tconst clusterBaseNumeric = offsetNumericUuid(\n\t\t\towningSession.sessionUuid,\n\t\t\tgenCountFromLocalId(baseLocalId) - 1,\n\t\t);\n\t\tconst clusterMaxNumeric = offsetNumericUuid(clusterBaseNumeric, capacity - 1);\n\t\tlet closestMatch: [NumericUuid, Session] | undefined =\n\t\t\tthis.uuidSpace.getPairOrNextLower(clusterMaxNumeric);\n\t\t// Find the first session that is not the owner of this new cluster.\n\t\t// Once we have that, check to see if its cluster chain overlaps with the new cluster.\n\t\t// Consider the following diagram of UUID space:\n\t\t// Cluster chain A: |----------------------|\n\t\t// Cluster chain B: |----------|\n\t\t// Cluster chain C: |-------|\n\t\t// While it is true that when adding a cluster to chain C, we would find\n\t\t// the next lower session (which is B) and erroneously determine we do not collide\n\t\t// with any other session, but this situation is impossible to get into as B would\n\t\t// have detected that it collided with A (or the other way around, depending on ordering).\n\t\twhile (closestMatch !== undefined && closestMatch[1] === owningSession) {\n\t\t\tclosestMatch = this.uuidSpace.nextLowerPair(closestMatch[0]);\n\t\t}\n\t\tif (closestMatch === undefined) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst [_, session] = closestMatch;\n\t\tassert(session !== owningSession, 0x761 /* Failed to attempt to detect collisions. */);\n\t\tconst lastCluster = session.getLastCluster();\n\t\tif (lastCluster === undefined) {\n\t\t\t// If the closest session is empty (the local session), then it is guaranteed (probabilistically) that there are no\n\t\t\t// non-empty sessions that have a cluster chain that starts prior to the empty session and collides with the cluster\n\t\t\t// we are checking, so we can return false.\n\t\t\treturn false;\n\t\t}\n\t\tconst lastAllocatedNumeric = offsetNumericUuid(\n\t\t\tsession.sessionUuid,\n\t\t\tgenCountFromLocalId(lastAllocatedLocal(lastCluster)) - 1,\n\t\t);\n\t\treturn lastAllocatedNumeric >= clusterBaseNumeric;\n\t}\n\n\tpublic equals(other: Sessions, includeLocalState: boolean): boolean {\n\t\tconst checkIsSubset = (sessionsA: Sessions, sessionsB: Sessions) => {\n\t\t\tconst first = sessionsA.sessions().next();\n\t\t\tconst firstSessionThis = first.done ? undefined : first.value;\n\t\t\tfor (const [stableId, session] of sessionsA.sessionCache.entries()) {\n\t\t\t\tconst otherSession = sessionsB.sessionCache.get(stableId);\n\t\t\t\tif (otherSession === undefined) {\n\t\t\t\t\tif (!session.isEmpty() || includeLocalState) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tassert(\n\t\t\t\t\t\tsession === firstSessionThis,\n\t\t\t\t\t\t0x762 /* The only non-empty session must be the local session. */,\n\t\t\t\t\t);\n\t\t\t\t} else if (!session.equals(otherSession)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn true;\n\t\t};\n\t\treturn checkIsSubset(this, other) && checkIsSubset(other, this);\n\t}\n}\n\n/**\n * The IDs created by a specific session, stored as a cluster chain to allow for fast conversions.\n */\nexport class Session {\n\t// All clusters created by this session, in creation order (thus sorted by base final and local ID).\n\tprivate readonly clusterChain: IdCluster[] = [];\n\t// The numeric form of the SessionId\n\tpublic readonly sessionUuid: NumericUuid;\n\n\tpublic constructor(sessionId: SessionId | NumericUuid) {\n\t\tthis.sessionUuid =\n\t\t\ttypeof sessionId === \"string\" ? numericUuidFromStableId(sessionId) : sessionId;\n\t}\n\n\t/**\n\t * Adds a new empty cluster to the cluster chain of this session.\n\t */\n\tpublic addNewCluster(\n\t\tbaseFinalId: FinalCompressedId,\n\t\tcapacity: number,\n\t\tcount: number,\n\t): IdCluster {\n\t\tconst lastCluster = this.getLastCluster();\n\t\tconst newCluster: IdCluster = {\n\t\t\tsession: this,\n\t\t\tbaseFinalId,\n\t\t\tbaseLocalId: (lastCluster === undefined\n\t\t\t\t? -1\n\t\t\t\t: lastAllocatedLocal(lastCluster) - 1) as LocalCompressedId,\n\t\t\tcapacity,\n\t\t\tcount,\n\t\t};\n\t\tthis.clusterChain.push(newCluster);\n\t\treturn newCluster;\n\t}\n\n\tpublic isEmpty(): boolean {\n\t\treturn this.clusterChain.length === 0;\n\t}\n\n\t/**\n\t * Returns the last cluster in this session's cluster chain, if any.\n\t */\n\tpublic getLastCluster(): IdCluster | undefined {\n\t\treturn this.clusterChain[this.clusterChain.length - 1];\n\t}\n\n\t/**\n\t * Converts the local ID from this session to a final ID, if possible.\n\t * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.\n\t */\n\tpublic tryConvertToFinal(\n\t\tsearchLocal: LocalCompressedId,\n\t\tincludeAllocated: boolean,\n\t): FinalCompressedId | undefined {\n\t\tconst containingCluster = this.getClusterByLocal(searchLocal, includeAllocated);\n\t\tif (containingCluster === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn getAlignedFinal(containingCluster, searchLocal);\n\t}\n\n\t/**\n\t * Returns the cluster containing the supplied local ID, if possible.\n\t * @param includeAllocated - true if the conversion should succeed even if the local ID aligns with a part of the cluster that is allocated but not finalized.\n\t */\n\tpublic getClusterByLocal(\n\t\tlocalId: LocalCompressedId,\n\t\tincludeAllocated: boolean,\n\t): IdCluster | undefined {\n\t\tconst lastValidLocal: (cluster: IdCluster) => LocalCompressedId = includeAllocated\n\t\t\t? lastAllocatedLocal\n\t\t\t: lastFinalizedLocal;\n\t\tconst matchedCluster = Session.binarySearch(\n\t\t\tlocalId,\n\t\t\tthis.clusterChain,\n\t\t\t(local, cluster): number => {\n\t\t\t\tconst lastLocal = lastValidLocal(cluster);\n\t\t\t\tif (local < lastLocal) {\n\t\t\t\t\treturn 1;\n\t\t\t\t} else if (local > cluster.baseLocalId) {\n\t\t\t\t\treturn -1;\n\t\t\t\t} else {\n\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t},\n\t\t);\n\t\treturn matchedCluster;\n\t}\n\n\t/**\n\t * Returns the cluster containing the supplied final ID, if possible.\n\t */\n\tpublic getClusterByAllocatedFinal(final: FinalCompressedId): IdCluster | undefined {\n\t\treturn Session.getContainingCluster(final, this.clusterChain);\n\t}\n\n\t/**\n\t * Returns the cluster from the supplied cluster chain containing the supplied final ID, if possible.\n\t * `clusterChain` must be sorted by final/local base ID.\n\t */\n\tpublic static getContainingCluster(\n\t\tfinalId: FinalCompressedId,\n\t\tclusterChain: readonly IdCluster[],\n\t): IdCluster | undefined {\n\t\treturn Session.binarySearch(finalId, clusterChain, (final, cluster) => {\n\t\t\tconst lastFinal = lastAllocatedFinal(cluster);\n\t\t\tif (final < cluster.baseFinalId) {\n\t\t\t\treturn -1;\n\t\t\t} else if (final > lastFinal) {\n\t\t\t\treturn 1;\n\t\t\t} else {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t});\n\t}\n\n\tstatic binarySearch<S, T>(\n\t\tsearch: S,\n\t\tarr: readonly T[],\n\t\tcomparator: (a: S, b: T) => number,\n\t): T | undefined {\n\t\tlet left = 0;\n\t\tlet right = arr.length - 1;\n\t\twhile (left <= right) {\n\t\t\tconst mid = Math.floor((left + right) / 2);\n\t\t\tconst c = comparator(search, arr[mid]);\n\t\t\tif (c === 0) {\n\t\t\t\treturn arr[mid]; // Found the target, return its index.\n\t\t\t} else if (c > 0) {\n\t\t\t\tleft = mid + 1; // Continue search on right half.\n\t\t\t} else {\n\t\t\t\tright = mid - 1; // Continue search on left half.\n\t\t\t}\n\t\t}\n\t\treturn undefined; // If we reach here, target is not in array.\n\t}\n\n\tpublic equals(other: Session): boolean {\n\t\tfor (let i = 0; i < this.clusterChain.length; i++) {\n\t\t\tif (!clustersEqual(this.clusterChain[i], other.clusterChain[i])) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn this.sessionUuid === other.sessionUuid;\n\t}\n}\n\n/**\n * A cluster of final (sequenced via consensus), sequentially allocated compressed IDs.\n * A final ID in a cluster decompresses to a sequentially allocated UUID that is the result of adding its offset within\n * the cluster to base UUID for the session that created it.\n */\nexport interface IdCluster {\n\t/**\n\t * The session that created this cluster.\n\t */\n\treadonly session: Session;\n\n\t/**\n\t * The first final ID in the cluster.\n\t */\n\treadonly baseFinalId: FinalCompressedId;\n\n\t/**\n\t * The local ID aligned with `baseFinalId`.\n\t */\n\treadonly baseLocalId: LocalCompressedId;\n\n\t/**\n\t * The total number of final IDs reserved for allocation in the cluster.\n\t * Clusters are reserved in blocks as a performance optimization.\n\t */\n\tcapacity: number;\n\n\t/**\n\t * The number of final IDs currently allocated in the cluster.\n\t */\n\tcount: number;\n}\n\nexport function clustersEqual(a: IdCluster, b: IdCluster): boolean {\n\treturn (\n\t\ta.session.sessionUuid === b.session.sessionUuid &&\n\t\ta.baseFinalId === b.baseFinalId &&\n\t\ta.baseLocalId === b.baseLocalId &&\n\t\ta.capacity === b.capacity &&\n\t\ta.count === b.count\n\t);\n}\n\n/**\n * Returns the final ID that is aligned with the supplied local ID within a cluster.\n * Includes allocated IDs.\n */\nexport function getAlignedFinal(\n\tcluster: IdCluster,\n\tlocalWithin: LocalCompressedId,\n): FinalCompressedId | undefined {\n\tconst clusterOffset =\n\t\tgenCountFromLocalId(localWithin) - genCountFromLocalId(cluster.baseLocalId);\n\tif (clusterOffset < cluster.capacity) {\n\t\treturn ((cluster.baseFinalId as number) + clusterOffset) as FinalCompressedId;\n\t}\n\treturn undefined;\n}\n\n/**\n * Returns the local ID that is aligned with the supplied final ID within a cluster.\n * Fails if the supplied ID does not fall within the cluster bounds.\n */\nexport function getAlignedLocal(\n\tcluster: IdCluster,\n\tfinalWithin: FinalCompressedId,\n): LocalCompressedId {\n\tassert(\n\t\tfinalWithin >= cluster.baseFinalId && finalWithin <= lastAllocatedFinal(cluster),\n\t\t0x763 /* Supplied ID is not within the cluster. */,\n\t);\n\tconst finalDelta = finalWithin - cluster.baseFinalId;\n\treturn (cluster.baseLocalId - finalDelta) as LocalCompressedId;\n}\n\n/**\n * Returns the last allocated final ID (i.e. any ID between base final and base final + capacity) within a cluster\n */\nexport function lastAllocatedFinal(cluster: IdCluster): FinalCompressedId {\n\treturn ((cluster.baseFinalId as number) + (cluster.capacity - 1)) as FinalCompressedId;\n}\n\n/**\n * Returns the last allocated final ID (i.e. any ID between base final and base final + count) within a cluster\n */\nexport function lastFinalizedFinal(cluster: IdCluster): FinalCompressedId {\n\treturn ((cluster.baseFinalId as number) + (cluster.count - 1)) as FinalCompressedId;\n}\n\n/**\n * Returns the last allocated local ID (i.e. any ID between base local and base local + capacity) within a cluster\n */\nexport function lastAllocatedLocal(cluster: IdCluster): LocalCompressedId {\n\treturn ((cluster.baseLocalId as number) - (cluster.capacity - 1)) as LocalCompressedId;\n}\n\n/**\n * Returns the last allocated local ID (i.e. any ID between base local and base local + count) within a cluster\n */\nexport function lastFinalizedLocal(cluster: IdCluster): LocalCompressedId {\n\treturn ((cluster.baseLocalId as number) - (cluster.count - 1)) as LocalCompressedId;\n}\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { SessionId, StableId } from "@fluidframework/runtime-definitions";
|
|
6
|
+
import { LocalCompressedId, NumericUuid } from "./identifiers";
|
|
7
|
+
/**
|
|
8
|
+
* Generate a random session ID
|
|
9
|
+
*/
|
|
10
|
+
export declare function createSessionId(): SessionId;
|
|
11
|
+
/**
|
|
12
|
+
* Asserts that the given string is a stable ID.
|
|
13
|
+
*/
|
|
14
|
+
export declare function assertIsStableId(stableId: string): StableId;
|
|
15
|
+
/**
|
|
16
|
+
* Asserts that the given string is a stable ID.
|
|
17
|
+
*/
|
|
18
|
+
export declare function assertIsSessionId(stableId: string): SessionId;
|
|
19
|
+
/**
|
|
20
|
+
* Generate a random stable ID
|
|
21
|
+
*/
|
|
22
|
+
export declare function generateStableId(): StableId;
|
|
23
|
+
/**
|
|
24
|
+
* Returns true iff the given string is a valid Version 4, variant 2 UUID
|
|
25
|
+
* 'xxxxxxxx-xxxx-4xxx-vxxx-xxxxxxxxxxxx'
|
|
26
|
+
*/
|
|
27
|
+
export declare function isStableId(str: string): str is StableId;
|
|
28
|
+
/**
|
|
29
|
+
* A numeric comparator used for sorting in ascending order.
|
|
30
|
+
*
|
|
31
|
+
* Handles +/-0 like Map: -0 is equal to +0.
|
|
32
|
+
*/
|
|
33
|
+
export declare function compareFiniteNumbers<T extends number>(a: T, b: T): number;
|
|
34
|
+
/**
|
|
35
|
+
* Compares strings lexically to form a strict partial ordering.
|
|
36
|
+
*/
|
|
37
|
+
export declare function compareStrings<T extends string>(a: T, b: T): number;
|
|
38
|
+
/**
|
|
39
|
+
* Compares bigints to form a strict partial ordering.
|
|
40
|
+
*/
|
|
41
|
+
export declare function compareBigints<T extends bigint>(a: T, b: T): number;
|
|
42
|
+
export declare function genCountFromLocalId(localId: LocalCompressedId): number;
|
|
43
|
+
export declare function localIdFromGenCount(genCount: number): LocalCompressedId;
|
|
44
|
+
export declare function numericUuidFromStableId(stableId: StableId): NumericUuid;
|
|
45
|
+
export declare function stableIdFromNumericUuid(numericUuid: NumericUuid): StableId;
|
|
46
|
+
export declare function offsetNumericUuid(numericUuid: NumericUuid, offset: number): NumericUuid;
|
|
47
|
+
export declare function subtractNumericUuids(a: NumericUuid, b: NumericUuid): NumericUuid;
|
|
48
|
+
export declare function addNumericUuids(a: NumericUuid, b: NumericUuid): NumericUuid;
|
|
49
|
+
//# sourceMappingURL=utilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../src/id-compressor/utilities.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,qCAAqC,CAAC;AAE1E,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAmB/D;;GAEG;AACH,wBAAgB,eAAe,IAAI,SAAS,CAE3C;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,QAAQ,CAG3D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAG7D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,QAAQ,CAE3C;AAED;;;GAGG;AACH,wBAAgB,UAAU,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