-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathmap.ts
More file actions
322 lines (288 loc) · 9.55 KB
/
map.ts
File metadata and controls
322 lines (288 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { assert } from "@fluidframework/core-utils/internal";
import type {
IChannelAttributes,
IFluidDataStoreRuntime,
IChannelStorageService,
} from "@fluidframework/datastore-definitions/internal";
import { MessageType } from "@fluidframework/driver-definitions/internal";
import { readAndParse } from "@fluidframework/driver-utils/internal";
import type {
ISummaryTreeWithStats,
ITelemetryContext,
IRuntimeMessageCollection,
IRuntimeMessagesContent,
ISequencedMessageEnvelope,
} from "@fluidframework/runtime-definitions/internal";
import { SummaryTreeBuilder } from "@fluidframework/runtime-utils/internal";
import type { IFluidSerializer } from "@fluidframework/shared-object-base/internal";
import { SharedObject } from "@fluidframework/shared-object-base/internal";
import type { ISharedMap, ISharedMapEvents } from "./interfaces.js";
import {
type IMapDataObjectSerializable,
type IMapOperation,
MapKernel,
} from "./mapKernel.js";
interface IMapSerializationFormat {
blobs?: string[];
content: IMapDataObjectSerializable;
}
const snapshotFileName = "header";
/**
* {@inheritDoc ISharedMap}
*/
export class SharedMap extends SharedObject<ISharedMapEvents> implements ISharedMap {
/**
* String representation for the class.
*/
public readonly [Symbol.toStringTag]: string = "SharedMap";
/**
* MapKernel which manages actual map operations.
*/
private readonly kernel: MapKernel;
/**
* Do not call the constructor. Instead, you should use the {@link SharedMap.create | create method}.
*
* @param id - String identifier.
* @param runtime - Data store runtime.
* @param attributes - The attributes for the map.
*/
public constructor(
id: string,
runtime: IFluidDataStoreRuntime,
attributes: IChannelAttributes,
) {
super(id, runtime, attributes, "fluid_map_");
this.kernel = new MapKernel(
this.serializer,
this.handle,
(op, localOpMetadata) => this.submitLocalMessage(op, localOpMetadata),
() => this.isAttached(),
this,
);
}
/**
* Get an iterator over the keys in this map.
* @returns The iterator
*/
public keys(): IterableIterator<string> {
return this.kernel.keys();
}
/**
* Get an iterator over the entries in this map.
* @returns The iterator
*/
// TODO: Use `unknown` instead (breaking change).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public entries(): IterableIterator<[string, any]> {
return this.kernel.entries();
}
/**
* Get an iterator over the values in this map.
* @returns The iterator
*/
// TODO: Use `unknown` instead (breaking change).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public values(): IterableIterator<any> {
return this.kernel.values();
}
/**
* Get an iterator over the entries in this map.
* @returns The iterator
*/
// TODO: Use `unknown` instead (breaking change).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public [Symbol.iterator](): IterableIterator<[string, any]> {
return this.kernel.entries();
}
/**
* The number of key/value pairs stored in the map.
*/
public get size(): number {
return this.kernel.size;
}
/**
* Executes the given callback on each entry in the map.
* @param callbackFn - Callback function
*/
// TODO: Use `unknown` instead (breaking change).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public forEach(callbackFn: (value: any, key: string, map: Map<string, any>) => void): void {
// eslint-disable-next-line unicorn/no-array-for-each, unicorn/no-array-callback-reference
this.kernel.forEach(callbackFn);
}
/**
* {@inheritDoc ISharedMap.get}
*/
// TODO: Use `unknown` instead (breaking change).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public get<T = any>(key: string): T | undefined {
return this.kernel.get<T>(key);
}
/**
* Check if a key exists in the map.
* @param key - The key to check
* @returns True if the key exists, false otherwise
*/
public has(key: string): boolean {
return this.kernel.has(key);
}
/**
* {@inheritDoc ISharedMap.set}
*/
public set(key: string, value: unknown): this {
this.kernel.set(key, value);
return this;
}
/**
* Delete a key from the map.
* @param key - Key to delete
* @returns True if the key existed and was deleted, false if it did not exist
*/
public delete(key: string): boolean {
return this.kernel.delete(key);
}
/**
* Clear all data from the map.
*/
public clear(): void {
this.kernel.clear();
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.summarizeCore}
*/
protected summarizeCore(
serializer: IFluidSerializer,
telemetryContext?: ITelemetryContext,
): ISummaryTreeWithStats {
let currentSize = 0;
let counter = 0;
let headerBlob: IMapDataObjectSerializable = {};
const blobs: string[] = [];
const builder = new SummaryTreeBuilder();
const data = this.kernel.getSerializedStorage(serializer);
// If single property exceeds this size, it goes into its own blob
const MinValueSizeSeparateSnapshotBlob = 8 * 1024;
// Maximum blob size for multiple map properties
// Should be bigger than MinValueSizeSeparateSnapshotBlob
const MaxSnapshotBlobSize = 16 * 1024;
// Partitioning algorithm:
// 1) Split large (over MinValueSizeSeparateSnapshotBlob = 8K) properties into their own blobs.
// Naming (across snapshots) of such blob does not have to be stable across snapshots,
// As de-duping process (in driver) should not care about paths, only content.
// 2) Split remaining properties into blobs of MaxSnapshotBlobSize (16K) size.
// This process does not produce stable partitioning. This means
// modification (including addition / deletion) of property can shift properties across blobs
// and result in non-incremental snapshot.
// This can be improved in the future, without being format breaking change, as loading sequence
// loads all blobs at once and partitioning schema has no impact on that process.
for (const [key, value] of Object.entries(data)) {
if (value.value && value.value.length >= MinValueSizeSeparateSnapshotBlob) {
const blobName = `blob${counter}`;
counter++;
blobs.push(blobName);
const content: IMapDataObjectSerializable = {
[key]: {
type: value.type,
value: JSON.parse(value.value) as unknown,
},
};
builder.addBlob(blobName, JSON.stringify(content));
} else {
currentSize += value.type.length + 21; // Approximation cost of property header
if (value.value) {
currentSize += value.value.length;
}
if (currentSize > MaxSnapshotBlobSize) {
const blobName = `blob${counter}`;
counter++;
blobs.push(blobName);
builder.addBlob(blobName, JSON.stringify(headerBlob));
headerBlob = {};
currentSize = 0;
}
headerBlob[key] = {
type: value.type,
value: value.value === undefined ? undefined : (JSON.parse(value.value) as unknown),
};
}
}
const header: IMapSerializationFormat = {
blobs,
content: headerBlob,
};
builder.addBlob(snapshotFileName, JSON.stringify(header));
return builder.getSummaryTree();
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.loadCore}
*/
protected async loadCore(storage: IChannelStorageService): Promise<void> {
const json = await readAndParse<object>(storage, snapshotFileName);
const newFormat = json as IMapSerializationFormat;
if (Array.isArray(newFormat.blobs)) {
this.kernel.populateFromSerializable(newFormat.content);
const blobContents = await Promise.all(
newFormat.blobs.map(async (blobName) =>
readAndParse<IMapDataObjectSerializable>(storage, blobName),
),
);
for (const blobContent of blobContents) {
this.kernel.populateFromSerializable(blobContent);
}
} else {
this.kernel.populateFromSerializable(json as IMapDataObjectSerializable);
}
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.onDisconnect}
*/
protected onDisconnect(): void {}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.reSubmitCore}
*/
protected override reSubmitCore(content: unknown, localOpMetadata: unknown): void {
this.kernel.tryResubmitMessage(content as IMapOperation, localOpMetadata);
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObjectCore.applyStashedOp}
*/
protected applyStashedOp(content: unknown): void {
this.kernel.tryApplyStashedOp(content as IMapOperation);
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.processMessagesCore}
*/
protected override processMessagesCore(messagesCollection: IRuntimeMessageCollection): void {
const { envelope, local, messagesContent } = messagesCollection;
for (const messageContent of messagesContent) {
this.processMessage(envelope, messageContent, local);
}
}
private processMessage(
messageEnvelope: ISequencedMessageEnvelope,
messageContent: IRuntimeMessagesContent,
local: boolean,
): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (messageEnvelope.type === MessageType.Operation) {
assert(
this.kernel.tryProcessMessage(
messageContent.contents as IMapOperation,
local,
messageContent.localOpMetadata,
),
0xab2 /* Map received an unrecognized op, possibly from a newer version */,
);
}
}
/**
* {@inheritDoc @fluidframework/shared-object-base#SharedObject.rollback}
*/
protected override rollback(content: unknown, localOpMetadata: unknown): void {
this.kernel.rollback(content, localOpMetadata);
}
}