保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
const fs = require('graceful-fs');
const { join, resolve } = require('path');
const _mkdirp = require('mkdirp');
const parseJson = require('parse-json');
const _rimraf = require('rimraf');
 
const promisify = require('./util/promisify');
 
const close = promisify(fs.close);
const mkdirp = promisify(_mkdirp);
const open = promisify(fs.open);
const read = promisify(fs.readFile);
const readdir = promisify(fs.readdir);
const readfd = promisify(fs.read);
const rename = promisify(fs.rename);
const rimraf = promisify(_rimraf);
const write = promisify(fs.writeFile);
 
const nextPow2 = n => {
  const exponent = Math.log(n) / Math.log(2);
  const nextExponent = Math.floor(exponent) + 1;
  return Math.pow(2, nextExponent);
};
 
const resizePow2 = (buffer, n) => {
  const tmpBuffer = Buffer.allocUnsafe(nextPow2(n));
  buffer.copy(tmpBuffer.slice(0, buffer.length));
  return tmpBuffer;
};
 
const MAX_CHUNK = 2 * 1024 * 1024;
const TMP_CHUNK = 0.5 * 1024 * 1024;
const MAX_CHUNK_PLUS = 2.5 * 1024 * 1024;
const LARGE_CONTENT = 64 * 1024;
 
let tmpBuffer = Buffer.allocUnsafe(TMP_CHUNK);
let outBuffer = Buffer.allocUnsafe(MAX_CHUNK_PLUS);
 
const _buffers = [];
 
const alloc = size => {
  const buffer = _buffers.pop();
  if (buffer && buffer.length >= size) {
    return buffer;
  }
  return Buffer.allocUnsafe(size);
};
const drop = buffer => _buffers.push(buffer);
 
class WriteOutput {
  constructor(length = 0, table = [], buffer = alloc(MAX_CHUNK_PLUS)) {
    this.length = length;
    this.table = table;
    this.buffer = buffer;
  }
 
  static clone(other) {
    return new WriteOutput(other.length, other.table, other.buffer);
  }
 
  take() {
    const output = WriteOutput.clone(this);
 
    this.length = 0;
    this.table = [];
    this.buffer = alloc(MAX_CHUNK_PLUS);
 
    return output;
  }
 
  add(key, content) {
    if (content !== null) {
      // Write content to a temporary buffer
      let length = tmpBuffer.utf8Write(content);
      while (length === tmpBuffer.length) {
        tmpBuffer = Buffer.allocUnsafe(tmpBuffer.length * 2);
        length = tmpBuffer.utf8Write(content);
      }
 
      const start = this.length;
      const end = start + length;
 
      // Ensure output buffer is long enough to add the new content
      if (end > this.buffer.length) {
        this.buffer = resizePow2(this.buffer, end);
      }
 
      // Copy temporary buffer to the end of the current output buffer
      tmpBuffer.copy(this.buffer.slice(start, end));
 
      this.table.push({
        name: key,
        start,
        end,
      });
      this.length = end;
    } else {
      this.table.push({
        name: key,
        start: -1,
        end: -1,
      });
    }
  }
}
 
class Semaphore {
  constructor(max) {
    this.max = max;
    this.count = 0;
    this.next = [];
  }
 
  async guard() {
    if (this.count < this.max) {
      this.count++;
      return new SemaphoreGuard(this);
    } else {
      return new Promise(resolve => {
        this.next.push(resolve);
      }).then(() => new SemaphoreGuard(this));
    }
  }
}
 
class SemaphoreGuard {
  constructor(parent) {
    this.parent = parent;
  }
 
  done() {
    const next = this.parent.next.shift();
    if (next) {
      next();
    } else {
      this.parent.count--;
    }
  }
}
 
class Append2 {
  constructor({ cacheDirPath: path, autoParse }) {
    this.path = path;
    this.autoParse = autoParse;
 
    this.inBuffer = Buffer.alloc(0);
    this._buffers = [];
    this.outBuffer = Buffer.alloc(0);
  }
 
  async _readFile(file) {
    const fd = await open(file, 'r+');
 
    let body = alloc(MAX_CHUNK_PLUS);
 
    await readfd(fd, body, 0, 4, null);
    const fullLength = body.readUInt32LE(0);
    if (fullLength > body.length) {
      drop(body);
      body = alloc(nextPow2(fullLength));
    }
    await readfd(fd, body, 0, fullLength, null);
 
    close(fd);
 
    const tableLength = body.readUInt32LE(0);
    const tableBody = body.utf8Slice(4, 4 + tableLength);
    const table = parseJson(tableBody);
    const content = body.slice(4 + tableLength);
 
    return [table, content, body];
  }
 
  async read() {
    const out = {};
    const size = { used: 0, total: 0 };
    const table = {};
    const order = {};
 
    await mkdirp(this.path);
 
    const items = await readdir(this.path);
    const logs = items.filter(item => /^log\d+$/.test(item));
    logs.sort();
    const reverseLogs = logs.reverse();
 
    const sema = new Semaphore(8);
 
    return Promise.all(
      reverseLogs.map(async (_file, index) => {
        const file = join(this.path, _file);
        const guard = await sema.guard();
 
        const [table, content, body] = await this._readFile(file);
 
        const keys = Object.keys(table);
        if (keys.length > 0) {
          size.total += table[keys.length - 1].end;
        }
 
        for (const entry of table) {
          if (
            typeof order[entry.name] === 'undefined' ||
            order[entry.name] > index
          ) {
            if (typeof order[entry.name] !== 'undefined') {
              size.used -= table[entry.name];
            }
 
            table[entry.name] = entry.end - entry.start;
            size.used += entry.end - entry.start;
 
            order[entry.name] = index;
 
            // Negative start positions are not set on the output. They are
            // treated as if they were deleted in a prior write. A future
            // compact will remove all instances of any old entries.
            if (entry.start >= 0) {
              await new Promise(process.nextTick);
              const data = content.utf8Slice(entry.start, entry.end);
              if (this.autoParse) {
                out[entry.name] = parseJson(data);
              } else {
                out[entry.name] = data;
              }
            } else {
              delete out[entry.name];
            }
          }
        }
 
        drop(body);
        guard.done();
      }),
    )
      .then(async () => {
        if (size.used / size.total < 0.6) {
          await this.compact(out);
        }
      })
      .then(() => out);
  }
 
  async _markLog() {
    const count = (await readdir(this.path)).filter(item =>
      /log\d+$/.test(item),
    ).length;
    const marker = Math.random()
      .toString(16)
      .substring(2)
      .padStart(13, '0');
    const logName = `log${count.toString().padStart(4, '0')}`;
    const file = resolve(this.path, logName);
    await write(file, marker);
    const writtenMarker = await read(file, 'utf8');
    if (marker === writtenMarker) {
      return file;
    }
    return null;
  }
 
  async _write(file, output) {
    // 4 bytes - full length
    // 4 bytes - length of table
    // x bytes - table
    // y bytes - content
 
    // Write table into a temporary buffer at position 8
    const content = JSON.stringify(output.table);
    let length = tmpBuffer.utf8Write(content, 8);
    // Make the temporary buffer longer if the space used is the same as the
    // length
    while (8 + length === tmpBuffer.length) {
      tmpBuffer = Buffer.allocUnsafe(nextPow2(8 + length));
      // Write again to see if the length is more due to the last buffer being
      // too short.
      length = tmpBuffer.utf8Write(content, 8);
    }
 
    // Ensure the buffer is long enough to fit the table and content.
    const end = 8 + length + output.length;
    if (end > tmpBuffer.length) {
      tmpBuffer = resizePow2(tmpBuffer, end);
    }
 
    // Copy the output after the table.
    output.buffer.copy(tmpBuffer.slice(8 + length, end));
 
    // Full length after this uint.
    tmpBuffer.writeUInt32LE(end - 4, 0);
    // Length of table after this uint.
    tmpBuffer.writeUInt32LE(length, 4);
 
    if (end > output.buffer.length) {
      output.buffer = alloc(nextPow2(end));
    }
    tmpBuffer.copy(output.buffer.slice(0, end));
 
    await write(file, output.buffer.slice(0, end));
    drop(output.buffer);
  }
 
  async _markAndWrite(output) {
    const file = await this._markLog();
    if (file !== null) {
      await this._write(file, output.take());
    }
  }
 
  // Write out a log chunk once the file reaches the maximum chunk size.
  async _writeAtMax(output) {
    while (output.length >= MAX_CHUNK) {
      await this._markAndWrite(output);
    }
  }
 
  // Write out a log chunk if their is any entries in the table.
  async _writeAtAny(output) {
    while (output.table.length > 0) {
      await this._markAndWrite(output);
    }
  }
 
  async write(ops) {
    let smallOutput = new WriteOutput();
    let largeOutput = new WriteOutput();
 
    const outputPromises = [];
 
    await mkdirp(this.path);
 
    for (const op of ops) {
      if (op.value !== null) {
        let content = op.value;
        if (typeof content !== 'string') {
          content = JSON.stringify(content);
        }
        if (content.length < LARGE_CONTENT) {
          smallOutput.add(op.key, content);
 
          await this._writeAtMax(smallOutput);
        } else {
          largeOutput.add(op.key, content);
 
          await this._writeAtMax(largeOutput);
        }
      } else {
        smallOutput.add(op.key, null);
 
        await this._writeAtMax(smallOutput);
      }
    }
 
    await this._writeAtAny(smallOutput);
    await this._writeAtAny(largeOutput);
 
    await Promise.all(outputPromises);
  }
 
  async sizes() {
    const size = {
      used: 0,
      total: 0,
    };
    const table = {};
    const order = {};
 
    await mkdirp(this.path);
 
    const items = await readdir(this.path);
    const logs = items.filter(item => /^log\d+$/.test(item));
    logs.sort();
    const reverseLogs = logs.reverse();
 
    const sema = new Semaphore(8);
 
    return Promise.all(
      reverseLogs.map(async (_file, index) => {
        const file = join(this.path, _file);
        const guard = await sema.guard();
 
        const [table, content, body] = await this._readFile(file);
 
        size.total += content.length;
 
        for (const entry of table) {
          if (
            typeof order[entry.name] === 'undefined' ||
            order[entry.name] > index
          ) {
            if (typeof order[entry.name] !== 'undefined') {
              size.used -= table[entry.name];
            }
            table[entry.name] = entry.end - entry.start;
            size.used += entry.end - entry.start;
 
            order[entry.name] = index;
          }
        }
 
        drop(body);
        guard.done();
      }),
    ).then(() => size);
  }
 
  async compact(_obj = this.read()) {
    const obj = await _obj;
    const ops = [];
    for (const key in obj) {
      ops.push({
        key,
        value: obj[key],
      });
    }
    const truePath = this.path;
    this.path += '~';
    await this.write(ops);
    this.path = truePath;
    await rimraf(this.path);
    await rename(`${this.path}~`, this.path);
  }
}
 
module.exports = Append2;