/
Shard.ts
96 lines (85 loc) · 2.85 KB
/
Shard.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ContainerClient, CommonOptions } from "@azure/storage-blob";
import { ChunkFactory } from "./ChunkFactory";
import { Chunk } from "./Chunk";
import { BlobChangeFeedEvent } from "./models/BlobChangeFeedEvent";
import { ShardCursor } from "./models/ChangeFeedCursor";
import { AbortSignalLike } from "@azure/core-http";
import { createSpan } from "./utils/tracing";
import { SpanStatusCode } from "@azure/core-tracing";
/**
* Options to configure {@link Shard.getChange} operation.
*/
export interface ShardGetChangeOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
export class Shard {
private readonly containerClient: ContainerClient;
private readonly chunkFactory: ChunkFactory;
private readonly chunks: string[];
private currentChunk: Chunk | undefined;
constructor(
containerClient: ContainerClient,
chunkFactory: ChunkFactory,
chunks: string[],
currentChunk: Chunk | undefined,
public readonly shardPath: string
) {
this.containerClient = containerClient;
this.chunkFactory = chunkFactory;
this.chunks = chunks;
this.currentChunk = currentChunk;
}
public hasNext(): boolean {
return (
this.chunks.length > 0 || (this.currentChunk !== undefined && this.currentChunk.hasNext())
);
}
public async getChange(
options: ShardGetChangeOptions = {}
): Promise<BlobChangeFeedEvent | undefined> {
const { span, updatedOptions } = createSpan("Shard-getChange", options);
try {
let event: BlobChangeFeedEvent | undefined = undefined;
while (event === undefined && this.hasNext()) {
event = await this.currentChunk!.getChange();
// Remove currentChunk if it doesn't have more events.
if (!this.currentChunk!.hasNext() && this.chunks.length > 0) {
this.currentChunk = await this.chunkFactory.create(
this.containerClient,
this.chunks.shift()!,
undefined,
undefined,
{
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
}
);
}
}
return event;
} catch (e: any) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: e.message,
});
throw e;
} finally {
span.end();
}
}
public getCursor(): ShardCursor | undefined {
return this.currentChunk === undefined
? undefined
: {
CurrentChunkPath: this.currentChunk.chunkPath,
BlockOffset: this.currentChunk.blockOffset,
EventIndex: this.currentChunk.eventIndex,
};
}
}