Skip to content

Commit

Permalink
Dedup factories
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigMacomber committed Feb 3, 2025
1 parent 8f6dd45 commit b49ab63
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/dds/tree/src/shared-tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

export {
type ISharedTree,
SharedTreeFactory,
type SharedTreeOptionsInternal,
type SharedTreeOptions,
SharedTree,
getBranch,
Expand Down
3 changes: 2 additions & 1 deletion packages/dds/tree/src/shared-tree/publicContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

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

/**
* {@inheritDoc @fluidframework/shared-object-base#ISharedObjectFactory."type"}
Expand All @@ -20,5 +21,5 @@ export const SharedTreeFactoryType = "https://graph.microsoft.com/types/tree";
export const SharedTreeAttributes: IChannelAttributes = {
type: SharedTreeFactoryType,
snapshotFormatVersion: "0.0.0",
packageVersion: "0.0.0",
packageVersion: pkgVersion,
};
31 changes: 0 additions & 31 deletions packages/dds/tree/src/shared-tree/sharedTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { assert } from "@fluidframework/core-utils/internal";
import type { ErasedType, IFluidHandle } from "@fluidframework/core-interfaces/internal";
import type {
IChannelAttributes,
IChannelFactory,
IFluidDataStoreRuntime,
IChannelServices,
IChannelStorageService,
} from "@fluidframework/datastore-definitions/internal";
import type { ISharedObject } from "@fluidframework/shared-object-base/internal";
Expand Down Expand Up @@ -84,7 +82,6 @@ import type { SharedTreeEditBuilder } from "./sharedTreeEditBuilder.js";
import { type TreeCheckout, type BranchableTree, createTreeCheckout } from "./treeCheckout.js";
import { breakingClass, fail, throwIfBroken } from "../util/index.js";
import type { IIdCompressor } from "@fluidframework/id-compressor";
import { SharedTreeAttributes, SharedTreeFactoryType } from "./publicContracts.js";

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

/**
* A channel factory that creates {@link ISharedTree}s.
*/
export class SharedTreeFactory implements IChannelFactory<ISharedTree> {
public readonly type: string = SharedTreeFactoryType;

public readonly attributes: IChannelAttributes = SharedTreeAttributes;

public constructor(private readonly options: SharedTreeOptionsInternal = {}) {}

public async load(
runtime: IFluidDataStoreRuntime,
id: string,
services: IChannelServices,
channelAttributes: Readonly<IChannelAttributes>,
): Promise<SharedTree> {
const tree = new SharedTree(id, runtime, channelAttributes, this.options);
await tree.load(services);
return tree;
}

public create(runtime: IFluidDataStoreRuntime, id: string): SharedTree {
const tree = new SharedTree(id, runtime, this.attributes, this.options);
tree.initializeLocal();
return tree;
}
}

function verboseFromCursor(
reader: ITreeCursor,
schema: ReadonlyMap<TreeNodeSchemaIdentifier, TreeNodeStoredSchema>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import {
import {
type TreeStoredContent,
type ISharedTreeEditor,
SharedTreeFactory,
Tree,
ForestTypeOptimized,
} from "../../../shared-tree/index.js";
Expand Down Expand Up @@ -83,6 +82,7 @@ import { brand } from "../../../util/index.js";
// eslint-disable-next-line import/no-internal-modules
import { ChunkedForest } from "../../../feature-libraries/chunked-forest/chunkedForest.js";
import { MockFluidDataStoreRuntime } from "@fluidframework/test-runtime-utils/internal";
import { TreeFactory } from "../../../treeFactory.js";

const options = {
jsonValidator: typeboxValidator,
Expand Down Expand Up @@ -395,7 +395,7 @@ describe("End to end chunked encoding", () => {
});

it("Initializing tree creates uniform chunks with encoded identifiers", async () => {
const factory = new SharedTreeFactory({
const factory = new TreeFactory({
jsonValidator: typeboxValidator,
forest: ForestTypeOptimized,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ import type {
TreeNodeSchemaIdentifier,
} from "../../../core/index.js";
import { type DownPath, toDownPath } from "../../../feature-libraries/index.js";
import {
Tree,
type ISharedTree,
type SharedTree,
type SharedTreeFactory,
} from "../../../shared-tree/index.js";
import { Tree, type ISharedTree, type SharedTree } from "../../../shared-tree/index.js";
import { fail, getOrCreate, makeArray } from "../../../util/index.js";

import {
Expand Down Expand Up @@ -69,6 +64,7 @@ import {
type TreeNode,
type TreeNodeSchema,
} from "../../../simple-tree/index.js";
import type { TreeFactory } from "../../../treeFactory.js";

export type FuzzView = SchematizingSimpleTreeView<typeof fuzzFieldSchema> & {
/**
Expand Down Expand Up @@ -98,7 +94,7 @@ export type FuzzTransactionView = SchematizingSimpleTreeView<typeof fuzzFieldSch
currentSchema: FuzzNodeSchema;
};

export interface FuzzTestState extends DDSFuzzTestState<SharedTreeFactory> {
export interface FuzzTestState extends DDSFuzzTestState<TreeFactory> {
/**
* Schematized view of clients and their nodeSchemas. Created lazily by viewFromState.
*
Expand All @@ -118,7 +114,7 @@ export interface FuzzTestState extends DDSFuzzTestState<SharedTreeFactory> {

export function viewFromState(
state: FuzzTestState,
client: Client<SharedTreeFactory> = state.client,
client: Client<TreeFactory> = state.client,
): FuzzView {
state.clientViews ??= new Map();
const view =
Expand Down Expand Up @@ -594,7 +590,7 @@ export const makeConstraintEditGenerator = (

export function makeOpGenerator(
weightsArg: Partial<EditGeneratorOpWeights> = defaultEditGeneratorOpWeights,
): AsyncGenerator<Operation, DDSFuzzTestState<SharedTreeFactory>> {
): AsyncGenerator<Operation, DDSFuzzTestState<TreeFactory>> {
const weights = {
...defaultEditGeneratorOpWeights,
...weightsArg,
Expand Down
17 changes: 9 additions & 8 deletions packages/dds/tree/src/test/shared-tree/fuzz/fuzzEditReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { IFluidHandle } from "@fluidframework/core-interfaces";

import type { Revertible } from "../../../core/index.js";
import type { DownPath } from "../../../feature-libraries/index.js";
import { Tree, type SharedTreeFactory } from "../../../shared-tree/index.js";
import { Tree } from "../../../shared-tree/index.js";
import { fail } from "../../../util/index.js";
import { validateFuzzTreeConsistency, viewCheckout } from "../../utils.js";

Expand Down Expand Up @@ -62,8 +62,9 @@ import {
type TreeNode,
type TreeNodeSchema,
} from "../../../simple-tree/index.js";
import type { TreeFactory } from "../../../treeFactory.js";

const syncFuzzReducer = combineReducers<Operation, DDSFuzzTestState<SharedTreeFactory>>({
const syncFuzzReducer = combineReducers<Operation, DDSFuzzTestState<TreeFactory>>({
treeEdit: (state, { edit }) => {
switch (edit.type) {
case "fieldEdit": {
Expand Down Expand Up @@ -92,18 +93,18 @@ const syncFuzzReducer = combineReducers<Operation, DDSFuzzTestState<SharedTreeFa
applyConstraint(state, operation);
},
});
export const fuzzReducer: AsyncReducer<
Operation,
DDSFuzzTestState<SharedTreeFactory>
> = async (state, operation) => syncFuzzReducer(state, operation);
export const fuzzReducer: AsyncReducer<Operation, DDSFuzzTestState<TreeFactory>> = async (
state,
operation,
) => syncFuzzReducer(state, operation);

export function checkTreesAreSynchronized(trees: readonly Client<SharedTreeFactory>[]) {
export function checkTreesAreSynchronized(trees: readonly Client<TreeFactory>[]) {
for (const tree of trees) {
validateFuzzTreeConsistency(trees[0], tree);
}
}

export function applySynchronizationOp(state: DDSFuzzTestState<SharedTreeFactory>) {
export function applySynchronizationOp(state: DDSFuzzTestState<TreeFactory>) {
state.containerRuntimeFactory.processAllMessages();
const connectedClients = state.clients.filter((client) => client.containerRuntime.connected);
if (connectedClients.length > 0) {
Expand Down
5 changes: 3 additions & 2 deletions packages/dds/tree/src/test/shared-tree/sharedTree.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TreeCompressionStrategy,
jsonableTreeFromCursor,
} from "../../feature-libraries/index.js";
import { SharedTreeFactory, type CheckoutFlexTreeView } from "../../shared-tree/index.js";
import type { CheckoutFlexTreeView } from "../../shared-tree/index.js";
import {
type JSDeepTree,
type JSWideTree,
Expand Down Expand Up @@ -49,6 +49,7 @@ import {
} from "../utils.js";
import { insert } from "../sequenceRootUtils.js";
import { cursorFromInsertable } from "../../simple-tree/index.js";
import { TreeFactory } from "../../treeFactory.js";

// number of nodes in test for wide trees
const nodesCountWide = [
Expand All @@ -64,7 +65,7 @@ const nodesCountDeep = [
];

// TODO: ADO#7111 Schema should be fixed to enable schema based encoding.
const factory = new SharedTreeFactory({
const factory = new TreeFactory({
jsonValidator: typeboxValidator,
treeEncodeType: TreeCompressionStrategy.Uncompressed,
});
Expand Down
25 changes: 12 additions & 13 deletions packages/dds/tree/src/test/shared-tree/sharedTree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import {
getBranch,
type ISharedTree,
type SharedTree,
SharedTreeFactory,
Tree,
type TreeCheckout,
} from "../../shared-tree/index.js";
Expand Down Expand Up @@ -90,7 +89,7 @@ import {
NumberArray,
validateViewConsistency,
} from "../utils.js";
import { configuredSharedTree } from "../../treeFactory.js";
import { configuredSharedTree, TreeFactory } from "../../treeFactory.js";
import type { ISharedObjectKind } from "@fluidframework/shared-object-base/internal";
import { TestAnchor } from "../testAnchor.js";
// eslint-disable-next-line import/no-internal-modules
Expand Down Expand Up @@ -177,7 +176,7 @@ describe("SharedTree", () => {
const provider = await TestTreeProvider.create(
2,
SummarizeType.disabled,
new SharedTreeFactory({
new TreeFactory({
jsonValidator: typeboxValidator,
treeEncodeType: TreeCompressionStrategy.Uncompressed,
}),
Expand Down Expand Up @@ -546,7 +545,7 @@ describe("SharedTree", () => {
// Detached trees are not connected to the sequencing service, but they still "sequence" their edits as if they were totally ordered.
// The second tree must take care to avoid sequencing its edits with sequence numbers that the first tree already used.
// 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.
const sharedTreeFactory = new SharedTreeFactory();
const sharedTreeFactory = new TreeFactory({});
const runtime = new MockFluidDataStoreRuntime({
idCompressor: createIdCompressor(),
attachState: AttachState.Detached,
Expand Down Expand Up @@ -1876,7 +1875,7 @@ describe("SharedTree", () => {
it("unspecified ForestType uses ObjectForest", () => {
const { trees } = new TestTreeProviderLite(
1,
new SharedTreeFactory({
new TreeFactory({
jsonValidator: typeboxValidator,
}),
);
Expand All @@ -1886,7 +1885,7 @@ describe("SharedTree", () => {
it("ForestType.Reference uses ObjectForest with additionalAsserts flag set to false", () => {
const { trees } = new TestTreeProviderLite(
1,
new SharedTreeFactory({
new TreeFactory({
jsonValidator: typeboxValidator,
forest: ForestTypeReference,
}),
Expand All @@ -1899,7 +1898,7 @@ describe("SharedTree", () => {
it("ForestType.Optimized uses ChunkedForest", () => {
const { trees } = new TestTreeProviderLite(
1,
new SharedTreeFactory({
new TreeFactory({
jsonValidator: typeboxValidator,
forest: ForestTypeOptimized,
}),
Expand All @@ -1910,7 +1909,7 @@ describe("SharedTree", () => {
it("ForestType.Expensive uses ObjectForest with additionalAsserts flag set to true", () => {
const { trees } = new TestTreeProviderLite(
1,
new SharedTreeFactory({
new TreeFactory({
jsonValidator: typeboxValidator,
forest: ForestTypeExpensiveDebug,
}),
Expand All @@ -1923,7 +1922,7 @@ describe("SharedTree", () => {
describe("Schema based op encoding", () => {
// TODO:#8915: This test will fail until bug #8915 is fixed
it.skip("uses the correct schema for subsequent edits after schema change.", async () => {
const factory = new SharedTreeFactory({
const factory = new TreeFactory({
jsonValidator: typeboxValidator,
treeEncodeType: TreeCompressionStrategy.Compressed,
});
Expand Down Expand Up @@ -1968,7 +1967,7 @@ describe("SharedTree", () => {

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

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

describe("Identifiers", () => {
it("Can use identifiers and the static Tree Apis", async () => {
const factory = new SharedTreeFactory({
const factory = new TreeFactory({
jsonValidator: typeboxValidator,
treeEncodeType: TreeCompressionStrategy.Compressed,
});
Expand Down Expand Up @@ -2107,7 +2106,7 @@ describe("SharedTree", () => {
});

it("throws an error if attaching during a transaction", () => {
const sharedTreeFactory = new SharedTreeFactory();
const sharedTreeFactory = new TreeFactory({});
const runtime = new MockFluidDataStoreRuntime({ idCompressor: createIdCompressor() });
const tree = sharedTreeFactory.create(runtime, "tree");
const runtimeFactory = new MockContainerRuntimeFactory();
Expand Down
4 changes: 2 additions & 2 deletions packages/dds/tree/src/test/shared-tree/summary.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
MockStorage,
} from "@fluidframework/test-runtime-utils/internal";

import { SharedTreeFactory } from "../../shared-tree/index.js";
import { TestTreeProviderLite, testIdCompressor } from "../utils.js";
import { TreeViewConfiguration, type ImplicitFieldSchema } from "../../simple-tree/index.js";
// eslint-disable-next-line import/no-internal-modules
Expand All @@ -33,6 +32,7 @@ import {
makeJsWideTreeWithEndValue,
WideRoot,
} from "../scalableTestTrees.js";
import { TreeFactory } from "../../treeFactory.js";

// TODO: these tests currently only cover tree content.
// It might make sense to extend them to cover complex collaboration windows.
Expand Down Expand Up @@ -114,7 +114,7 @@ describe("Summary benchmarks", () => {
type: BenchmarkType,
) {
let summaryTree: ITree;
const factory = new SharedTreeFactory();
const factory = new TreeFactory({});
benchmark({
title,
type,
Expand Down
7 changes: 4 additions & 3 deletions packages/dds/tree/src/test/shared-tree/undo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
rootFieldKey,
} from "../../core/index.js";
import { singleJsonCursor } from "../json/index.js";
import { SharedTreeFactory, type ITreeCheckout } from "../../shared-tree/index.js";
import type { ITreeCheckout } from "../../shared-tree/index.js";
import { type JsonCompatible, brand } from "../../util/index.js";
import {
createTestUndoRedoStacks,
Expand All @@ -34,6 +34,7 @@ import {
} from "../../simple-tree/index.js";
// eslint-disable-next-line import/no-internal-modules
import { initialize } from "../../shared-tree/schematizeTree.js";
import { TreeFactory } from "../../treeFactory.js";

const rootPath: UpPath = {
parent: undefined,
Expand Down Expand Up @@ -485,7 +486,7 @@ describe("Undo and redo", () => {
it("can undo while detached", () => {
const sf = new SchemaFactory(undefined);
class Schema extends sf.object("Object", { foo: sf.number }) {}
const sharedTreeFactory = new SharedTreeFactory();
const sharedTreeFactory = new TreeFactory({});
const runtime = new MockFluidDataStoreRuntime({ idCompressor: createIdCompressor() });
const tree = sharedTreeFactory.create(runtime, "tree");
const view = tree.viewWith(new TreeViewConfiguration({ schema: Schema }));
Expand Down Expand Up @@ -683,7 +684,7 @@ describe("Undo and redo", () => {
* @param attachTree - whether or not the SharedTree should be attached to the Fluid runtime
*/
export function createCheckout(json: JsonCompatible[], attachTree: boolean): ITreeCheckout {
const sharedTreeFactory = new SharedTreeFactory();
const sharedTreeFactory = new TreeFactory({});
const runtime = new MockFluidDataStoreRuntime({ idCompressor: createIdCompressor() });
const tree = sharedTreeFactory.create(runtime, "tree");
const runtimeFactory = new MockContainerRuntimeFactory();
Expand Down
Loading

0 comments on commit b49ab63

Please sign in to comment.