1 | // Copyright © 2017 IBM Corp. All rights reserved.
|
2 | //
|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | // you may not use this file except in compliance with the License.
|
5 | // You may obtain a copy of the License at
|
6 | //
|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
8 | //
|
9 | // Unless required by applicable law or agreed to in writing, software
|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | // See the License for the specific language governing permissions and
|
13 | // limitations under the License.
|
14 | ;
|
15 |
|
16 | const fs = require('fs');
|
17 | const stream = require('stream');
|
18 | const liner = require('./liner.js');
|
19 |
|
20 | const onLine = function(onCommand, getDocs) {
|
21 | const change = new stream.Transform({ objectMode: true });
|
22 |
|
23 | change._transform = function(line, encoding, done) {
|
24 | if (line && line[0] === ':') {
|
25 | const obj = {
|
26 | command: null,
|
27 | batch: null,
|
28 | docs: []
|
29 | };
|
30 |
|
31 | let matches;
|
32 |
|
33 | // extract command
|
34 | matches = line.match(/^:([a-z_]+) ?/);
|
35 | if (matches) {
|
36 | obj.command = matches[1];
|
37 | }
|
38 |
|
39 | // extract batch
|
40 | matches = line.match(/ batch([0-9]+)/);
|
41 | if (matches) {
|
42 | obj.batch = parseInt(matches[1]);
|
43 | }
|
44 |
|
45 | // extract doc ids
|
46 | if (getDocs && obj.command === 't') {
|
47 | const json = line.replace(/^.* batch[0-9]+ /, '').trim();
|
48 | obj.docs = JSON.parse(json);
|
49 | }
|
50 | onCommand(obj);
|
51 | }
|
52 | done();
|
53 | };
|
54 | return change;
|
55 | };
|
56 |
|
57 | /**
|
58 | * Generate a list of remaining batches from a download file.
|
59 | *
|
60 | * @param {string} log - log file name
|
61 | * @param {function} callback - callback with err, {changesComplete: N, batches: N}.
|
62 | * changesComplete signifies whether the log file appeared to
|
63 | * have completed reading the changes feed (contains :changes_complete).
|
64 | * batches are remaining batch IDs for download.
|
65 | */
|
66 | module.exports = function(log, callback) {
|
67 | // our sense of state
|
68 | const state = {
|
69 |
|
70 | };
|
71 | let changesComplete = false;
|
72 |
|
73 | // called with each line from the log file
|
74 | const onCommand = function(obj) {
|
75 | if (obj.command === 't') {
|
76 | state[obj.batch] = true;
|
77 | } else if (obj.command === 'd') {
|
78 | delete state[obj.batch];
|
79 | } else if (obj.command === 'changes_complete') {
|
80 | changesComplete = true;
|
81 | }
|
82 | };
|
83 |
|
84 | // stream through the previous log file
|
85 | fs.createReadStream(log)
|
86 | .pipe(liner())
|
87 | .pipe(onLine(onCommand, false))
|
88 | .on('finish', function() {
|
89 | const obj = { changesComplete: changesComplete, batches: state };
|
90 | callback(null, obj);
|
91 | });
|
92 | };
|