@fluid-experimental/attributor 2.0.0-internal.3.3.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/.eslintrc.js +20 -0
- package/.mocharc.js +12 -0
- package/LICENSE +21 -0
- package/README.md +106 -0
- package/api-extractor.json +4 -0
- package/dist/attributor.d.ts +56 -0
- package/dist/attributor.d.ts.map +1 -0
- package/dist/attributor.js +67 -0
- package/dist/attributor.js.map +1 -0
- package/dist/encoders.d.ts +28 -0
- package/dist/encoders.d.ts.map +1 -0
- package/dist/encoders.js +80 -0
- package/dist/encoders.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/lz4Encoder.d.ts +7 -0
- package/dist/lz4Encoder.d.ts.map +1 -0
- package/dist/lz4Encoder.js +29 -0
- package/dist/lz4Encoder.js.map +1 -0
- package/dist/mixinAttributor.d.ts +60 -0
- package/dist/mixinAttributor.d.ts.map +1 -0
- package/dist/mixinAttributor.js +153 -0
- package/dist/mixinAttributor.js.map +1 -0
- package/dist/stringInterner.d.ts +55 -0
- package/dist/stringInterner.d.ts.map +1 -0
- package/dist/stringInterner.js +66 -0
- package/dist/stringInterner.js.map +1 -0
- package/lib/attributor.d.ts +56 -0
- package/lib/attributor.d.ts.map +1 -0
- package/lib/attributor.js +62 -0
- package/lib/attributor.js.map +1 -0
- package/lib/encoders.d.ts +28 -0
- package/lib/encoders.d.ts.map +1 -0
- package/lib/encoders.js +75 -0
- package/lib/encoders.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +7 -0
- package/lib/index.js.map +1 -0
- package/lib/lz4Encoder.d.ts +7 -0
- package/lib/lz4Encoder.d.ts.map +1 -0
- package/lib/lz4Encoder.js +25 -0
- package/lib/lz4Encoder.js.map +1 -0
- package/lib/mixinAttributor.d.ts +60 -0
- package/lib/mixinAttributor.d.ts.map +1 -0
- package/lib/mixinAttributor.js +148 -0
- package/lib/mixinAttributor.js.map +1 -0
- package/lib/stringInterner.d.ts +55 -0
- package/lib/stringInterner.d.ts.map +1 -0
- package/lib/stringInterner.js +62 -0
- package/lib/stringInterner.js.map +1 -0
- package/package.json +106 -0
- package/prettier.config.cjs +8 -0
- package/src/attributor.ts +105 -0
- package/src/encoders.ts +112 -0
- package/src/index.ts +12 -0
- package/src/lz4Encoder.ts +27 -0
- package/src/mixinAttributor.ts +283 -0
- package/src/stringInterner.ts +86 -0
- package/tsconfig.esnext.json +7 -0
- package/tsconfig.json +10 -0
package/src/encoders.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { assert } from "@fluidframework/common-utils";
|
|
6
|
+
import { Jsonable } from "@fluidframework/datastore-definitions";
|
|
7
|
+
import { IUser } from "@fluidframework/protocol-definitions";
|
|
8
|
+
import { AttributionInfo } from "@fluidframework/runtime-definitions";
|
|
9
|
+
import { IAttributor } from "./attributor";
|
|
10
|
+
import { InternedStringId, MutableStringInterner } from "./stringInterner";
|
|
11
|
+
|
|
12
|
+
export interface Encoder<TDecoded, TEncoded> {
|
|
13
|
+
encode(decoded: TDecoded): TEncoded;
|
|
14
|
+
|
|
15
|
+
decode(encoded: TEncoded): TDecoded;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Note: the encoded format doesn't matter as long as it's serializable;
|
|
19
|
+
// these types could be weakened.
|
|
20
|
+
export type TimestampEncoder = Encoder<number[], number[]>;
|
|
21
|
+
|
|
22
|
+
export const deltaEncoder: TimestampEncoder = {
|
|
23
|
+
encode: (timestamps: number[]) => {
|
|
24
|
+
const deltaTimestamps: number[] = new Array(timestamps.length);
|
|
25
|
+
let prev = 0;
|
|
26
|
+
for (let i = 0; i < timestamps.length; i++) {
|
|
27
|
+
deltaTimestamps[i] = timestamps[i] - prev;
|
|
28
|
+
prev = timestamps[i];
|
|
29
|
+
}
|
|
30
|
+
return deltaTimestamps;
|
|
31
|
+
},
|
|
32
|
+
decode: (encoded: Jsonable) => {
|
|
33
|
+
assert(
|
|
34
|
+
Array.isArray(encoded),
|
|
35
|
+
0x4b0 /* Encoded timestamps should be an array of nummbers */,
|
|
36
|
+
);
|
|
37
|
+
const timestamps: number[] = new Array(encoded.length);
|
|
38
|
+
let cumulativeSum = 0;
|
|
39
|
+
for (let i = 0; i < encoded.length; i++) {
|
|
40
|
+
cumulativeSum += encoded[i];
|
|
41
|
+
timestamps[i] = cumulativeSum;
|
|
42
|
+
}
|
|
43
|
+
return timestamps;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type IAttributorSerializer = Encoder<IAttributor, SerializedAttributor>;
|
|
48
|
+
|
|
49
|
+
export interface SerializedAttributor {
|
|
50
|
+
interner: readonly string[] /* result of calling getSerializable() on a StringInterner */;
|
|
51
|
+
seqs: number[];
|
|
52
|
+
timestamps: number[];
|
|
53
|
+
attributionRefs: InternedStringId[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class AttributorSerializer implements IAttributorSerializer {
|
|
57
|
+
constructor(
|
|
58
|
+
private readonly makeAttributor: (
|
|
59
|
+
entries: Iterable<[number, AttributionInfo]>,
|
|
60
|
+
) => IAttributor,
|
|
61
|
+
private readonly timestampEncoder: TimestampEncoder,
|
|
62
|
+
) {}
|
|
63
|
+
|
|
64
|
+
public encode(attributor: IAttributor): SerializedAttributor {
|
|
65
|
+
const interner = new MutableStringInterner();
|
|
66
|
+
const seqs: number[] = [];
|
|
67
|
+
const timestamps: number[] = [];
|
|
68
|
+
const attributionRefs: InternedStringId[] = [];
|
|
69
|
+
for (const [seq, { user, timestamp }] of attributor.entries()) {
|
|
70
|
+
seqs.push(seq);
|
|
71
|
+
timestamps.push(timestamp);
|
|
72
|
+
const ref = interner.getOrCreateInternedId(JSON.stringify(user));
|
|
73
|
+
attributionRefs.push(ref);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const serialized: SerializedAttributor = {
|
|
77
|
+
interner: interner.getSerializable(),
|
|
78
|
+
seqs,
|
|
79
|
+
timestamps: this.timestampEncoder.encode(timestamps),
|
|
80
|
+
attributionRefs,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return serialized;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public decode(encoded: SerializedAttributor): IAttributor {
|
|
87
|
+
const interner = new MutableStringInterner(encoded.interner);
|
|
88
|
+
const { seqs, timestamps: encodedTimestamps, attributionRefs } = encoded;
|
|
89
|
+
const timestamps = this.timestampEncoder.decode(encodedTimestamps);
|
|
90
|
+
assert(
|
|
91
|
+
seqs.length === timestamps.length && timestamps.length === attributionRefs.length,
|
|
92
|
+
0x4b1 /* serialized attribution columns should have the same length */,
|
|
93
|
+
);
|
|
94
|
+
const entries = new Array(seqs.length);
|
|
95
|
+
for (let i = 0; i < seqs.length; i++) {
|
|
96
|
+
const key = seqs[i];
|
|
97
|
+
const timestamp = timestamps[i];
|
|
98
|
+
const ref = attributionRefs[i];
|
|
99
|
+
const user: IUser = JSON.parse(interner.getString(ref));
|
|
100
|
+
entries[i] = [key, { user, timestamp }];
|
|
101
|
+
}
|
|
102
|
+
return this.makeAttributor(entries);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @returns an encoder which composes `a` and `b`.
|
|
108
|
+
*/
|
|
109
|
+
export const chain = <T1, T2, T3>(a: Encoder<T1, T2>, b: Encoder<T2, T3>): Encoder<T1, T3> => ({
|
|
110
|
+
encode: (content) => b.encode(a.encode(content)),
|
|
111
|
+
decode: (content) => a.decode(b.decode(content)),
|
|
112
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
export { Attributor, OpStreamAttributor, IAttributor } from "./attributor";
|
|
6
|
+
export {
|
|
7
|
+
createRuntimeAttributor,
|
|
8
|
+
enableOnNewFileKey,
|
|
9
|
+
IProvideRuntimeAttributor,
|
|
10
|
+
IRuntimeAttributor,
|
|
11
|
+
mixinAttributor,
|
|
12
|
+
} from "./mixinAttributor";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { compress, decompress } from "lz4js";
|
|
6
|
+
import { bufferToString, stringToBuffer } from "@fluidframework/common-utils";
|
|
7
|
+
import { Jsonable } from "@fluidframework/datastore-definitions";
|
|
8
|
+
import { Encoder } from "./encoders";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @alpha
|
|
12
|
+
*/
|
|
13
|
+
export function makeLZ4Encoder<T>(): Encoder<Jsonable<T>, string> {
|
|
14
|
+
return {
|
|
15
|
+
encode: (decoded: Jsonable<T>) => {
|
|
16
|
+
const uncompressed = new TextEncoder().encode(JSON.stringify(decoded));
|
|
17
|
+
const compressed = compress(uncompressed);
|
|
18
|
+
return bufferToString(compressed, "base64");
|
|
19
|
+
},
|
|
20
|
+
decode: (serializedSummary: string): Jsonable<T> => {
|
|
21
|
+
const compressed = new Uint8Array(stringToBuffer(serializedSummary, "base64"));
|
|
22
|
+
const uncompressed = decompress(compressed);
|
|
23
|
+
const decoded: Jsonable<T> = JSON.parse(new TextDecoder().decode(uncompressed));
|
|
24
|
+
return decoded;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
IDocumentMessage,
|
|
7
|
+
ISequencedDocumentMessage,
|
|
8
|
+
ISnapshotTree,
|
|
9
|
+
} from "@fluidframework/protocol-definitions";
|
|
10
|
+
import { IAudience, IContainerContext, IDeltaManager } from "@fluidframework/container-definitions";
|
|
11
|
+
import { ContainerRuntime } from "@fluidframework/container-runtime";
|
|
12
|
+
import type { IContainerRuntimeOptions } from "@fluidframework/container-runtime";
|
|
13
|
+
import {
|
|
14
|
+
AttributionInfo,
|
|
15
|
+
AttributionKey,
|
|
16
|
+
ISummaryTreeWithStats,
|
|
17
|
+
ITelemetryContext,
|
|
18
|
+
NamedFluidDataStoreRegistryEntries,
|
|
19
|
+
} from "@fluidframework/runtime-definitions";
|
|
20
|
+
import { addSummarizeResultToSummary, SummaryTreeBuilder } from "@fluidframework/runtime-utils";
|
|
21
|
+
import { IContainerRuntime } from "@fluidframework/container-runtime-definitions";
|
|
22
|
+
import { IRequest, IResponse, FluidObject } from "@fluidframework/core-interfaces";
|
|
23
|
+
import { assert, bufferToString, unreachableCase } from "@fluidframework/common-utils";
|
|
24
|
+
import { UsageError } from "@fluidframework/container-utils";
|
|
25
|
+
import { loggerToMonitoringContext } from "@fluidframework/telemetry-utils";
|
|
26
|
+
import { Attributor, IAttributor, OpStreamAttributor } from "./attributor";
|
|
27
|
+
import { AttributorSerializer, chain, deltaEncoder, Encoder } from "./encoders";
|
|
28
|
+
import { makeLZ4Encoder } from "./lz4Encoder";
|
|
29
|
+
|
|
30
|
+
// Summary tree keys
|
|
31
|
+
const attributorTreeName = ".attributor";
|
|
32
|
+
const opBlobName = "op";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @alpha
|
|
36
|
+
* Feature Gate Key -
|
|
37
|
+
* Whether or not a container runtime instantiated using `mixinAttributor`'s load should generate an attributor on
|
|
38
|
+
* new files. See package README for more notes on integration.
|
|
39
|
+
*/
|
|
40
|
+
export const enableOnNewFileKey = "Fluid.Attribution.EnableOnNewFile";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @alpha
|
|
44
|
+
*/
|
|
45
|
+
export const IRuntimeAttributor: keyof IProvideRuntimeAttributor = "IRuntimeAttributor";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @alpha
|
|
49
|
+
*/
|
|
50
|
+
export interface IProvideRuntimeAttributor {
|
|
51
|
+
readonly IRuntimeAttributor: IRuntimeAttributor;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Provides access to attribution information stored on the container runtime.
|
|
56
|
+
*
|
|
57
|
+
* Attributors are only populated after the container runtime they are injected into has initialized.
|
|
58
|
+
* @sealed
|
|
59
|
+
* @alpha
|
|
60
|
+
*/
|
|
61
|
+
export interface IRuntimeAttributor extends IProvideRuntimeAttributor {
|
|
62
|
+
/**
|
|
63
|
+
* @throws - If no AttributionInfo exists for this key.
|
|
64
|
+
*/
|
|
65
|
+
get(key: AttributionKey): AttributionInfo;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @returns - Whether any AttributionInfo exists for the provided key.
|
|
69
|
+
*/
|
|
70
|
+
has(key: AttributionKey): boolean;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @returns - Whether the runtime is currently tracking attribution information for the loaded container.
|
|
74
|
+
* See {@link mixinAttributor} for more details on when this happens.
|
|
75
|
+
*/
|
|
76
|
+
readonly isEnabled: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @returns an IRuntimeAttributor for usage with `mixinAttributor`. The attributor will only be populated with data
|
|
81
|
+
* once it's passed via scope to a container runtime load flow. See {@link mixinAttributor}.
|
|
82
|
+
* @alpha
|
|
83
|
+
*/
|
|
84
|
+
export function createRuntimeAttributor(): IRuntimeAttributor {
|
|
85
|
+
return new RuntimeAttributor();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Mixes in logic to load and store runtime-based attribution functionality.
|
|
90
|
+
*
|
|
91
|
+
* The `scope` passed to `load` should implement `IProvideRuntimeAttributor`.
|
|
92
|
+
*
|
|
93
|
+
* Existing documents without stored attributors will not start storing attribution information: if an
|
|
94
|
+
* IRuntimeAttributor is passed via scope to load a document that never previously had attribution information,
|
|
95
|
+
* that attributor's `has` method will always return `false`.
|
|
96
|
+
* @param Base - base class, inherits from FluidAttributorRuntime
|
|
97
|
+
* @alpha
|
|
98
|
+
*/
|
|
99
|
+
export const mixinAttributor = (Base: typeof ContainerRuntime = ContainerRuntime) =>
|
|
100
|
+
class ContainerRuntimeWithAttributor extends Base {
|
|
101
|
+
public static async load(
|
|
102
|
+
context: IContainerContext,
|
|
103
|
+
registryEntries: NamedFluidDataStoreRegistryEntries,
|
|
104
|
+
requestHandler?:
|
|
105
|
+
| ((request: IRequest, runtime: IContainerRuntime) => Promise<IResponse>)
|
|
106
|
+
| undefined,
|
|
107
|
+
runtimeOptions: IContainerRuntimeOptions | undefined = {},
|
|
108
|
+
containerScope: FluidObject | undefined = context.scope,
|
|
109
|
+
existing?: boolean | undefined,
|
|
110
|
+
ctor: typeof ContainerRuntime = ContainerRuntimeWithAttributor as unknown as typeof ContainerRuntime,
|
|
111
|
+
): Promise<ContainerRuntime> {
|
|
112
|
+
const runtimeAttributor = (
|
|
113
|
+
containerScope as FluidObject<IProvideRuntimeAttributor> | undefined
|
|
114
|
+
)?.IRuntimeAttributor;
|
|
115
|
+
if (!runtimeAttributor) {
|
|
116
|
+
throw new UsageError(
|
|
117
|
+
"ContainerRuntimeWithAttributor must be passed a scope implementing IProvideRuntimeAttributor",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const pendingRuntimeState = context.pendingLocalState as {
|
|
122
|
+
baseSnapshot?: ISnapshotTree;
|
|
123
|
+
};
|
|
124
|
+
const baseSnapshot: ISnapshotTree | undefined =
|
|
125
|
+
pendingRuntimeState?.baseSnapshot ?? context.baseSnapshot;
|
|
126
|
+
|
|
127
|
+
const { audience, deltaManager } = context;
|
|
128
|
+
assert(
|
|
129
|
+
audience !== undefined,
|
|
130
|
+
0x508 /* Audience must exist when instantiating attribution-providing runtime */,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const mc = loggerToMonitoringContext(context.taggedLogger);
|
|
134
|
+
const shouldTrackAttribution = mc.config.getBoolean(enableOnNewFileKey) ?? false;
|
|
135
|
+
if (shouldTrackAttribution) {
|
|
136
|
+
(context.options.attribution ??= {}).track = true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const runtime = (await Base.load(
|
|
140
|
+
context,
|
|
141
|
+
registryEntries,
|
|
142
|
+
requestHandler,
|
|
143
|
+
runtimeOptions,
|
|
144
|
+
containerScope,
|
|
145
|
+
existing,
|
|
146
|
+
ctor,
|
|
147
|
+
)) as ContainerRuntimeWithAttributor;
|
|
148
|
+
runtime.runtimeAttributor = runtimeAttributor as RuntimeAttributor;
|
|
149
|
+
|
|
150
|
+
// Note: this fetches attribution blobs relatively eagerly in the load flow; we may want to optimize
|
|
151
|
+
// this to avoid blocking on such information until application actually requests some op-based attribution
|
|
152
|
+
// info or we need to summarize. All that really needs to happen immediately is to start recording
|
|
153
|
+
// op seq# -> attributionInfo for new ops.
|
|
154
|
+
await runtime.runtimeAttributor.initialize(
|
|
155
|
+
deltaManager,
|
|
156
|
+
audience,
|
|
157
|
+
baseSnapshot,
|
|
158
|
+
async (id) => runtime.storage.readBlob(id),
|
|
159
|
+
shouldTrackAttribution,
|
|
160
|
+
);
|
|
161
|
+
return runtime;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private runtimeAttributor: RuntimeAttributor | undefined;
|
|
165
|
+
|
|
166
|
+
protected addContainerStateToSummary(
|
|
167
|
+
summaryTree: ISummaryTreeWithStats,
|
|
168
|
+
fullTree: boolean,
|
|
169
|
+
trackState: boolean,
|
|
170
|
+
telemetryContext?: ITelemetryContext,
|
|
171
|
+
) {
|
|
172
|
+
super.addContainerStateToSummary(summaryTree, fullTree, trackState, telemetryContext);
|
|
173
|
+
const attributorSummary = this.runtimeAttributor?.summarize();
|
|
174
|
+
if (attributorSummary) {
|
|
175
|
+
addSummarizeResultToSummary(summaryTree, attributorTreeName, attributorSummary);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} as unknown as typeof ContainerRuntime;
|
|
179
|
+
|
|
180
|
+
class RuntimeAttributor implements IRuntimeAttributor {
|
|
181
|
+
public get IRuntimeAttributor(): IRuntimeAttributor {
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public get(key: AttributionKey): AttributionInfo {
|
|
186
|
+
assert(
|
|
187
|
+
this.opAttributor !== undefined,
|
|
188
|
+
0x509 /* RuntimeAttributor must be initialized before getAttributionInfo can be called */,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
if (key.type === "detached") {
|
|
192
|
+
throw new Error("Attribution of detached keys is not yet supported.");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (key.type === "local") {
|
|
196
|
+
// Note: we can *almost* orchestrate this correctly with internal-only changes by looking up the current
|
|
197
|
+
// client id in the audience. However, for read->write client transition, the container might have not yet
|
|
198
|
+
// received a client id. This is left as a TODO as it might be more easily solved once the detached case
|
|
199
|
+
// is settled (e.g. if it's reasonable for the host to know the current user information at container
|
|
200
|
+
// creation time, we could just use that here as well).
|
|
201
|
+
throw new Error("Attribution of local keys is not yet supported.");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return this.opAttributor.getAttributionInfo(key.seq);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
public has(key: AttributionKey): boolean {
|
|
208
|
+
if (key.type === "detached") {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (key.type === "local") {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return this.opAttributor?.tryGetAttributionInfo(key.seq) !== undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private encoder: Encoder<IAttributor, string> = {
|
|
220
|
+
encode: unreachableCase,
|
|
221
|
+
decode: unreachableCase,
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
private opAttributor: IAttributor | undefined;
|
|
225
|
+
public isEnabled = false;
|
|
226
|
+
|
|
227
|
+
public async initialize(
|
|
228
|
+
deltaManager: IDeltaManager<ISequencedDocumentMessage, IDocumentMessage>,
|
|
229
|
+
audience: IAudience,
|
|
230
|
+
baseSnapshot: ISnapshotTree | undefined,
|
|
231
|
+
readBlob: (id: string) => Promise<ArrayBufferLike>,
|
|
232
|
+
shouldAddAttributorOnNewFile: boolean,
|
|
233
|
+
): Promise<void> {
|
|
234
|
+
const attributorTree = baseSnapshot?.trees[attributorTreeName];
|
|
235
|
+
// Existing documents that don't already have a snapshot containing runtime attribution info shouldn't
|
|
236
|
+
// inject any for now--this causes some back-compat integration problems that aren't fully worked out.
|
|
237
|
+
const shouldExcludeAttributor =
|
|
238
|
+
(baseSnapshot !== undefined && attributorTree === undefined) ||
|
|
239
|
+
(baseSnapshot === undefined && !shouldAddAttributorOnNewFile);
|
|
240
|
+
if (shouldExcludeAttributor) {
|
|
241
|
+
// This gives a consistent error for calls to `get` on keys that don't exist.
|
|
242
|
+
this.opAttributor = new Attributor();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this.isEnabled = true;
|
|
247
|
+
this.encoder = chain(
|
|
248
|
+
new AttributorSerializer(
|
|
249
|
+
(entries) => new OpStreamAttributor(deltaManager, audience, entries),
|
|
250
|
+
deltaEncoder,
|
|
251
|
+
),
|
|
252
|
+
makeLZ4Encoder(),
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
if (attributorTree !== undefined) {
|
|
256
|
+
const id = attributorTree.blobs[opBlobName];
|
|
257
|
+
assert(
|
|
258
|
+
id !== undefined,
|
|
259
|
+
0x50a /* Attributor tree should have op attributor summary blob. */,
|
|
260
|
+
);
|
|
261
|
+
const blobContents = await readBlob(id);
|
|
262
|
+
const attributorSnapshot = bufferToString(blobContents, "utf8");
|
|
263
|
+
this.opAttributor = this.encoder.decode(attributorSnapshot);
|
|
264
|
+
} else {
|
|
265
|
+
this.opAttributor = new OpStreamAttributor(deltaManager, audience);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
public summarize(): ISummaryTreeWithStats | undefined {
|
|
270
|
+
if (!this.isEnabled) {
|
|
271
|
+
// Loaded existing document without attributor data: avoid injecting any data.
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
assert(
|
|
276
|
+
this.opAttributor !== undefined,
|
|
277
|
+
0x50b /* RuntimeAttributor should be initialized before summarization */,
|
|
278
|
+
);
|
|
279
|
+
const builder = new SummaryTreeBuilder();
|
|
280
|
+
builder.addBlob(opBlobName, this.encoder.encode(this.opAttributor));
|
|
281
|
+
return builder.getSummaryTree();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { UsageError } from "@fluidframework/container-utils";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The ID of the string that has been interned, which can be used by a {@link StringInterner} to retrieve the
|
|
10
|
+
* original string.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export type InternedStringId = number & {
|
|
14
|
+
readonly InternedStringId: "e221abc9-9d17-4493-8db0-70c871a1c27c";
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Interns strings as integers.
|
|
19
|
+
*/
|
|
20
|
+
export interface StringInterner {
|
|
21
|
+
getInternedId(input: string): InternedStringId | undefined;
|
|
22
|
+
getString(internedId: number): string;
|
|
23
|
+
getSerializable(): readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Interns strings as integers.
|
|
28
|
+
* Given a string, this class will produce a unique integer associated with that string that can then be used to
|
|
29
|
+
* retrieve the string.
|
|
30
|
+
*/
|
|
31
|
+
export class MutableStringInterner implements StringInterner {
|
|
32
|
+
private readonly stringToInternedIdMap = new Map<string, InternedStringId>();
|
|
33
|
+
private readonly internedStrings: string[] = [];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param inputStrings - A list of strings to intern in the order given. Can be used to rehydrate from a previous
|
|
37
|
+
* `StringInterner`'s {@link StringInterner.getSerializable} return value.
|
|
38
|
+
*/
|
|
39
|
+
constructor(inputStrings: readonly string[] = []) {
|
|
40
|
+
for (const value of inputStrings) {
|
|
41
|
+
this.getOrCreateInternedId(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param input - The string to get the associated intern ID for
|
|
47
|
+
* @returns an intern ID that is uniquely associated with the input string
|
|
48
|
+
*/
|
|
49
|
+
public getOrCreateInternedId(input: string): InternedStringId {
|
|
50
|
+
return this.getInternedId(input) ?? this.createNewId(input);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public getInternedId(input: string): InternedStringId | undefined {
|
|
54
|
+
return this.stringToInternedIdMap.get(input);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
*
|
|
59
|
+
* @param internId - The intern ID to get the associated string for. Can only retrieve strings that have been
|
|
60
|
+
* used as inputs to calls of `getInternId`.
|
|
61
|
+
* @returns a string that is uniquely associated with the given intern ID
|
|
62
|
+
*/
|
|
63
|
+
public getString(internId: number): string {
|
|
64
|
+
const result = this.internedStrings[internId];
|
|
65
|
+
if (!result) {
|
|
66
|
+
throw new UsageError(`No string associated with ${internId}.`);
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @returns The list of strings interned where the indices map to the associated {@link InternedStringId} of
|
|
73
|
+
* each string.
|
|
74
|
+
*/
|
|
75
|
+
public getSerializable(): readonly string[] {
|
|
76
|
+
return this.internedStrings;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Create a new interned id. Assumes without validation that the input doesn't already have an interned id. */
|
|
80
|
+
private createNewId(input: string): InternedStringId {
|
|
81
|
+
const internedId = this.stringToInternedIdMap.size as InternedStringId;
|
|
82
|
+
this.stringToInternedIdMap.set(input, internedId);
|
|
83
|
+
this.internedStrings.push(input);
|
|
84
|
+
return internedId;
|
|
85
|
+
}
|
|
86
|
+
}
|