@fluid-experimental/tree 2.118.0 → 3.0.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 +22 -4
- package/README.md +52 -48
- package/{api-extractor.json → api-extractor/api-extractor-model.json} +1 -6
- package/api-extractor/api-extractor-report.json +9 -0
- package/dist/ChangeCompression.js +4 -3
- package/dist/ChangeCompression.js.map +1 -1
- package/dist/ChangeTypes.d.ts.map +1 -1
- package/dist/Checkout.js +28 -1
- package/dist/Checkout.js.map +1 -1
- package/dist/Common.js +27 -27
- package/dist/Common.js.map +1 -1
- package/dist/EditLog.d.ts.map +1 -1
- package/dist/EditLog.js +15 -11
- package/dist/EditLog.js.map +1 -1
- package/dist/EditUtilities.js +18 -18
- package/dist/EditUtilities.js.map +1 -1
- package/dist/Forest.js +12 -3
- package/dist/Forest.js.map +1 -1
- package/dist/HistoryEditFactory.js +2 -2
- package/dist/HistoryEditFactory.js.map +1 -1
- package/dist/IdConversion.js +4 -5
- package/dist/IdConversion.js.map +1 -1
- package/dist/Identifiers.js +1 -2
- package/dist/Identifiers.js.map +1 -1
- package/dist/LazyCheckout.js +1 -0
- package/dist/LazyCheckout.js.map +1 -1
- package/dist/LogViewer.js +50 -16
- package/dist/LogViewer.js.map +1 -1
- package/dist/MergeHealth.js +121 -123
- package/dist/MergeHealth.js.map +1 -1
- package/dist/NodeIdUtilities.js +4 -5
- package/dist/NodeIdUtilities.js.map +1 -1
- package/dist/PayloadUtilities.js +1 -2
- package/dist/PayloadUtilities.js.map +1 -1
- package/dist/RevisionValueCache.js +18 -6
- package/dist/RevisionValueCache.js.map +1 -1
- package/dist/RevisionView.js +3 -3
- package/dist/RevisionView.js.map +1 -1
- package/dist/SerializationUtilities.js +4 -5
- package/dist/SerializationUtilities.js.map +1 -1
- package/dist/SharedTree.js +69 -33
- package/dist/SharedTree.js.map +1 -1
- package/dist/SharedTreeEncoder.js +4 -2
- package/dist/SharedTreeEncoder.js.map +1 -1
- package/dist/StringInterner.js +2 -2
- package/dist/StringInterner.js.map +1 -1
- package/dist/Summary.js +1 -2
- package/dist/Summary.js.map +1 -1
- package/dist/SummaryBackCompatibility.js +2 -3
- package/dist/SummaryBackCompatibility.js.map +1 -1
- package/dist/SummaryTestUtilities.js +2 -3
- package/dist/SummaryTestUtilities.js.map +1 -1
- package/dist/Transaction.js +4 -0
- package/dist/Transaction.js.map +1 -1
- package/dist/TransactionInternal.js +8 -7
- package/dist/TransactionInternal.js.map +1 -1
- package/dist/TreeCompressor.js +2 -0
- package/dist/TreeCompressor.js.map +1 -1
- package/dist/TreeNodeHandle.js +2 -0
- package/dist/TreeNodeHandle.js.map +1 -1
- package/dist/TreeView.js +9 -0
- package/dist/TreeView.js.map +1 -1
- package/dist/TreeViewUtilities.js +3 -4
- package/dist/TreeViewUtilities.js.map +1 -1
- package/dist/UndoRedoHandler.js +9 -6
- package/dist/UndoRedoHandler.js.map +1 -1
- package/dist/UuidUtilities.js +6 -6
- package/dist/UuidUtilities.js.map +1 -1
- package/dist/id-compressor/AppendOnlySortedMap.js +11 -8
- package/dist/id-compressor/AppendOnlySortedMap.js.map +1 -1
- package/dist/id-compressor/IdCompressor.js +73 -61
- package/dist/id-compressor/IdCompressor.js.map +1 -1
- package/dist/id-compressor/IdRange.js +1 -2
- package/dist/id-compressor/IdRange.js.map +1 -1
- package/dist/id-compressor/NumericUuid.js +7 -8
- package/dist/id-compressor/NumericUuid.js.map +1 -1
- package/dist/id-compressor/SessionIdNormalizer.js +9 -8
- package/dist/id-compressor/SessionIdNormalizer.js.map +1 -1
- package/dist/migration-shim/migrationDeltaHandler.js +6 -1
- package/dist/migration-shim/migrationDeltaHandler.js.map +1 -1
- package/dist/migration-shim/migrationShim.js +46 -30
- package/dist/migration-shim/migrationShim.js.map +1 -1
- package/dist/migration-shim/migrationShimFactory.js +3 -0
- package/dist/migration-shim/migrationShimFactory.js.map +1 -1
- package/dist/migration-shim/sharedTreeDeltaHandler.js +3 -1
- package/dist/migration-shim/sharedTreeDeltaHandler.js.map +1 -1
- package/dist/migration-shim/sharedTreeShim.js +7 -0
- package/dist/migration-shim/sharedTreeShim.js.map +1 -1
- package/dist/migration-shim/sharedTreeShimFactory.js +1 -0
- package/dist/migration-shim/sharedTreeShimFactory.js.map +1 -1
- package/dist/migration-shim/shimChannelServices.js +1 -0
- package/dist/migration-shim/shimChannelServices.js.map +1 -1
- package/dist/migration-shim/shimDeltaConnection.js +6 -1
- package/dist/migration-shim/shimDeltaConnection.js.map +1 -1
- package/dist/migration-shim/shimHandle.js +1 -0
- package/dist/migration-shim/shimHandle.js.map +1 -1
- package/dist/migration-shim/utils.js +3 -4
- package/dist/migration-shim/utils.js.map +1 -1
- package/dist/persisted-types/0.1.1.d.ts.map +1 -1
- package/lib/ChangeCompression.js +1 -0
- package/lib/ChangeCompression.js.map +1 -1
- package/lib/ChangeTypes.d.ts.map +1 -1
- package/lib/Checkout.js +28 -1
- package/lib/Checkout.js.map +1 -1
- package/lib/Common.js +1 -1
- package/lib/Common.js.map +1 -1
- package/lib/EditLog.d.ts.map +1 -1
- package/lib/EditLog.js +12 -8
- package/lib/EditLog.js.map +1 -1
- package/lib/Forest.js +9 -0
- package/lib/Forest.js.map +1 -1
- package/lib/LazyCheckout.js +1 -0
- package/lib/LazyCheckout.js.map +1 -1
- package/lib/LogViewer.js +50 -16
- package/lib/LogViewer.js.map +1 -1
- package/lib/MergeHealth.js +119 -121
- package/lib/MergeHealth.js.map +1 -1
- package/lib/RevisionValueCache.js +18 -6
- package/lib/RevisionValueCache.js.map +1 -1
- package/lib/SharedTree.js +69 -33
- package/lib/SharedTree.js.map +1 -1
- package/lib/SharedTreeEncoder.js +4 -2
- package/lib/SharedTreeEncoder.js.map +1 -1
- package/lib/StringInterner.js +2 -2
- package/lib/StringInterner.js.map +1 -1
- package/lib/Transaction.js +4 -0
- package/lib/Transaction.js.map +1 -1
- package/lib/TransactionInternal.js +8 -7
- package/lib/TransactionInternal.js.map +1 -1
- package/lib/TreeCompressor.js +2 -0
- package/lib/TreeCompressor.js.map +1 -1
- package/lib/TreeNodeHandle.js +2 -0
- package/lib/TreeNodeHandle.js.map +1 -1
- package/lib/TreeView.js +9 -0
- package/lib/TreeView.js.map +1 -1
- package/lib/UndoRedoHandler.js +9 -6
- package/lib/UndoRedoHandler.js.map +1 -1
- package/lib/id-compressor/AppendOnlySortedMap.js +11 -8
- package/lib/id-compressor/AppendOnlySortedMap.js.map +1 -1
- package/lib/id-compressor/IdCompressor.js +69 -57
- package/lib/id-compressor/IdCompressor.js.map +1 -1
- package/lib/id-compressor/SessionIdNormalizer.js +9 -8
- package/lib/id-compressor/SessionIdNormalizer.js.map +1 -1
- package/lib/migration-shim/migrationDeltaHandler.js +6 -1
- package/lib/migration-shim/migrationDeltaHandler.js.map +1 -1
- package/lib/migration-shim/migrationShim.js +46 -30
- package/lib/migration-shim/migrationShim.js.map +1 -1
- package/lib/migration-shim/migrationShimFactory.js +3 -0
- package/lib/migration-shim/migrationShimFactory.js.map +1 -1
- package/lib/migration-shim/sharedTreeDeltaHandler.js +3 -1
- package/lib/migration-shim/sharedTreeDeltaHandler.js.map +1 -1
- package/lib/migration-shim/sharedTreeShim.js +7 -0
- package/lib/migration-shim/sharedTreeShim.js.map +1 -1
- package/lib/migration-shim/sharedTreeShimFactory.js +1 -0
- package/lib/migration-shim/sharedTreeShimFactory.js.map +1 -1
- package/lib/migration-shim/shimChannelServices.js +1 -0
- package/lib/migration-shim/shimChannelServices.js.map +1 -1
- package/lib/migration-shim/shimDeltaConnection.js +6 -1
- package/lib/migration-shim/shimDeltaConnection.js.map +1 -1
- package/lib/migration-shim/shimHandle.js +1 -0
- package/lib/migration-shim/shimHandle.js.map +1 -1
- package/lib/persisted-types/0.1.1.d.ts.map +1 -1
- package/package.json +37 -41
- package/tsconfig.json +3 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TreeNodeHandle.js","sourceRoot":"","sources":["../src/TreeNodeHandle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I,CAAC,CAAC,CAAC;oBACxE,OAAO,aAAa,CAAC,IAAgC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;gBAC5E,CAAC;gBACD,YAAY,EAAE,IAAI;gBAClB,UAAU,EAAE,IAAI;aAChB,CAAC,CAAC;QACJ,CAAC;QAED,OAAO,aAAa,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,IAAW,IAAI;QACd,OAAO,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACnD,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { fail, memoizeGetter } from './Common.js';\nimport { Definition, NodeId } from './Identifiers.js';\nimport { TreeView, TreeViewNode } from './TreeView.js';\nimport { Payload, TraitMap, TreeNode } from './persisted-types/index.js';\n\n/**\n * A handle to a `TreeNode` that exists within a specific `TreeView`. This type provides a convenient\n * API for traversing trees of nodes in a TreeView and is not designed to provide maximum runtime\n * performance; if performance is a concern, consider using the TreeView and TreeViewNode APIs directly.\n * @internal\n */\nexport class TreeNodeHandle implements TreeNode<TreeNodeHandle, NodeId> {\n\tprivate readonly view: TreeView;\n\tprivate readonly viewNode: TreeViewNode;\n\n\t/** Construct a handle which references the node with the given id in the given `TreeView` */\n\tpublic constructor(view: TreeView, nodeId: NodeId) {\n\t\tthis.view = view;\n\t\tthis.viewNode = view.tryGetViewNode(nodeId) ?? fail('Failed to create handle: node is not present in view');\n\t}\n\n\tpublic get payload(): Payload | undefined {\n\t\treturn this.node.payload;\n\t}\n\n\tpublic get definition(): Definition {\n\t\treturn this.node.definition;\n\t}\n\n\tpublic get identifier(): NodeId {\n\t\treturn this.node.identifier;\n\t}\n\n\tpublic get traits(): TraitMap<TreeNodeHandle> {\n\t\t// Construct a new trait map that wraps each node in each trait in a handle\n\t\tconst traitMap: TraitMap<TreeNodeHandle> = {};\n\t\tconst { view } = this;\n\t\tfor (const [label, trait] of this.node.traits.entries()) {\n\t\t\tObject.defineProperty(traitMap, label, {\n\t\t\t\tget() {\n\t\t\t\t\tconst handleTrait = trait.map((node) => new TreeNodeHandle(view, node));\n\t\t\t\t\treturn memoizeGetter(this as TraitMap<TreeNodeHandle>, label, handleTrait);\n\t\t\t\t},\n\t\t\t\tconfigurable: true,\n\t\t\t\tenumerable: true,\n\t\t\t});\n\t\t}\n\n\t\treturn memoizeGetter(this, 'traits', traitMap);\n\t}\n\n\t/**\n\t * Get a `TreeViewNode` for the tree view node that this handle references\n\t */\n\tpublic get node(): TreeViewNode {\n\t\treturn memoizeGetter(this, 'node', this.viewNode);\n\t}\n}\n"]}
|
package/lib/TreeView.js
CHANGED
|
@@ -11,6 +11,15 @@ import { Side } from './persisted-types/index.js';
|
|
|
11
11
|
* @alpha
|
|
12
12
|
*/
|
|
13
13
|
export class TreeView {
|
|
14
|
+
root;
|
|
15
|
+
forest;
|
|
16
|
+
rootNode;
|
|
17
|
+
/**
|
|
18
|
+
* A cache of node's index within their parent trait.
|
|
19
|
+
* Used to avoid redundant linear scans of traits.
|
|
20
|
+
* Not shared across views; initialized to empty each time a TreeView is created.
|
|
21
|
+
*/
|
|
22
|
+
traitIndicesCache;
|
|
14
23
|
constructor(root, forest) {
|
|
15
24
|
this.root = root;
|
|
16
25
|
this.forest = forest;
|
package/lib/TreeView.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TreeView.js","sourceRoot":"","sources":["../src/TreeView.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAE7D,OAAO,EAAE,qBAAqB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAiB,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAElE,OAAO,EAAY,IAAI,EAAE,MAAM,4BAA4B,CAAC;AAsE5D;;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MAAM,IAAI,CAAC;QACX,KAAK,MAAM,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YACpD,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACrC,KAAK,MAAM,OAAO,IAAI,KAAK,IAAI,IAAI,CAAC,2BAA2B,CAAC,EAAE,CAAC;gBAClE,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;gBACxC,KAAK,CAAC,CAAC,IAAI,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;YAC3C,CAAC;QACF,CAAC;IACF,CAAC;IAEO,cAAc,CAAC,IAAU,EAAE,aAA4B;QAC9D,OAAO,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,CAAgB,CAAC,CAAC,CAAE,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,MAAqB,CAAC;IACtG,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,IAAc;QAC1B,MAAM,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,kEAAkE,CAAC,CAAC;QAC1G,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;CACD;AAED,SAAS,QAAQ,CAAC,IAAU,EAAE,KAAqB;IAClD,OAAO,CAAC,IAAI,GAAG,KAAK,CAAe,CAAC;AACrC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from '@fluidframework/core-utils/internal';\n\nimport { copyPropertyIfDefined, fail } from './Common.js';\nimport { Delta, Forest, isParentedForestNode } from './Forest.js';\nimport { NodeId, TraitLabel } from './Identifiers.js';\nimport { NodeData, Side } from './persisted-types/index.js';\n\n/**\n * Specifies the location of a trait (a labeled sequence of nodes) within the tree.\n * @alpha\n */\nexport interface TraitLocation {\n\treadonly parent: NodeId;\n\treadonly label: TraitLabel;\n}\n\n/**\n * An immutable view of a distributed tree node.\n * @alpha\n */\nexport interface TreeViewNode extends NodeData<NodeId> {\n\t/** The IDs of the children under this node */\n\treadonly traits: ReadonlyMap<TraitLabel, readonly NodeId[]>;\n\t/** The parent and trait under which this node resides. Undefined iff this is the root node of the tree (i.e. {@link initialTree}). */\n\treadonly parentage?: TraitLocation;\n}\n\n/**\n * Index of a place within a trait.\n * 0 = before all nodes,\n * 1 = after first node,\n * etc.\n * @alpha\n */\nexport type PlaceIndex = number & { readonly PlaceIndex: unique symbol };\n\n/**\n * Index of a node within a trait.\n * 0 = first node,\n * 1 = second node,\n * etc.\n * @alpha\n */\nexport type TraitNodeIndex = number & { readonly TraitNodeIndex: unique symbol };\n\n/**\n * A place within a particular `TreeView` that is anchored relative to a specific node in the tree, or relative to the outside of the trait.\n * Valid iff 'trait' is valid and, if provided, sibling is in the Location specified by 'trait'.\n * @alpha\n */\nexport interface TreeViewPlace {\n\treadonly sibling?: NodeId;\n\treadonly side: Side;\n\treadonly trait: TraitLocation;\n}\n\n/**\n * Specifies the range of nodes from `start` to `end` within a trait within a particular `TreeView`.\n * Valid iff start and end are valid and are within the same trait.\n * @alpha\n */\nexport interface TreeViewRange {\n\treadonly start: TreeViewPlace;\n\treadonly end: TreeViewPlace;\n}\n\n/**\n * Contains some redundant information. Use only in computations between edits. Do not store.\n * @internal\n */\nexport interface NodeInTrait {\n\treadonly trait: TraitLocation;\n\treadonly index: TraitNodeIndex;\n}\n\n/**\n * A view of a distributed tree.\n * @alpha\n */\nexport abstract class TreeView {\n\tpublic readonly root: NodeId;\n\tprotected readonly forest: Forest;\n\tprivate readonly rootNode: TreeViewNode;\n\n\t/**\n\t * A cache of node's index within their parent trait.\n\t * Used to avoid redundant linear scans of traits.\n\t * Not shared across views; initialized to empty each time a TreeView is created.\n\t */\n\tprivate traitIndicesCache?: Map<NodeId, TraitNodeIndex>;\n\n\tprotected constructor(root: NodeId, forest: Forest) {\n\t\tthis.root = root;\n\t\tthis.forest = forest;\n\t\tthis.rootNode = this.getViewNode(root);\n\t}\n\n\t/** @returns the number of nodes in this view */\n\tpublic get size(): number {\n\t\treturn this.forest.size;\n\t}\n\n\t/** @returns true iff a node with the given id exists in this view */\n\tpublic hasNode(id: NodeId): boolean {\n\t\treturn this.forest.has(id);\n\t}\n\n\t/**\n\t * @returns the index just after place (which specifies a location between items).\n\t * Performance note: this is O(siblings in trait).\n\t */\n\tpublic findIndexWithinTrait(place: TreeViewPlace): PlaceIndex {\n\t\tif (place.sibling === undefined) {\n\t\t\treturn this.getIndexOfSide(place.side, place.trait);\n\t\t}\n\t\treturn getIndex(place.side, this.getIndexInTrait(place.sibling));\n\t}\n\n\t/** @returns the node associated with the given id in this view. Fails if the node does not exist in this view. */\n\tpublic getViewNode(id: NodeId): TreeViewNode {\n\t\treturn this.tryGetViewNode(id) ?? fail('NodeId not found');\n\t}\n\n\t/** @returns the node associated with the given id in this view, or undefined if the node does not exist in this view */\n\tpublic tryGetViewNode(id: NodeId): TreeViewNode | undefined {\n\t\tconst forestNode = this.forest.tryGet(id);\n\t\tif (forestNode !== undefined && isParentedForestNode(forestNode)) {\n\t\t\tconst viewNode: TreeViewNode = {\n\t\t\t\tdefinition: forestNode.definition,\n\t\t\t\tidentifier: forestNode.identifier,\n\t\t\t\ttraits: forestNode.traits,\n\t\t\t\tparentage: {\n\t\t\t\t\tlabel: forestNode.traitParent,\n\t\t\t\t\tparent: forestNode.parentId,\n\t\t\t\t},\n\t\t\t};\n\t\t\tcopyPropertyIfDefined(forestNode, viewNode, 'payload');\n\t\t\treturn viewNode;\n\t\t}\n\n\t\treturn forestNode;\n\t}\n\n\t/**\n\t * @returns the label of the trait under which a node with the given id resides. Fails if the node does not exist in this view or if\n\t * it is the root node.\n\t */\n\tpublic getTraitLabel(id: NodeId): TraitLabel {\n\t\treturn this.forest.getParent(id).traitParent;\n\t}\n\n\t/**\n\t * @returns the label of the trait under which a node with the given id resides, or undefined if the node is not present in this\n\t * view or if it is the root node\n\t */\n\tpublic tryGetTraitLabel(id: NodeId): TraitLabel | undefined {\n\t\treturn this.forest.tryGetParent(id)?.traitParent;\n\t}\n\n\t/**\n\t * @returns the parent of the node with the given id. Fails if the node does not exist in this view or if it is the root node.\n\t */\n\tpublic getParentViewNode(id: NodeId): TreeViewNode {\n\t\tconst parentInfo = this.forest.getParent(id);\n\t\treturn this.getViewNode(parentInfo.parentId);\n\t}\n\n\t/**\n\t * @returns the parent of the node with the given id. Returns undefined if the node does not exist in this view or if it is the root\n\t * node.\n\t */\n\tpublic tryGetParentViewNode(id: NodeId): TreeViewNode | undefined {\n\t\tconst parentInfo = this.forest.tryGetParent(id);\n\t\tif (parentInfo === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn this.getViewNode(parentInfo.parentId);\n\t}\n\n\t/**\n\t * @returns the trait location of the node with the given id. Fails if the node does not exist in this view or of it is the root\n\t * node\n\t */\n\tpublic getTraitLocation(id: NodeId): TraitLocation {\n\t\tconst parentData = this.forest.getParent(id);\n\t\treturn {\n\t\t\tparent: parentData.parentId,\n\t\t\tlabel: parentData.traitParent,\n\t\t};\n\t}\n\n\t/**\n\t * @returns the trait location of the node with the given id, or undefined if the node does not exist in this view or if it is the root\n\t * node\n\t */\n\tpublic tryGetTraitLocation(id: NodeId): TraitLocation | undefined {\n\t\tconst parentData = this.forest.tryGetParent(id);\n\t\tif (parentData === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn {\n\t\t\tparent: parentData.parentId,\n\t\t\tlabel: parentData.traitParent,\n\t\t};\n\t}\n\n\t/**\n\t * @returns the index within the trait under which the node with the given id resides. The node must exist in this view and must have a\n\t * parent.\n\t * Performance note: this is O(siblings in trait).\n\t */\n\tpublic getIndexInTrait(id: NodeId): TraitNodeIndex {\n\t\tconst index = this.tryGetIndexInTrait(id);\n\t\treturn index ?? fail('ID does not exist in the forest.');\n\t}\n\n\t/**\n\t * @returns the index within the trait under which the node with the given id resides, or undefined if the node does not exist in this\n\t * view or does not have a parent.\n\t * Performance note: this is O(siblings in trait).\n\t */\n\tpublic tryGetIndexInTrait(id: NodeId): TraitNodeIndex | undefined {\n\t\tconst cachedIndex = this.traitIndicesCache?.get(id);\n\t\tif (cachedIndex !== undefined) {\n\t\t\treturn cachedIndex;\n\t\t}\n\n\t\tconst parentData = this.forest.tryGetParent(id);\n\t\tif (parentData === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst parent = this.forest.tryGet(parentData.parentId);\n\t\tif (parent === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst trait = parent.traits.get(parentData.traitParent) ?? fail('inconsistent forest: trait parent not found');\n\t\tlet foundIndex: TraitNodeIndex | undefined;\n\t\tif (trait.length === 0) {\n\t\t\treturn foundIndex;\n\t\t}\n\n\t\tthis.traitIndicesCache ??= new Map();\n\t\tfor (let i = 0; i < trait.length; i++) {\n\t\t\tconst nodeInTrait = trait[i];\n\t\t\tconst index = i as TraitNodeIndex;\n\t\t\tthis.traitIndicesCache.set(nodeInTrait, index);\n\t\t\tif (nodeInTrait === id) {\n\t\t\t\tfoundIndex = index;\n\t\t\t}\n\t\t}\n\n\t\treturn foundIndex;\n\t}\n\n\t/**\n\t * @returns the trait at the given location. If no such trait exists, returns an empty trait.\n\t */\n\tpublic getTrait(traitLocation: TraitLocation): readonly NodeId[] {\n\t\treturn this.getViewNode(traitLocation.parent).traits.get(traitLocation.label) ?? [];\n\t}\n\n\t/** Asserts that the view's internal state is consistent. Useful for testing/validation. */\n\tpublic assertConsistent(): void {\n\t\tthis.forest.assertConsistent();\n\t}\n\n\tpublic [Symbol.iterator](): IterableIterator<TreeViewNode> {\n\t\treturn this.iterateNodeDescendants(this.rootNode);\n\t}\n\n\t/** @returns true iff the given view is equal to this view */\n\tpublic abstract equals(view: TreeView): boolean;\n\n\t/**\n\t * @returns true iff the given view's forest of nodes is equivalent to this view's forest of nodes\n\t * @param strict - if true, the views' forests must be the same object, otherwise they must merely be equivalent\n\t */\n\tpublic hasEqualForest(view: TreeView, strict = false): boolean {\n\t\tif (this.root === view.root) {\n\t\t\t// TODO:#49100:Perf: make this faster and/or remove use by PrefetchingCheckout.\n\t\t\treturn strict ? this.forest === view.forest : this.forest.equals(view.forest);\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tprivate *iterateNodeDescendants(node: TreeViewNode): IterableIterator<TreeViewNode> {\n\t\tyield node;\n\t\tfor (const label of [...node.traits.keys()].sort()) {\n\t\t\tconst trait = node.traits.get(label);\n\t\t\tfor (const childId of trait ?? fail('Expected trait with label')) {\n\t\t\t\tconst child = this.getViewNode(childId);\n\t\t\t\tyield* this.iterateNodeDescendants(child);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getIndexOfSide(side: Side, traitLocation: TraitLocation): PlaceIndex {\n\t\treturn side === Side.After ? (0 as PlaceIndex) : (this.getTrait(traitLocation).length as PlaceIndex);\n\t}\n\n\t/**\n\t * Calculate the difference between two `TreeView`s\n\t * @param view - the other view to compare to this one\n\t * @returns A {@link Delta} which nodes must be changed, added, and removed to get from `this` to `view`.\n\t * The views must share a root.\n\t */\n\tpublic delta(view: TreeView): Delta<NodeId> {\n\t\tassert(this.root === view.root, 0x63d /* Delta can only be calculated between views that share a root */);\n\t\treturn this.forest.delta(view.forest);\n\t}\n}\n\nfunction getIndex(side: Side, index: TraitNodeIndex): PlaceIndex {\n\treturn (side + index) as PlaceIndex;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TreeView.js","sourceRoot":"","sources":["../src/TreeView.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAE7D,OAAO,EAAE,qBAAqB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAiB,oBAAoB,EAAE,MAAM,aAAa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C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC/E,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;IAEO,CAAC,sBAAsB,CAAC,IAAkB;QACjD,MAAM,IAAI,CAAC;QACX,KAAK,MAAM,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YACpD,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACrC,KAAK,MAAM,OAAO,IAAI,KAAK,IAAI,IAAI,CAAC,2BAA2B,CAAC,EAAE,CAAC;gBAClE,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;gBACxC,KAAK,CAAC,CAAC,IAAI,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;YAC3C,CAAC;QACF,CAAC;IACF,CAAC;IAEO,cAAc,CAAC,IAAU,EAAE,aAA4B;QAC9D,OAAO,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,CAAgB,CAAC,CAAC,CAAE,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,MAAqB,CAAC;IACtG,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,IAAc;QAC1B,MAAM,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,kEAAkE,CAAC,CAAC;QAC1G,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;CACD;AAED,SAAS,QAAQ,CAAC,IAAU,EAAE,KAAqB;IAClD,OAAO,CAAC,IAAI,GAAG,KAAK,CAAe,CAAC;AACrC,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from '@fluidframework/core-utils/internal';\n\nimport { copyPropertyIfDefined, fail } from './Common.js';\nimport { Delta, Forest, isParentedForestNode } from './Forest.js';\nimport { NodeId, TraitLabel } from './Identifiers.js';\nimport { NodeData, Side } from './persisted-types/index.js';\n\n/**\n * Specifies the location of a trait (a labeled sequence of nodes) within the tree.\n * @alpha\n */\nexport interface TraitLocation {\n\treadonly parent: NodeId;\n\treadonly label: TraitLabel;\n}\n\n/**\n * An immutable view of a distributed tree node.\n * @alpha\n */\nexport interface TreeViewNode extends NodeData<NodeId> {\n\t/** The IDs of the children under this node */\n\treadonly traits: ReadonlyMap<TraitLabel, readonly NodeId[]>;\n\t/** The parent and trait under which this node resides. Undefined iff this is the root node of the tree (i.e. {@link initialTree}). */\n\treadonly parentage?: TraitLocation;\n}\n\n/**\n * Index of a place within a trait.\n * 0 = before all nodes,\n * 1 = after first node,\n * etc.\n * @alpha\n */\nexport type PlaceIndex = number & { readonly PlaceIndex: unique symbol };\n\n/**\n * Index of a node within a trait.\n * 0 = first node,\n * 1 = second node,\n * etc.\n * @alpha\n */\nexport type TraitNodeIndex = number & { readonly TraitNodeIndex: unique symbol };\n\n/**\n * A place within a particular `TreeView` that is anchored relative to a specific node in the tree, or relative to the outside of the trait.\n * Valid iff 'trait' is valid and, if provided, sibling is in the Location specified by 'trait'.\n * @alpha\n */\nexport interface TreeViewPlace {\n\treadonly sibling?: NodeId;\n\treadonly side: Side;\n\treadonly trait: TraitLocation;\n}\n\n/**\n * Specifies the range of nodes from `start` to `end` within a trait within a particular `TreeView`.\n * Valid iff start and end are valid and are within the same trait.\n * @alpha\n */\nexport interface TreeViewRange {\n\treadonly start: TreeViewPlace;\n\treadonly end: TreeViewPlace;\n}\n\n/**\n * Contains some redundant information. Use only in computations between edits. Do not store.\n * @internal\n */\nexport interface NodeInTrait {\n\treadonly trait: TraitLocation;\n\treadonly index: TraitNodeIndex;\n}\n\n/**\n * A view of a distributed tree.\n * @alpha\n */\nexport abstract class TreeView {\n\tpublic readonly root: NodeId;\n\tprotected readonly forest: Forest;\n\tprivate readonly rootNode: TreeViewNode;\n\n\t/**\n\t * A cache of node's index within their parent trait.\n\t * Used to avoid redundant linear scans of traits.\n\t * Not shared across views; initialized to empty each time a TreeView is created.\n\t */\n\tprivate traitIndicesCache?: Map<NodeId, TraitNodeIndex>;\n\n\tprotected constructor(root: NodeId, forest: Forest) {\n\t\tthis.root = root;\n\t\tthis.forest = forest;\n\t\tthis.rootNode = this.getViewNode(root);\n\t}\n\n\t/** @returns the number of nodes in this view */\n\tpublic get size(): number {\n\t\treturn this.forest.size;\n\t}\n\n\t/** @returns true iff a node with the given id exists in this view */\n\tpublic hasNode(id: NodeId): boolean {\n\t\treturn this.forest.has(id);\n\t}\n\n\t/**\n\t * @returns the index just after place (which specifies a location between items).\n\t * Performance note: this is O(siblings in trait).\n\t */\n\tpublic findIndexWithinTrait(place: TreeViewPlace): PlaceIndex {\n\t\tif (place.sibling === undefined) {\n\t\t\treturn this.getIndexOfSide(place.side, place.trait);\n\t\t}\n\t\treturn getIndex(place.side, this.getIndexInTrait(place.sibling));\n\t}\n\n\t/** @returns the node associated with the given id in this view. Fails if the node does not exist in this view. */\n\tpublic getViewNode(id: NodeId): TreeViewNode {\n\t\treturn this.tryGetViewNode(id) ?? fail('NodeId not found');\n\t}\n\n\t/** @returns the node associated with the given id in this view, or undefined if the node does not exist in this view */\n\tpublic tryGetViewNode(id: NodeId): TreeViewNode | undefined {\n\t\tconst forestNode = this.forest.tryGet(id);\n\t\tif (forestNode !== undefined && isParentedForestNode(forestNode)) {\n\t\t\tconst viewNode: TreeViewNode = {\n\t\t\t\tdefinition: forestNode.definition,\n\t\t\t\tidentifier: forestNode.identifier,\n\t\t\t\ttraits: forestNode.traits,\n\t\t\t\tparentage: {\n\t\t\t\t\tlabel: forestNode.traitParent,\n\t\t\t\t\tparent: forestNode.parentId,\n\t\t\t\t},\n\t\t\t};\n\t\t\tcopyPropertyIfDefined(forestNode, viewNode, 'payload');\n\t\t\treturn viewNode;\n\t\t}\n\n\t\treturn forestNode;\n\t}\n\n\t/**\n\t * @returns the label of the trait under which a node with the given id resides. Fails if the node does not exist in this view or if\n\t * it is the root node.\n\t */\n\tpublic getTraitLabel(id: NodeId): TraitLabel {\n\t\treturn this.forest.getParent(id).traitParent;\n\t}\n\n\t/**\n\t * @returns the label of the trait under which a node with the given id resides, or undefined if the node is not present in this\n\t * view or if it is the root node\n\t */\n\tpublic tryGetTraitLabel(id: NodeId): TraitLabel | undefined {\n\t\treturn this.forest.tryGetParent(id)?.traitParent;\n\t}\n\n\t/**\n\t * @returns the parent of the node with the given id. Fails if the node does not exist in this view or if it is the root node.\n\t */\n\tpublic getParentViewNode(id: NodeId): TreeViewNode {\n\t\tconst parentInfo = this.forest.getParent(id);\n\t\treturn this.getViewNode(parentInfo.parentId);\n\t}\n\n\t/**\n\t * @returns the parent of the node with the given id. Returns undefined if the node does not exist in this view or if it is the root\n\t * node.\n\t */\n\tpublic tryGetParentViewNode(id: NodeId): TreeViewNode | undefined {\n\t\tconst parentInfo = this.forest.tryGetParent(id);\n\t\tif (parentInfo === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn this.getViewNode(parentInfo.parentId);\n\t}\n\n\t/**\n\t * @returns the trait location of the node with the given id. Fails if the node does not exist in this view or of it is the root\n\t * node\n\t */\n\tpublic getTraitLocation(id: NodeId): TraitLocation {\n\t\tconst parentData = this.forest.getParent(id);\n\t\treturn {\n\t\t\tparent: parentData.parentId,\n\t\t\tlabel: parentData.traitParent,\n\t\t};\n\t}\n\n\t/**\n\t * @returns the trait location of the node with the given id, or undefined if the node does not exist in this view or if it is the root\n\t * node\n\t */\n\tpublic tryGetTraitLocation(id: NodeId): TraitLocation | undefined {\n\t\tconst parentData = this.forest.tryGetParent(id);\n\t\tif (parentData === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn {\n\t\t\tparent: parentData.parentId,\n\t\t\tlabel: parentData.traitParent,\n\t\t};\n\t}\n\n\t/**\n\t * @returns the index within the trait under which the node with the given id resides. The node must exist in this view and must have a\n\t * parent.\n\t * Performance note: this is O(siblings in trait).\n\t */\n\tpublic getIndexInTrait(id: NodeId): TraitNodeIndex {\n\t\tconst index = this.tryGetIndexInTrait(id);\n\t\treturn index ?? fail('ID does not exist in the forest.');\n\t}\n\n\t/**\n\t * @returns the index within the trait under which the node with the given id resides, or undefined if the node does not exist in this\n\t * view or does not have a parent.\n\t * Performance note: this is O(siblings in trait).\n\t */\n\tpublic tryGetIndexInTrait(id: NodeId): TraitNodeIndex | undefined {\n\t\tconst cachedIndex = this.traitIndicesCache?.get(id);\n\t\tif (cachedIndex !== undefined) {\n\t\t\treturn cachedIndex;\n\t\t}\n\n\t\tconst parentData = this.forest.tryGetParent(id);\n\t\tif (parentData === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst parent = this.forest.tryGet(parentData.parentId);\n\t\tif (parent === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst trait = parent.traits.get(parentData.traitParent) ?? fail('inconsistent forest: trait parent not found');\n\t\tlet foundIndex: TraitNodeIndex | undefined;\n\t\tif (trait.length === 0) {\n\t\t\treturn foundIndex;\n\t\t}\n\n\t\tthis.traitIndicesCache ??= new Map();\n\t\tfor (let i = 0; i < trait.length; i++) {\n\t\t\tconst nodeInTrait = trait[i];\n\t\t\tconst index = i as TraitNodeIndex;\n\t\t\tthis.traitIndicesCache.set(nodeInTrait, index);\n\t\t\tif (nodeInTrait === id) {\n\t\t\t\tfoundIndex = index;\n\t\t\t}\n\t\t}\n\n\t\treturn foundIndex;\n\t}\n\n\t/**\n\t * @returns the trait at the given location. If no such trait exists, returns an empty trait.\n\t */\n\tpublic getTrait(traitLocation: TraitLocation): readonly NodeId[] {\n\t\treturn this.getViewNode(traitLocation.parent).traits.get(traitLocation.label) ?? [];\n\t}\n\n\t/** Asserts that the view's internal state is consistent. Useful for testing/validation. */\n\tpublic assertConsistent(): void {\n\t\tthis.forest.assertConsistent();\n\t}\n\n\tpublic [Symbol.iterator](): IterableIterator<TreeViewNode> {\n\t\treturn this.iterateNodeDescendants(this.rootNode);\n\t}\n\n\t/** @returns true iff the given view is equal to this view */\n\tpublic abstract equals(view: TreeView): boolean;\n\n\t/**\n\t * @returns true iff the given view's forest of nodes is equivalent to this view's forest of nodes\n\t * @param strict - if true, the views' forests must be the same object, otherwise they must merely be equivalent\n\t */\n\tpublic hasEqualForest(view: TreeView, strict = false): boolean {\n\t\tif (this.root === view.root) {\n\t\t\t// TODO:#49100:Perf: make this faster and/or remove use by PrefetchingCheckout.\n\t\t\treturn strict ? this.forest === view.forest : this.forest.equals(view.forest);\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tprivate *iterateNodeDescendants(node: TreeViewNode): IterableIterator<TreeViewNode> {\n\t\tyield node;\n\t\tfor (const label of [...node.traits.keys()].sort()) {\n\t\t\tconst trait = node.traits.get(label);\n\t\t\tfor (const childId of trait ?? fail('Expected trait with label')) {\n\t\t\t\tconst child = this.getViewNode(childId);\n\t\t\t\tyield* this.iterateNodeDescendants(child);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getIndexOfSide(side: Side, traitLocation: TraitLocation): PlaceIndex {\n\t\treturn side === Side.After ? (0 as PlaceIndex) : (this.getTrait(traitLocation).length as PlaceIndex);\n\t}\n\n\t/**\n\t * Calculate the difference between two `TreeView`s\n\t * @param view - the other view to compare to this one\n\t * @returns A {@link Delta} which nodes must be changed, added, and removed to get from `this` to `view`.\n\t * The views must share a root.\n\t */\n\tpublic delta(view: TreeView): Delta<NodeId> {\n\t\tassert(this.root === view.root, 0x63d /* Delta can only be calculated between views that share a root */);\n\t\treturn this.forest.delta(view.forest);\n\t}\n}\n\nfunction getIndex(side: Side, index: TraitNodeIndex): PlaceIndex {\n\treturn (side + index) as PlaceIndex;\n}\n"]}
|
package/lib/UndoRedoHandler.js
CHANGED
|
@@ -11,14 +11,9 @@ import { SharedTreeEvent } from './EventTypes.js';
|
|
|
11
11
|
* @alpha
|
|
12
12
|
*/
|
|
13
13
|
export class SharedTreeUndoRedoHandler {
|
|
14
|
+
stackManager;
|
|
14
15
|
constructor(stackManager) {
|
|
15
16
|
this.stackManager = stackManager;
|
|
16
|
-
this.treeDeltaHandler = (eventArguments) => {
|
|
17
|
-
const { editId, local, tree } = eventArguments;
|
|
18
|
-
if (local) {
|
|
19
|
-
this.stackManager.pushToCurrentOperation(new SharedTreeRevertible(editId, assertNotUndefined(tree, 'An edit committed event for a revertible edit should include the target SharedTree in its arguments.')));
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
17
|
}
|
|
23
18
|
/**
|
|
24
19
|
* Attach a shared tree to this handler. Each edit from the tree will invoke `this.stackManager`'s
|
|
@@ -34,11 +29,19 @@ export class SharedTreeUndoRedoHandler {
|
|
|
34
29
|
detachTree(tree) {
|
|
35
30
|
tree.off(SharedTreeEvent.EditCommitted, this.treeDeltaHandler);
|
|
36
31
|
}
|
|
32
|
+
treeDeltaHandler = (eventArguments) => {
|
|
33
|
+
const { editId, local, tree } = eventArguments;
|
|
34
|
+
if (local) {
|
|
35
|
+
this.stackManager.pushToCurrentOperation(new SharedTreeRevertible(editId, assertNotUndefined(tree, 'An edit committed event for a revertible edit should include the target SharedTree in its arguments.')));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Tracks a change on a shared tree and allows reverting it
|
|
40
41
|
*/
|
|
41
42
|
export class SharedTreeRevertible {
|
|
43
|
+
editId;
|
|
44
|
+
tree;
|
|
42
45
|
constructor(editId, tree) {
|
|
43
46
|
this.editId = editId;
|
|
44
47
|
this.tree = tree;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UndoRedoHandler.js","sourceRoot":"","sources":["../src/UndoRedoHandler.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MA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AO;QACb,OAAO;IACR,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assertNotUndefined } from './Common.js';\nimport { SharedTreeEvent } from './EventTypes.js';\nimport { EditId } from './Identifiers.js';\nimport { EditCommittedEventArguments, SharedTree } from './SharedTree.js';\n\n// TODO: We temporarily duplicate these contracts from 'framework/undo-redo' to unblock development\n// while we decide on the correct layering for undo.\n\n/**\n * A revertible change\n *\n * @alpha\n */\nexport interface IRevertible {\n\t/**\n\t * Revert the change\n\t */\n\trevert();\n\n\t/**\n\t * Discard the change, freeing any associated resources.\n\t */\n\tdiscard();\n}\n\n/**\n * A consumer of revertible changes.\n *\n * This interface is typically implemented by a stack which may optionally aggregate multiple\n * changes into one operation.\n *\n * @alpha\n */\nexport interface IUndoConsumer {\n\t/**\n\t * Push a revertible to the current operation. Invoked for each change on undo consumers subscribed to a SharedTree.\n\t */\n\tpushToCurrentOperation(revertible: IRevertible);\n}\n\n/**\n * A shared tree undo redo handler that will add revertible local tree changes to the provided\n * undo redo stack manager\n *\n * @alpha\n */\nexport class SharedTreeUndoRedoHandler {\n\tconstructor(private readonly stackManager: IUndoConsumer) {}\n\n\t/**\n\t * Attach a shared tree to this handler. Each edit from the tree will invoke `this.stackManager`'s\n\t * {@link IUndoConsumer.pushToCurrentOperation} method with an associated {@link IRevertible}.\n\t */\n\tpublic attachTree(tree: SharedTree): void {\n\t\ttree.on(SharedTreeEvent.EditCommitted, this.treeDeltaHandler);\n\t}\n\n\t/**\n\t * Detach a shared tree from this handler. Edits from the tree will no longer cause `this.stackManager`'s\n\t * {@link IUndoConsumer.pushToCurrentOperation} to be called.\n\t */\n\tpublic detachTree(tree: SharedTree): void {\n\t\ttree.off(SharedTreeEvent.EditCommitted, this.treeDeltaHandler);\n\t}\n\n\tprivate readonly treeDeltaHandler = (eventArguments: EditCommittedEventArguments): void => {\n\t\tconst { editId, local, tree } = eventArguments;\n\n\t\tif (local) {\n\t\t\tthis.stackManager.pushToCurrentOperation(\n\t\t\t\tnew SharedTreeRevertible(\n\t\t\t\t\teditId,\n\t\t\t\t\tassertNotUndefined(\n\t\t\t\t\t\ttree,\n\t\t\t\t\t\t'An edit committed event for a revertible edit should include the target SharedTree in its arguments.'\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\t};\n}\n\n/**\n * Tracks a change on a shared tree and allows reverting it\n */\nexport class SharedTreeRevertible implements IRevertible {\n\tconstructor(\n\t\tprivate readonly editId: EditId,\n\t\tprivate readonly tree: SharedTree\n\t) {}\n\n\tpublic revert(): void {\n\t\tthis.tree.revert(this.editId);\n\t}\n\n\tpublic discard(): void {\n\t\treturn;\n\t}\n}\n"]}
|
|
@@ -10,12 +10,13 @@ import { fail } from '../Common.js';
|
|
|
10
10
|
* Supports appending and searching.
|
|
11
11
|
*/
|
|
12
12
|
export class AppendOnlySortedMap {
|
|
13
|
+
comparator;
|
|
14
|
+
elements = [];
|
|
13
15
|
/**
|
|
14
16
|
* @param comparator - a comparator for keys
|
|
15
17
|
*/
|
|
16
18
|
constructor(comparator) {
|
|
17
19
|
this.comparator = comparator;
|
|
18
|
-
this.elements = [];
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* Gets the number of entries in this map
|
|
@@ -258,6 +259,10 @@ export class AppendOnlySortedMap {
|
|
|
258
259
|
}
|
|
259
260
|
return keyIndex;
|
|
260
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* The value xor'd with the result index when a search fails.
|
|
264
|
+
*/
|
|
265
|
+
static failureXor = -1;
|
|
261
266
|
/**
|
|
262
267
|
* Performs a binary search on the sorted array.
|
|
263
268
|
* @returns the index of the key for `search`, or (if not present) the index it would have been inserted into xor'd
|
|
@@ -288,22 +293,17 @@ export class AppendOnlySortedMap {
|
|
|
288
293
|
return (mid * 2) ^ AppendOnlySortedMap.failureXor;
|
|
289
294
|
}
|
|
290
295
|
}
|
|
291
|
-
/**
|
|
292
|
-
* The value xor'd with the result index when a search fails.
|
|
293
|
-
*/
|
|
294
|
-
AppendOnlySortedMap.failureXor = -1;
|
|
295
296
|
/**
|
|
296
297
|
* A map in which entries are always added in both key-sorted and value-sorted order.
|
|
297
298
|
* Supports appending and searching.
|
|
298
299
|
*/
|
|
299
300
|
export class AppendOnlyDoublySortedMap extends AppendOnlySortedMap {
|
|
301
|
+
extractSearchValue;
|
|
302
|
+
valueComparator;
|
|
300
303
|
constructor(keyComparator, extractSearchValue, valueComparator) {
|
|
301
304
|
super(keyComparator);
|
|
302
305
|
this.extractSearchValue = extractSearchValue;
|
|
303
306
|
this.valueComparator = valueComparator;
|
|
304
|
-
this.compareValues = (search, _, value) => {
|
|
305
|
-
return this.valueComparator(search, this.extractSearchValue(value));
|
|
306
|
-
};
|
|
307
307
|
}
|
|
308
308
|
append(key, value) {
|
|
309
309
|
if (this.elements.length !== 0 &&
|
|
@@ -312,6 +312,9 @@ export class AppendOnlyDoublySortedMap extends AppendOnlySortedMap {
|
|
|
312
312
|
}
|
|
313
313
|
super.append(key, value);
|
|
314
314
|
}
|
|
315
|
+
compareValues = (search, _, value) => {
|
|
316
|
+
return this.valueComparator(search, this.extractSearchValue(value));
|
|
317
|
+
};
|
|
315
318
|
/**
|
|
316
319
|
* @param value - the value to lookup.
|
|
317
320
|
* @returns the key associated with `value` if such an entry exists, and undefined otherwise.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AppendOnlySortedMap.js","sourceRoot":"","sources":["../../src/id-compressor/AppendOnlySortedMap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,+BAA+B;AAE/B,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAE7D,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IAG/B;;OAEG;IACH,YAAsC,UAAkC;QAAlC,eAAU,GAAV,UAAU,CAAwB;QALrD,aAAQ,G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,CAAC;QACpB,IAAI,IAAuC,CAAC;QAC5C,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;YACjC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,MAAM,CACL,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAC1F,KAAK,CAAC,mCAAmC,CACzC,CAAC;YACH,CAAC;YACD,IAAI,GAAG,EAAE,CAAC;QACX,CAAC;IACF,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable no-bitwise */\n\nimport { assert } from '@fluidframework/core-utils/internal';\n\nimport { fail } from '../Common.js';\n\n/**\n * A map in which entries are always added in key-sorted order.\n * Supports appending and searching.\n */\nexport class AppendOnlySortedMap<K, V> {\n\tprotected readonly elements: (K | V)[] = [];\n\n\t/**\n\t * @param comparator - a comparator for keys\n\t */\n\tpublic constructor(protected readonly comparator: (a: K, b: K) => number) {}\n\n\t/**\n\t * Gets the number of entries in this map\n\t */\n\tpublic get size(): number {\n\t\treturn this.elements.length / 2;\n\t}\n\n\t/**\n\t * Gets the min key in the map.\n\t */\n\tpublic minKey(): K | undefined {\n\t\treturn this.elements[0] as K | undefined;\n\t}\n\n\t/**\n\t * Gets the max key in the map.\n\t */\n\tpublic maxKey(): K | undefined {\n\t\treturn this.elements[this.elements.length - 2] as K | undefined;\n\t}\n\n\t/**\n\t * Gets the min value in the map.\n\t */\n\tpublic minValue(): V | undefined {\n\t\treturn this.elements[1] as V | undefined;\n\t}\n\n\t/**\n\t * Gets the max value in the map.\n\t */\n\tpublic maxValue(): V | undefined {\n\t\treturn this.elements[this.elements.length - 1] as V | undefined;\n\t}\n\n\t/**\n\t * Gets the first entry in the map.\n\t */\n\tpublic first(): [K, V] | undefined {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn [elements[0] as K, elements[1] as V];\n\t}\n\n\t/**\n\t * Gets the last entry in the map.\n\t */\n\tpublic last(): [K, V] | undefined {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst lastKeyIndex = length - 2;\n\t\treturn [elements[lastKeyIndex] as K, elements[lastKeyIndex + 1] as V];\n\t}\n\n\t/**\n\t * Returns the element at the insertion index.\n\t */\n\tpublic getAtIndex(index: number): [K, V] | undefined {\n\t\tconst realIndex = index * 2;\n\t\tconst { elements } = this;\n\t\tif (realIndex < 0 || realIndex > elements.length - 1) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn [elements[realIndex] as K, elements[realIndex + 1] as V];\n\t}\n\n\t/**\n\t * Gets an iterable of the entries in the map.\n\t */\n\tpublic *entries(): IterableIterator<readonly [K, V]> {\n\t\tconst { elements } = this;\n\t\tfor (let i = 0; i < elements.length; i += 2) {\n\t\t\tyield [elements[i] as K, elements[i + 1] as V];\n\t\t}\n\t}\n\n\t/**\n\t * Gets an iterable of the keys in the map.\n\t */\n\tpublic *keys(): IterableIterator<K> {\n\t\tconst { elements } = this;\n\t\tfor (let i = 0; i < elements.length; i += 2) {\n\t\t\tyield elements[i] as K;\n\t\t}\n\t}\n\n\t/**\n\t * Gets an iterable of the values in the map.\n\t */\n\tpublic *values(): IterableIterator<V> {\n\t\tconst { elements } = this;\n\t\tfor (let i = 0; i < elements.length; i += 2) {\n\t\t\tyield elements[i + 1] as V;\n\t\t}\n\t}\n\n\t/**\n\t * Gets an iterable of the entries in the map, reversed.\n\t */\n\tpublic *entriesReversed(): IterableIterator<readonly [K, V]> {\n\t\tconst { elements } = this;\n\t\tfor (let i = elements.length - 2; i >= 0; i -= 2) {\n\t\t\tyield [elements[i] as K, elements[i + 1] as V];\n\t\t}\n\t}\n\n\t/**\n\t * Adds a new key/value pair to the map. `key` must be \\> to all keys in the map.\n\t * @param key - The key to add.\n\t * @param value - The value to add.\n\t */\n\tpublic append(key: K, value: V): void {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length !== 0 && this.comparator(key, this.maxKey() as K) <= 0) {\n\t\t\tfail('Inserted key must be > all others in the map.');\n\t\t}\n\t\telements.push(key);\n\t\telements.push(value);\n\t}\n\n\t/**\n\t * Gets the value associated with `key` if such an entry exists, and `undefined` otherwise.\n\t * @param key - The key to lookup.\n\t */\n\tpublic get(key: K): V | undefined {\n\t\tconst index = AppendOnlySortedMap.keyIndexOf(this.elements, key, this.comparator);\n\t\tif (index < 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn this.elements[index + 1] as V;\n\t}\n\n\t/**\n\t * Gets the entry associated with `key` if such an entry exists, the entry associated with the next lower key if\n\t * such an entry exists, and `undefined` otherwise.\n\t * @param key - The key to lookup.\n\t */\n\tpublic getPairOrNextLower(key: K): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextLowerBy(key, this.comparator);\n\t}\n\n\t/**\n\t * Gets the entry associated with `key` if such an entry exists, the entry associated with the next higher key if\n\t * such an entry exists, and `undefined` otherwise.\n\t * @param key - The key to lookup.\n\t */\n\tpublic getPairOrNextHigher(key: K): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextHigherBy(key, this.comparator);\n\t}\n\n\t/**\n\t * Compares two `AppendOnlySortedMap`s.\n\t */\n\tpublic equals(other: AppendOnlySortedMap<K, V>, compareValues: (a: V, b: V) => boolean): boolean {\n\t\tif (other === this) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (this.elements.length !== other.elements.length) {\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (let i = this.elements.length - 2; i >= 0; i -= 2) {\n\t\t\tconst keyThis = this.elements[i] as K;\n\t\t\tconst valueThis = this.elements[i + 1] as V;\n\t\t\tconst keyOther = other.elements[i] as K;\n\t\t\tconst valueOther = other.elements[i + 1] as V;\n\t\t\tif (this.comparator(keyThis, keyOther) !== 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (!compareValues(valueThis, valueOther)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Test-only expensive assertions to check the internal validity of the data structure.\n\t */\n\tpublic assertValid(): void {\n\t\tlet prev: readonly [K, unknown] | undefined;\n\t\tfor (const kv of this.entries()) {\n\t\t\tif (prev !== undefined) {\n\t\t\t\tassert(this.comparator(kv[0], prev[0]) > 0, 0x63e /* Keys in map must be sorted. */);\n\t\t\t}\n\t\t\tprev = kv;\n\t\t}\n\t}\n\n\t/**\n\t * Queries a range of entries.\n\t * @param from - the key to start the range query at, inclusive.\n\t * @param to - the key to end the range query at, inclusive.\n\t * @returns the range of entries.\n\t */\n\tpublic *getRange(from: K, to: K): IterableIterator<readonly [K, V]> {\n\t\tconst keyIndexFrom = this.getKeyIndexOfOrNextHigher(from, this.comparator);\n\t\tif (keyIndexFrom === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst keyIndexTo = this.getKeyIndexOfOrNextLower(to, this.comparator);\n\t\tif (keyIndexTo === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tfor (let i = keyIndexFrom; i <= keyIndexTo; i += 2) {\n\t\t\tyield [this.elements[i] as K, this.elements[i + 1] as V];\n\t\t}\n\t}\n\n\tprotected getPairOrNextLowerBy<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): readonly [K, V] | undefined {\n\t\tconst keyIndex = this.getKeyIndexOfOrNextLower(search, comparator);\n\t\tif (keyIndex === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn [this.elements[keyIndex] as K, this.elements[keyIndex + 1] as V];\n\t}\n\n\tprivate getKeyIndexOfOrNextLower<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): number | undefined {\n\t\tconst { elements } = this;\n\t\tif (elements.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tlet keyIndex = AppendOnlySortedMap.keyIndexOf(elements, search, comparator);\n\t\tif (keyIndex < 0) {\n\t\t\tkeyIndex ^= AppendOnlySortedMap.failureXor;\n\t\t\tif (keyIndex > 0) {\n\t\t\t\treturn keyIndex - 2;\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\treturn keyIndex;\n\t}\n\n\tprotected getPairOrNextHigherBy<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): readonly [K, V] | undefined {\n\t\tconst keyIndex = this.getKeyIndexOfOrNextHigher(search, comparator);\n\t\tif (keyIndex === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn [this.elements[keyIndex] as K, this.elements[keyIndex + 1] as V];\n\t}\n\n\tprivate getKeyIndexOfOrNextHigher<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): number | undefined {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tlet keyIndex = AppendOnlySortedMap.keyIndexOf(elements, search, comparator);\n\t\tif (keyIndex < 0) {\n\t\t\tkeyIndex ^= AppendOnlySortedMap.failureXor;\n\t\t\tif (keyIndex < length) {\n\t\t\t\treturn keyIndex;\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\treturn keyIndex;\n\t}\n\n\t/**\n\t * The value xor'd with the result index when a search fails.\n\t */\n\tpublic static readonly failureXor = -1;\n\n\t/**\n\t * Performs a binary search on the sorted array.\n\t * @returns the index of the key for `search`, or (if not present) the index it would have been inserted into xor'd\n\t * with `failureXor`. Note that negating is not an adequate solution as that could result in -0.\n\t */\n\tpublic static keyIndexOf<T, K, V>(\n\t\telements: readonly (K | V)[],\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): number {\n\t\t// Low, high, and mid are addresses of [K,V] pairs and *not* key indices\n\t\tlet low = 0;\n\t\tlet high = elements.length / 2;\n\t\tlet mid = high >> 1;\n\t\twhile (low < high) {\n\t\t\tconst keyIndex = mid * 2;\n\t\t\tconst c = comparator(search, elements[keyIndex] as K, elements[keyIndex + 1] as V);\n\t\t\tif (c > 0) {\n\t\t\t\tlow = mid + 1;\n\t\t\t} else if (c < 0) {\n\t\t\t\thigh = mid;\n\t\t\t} else if (c === 0) {\n\t\t\t\treturn keyIndex;\n\t\t\t} else {\n\t\t\t\tfail('Invalid comparator.');\n\t\t\t}\n\t\t\tmid = (low + high) >> 1;\n\t\t}\n\t\treturn (mid * 2) ^ AppendOnlySortedMap.failureXor;\n\t}\n}\n\n/**\n * A map in which entries are always added in both key-sorted and value-sorted order.\n * Supports appending and searching.\n */\nexport class AppendOnlyDoublySortedMap<K, V, S> extends AppendOnlySortedMap<K, V> {\n\tpublic constructor(\n\t\tkeyComparator: (a: K, b: K) => number,\n\t\tprivate readonly extractSearchValue: (value: V) => S,\n\t\tprivate readonly valueComparator: (search: S, value: S) => number\n\t) {\n\t\tsuper(keyComparator);\n\t}\n\n\tpublic append(key: K, value: V): void {\n\t\tif (\n\t\t\tthis.elements.length !== 0 &&\n\t\t\tthis.valueComparator(this.extractSearchValue(value), this.extractSearchValue(this.maxValue() as V)) <= 0\n\t\t) {\n\t\t\tfail('Inserted value must be > all others in the map.');\n\t\t}\n\t\tsuper.append(key, value);\n\t}\n\n\tprivate readonly compareValues = (search: S, _: K, value: V): number => {\n\t\treturn this.valueComparator(search, this.extractSearchValue(value));\n\t};\n\n\t/**\n\t * @param value - the value to lookup.\n\t * @returns the key associated with `value` if such an entry exists, and undefined otherwise.\n\t */\n\tpublic getByValue(value: S): K | undefined {\n\t\tconst index = AppendOnlySortedMap.keyIndexOf(this.elements, value, this.compareValues);\n\t\treturn this.elements[index]?.[0];\n\t}\n\n\t/**\n\t * @param searchValue - the search value to lookup.\n\t * @returns the entry who's value, when run through the extractor provided to the constructor, matches\n\t * `searchValue`. If no such entry exists, this method returns the next lower entry as determined by the value\n\t * comparator provided to the constructor. If no such entry exists, this method returns undefined.\n\t */\n\tpublic getPairOrNextLowerByValue(searchValue: S): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextLowerBy(searchValue, this.compareValues);\n\t}\n\n\t/**\n\t * @param searchValue - the search value to lookup.\n\t * @returns the entry who's value, when run through the extractor provided to the constructor, matches `searchValue`. If no such entry\n\t * exists, this method returns the next higher entry as determined by the value comparator provided to the constructor. If no such entry\n\t * exists, this method returns undefined.\n\t */\n\tpublic getPairOrNextHigherByValue(searchValue: S): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextHigherBy(searchValue, this.compareValues);\n\t}\n\n\t/**\n\t * Test-only expensive assertions to check the internal validity of the data structure.\n\t */\n\tpublic assertValid(): void {\n\t\tsuper.assertValid();\n\t\tlet prev: readonly [unknown, V] | undefined;\n\t\tfor (const kv of this.entries()) {\n\t\t\tif (prev !== undefined) {\n\t\t\t\tassert(\n\t\t\t\t\tthis.valueComparator(this.extractSearchValue(kv[1]), this.extractSearchValue(prev[1])) > 0,\n\t\t\t\t\t0x63f /* Values in map must be sorted. */\n\t\t\t\t);\n\t\t\t}\n\t\t\tprev = kv;\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AppendOnlySortedMap.js","sourceRoot":"","sources":["../../src/id-compressor/AppendOnlySortedMap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,+BAA+B;AAE/B,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAE7D,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IAMO;IALnB,QAAQ,GAAc,EAAE,CAAC;IAE5C;;OAEG;IACH,YAAsC,UAAkC;QAAlC,eAAU,GAAV,UAAU,CAAwB;IAAG,CAAC;IAE5E;;OAEG;IAC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QAC5C,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;YACjC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,MAAM,CACL,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAC1F,KAAK,CAAC,mCAAmC,CACzC,CAAC;YACH,CAAC;YACD,IAAI,GAAG,EAAE,CAAC;QACX,CAAC;IACF,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable no-bitwise */\n\nimport { assert } from '@fluidframework/core-utils/internal';\n\nimport { fail } from '../Common.js';\n\n/**\n * A map in which entries are always added in key-sorted order.\n * Supports appending and searching.\n */\nexport class AppendOnlySortedMap<K, V> {\n\tprotected readonly elements: (K | V)[] = [];\n\n\t/**\n\t * @param comparator - a comparator for keys\n\t */\n\tpublic constructor(protected readonly comparator: (a: K, b: K) => number) {}\n\n\t/**\n\t * Gets the number of entries in this map\n\t */\n\tpublic get size(): number {\n\t\treturn this.elements.length / 2;\n\t}\n\n\t/**\n\t * Gets the min key in the map.\n\t */\n\tpublic minKey(): K | undefined {\n\t\treturn this.elements[0] as K | undefined;\n\t}\n\n\t/**\n\t * Gets the max key in the map.\n\t */\n\tpublic maxKey(): K | undefined {\n\t\treturn this.elements[this.elements.length - 2] as K | undefined;\n\t}\n\n\t/**\n\t * Gets the min value in the map.\n\t */\n\tpublic minValue(): V | undefined {\n\t\treturn this.elements[1] as V | undefined;\n\t}\n\n\t/**\n\t * Gets the max value in the map.\n\t */\n\tpublic maxValue(): V | undefined {\n\t\treturn this.elements[this.elements.length - 1] as V | undefined;\n\t}\n\n\t/**\n\t * Gets the first entry in the map.\n\t */\n\tpublic first(): [K, V] | undefined {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn [elements[0] as K, elements[1] as V];\n\t}\n\n\t/**\n\t * Gets the last entry in the map.\n\t */\n\tpublic last(): [K, V] | undefined {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst lastKeyIndex = length - 2;\n\t\treturn [elements[lastKeyIndex] as K, elements[lastKeyIndex + 1] as V];\n\t}\n\n\t/**\n\t * Returns the element at the insertion index.\n\t */\n\tpublic getAtIndex(index: number): [K, V] | undefined {\n\t\tconst realIndex = index * 2;\n\t\tconst { elements } = this;\n\t\tif (realIndex < 0 || realIndex > elements.length - 1) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn [elements[realIndex] as K, elements[realIndex + 1] as V];\n\t}\n\n\t/**\n\t * Gets an iterable of the entries in the map.\n\t */\n\tpublic *entries(): IterableIterator<readonly [K, V]> {\n\t\tconst { elements } = this;\n\t\tfor (let i = 0; i < elements.length; i += 2) {\n\t\t\tyield [elements[i] as K, elements[i + 1] as V];\n\t\t}\n\t}\n\n\t/**\n\t * Gets an iterable of the keys in the map.\n\t */\n\tpublic *keys(): IterableIterator<K> {\n\t\tconst { elements } = this;\n\t\tfor (let i = 0; i < elements.length; i += 2) {\n\t\t\tyield elements[i] as K;\n\t\t}\n\t}\n\n\t/**\n\t * Gets an iterable of the values in the map.\n\t */\n\tpublic *values(): IterableIterator<V> {\n\t\tconst { elements } = this;\n\t\tfor (let i = 0; i < elements.length; i += 2) {\n\t\t\tyield elements[i + 1] as V;\n\t\t}\n\t}\n\n\t/**\n\t * Gets an iterable of the entries in the map, reversed.\n\t */\n\tpublic *entriesReversed(): IterableIterator<readonly [K, V]> {\n\t\tconst { elements } = this;\n\t\tfor (let i = elements.length - 2; i >= 0; i -= 2) {\n\t\t\tyield [elements[i] as K, elements[i + 1] as V];\n\t\t}\n\t}\n\n\t/**\n\t * Adds a new key/value pair to the map. `key` must be \\> to all keys in the map.\n\t * @param key - The key to add.\n\t * @param value - The value to add.\n\t */\n\tpublic append(key: K, value: V): void {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length !== 0 && this.comparator(key, this.maxKey() as K) <= 0) {\n\t\t\tfail('Inserted key must be > all others in the map.');\n\t\t}\n\t\telements.push(key);\n\t\telements.push(value);\n\t}\n\n\t/**\n\t * Gets the value associated with `key` if such an entry exists, and `undefined` otherwise.\n\t * @param key - The key to lookup.\n\t */\n\tpublic get(key: K): V | undefined {\n\t\tconst index = AppendOnlySortedMap.keyIndexOf(this.elements, key, this.comparator);\n\t\tif (index < 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn this.elements[index + 1] as V;\n\t}\n\n\t/**\n\t * Gets the entry associated with `key` if such an entry exists, the entry associated with the next lower key if\n\t * such an entry exists, and `undefined` otherwise.\n\t * @param key - The key to lookup.\n\t */\n\tpublic getPairOrNextLower(key: K): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextLowerBy(key, this.comparator);\n\t}\n\n\t/**\n\t * Gets the entry associated with `key` if such an entry exists, the entry associated with the next higher key if\n\t * such an entry exists, and `undefined` otherwise.\n\t * @param key - The key to lookup.\n\t */\n\tpublic getPairOrNextHigher(key: K): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextHigherBy(key, this.comparator);\n\t}\n\n\t/**\n\t * Compares two `AppendOnlySortedMap`s.\n\t */\n\tpublic equals(other: AppendOnlySortedMap<K, V>, compareValues: (a: V, b: V) => boolean): boolean {\n\t\tif (other === this) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (this.elements.length !== other.elements.length) {\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (let i = this.elements.length - 2; i >= 0; i -= 2) {\n\t\t\tconst keyThis = this.elements[i] as K;\n\t\t\tconst valueThis = this.elements[i + 1] as V;\n\t\t\tconst keyOther = other.elements[i] as K;\n\t\t\tconst valueOther = other.elements[i + 1] as V;\n\t\t\tif (this.comparator(keyThis, keyOther) !== 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (!compareValues(valueThis, valueOther)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Test-only expensive assertions to check the internal validity of the data structure.\n\t */\n\tpublic assertValid(): void {\n\t\tlet prev: readonly [K, unknown] | undefined;\n\t\tfor (const kv of this.entries()) {\n\t\t\tif (prev !== undefined) {\n\t\t\t\tassert(this.comparator(kv[0], prev[0]) > 0, 0x63e /* Keys in map must be sorted. */);\n\t\t\t}\n\t\t\tprev = kv;\n\t\t}\n\t}\n\n\t/**\n\t * Queries a range of entries.\n\t * @param from - the key to start the range query at, inclusive.\n\t * @param to - the key to end the range query at, inclusive.\n\t * @returns the range of entries.\n\t */\n\tpublic *getRange(from: K, to: K): IterableIterator<readonly [K, V]> {\n\t\tconst keyIndexFrom = this.getKeyIndexOfOrNextHigher(from, this.comparator);\n\t\tif (keyIndexFrom === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst keyIndexTo = this.getKeyIndexOfOrNextLower(to, this.comparator);\n\t\tif (keyIndexTo === undefined) {\n\t\t\treturn;\n\t\t}\n\n\t\tfor (let i = keyIndexFrom; i <= keyIndexTo; i += 2) {\n\t\t\tyield [this.elements[i] as K, this.elements[i + 1] as V];\n\t\t}\n\t}\n\n\tprotected getPairOrNextLowerBy<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): readonly [K, V] | undefined {\n\t\tconst keyIndex = this.getKeyIndexOfOrNextLower(search, comparator);\n\t\tif (keyIndex === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn [this.elements[keyIndex] as K, this.elements[keyIndex + 1] as V];\n\t}\n\n\tprivate getKeyIndexOfOrNextLower<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): number | undefined {\n\t\tconst { elements } = this;\n\t\tif (elements.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tlet keyIndex = AppendOnlySortedMap.keyIndexOf(elements, search, comparator);\n\t\tif (keyIndex < 0) {\n\t\t\tkeyIndex ^= AppendOnlySortedMap.failureXor;\n\t\t\tif (keyIndex > 0) {\n\t\t\t\treturn keyIndex - 2;\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\treturn keyIndex;\n\t}\n\n\tprotected getPairOrNextHigherBy<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): readonly [K, V] | undefined {\n\t\tconst keyIndex = this.getKeyIndexOfOrNextHigher(search, comparator);\n\t\tif (keyIndex === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn [this.elements[keyIndex] as K, this.elements[keyIndex + 1] as V];\n\t}\n\n\tprivate getKeyIndexOfOrNextHigher<T>(\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): number | undefined {\n\t\tconst { elements } = this;\n\t\tconst { length } = elements;\n\t\tif (length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tlet keyIndex = AppendOnlySortedMap.keyIndexOf(elements, search, comparator);\n\t\tif (keyIndex < 0) {\n\t\t\tkeyIndex ^= AppendOnlySortedMap.failureXor;\n\t\t\tif (keyIndex < length) {\n\t\t\t\treturn keyIndex;\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\t\treturn keyIndex;\n\t}\n\n\t/**\n\t * The value xor'd with the result index when a search fails.\n\t */\n\tpublic static readonly failureXor = -1;\n\n\t/**\n\t * Performs a binary search on the sorted array.\n\t * @returns the index of the key for `search`, or (if not present) the index it would have been inserted into xor'd\n\t * with `failureXor`. Note that negating is not an adequate solution as that could result in -0.\n\t */\n\tpublic static keyIndexOf<T, K, V>(\n\t\telements: readonly (K | V)[],\n\t\tsearch: T,\n\t\tcomparator: (search: T, key: K, value: V) => number\n\t): number {\n\t\t// Low, high, and mid are addresses of [K,V] pairs and *not* key indices\n\t\tlet low = 0;\n\t\tlet high = elements.length / 2;\n\t\tlet mid = high >> 1;\n\t\twhile (low < high) {\n\t\t\tconst keyIndex = mid * 2;\n\t\t\tconst c = comparator(search, elements[keyIndex] as K, elements[keyIndex + 1] as V);\n\t\t\tif (c > 0) {\n\t\t\t\tlow = mid + 1;\n\t\t\t} else if (c < 0) {\n\t\t\t\thigh = mid;\n\t\t\t} else if (c === 0) {\n\t\t\t\treturn keyIndex;\n\t\t\t} else {\n\t\t\t\tfail('Invalid comparator.');\n\t\t\t}\n\t\t\tmid = (low + high) >> 1;\n\t\t}\n\t\treturn (mid * 2) ^ AppendOnlySortedMap.failureXor;\n\t}\n}\n\n/**\n * A map in which entries are always added in both key-sorted and value-sorted order.\n * Supports appending and searching.\n */\nexport class AppendOnlyDoublySortedMap<K, V, S> extends AppendOnlySortedMap<K, V> {\n\tpublic constructor(\n\t\tkeyComparator: (a: K, b: K) => number,\n\t\tprivate readonly extractSearchValue: (value: V) => S,\n\t\tprivate readonly valueComparator: (search: S, value: S) => number\n\t) {\n\t\tsuper(keyComparator);\n\t}\n\n\tpublic append(key: K, value: V): void {\n\t\tif (\n\t\t\tthis.elements.length !== 0 &&\n\t\t\tthis.valueComparator(this.extractSearchValue(value), this.extractSearchValue(this.maxValue() as V)) <= 0\n\t\t) {\n\t\t\tfail('Inserted value must be > all others in the map.');\n\t\t}\n\t\tsuper.append(key, value);\n\t}\n\n\tprivate readonly compareValues = (search: S, _: K, value: V): number => {\n\t\treturn this.valueComparator(search, this.extractSearchValue(value));\n\t};\n\n\t/**\n\t * @param value - the value to lookup.\n\t * @returns the key associated with `value` if such an entry exists, and undefined otherwise.\n\t */\n\tpublic getByValue(value: S): K | undefined {\n\t\tconst index = AppendOnlySortedMap.keyIndexOf(this.elements, value, this.compareValues);\n\t\treturn this.elements[index]?.[0];\n\t}\n\n\t/**\n\t * @param searchValue - the search value to lookup.\n\t * @returns the entry who's value, when run through the extractor provided to the constructor, matches\n\t * `searchValue`. If no such entry exists, this method returns the next lower entry as determined by the value\n\t * comparator provided to the constructor. If no such entry exists, this method returns undefined.\n\t */\n\tpublic getPairOrNextLowerByValue(searchValue: S): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextLowerBy(searchValue, this.compareValues);\n\t}\n\n\t/**\n\t * @param searchValue - the search value to lookup.\n\t * @returns the entry who's value, when run through the extractor provided to the constructor, matches `searchValue`. If no such entry\n\t * exists, this method returns the next higher entry as determined by the value comparator provided to the constructor. If no such entry\n\t * exists, this method returns undefined.\n\t */\n\tpublic getPairOrNextHigherByValue(searchValue: S): readonly [K, V] | undefined {\n\t\treturn this.getPairOrNextHigherBy(searchValue, this.compareValues);\n\t}\n\n\t/**\n\t * Test-only expensive assertions to check the internal validity of the data structure.\n\t */\n\tpublic assertValid(): void {\n\t\tsuper.assertValid();\n\t\tlet prev: readonly [unknown, V] | undefined;\n\t\tfor (const kv of this.entries()) {\n\t\t\tif (prev !== undefined) {\n\t\t\t\tassert(\n\t\t\t\t\tthis.valueComparator(this.extractSearchValue(kv[1]), this.extractSearchValue(prev[1])) > 0,\n\t\t\t\t\t0x63f /* Values in map must be sorted. */\n\t\t\t\t);\n\t\t\t}\n\t\t\tprev = kv;\n\t\t}\n\t}\n}\n"]}
|
|
@@ -108,6 +108,17 @@ const nonStableOverridePrefix = '\ue15e'; // A character in the Private Use Area
|
|
|
108
108
|
*
|
|
109
109
|
*/
|
|
110
110
|
export class IdCompressor {
|
|
111
|
+
localSessionId;
|
|
112
|
+
reservedIdCount;
|
|
113
|
+
/**
|
|
114
|
+
* Max allowed cluster size
|
|
115
|
+
*/
|
|
116
|
+
static maxClusterSize = 2 ** 20;
|
|
117
|
+
/**
|
|
118
|
+
* Trivially reach consensus on default cluster size and reserved IDs.
|
|
119
|
+
* These initial values must *NOT* change without careful consideration to compatibility.
|
|
120
|
+
*/
|
|
121
|
+
newClusterCapacity = defaultClusterCapacity;
|
|
111
122
|
/**
|
|
112
123
|
* The size of each newly created ID cluster.
|
|
113
124
|
*/
|
|
@@ -129,6 +140,64 @@ export class IdCompressor {
|
|
|
129
140
|
get attributionId() {
|
|
130
141
|
return this.localSession.attributionId;
|
|
131
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Session ID -\> data about the session's current cluster.
|
|
145
|
+
* Sessions are mutable, and thus should only be created via `createSession`.
|
|
146
|
+
*/
|
|
147
|
+
sessions = new Map();
|
|
148
|
+
/**
|
|
149
|
+
* The `IdCompressor`'s current local session.
|
|
150
|
+
*/
|
|
151
|
+
localSession;
|
|
152
|
+
/**
|
|
153
|
+
* The base final ID of the next cluster to be created.
|
|
154
|
+
*/
|
|
155
|
+
nextClusterBaseFinalId = 0;
|
|
156
|
+
/**
|
|
157
|
+
* Total number of IDs created locally during the current session.
|
|
158
|
+
*/
|
|
159
|
+
localIdCount = 0;
|
|
160
|
+
/**
|
|
161
|
+
* The most recent (i.e. smallest, due to being negative) local ID in a range returned by `takeNextCreationRange`.
|
|
162
|
+
* Undefined if no non-empty ranges have ever been returned by this compressor.
|
|
163
|
+
*/
|
|
164
|
+
lastTakenLocalId;
|
|
165
|
+
/**
|
|
166
|
+
* Maps local IDs to override strings. This will contain an entry for every override assigned to a local ID generated during
|
|
167
|
+
* the current session, and retains entries for the lifetime of this compressor.
|
|
168
|
+
*/
|
|
169
|
+
localOverrides = new AppendOnlySortedMap(compareFiniteNumbersReversed);
|
|
170
|
+
/**
|
|
171
|
+
* Maps local IDs to the final ID they are associated with (if any), and maps final IDs to the corresponding local ID (if any).
|
|
172
|
+
* This is used to efficiently compute normalization. This map can be thought of as mapping ranges of "optimistic uncertainty"
|
|
173
|
+
* (local IDs) to the result of consensus (reserved ranges of final IDs, a.k.a. clusters). Any given range of local IDs
|
|
174
|
+
* does not necessarily span an entire cluster, as some session-space IDs may be allocated *after* a cluster has been allocated
|
|
175
|
+
* 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.
|
|
176
|
+
* However, there is always a range of local IDs with size \>= 1 associated with the beginning of every cluster, as clusters are only
|
|
177
|
+
* created *after* they are needed and thus there is some period of uncertainty after local IDs have been handed out but before the
|
|
178
|
+
* range containing them has been finalized. There may also be ranges of local IDs that do not start at the beginning of a
|
|
179
|
+
* cluster; this happens when a cluster is expanded instead of allocating a new one.
|
|
180
|
+
* Additionally, session space IDs associated with an override string will also always be local IDs, because there is uncertainty as
|
|
181
|
+
* to whether another client simultaneously allocated the same override and could get sequenced first (a.k.a. unification) and its
|
|
182
|
+
* final ID would be associated with that override.
|
|
183
|
+
* See `SessionIdNormalizer` for more.
|
|
184
|
+
*/
|
|
185
|
+
sessionIdNormalizer = new SessionIdNormalizer();
|
|
186
|
+
/**
|
|
187
|
+
* Contains entries for cluster base UUIDs and override strings (both local and final).
|
|
188
|
+
* As a performance optimization, entries for finalized strings also include the containing cluster object.
|
|
189
|
+
* This can be viewed as three separate tables: the inverse table for `localOverrides`, the inverse table for the union of all
|
|
190
|
+
* the overrides of the clusters in `finalIdToCluster`, and the inverse lookup of cluster base UUIDs to their clusters.
|
|
191
|
+
* This is unified as a performance optimization, as the common case does not have overridden IDs. It is a btree due to the need
|
|
192
|
+
* to make range queries.
|
|
193
|
+
*/
|
|
194
|
+
clustersAndOverridesInversion = new BTree(undefined, compareStrings);
|
|
195
|
+
/**
|
|
196
|
+
* Maps the first final ID in a cluster to its owning cluster.
|
|
197
|
+
* Can be searched in O(log n) to determine clusters for any final ID.
|
|
198
|
+
*/
|
|
199
|
+
finalIdToCluster = new AppendOnlySortedMap(compareFiniteNumbers);
|
|
200
|
+
logger;
|
|
132
201
|
/**
|
|
133
202
|
* @param localSessionId - the `IdCompressor`'s current local session ID.
|
|
134
203
|
* @param reservedIdCount - the number of IDs that will be known by this compressor without relying on consensus.
|
|
@@ -143,59 +212,6 @@ export class IdCompressor {
|
|
|
143
212
|
constructor(localSessionId, reservedIdCount, attributionId, logger) {
|
|
144
213
|
this.localSessionId = localSessionId;
|
|
145
214
|
this.reservedIdCount = reservedIdCount;
|
|
146
|
-
/**
|
|
147
|
-
* Trivially reach consensus on default cluster size and reserved IDs.
|
|
148
|
-
* These initial values must *NOT* change without careful consideration to compatibility.
|
|
149
|
-
*/
|
|
150
|
-
this.newClusterCapacity = defaultClusterCapacity;
|
|
151
|
-
/**
|
|
152
|
-
* Session ID -\> data about the session's current cluster.
|
|
153
|
-
* Sessions are mutable, and thus should only be created via `createSession`.
|
|
154
|
-
*/
|
|
155
|
-
this.sessions = new Map();
|
|
156
|
-
/**
|
|
157
|
-
* The base final ID of the next cluster to be created.
|
|
158
|
-
*/
|
|
159
|
-
this.nextClusterBaseFinalId = 0;
|
|
160
|
-
/**
|
|
161
|
-
* Total number of IDs created locally during the current session.
|
|
162
|
-
*/
|
|
163
|
-
this.localIdCount = 0;
|
|
164
|
-
/**
|
|
165
|
-
* Maps local IDs to override strings. This will contain an entry for every override assigned to a local ID generated during
|
|
166
|
-
* the current session, and retains entries for the lifetime of this compressor.
|
|
167
|
-
*/
|
|
168
|
-
this.localOverrides = new AppendOnlySortedMap(compareFiniteNumbersReversed);
|
|
169
|
-
/**
|
|
170
|
-
* Maps local IDs to the final ID they are associated with (if any), and maps final IDs to the corresponding local ID (if any).
|
|
171
|
-
* This is used to efficiently compute normalization. This map can be thought of as mapping ranges of "optimistic uncertainty"
|
|
172
|
-
* (local IDs) to the result of consensus (reserved ranges of final IDs, a.k.a. clusters). Any given range of local IDs
|
|
173
|
-
* does not necessarily span an entire cluster, as some session-space IDs may be allocated *after* a cluster has been allocated
|
|
174
|
-
* 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.
|
|
175
|
-
* However, there is always a range of local IDs with size \>= 1 associated with the beginning of every cluster, as clusters are only
|
|
176
|
-
* created *after* they are needed and thus there is some period of uncertainty after local IDs have been handed out but before the
|
|
177
|
-
* range containing them has been finalized. There may also be ranges of local IDs that do not start at the beginning of a
|
|
178
|
-
* cluster; this happens when a cluster is expanded instead of allocating a new one.
|
|
179
|
-
* Additionally, session space IDs associated with an override string will also always be local IDs, because there is uncertainty as
|
|
180
|
-
* to whether another client simultaneously allocated the same override and could get sequenced first (a.k.a. unification) and its
|
|
181
|
-
* final ID would be associated with that override.
|
|
182
|
-
* See `SessionIdNormalizer` for more.
|
|
183
|
-
*/
|
|
184
|
-
this.sessionIdNormalizer = new SessionIdNormalizer();
|
|
185
|
-
/**
|
|
186
|
-
* Contains entries for cluster base UUIDs and override strings (both local and final).
|
|
187
|
-
* As a performance optimization, entries for finalized strings also include the containing cluster object.
|
|
188
|
-
* This can be viewed as three separate tables: the inverse table for `localOverrides`, the inverse table for the union of all
|
|
189
|
-
* the overrides of the clusters in `finalIdToCluster`, and the inverse lookup of cluster base UUIDs to their clusters.
|
|
190
|
-
* This is unified as a performance optimization, as the common case does not have overridden IDs. It is a btree due to the need
|
|
191
|
-
* to make range queries.
|
|
192
|
-
*/
|
|
193
|
-
this.clustersAndOverridesInversion = new BTree(undefined, compareStrings);
|
|
194
|
-
/**
|
|
195
|
-
* Maps the first final ID in a cluster to its owning cluster.
|
|
196
|
-
* Can be searched in O(log n) to determine clusters for any final ID.
|
|
197
|
-
*/
|
|
198
|
-
this.finalIdToCluster = new AppendOnlySortedMap(compareFiniteNumbers);
|
|
199
215
|
assert(reservedIdCount >= 0, 0x642 /* reservedIdCount must be non-negative */);
|
|
200
216
|
if (attributionId !== undefined) {
|
|
201
217
|
assertIsUuidString(attributionId);
|
|
@@ -1351,10 +1367,6 @@ export class IdCompressor {
|
|
|
1351
1367
|
return serialized;
|
|
1352
1368
|
}
|
|
1353
1369
|
}
|
|
1354
|
-
/**
|
|
1355
|
-
* Max allowed cluster size
|
|
1356
|
-
*/
|
|
1357
|
-
IdCompressor.maxClusterSize = 2 ** 20;
|
|
1358
1370
|
/**
|
|
1359
1371
|
* The version of `IdCompressor` that is currently persisted.
|
|
1360
1372
|
*/
|