/
multi-ring-buffer.ts
348 lines (294 loc) · 10.4 KB
/
multi-ring-buffer.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// *****************************************************************************
// Copyright (C) 2017 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import * as stream from 'stream';
import { inject, injectable } from '@theia/core/shared/inversify';
import { Disposable } from '@theia/core/lib/common';
/**
* The MultiRingBuffer is a ring buffer implementation that allows
* multiple independent readers.
*
* These readers are created using the getReader or getStream functions
* to create a reader that can be read using deq() or one that is a readable stream.
*/
export class MultiRingBufferReadableStream extends stream.Readable implements Disposable {
protected more = false;
protected disposed = false;
constructor(protected readonly ringBuffer: MultiRingBuffer,
protected readonly reader: number,
protected readonly encoding: BufferEncoding = 'utf8'
) {
super();
this.setEncoding(encoding);
}
override _read(size: number): void {
this.more = true;
this.deq(size);
}
override _destroy(err: Error | null, callback: (err: Error | null) => void): void {
this.ringBuffer.closeStream(this);
this.ringBuffer.closeReader(this.reader);
this.disposed = true;
this.removeAllListeners();
callback(err);
}
onData(): void {
if (this.more === true) {
this.deq(-1);
}
}
deq(size: number): void {
if (this.disposed === true) {
return;
}
let buffer = undefined;
do {
buffer = this.ringBuffer.deq(this.reader, size, this.encoding);
if (buffer !== undefined) {
this.more = this.push(buffer, this.encoding);
}
}
while (buffer !== undefined && this.more === true && this.disposed === false);
}
dispose(): void {
this.destroy();
}
}
export const MultiRingBufferOptions = Symbol('MultiRingBufferOptions');
export interface MultiRingBufferOptions {
readonly size: number,
readonly encoding?: BufferEncoding,
}
export interface WrappedPosition { newPos: number, wrap: boolean }
@injectable()
export class MultiRingBuffer implements Disposable {
protected readonly buffer: Buffer;
protected head: number = -1;
protected tail: number = -1;
protected readonly maxSize: number;
protected readonly encoding: BufferEncoding;
/* <id, position> */
protected readonly readers: Map<number, number>;
/* <stream : id> */
protected readonly streams: Map<MultiRingBufferReadableStream, number>;
protected readerId = 0;
constructor(
@inject(MultiRingBufferOptions) protected readonly options: MultiRingBufferOptions
) {
this.maxSize = options.size;
if (options.encoding !== undefined) {
this.encoding = options.encoding;
} else {
this.encoding = 'utf8';
}
this.buffer = Buffer.alloc(this.maxSize);
this.readers = new Map();
this.streams = new Map();
}
enq(str: string, encoding = 'utf8'): void {
let buffer: Buffer = Buffer.from(str, encoding as BufferEncoding);
// Take the last elements of string if it's too big, drop the rest
if (buffer.length > this.maxSize) {
buffer = buffer.slice(buffer.length - this.maxSize);
}
if (buffer.length === 0) {
return;
}
// empty
if (this.head === -1 && this.tail === -1) {
this.head = 0;
this.tail = 0;
buffer.copy(this.buffer, this.head, 0, buffer.length);
this.head = buffer.length - 1;
this.onData(0);
return;
}
const startHead = this.inc(this.head, 1).newPos;
if (this.inc(startHead, buffer.length).wrap === true) {
buffer.copy(this.buffer, startHead, 0, this.maxSize - startHead);
buffer.copy(this.buffer, 0, this.maxSize - startHead);
} else {
buffer.copy(this.buffer, startHead);
}
this.incTails(buffer.length);
this.head = this.inc(this.head, buffer.length).newPos;
this.onData(startHead);
}
getReader(): number {
this.readers.set(this.readerId, this.tail);
return this.readerId++;
}
closeReader(id: number): void {
this.readers.delete(id);
}
getStream(encoding?: BufferEncoding): MultiRingBufferReadableStream {
const reader = this.getReader();
const readableStream = new MultiRingBufferReadableStream(this, reader, encoding);
this.streams.set(readableStream, reader);
return readableStream;
}
closeStream(readableStream: MultiRingBufferReadableStream): void {
this.streams.delete(<MultiRingBufferReadableStream>readableStream);
}
protected onData(start: number): void {
/* Any stream that has read everything already
* Should go back to the last buffer in start offset */
for (const [id, pos] of this.readers) {
if (pos === -1) {
this.readers.set(id, start);
}
}
/* Notify the streams there's new data. */
for (const [readableStream] of this.streams) {
readableStream.onData();
}
}
deq(id: number, size = -1, encoding: BufferEncoding = 'utf8'): string | undefined {
const pos = this.readers.get(id);
if (pos === undefined || pos === -1) {
return undefined;
}
if (size === 0) {
return undefined;
}
let buffer = '';
const maxDeqSize = this.sizeForReader(id);
const wrapped = this.isWrapped(pos, this.head);
let deqSize;
if (size === -1) {
deqSize = maxDeqSize;
} else {
deqSize = Math.min(size, maxDeqSize);
}
if (wrapped === false) { // no wrap
buffer = this.buffer.toString(encoding, pos, pos + deqSize);
} else { // wrap
buffer = buffer.concat(this.buffer.toString(encoding, pos, this.maxSize),
this.buffer.toString(encoding, 0, deqSize - (this.maxSize - pos)));
}
const lastIndex = this.inc(pos, deqSize - 1).newPos;
// everything is read
if (lastIndex === this.head) {
this.readers.set(id, -1);
} else {
this.readers.set(id, this.inc(pos, deqSize).newPos);
}
return buffer;
}
sizeForReader(id: number): number {
const pos = this.readers.get(id);
if (pos === undefined) {
return 0;
}
return this.sizeFrom(pos, this.head, this.isWrapped(pos, this.head));
}
size(): number {
return this.sizeFrom(this.tail, this.head, this.isWrapped(this.tail, this.head));
}
protected isWrapped(from: number, to: number): boolean {
if (to < from) {
return true;
} else {
return false;
}
}
protected sizeFrom(from: number, to: number, wrap: boolean): number {
if (from === -1 || to === -1) {
return 0;
} else {
if (wrap === false) {
return to - from + 1;
} else {
return to + 1 + this.maxSize - from;
}
}
}
emptyForReader(id: number): boolean {
const pos = this.readers.get(id);
if (pos === undefined || pos === -1) {
return true;
} else {
return false;
}
}
empty(): boolean {
if (this.head === -1 && this.tail === -1) {
return true;
} else {
return false;
}
}
streamsSize(): number {
return this.streams.size;
}
readersSize(): number {
return this.readers.size;
}
/**
* Dispose all the attached readers/streams.
*/
dispose(): void {
for (const readableStream of this.streams.keys()) {
readableStream.dispose();
}
}
/* Position should be incremented if it goes pass end. */
protected shouldIncPos(pos: number, end: number, size: number): boolean {
const { newPos: newHead, wrap } = this.inc(end, size);
/* Tail Head */
if (this.isWrapped(pos, end) === false) {
// Head needs to wrap to push the tail
if (wrap === true && newHead >= pos) {
return true;
}
} else { /* Head Tail */
// If we wrap head is pushing tail, or if it goes over pos
if (wrap === true || newHead >= pos) {
return true;
}
}
return false;
}
protected incTailSize(pos: number, head: number, size: number): WrappedPosition {
const { newPos: newHead } = this.inc(head, size);
/* New tail is 1 past newHead. */
return this.inc(newHead, 1);
}
protected incTail(pos: number, size: number): WrappedPosition {
if (this.shouldIncPos(pos, this.head, size) === false) {
return { newPos: pos, wrap: false };
}
return this.incTailSize(pos, this.head, size);
}
/* Increment the main tail and all the reader positions. */
protected incTails(size: number): void {
this.tail = this.incTail(this.tail, size).newPos;
for (const [id, pos] of this.readers) {
if (pos !== -1) {
if (this.shouldIncPos(pos, this.tail, size) === true) {
this.readers.set(id, this.tail);
}
}
}
}
protected inc(pos: number, size: number): WrappedPosition {
if (size === 0) {
return { newPos: pos, wrap: false };
}
const newPos = (pos + size) % this.maxSize;
const wrap = newPos <= pos;
return { newPos, wrap };
}
}