Skip to content

Commit b49ab63

Browse files
committed
Dedup factories
1 parent 8f6dd45 commit b49ab63

File tree

15 files changed

+69
-103
lines changed

15 files changed

+69
-103
lines changed

packages/dds/tree/src/shared-tree/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
export {
77
type ISharedTree,
8-
SharedTreeFactory,
8+
type SharedTreeOptionsInternal,
99
type SharedTreeOptions,
1010
SharedTree,
1111
getBranch,

packages/dds/tree/src/shared-tree/publicContracts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import type { IChannelAttributes } from "@fluidframework/datastore-definitions/internal";
7+
import { pkgVersion } from "../packageVersion.js";
78

89
/**
910
* {@inheritDoc @fluidframework/shared-object-base#ISharedObjectFactory."type"}
@@ -20,5 +21,5 @@ export const SharedTreeFactoryType = "https://graph.microsoft.com/types/tree";
2021
export const SharedTreeAttributes: IChannelAttributes = {
2122
type: SharedTreeFactoryType,
2223
snapshotFormatVersion: "0.0.0",
23-
packageVersion: "0.0.0",
24+
packageVersion: pkgVersion,
2425
};

packages/dds/tree/src/shared-tree/sharedTree.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { assert } from "@fluidframework/core-utils/internal";
77
import type { ErasedType, IFluidHandle } from "@fluidframework/core-interfaces/internal";
88
import type {
99
IChannelAttributes,
10-
IChannelFactory,
1110
IFluidDataStoreRuntime,
12-
IChannelServices,
1311
IChannelStorageService,
1412
} from "@fluidframework/datastore-definitions/internal";
1513
import type { ISharedObject } from "@fluidframework/shared-object-base/internal";
@@ -84,7 +82,6 @@ import type { SharedTreeEditBuilder } from "./sharedTreeEditBuilder.js";
8482
import { type TreeCheckout, type BranchableTree, createTreeCheckout } from "./treeCheckout.js";
8583
import { breakingClass, fail, throwIfBroken } from "../util/index.js";
8684
import type { IIdCompressor } from "@fluidframework/id-compressor";
87-
import { SharedTreeAttributes, SharedTreeFactoryType } from "./publicContracts.js";
8885

8986
/**
9087
* Copy of data from an {@link ISharedTree} at some point in time.
@@ -642,34 +639,6 @@ export const defaultSharedTreeOptions: Required<SharedTreeOptionsInternal> = {
642639
disposeForksAfterTransaction: true,
643640
};
644641

645-
/**
646-
* A channel factory that creates {@link ISharedTree}s.
647-
*/
648-
export class SharedTreeFactory implements IChannelFactory<ISharedTree> {
649-
public readonly type: string = SharedTreeFactoryType;
650-
651-
public readonly attributes: IChannelAttributes = SharedTreeAttributes;
652-
653-
public constructor(private readonly options: SharedTreeOptionsInternal = {}) {}
654-
655-
public async load(
656-
runtime: IFluidDataStoreRuntime,
657-
id: string,
658-
services: IChannelServices,
659-
channelAttributes: Readonly<IChannelAttributes>,
660-
): Promise<SharedTree> {
661-
const tree = new SharedTree(id, runtime, channelAttributes, this.options);
662-
await tree.load(services);
663-
return tree;
664-
}
665-
666-
public create(runtime: IFluidDataStoreRuntime, id: string): SharedTree {
667-
const tree = new SharedTree(id, runtime, this.attributes, this.options);
668-
tree.initializeLocal();
669-
return tree;
670-
}
671-
}
672-
673642
function verboseFromCursor(
674643
reader: ITreeCursor,
675644
schema: ReadonlyMap<TreeNodeSchemaIdentifier, TreeNodeStoredSchema>,

packages/dds/tree/src/test/feature-libraries/chunked-forest/chunkEncodingEndToEnd.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import {
5050
import {
5151
type TreeStoredContent,
5252
type ISharedTreeEditor,
53-
SharedTreeFactory,
5453
Tree,
5554
ForestTypeOptimized,
5655
} from "../../../shared-tree/index.js";
@@ -83,6 +82,7 @@ import { brand } from "../../../util/index.js";
8382
// eslint-disable-next-line import/no-internal-modules
8483
import { ChunkedForest } from "../../../feature-libraries/chunked-forest/chunkedForest.js";
8584
import { MockFluidDataStoreRuntime } from "@fluidframework/test-runtime-utils/internal";
85+
import { TreeFactory } from "../../../treeFactory.js";
8686

8787
const options = {
8888
jsonValidator: typeboxValidator,
@@ -395,7 +395,7 @@ describe("End to end chunked encoding", () => {
395395
});
396396

397397
it("Initializing tree creates uniform chunks with encoded identifiers", async () => {
398-
const factory = new SharedTreeFactory({
398+
const factory = new TreeFactory({
399399
jsonValidator: typeboxValidator,
400400
forest: ForestTypeOptimized,
401401
});

packages/dds/tree/src/test/shared-tree/fuzz/fuzzEditGenerators.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ import type {
2424
TreeNodeSchemaIdentifier,
2525
} from "../../../core/index.js";
2626
import { type DownPath, toDownPath } from "../../../feature-libraries/index.js";
27-
import {
28-
Tree,
29-
type ISharedTree,
30-
type SharedTree,
31-
type SharedTreeFactory,
32-
} from "../../../shared-tree/index.js";
27+
import { Tree, type ISharedTree, type SharedTree } from "../../../shared-tree/index.js";
3328
import { fail, getOrCreate, makeArray } from "../../../util/index.js";
3429

3530
import {
@@ -69,6 +64,7 @@ import {
6964
type TreeNode,
7065
type TreeNodeSchema,
7166
} from "../../../simple-tree/index.js";
67+
import type { TreeFactory } from "../../../treeFactory.js";
7268

7369
export type FuzzView = SchematizingSimpleTreeView<typeof fuzzFieldSchema> & {
7470
/**
@@ -98,7 +94,7 @@ export type FuzzTransactionView = SchematizingSimpleTreeView<typeof fuzzFieldSch
9894
currentSchema: FuzzNodeSchema;
9995
};
10096

101-
export interface FuzzTestState extends DDSFuzzTestState<SharedTreeFactory> {
97+
export interface FuzzTestState extends DDSFuzzTestState<TreeFactory> {
10298
/**
10399
* Schematized view of clients and their nodeSchemas. Created lazily by viewFromState.
104100
*
@@ -118,7 +114,7 @@ export interface FuzzTestState extends DDSFuzzTestState<SharedTreeFactory> {
118114

119115
export function viewFromState(
120116
state: FuzzTestState,
121-
client: Client<SharedTreeFactory> = state.client,
117+
client: Client<TreeFactory> = state.client,
122118
): FuzzView {
123119
state.clientViews ??= new Map();
124120
const view =
@@ -594,7 +590,7 @@ export const makeConstraintEditGenerator = (
594590

595591
export function makeOpGenerator(
596592
weightsArg: Partial<EditGeneratorOpWeights> = defaultEditGeneratorOpWeights,
597-
): AsyncGenerator<Operation, DDSFuzzTestState<SharedTreeFactory>> {
593+
): AsyncGenerator<Operation, DDSFuzzTestState<TreeFactory>> {
598594
const weights = {
599595
...defaultEditGeneratorOpWeights,
600596
...weightsArg,

packages/dds/tree/src/test/shared-tree/fuzz/fuzzEditReducers.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { IFluidHandle } from "@fluidframework/core-interfaces";
1212

1313
import type { Revertible } from "../../../core/index.js";
1414
import type { DownPath } from "../../../feature-libraries/index.js";
15-
import { Tree, type SharedTreeFactory } from "../../../shared-tree/index.js";
15+
import { Tree } from "../../../shared-tree/index.js";
1616
import { fail } from "../../../util/index.js";
1717
import { validateFuzzTreeConsistency, viewCheckout } from "../../utils.js";
1818

@@ -62,8 +62,9 @@ import {
6262
type TreeNode,
6363
type TreeNodeSchema,
6464
} from "../../../simple-tree/index.js";
65+
import type { TreeFactory } from "../../../treeFactory.js";
6566

66-
const syncFuzzReducer = combineReducers<Operation, DDSFuzzTestState<SharedTreeFactory>>({
67+
const syncFuzzReducer = combineReducers<Operation, DDSFuzzTestState<TreeFactory>>({
6768
treeEdit: (state, { edit }) => {
6869
switch (edit.type) {
6970
case "fieldEdit": {
@@ -92,18 +93,18 @@ const syncFuzzReducer = combineReducers<Operation, DDSFuzzTestState<SharedTreeFa
9293
applyConstraint(state, operation);
9394
},
9495
});
95-
export const fuzzReducer: AsyncReducer<
96-
Operation,
97-
DDSFuzzTestState<SharedTreeFactory>
98-
> = async (state, operation) => syncFuzzReducer(state, operation);
96+
export const fuzzReducer: AsyncReducer<Operation, DDSFuzzTestState<TreeFactory>> = async (
97+
state,
98+
operation,
99+
) => syncFuzzReducer(state, operation);
99100

100-
export function checkTreesAreSynchronized(trees: readonly Client<SharedTreeFactory>[]) {
101+
export function checkTreesAreSynchronized(trees: readonly Client<TreeFactory>[]) {
101102
for (const tree of trees) {
102103
validateFuzzTreeConsistency(trees[0], tree);
103104
}
104105
}
105106

106-
export function applySynchronizationOp(state: DDSFuzzTestState<SharedTreeFactory>) {
107+
export function applySynchronizationOp(state: DDSFuzzTestState<TreeFactory>) {
107108
state.containerRuntimeFactory.processAllMessages();
108109
const connectedClients = state.clients.filter((client) => client.containerRuntime.connected);
109110
if (connectedClients.length > 0) {

packages/dds/tree/src/test/shared-tree/sharedTree.bench.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
TreeCompressionStrategy,
2121
jsonableTreeFromCursor,
2222
} from "../../feature-libraries/index.js";
23-
import { SharedTreeFactory, type CheckoutFlexTreeView } from "../../shared-tree/index.js";
23+
import type { CheckoutFlexTreeView } from "../../shared-tree/index.js";
2424
import {
2525
type JSDeepTree,
2626
type JSWideTree,
@@ -49,6 +49,7 @@ import {
4949
} from "../utils.js";
5050
import { insert } from "../sequenceRootUtils.js";
5151
import { cursorFromInsertable } from "../../simple-tree/index.js";
52+
import { TreeFactory } from "../../treeFactory.js";
5253

5354
// number of nodes in test for wide trees
5455
const nodesCountWide = [
@@ -64,7 +65,7 @@ const nodesCountDeep = [
6465
];
6566

6667
// TODO: ADO#7111 Schema should be fixed to enable schema based encoding.
67-
const factory = new SharedTreeFactory({
68+
const factory = new TreeFactory({
6869
jsonValidator: typeboxValidator,
6970
treeEncodeType: TreeCompressionStrategy.Uncompressed,
7071
});

packages/dds/tree/src/test/shared-tree/sharedTree.spec.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import {
5353
getBranch,
5454
type ISharedTree,
5555
type SharedTree,
56-
SharedTreeFactory,
5756
Tree,
5857
type TreeCheckout,
5958
} from "../../shared-tree/index.js";
@@ -90,7 +89,7 @@ import {
9089
NumberArray,
9190
validateViewConsistency,
9291
} from "../utils.js";
93-
import { configuredSharedTree } from "../../treeFactory.js";
92+
import { configuredSharedTree, TreeFactory } from "../../treeFactory.js";
9493
import type { ISharedObjectKind } from "@fluidframework/shared-object-base/internal";
9594
import { TestAnchor } from "../testAnchor.js";
9695
// eslint-disable-next-line import/no-internal-modules
@@ -177,7 +176,7 @@ describe("SharedTree", () => {
177176
const provider = await TestTreeProvider.create(
178177
2,
179178
SummarizeType.disabled,
180-
new SharedTreeFactory({
179+
new TreeFactory({
181180
jsonValidator: typeboxValidator,
182181
treeEncodeType: TreeCompressionStrategy.Uncompressed,
183182
}),
@@ -546,7 +545,7 @@ describe("SharedTree", () => {
546545
// Detached trees are not connected to the sequencing service, but they still "sequence" their edits as if they were totally ordered.
547546
// The second tree must take care to avoid sequencing its edits with sequence numbers that the first tree already used.
548547
// If it doesn't, the second tree will throw an error when trying to sequence a commit with sequence number that has "gone backwards" and this test will fail.
549-
const sharedTreeFactory = new SharedTreeFactory();
548+
const sharedTreeFactory = new TreeFactory({});
550549
const runtime = new MockFluidDataStoreRuntime({
551550
idCompressor: createIdCompressor(),
552551
attachState: AttachState.Detached,
@@ -1876,7 +1875,7 @@ describe("SharedTree", () => {
18761875
it("unspecified ForestType uses ObjectForest", () => {
18771876
const { trees } = new TestTreeProviderLite(
18781877
1,
1879-
new SharedTreeFactory({
1878+
new TreeFactory({
18801879
jsonValidator: typeboxValidator,
18811880
}),
18821881
);
@@ -1886,7 +1885,7 @@ describe("SharedTree", () => {
18861885
it("ForestType.Reference uses ObjectForest with additionalAsserts flag set to false", () => {
18871886
const { trees } = new TestTreeProviderLite(
18881887
1,
1889-
new SharedTreeFactory({
1888+
new TreeFactory({
18901889
jsonValidator: typeboxValidator,
18911890
forest: ForestTypeReference,
18921891
}),
@@ -1899,7 +1898,7 @@ describe("SharedTree", () => {
18991898
it("ForestType.Optimized uses ChunkedForest", () => {
19001899
const { trees } = new TestTreeProviderLite(
19011900
1,
1902-
new SharedTreeFactory({
1901+
new TreeFactory({
19031902
jsonValidator: typeboxValidator,
19041903
forest: ForestTypeOptimized,
19051904
}),
@@ -1910,7 +1909,7 @@ describe("SharedTree", () => {
19101909
it("ForestType.Expensive uses ObjectForest with additionalAsserts flag set to true", () => {
19111910
const { trees } = new TestTreeProviderLite(
19121911
1,
1913-
new SharedTreeFactory({
1912+
new TreeFactory({
19141913
jsonValidator: typeboxValidator,
19151914
forest: ForestTypeExpensiveDebug,
19161915
}),
@@ -1923,7 +1922,7 @@ describe("SharedTree", () => {
19231922
describe("Schema based op encoding", () => {
19241923
// TODO:#8915: This test will fail until bug #8915 is fixed
19251924
it.skip("uses the correct schema for subsequent edits after schema change.", async () => {
1926-
const factory = new SharedTreeFactory({
1925+
const factory = new TreeFactory({
19271926
jsonValidator: typeboxValidator,
19281927
treeEncodeType: TreeCompressionStrategy.Compressed,
19291928
});
@@ -1968,7 +1967,7 @@ describe("SharedTree", () => {
19681967

19691968
it("properly encodes ops using specified compression strategy", async () => {
19701969
// Check that ops are using uncompressed encoding with "Uncompressed" treeEncodeType
1971-
const factory = new SharedTreeFactory({
1970+
const factory = new TreeFactory({
19721971
jsonValidator: typeboxValidator,
19731972
treeEncodeType: TreeCompressionStrategy.Uncompressed,
19741973
});
@@ -2011,7 +2010,7 @@ describe("SharedTree", () => {
20112010
assert.deepEqual(encodedTreeData.data[0][1], expectedUncompressedTreeData);
20122011

20132012
// Check that ops are encoded using schema based compression with "Compressed" treeEncodeType
2014-
const factory2 = new SharedTreeFactory({
2013+
const factory2 = new TreeFactory({
20152014
jsonValidator: typeboxValidator,
20162015
treeEncodeType: TreeCompressionStrategy.Compressed,
20172016
});
@@ -2043,7 +2042,7 @@ describe("SharedTree", () => {
20432042

20442043
describe("Identifiers", () => {
20452044
it("Can use identifiers and the static Tree Apis", async () => {
2046-
const factory = new SharedTreeFactory({
2045+
const factory = new TreeFactory({
20472046
jsonValidator: typeboxValidator,
20482047
treeEncodeType: TreeCompressionStrategy.Compressed,
20492048
});
@@ -2107,7 +2106,7 @@ describe("SharedTree", () => {
21072106
});
21082107

21092108
it("throws an error if attaching during a transaction", () => {
2110-
const sharedTreeFactory = new SharedTreeFactory();
2109+
const sharedTreeFactory = new TreeFactory({});
21112110
const runtime = new MockFluidDataStoreRuntime({ idCompressor: createIdCompressor() });
21122111
const tree = sharedTreeFactory.create(runtime, "tree");
21132112
const runtimeFactory = new MockContainerRuntimeFactory();

packages/dds/tree/src/test/shared-tree/summary.bench.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
MockStorage,
2323
} from "@fluidframework/test-runtime-utils/internal";
2424

25-
import { SharedTreeFactory } from "../../shared-tree/index.js";
2625
import { TestTreeProviderLite, testIdCompressor } from "../utils.js";
2726
import { TreeViewConfiguration, type ImplicitFieldSchema } from "../../simple-tree/index.js";
2827
// eslint-disable-next-line import/no-internal-modules
@@ -33,6 +32,7 @@ import {
3332
makeJsWideTreeWithEndValue,
3433
WideRoot,
3534
} from "../scalableTestTrees.js";
35+
import { TreeFactory } from "../../treeFactory.js";
3636

3737
// TODO: these tests currently only cover tree content.
3838
// It might make sense to extend them to cover complex collaboration windows.
@@ -114,7 +114,7 @@ describe("Summary benchmarks", () => {
114114
type: BenchmarkType,
115115
) {
116116
let summaryTree: ITree;
117-
const factory = new SharedTreeFactory();
117+
const factory = new TreeFactory({});
118118
benchmark({
119119
title,
120120
type,

packages/dds/tree/src/test/shared-tree/undo.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
rootFieldKey,
1212
} from "../../core/index.js";
1313
import { singleJsonCursor } from "../json/index.js";
14-
import { SharedTreeFactory, type ITreeCheckout } from "../../shared-tree/index.js";
14+
import type { ITreeCheckout } from "../../shared-tree/index.js";
1515
import { type JsonCompatible, brand } from "../../util/index.js";
1616
import {
1717
createTestUndoRedoStacks,
@@ -34,6 +34,7 @@ import {
3434
} from "../../simple-tree/index.js";
3535
// eslint-disable-next-line import/no-internal-modules
3636
import { initialize } from "../../shared-tree/schematizeTree.js";
37+
import { TreeFactory } from "../../treeFactory.js";
3738

3839
const rootPath: UpPath = {
3940
parent: undefined,
@@ -485,7 +486,7 @@ describe("Undo and redo", () => {
485486
it("can undo while detached", () => {
486487
const sf = new SchemaFactory(undefined);
487488
class Schema extends sf.object("Object", { foo: sf.number }) {}
488-
const sharedTreeFactory = new SharedTreeFactory();
489+
const sharedTreeFactory = new TreeFactory({});
489490
const runtime = new MockFluidDataStoreRuntime({ idCompressor: createIdCompressor() });
490491
const tree = sharedTreeFactory.create(runtime, "tree");
491492
const view = tree.viewWith(new TreeViewConfiguration({ schema: Schema }));
@@ -683,7 +684,7 @@ describe("Undo and redo", () => {
683684
* @param attachTree - whether or not the SharedTree should be attached to the Fluid runtime
684685
*/
685686
export function createCheckout(json: JsonCompatible[], attachTree: boolean): ITreeCheckout {
686-
const sharedTreeFactory = new SharedTreeFactory();
687+
const sharedTreeFactory = new TreeFactory({});
687688
const runtime = new MockFluidDataStoreRuntime({ idCompressor: createIdCompressor() });
688689
const tree = sharedTreeFactory.create(runtime, "tree");
689690
const runtimeFactory = new MockContainerRuntimeFactory();

0 commit comments

Comments
 (0)