-
Notifications
You must be signed in to change notification settings - Fork 3
/
restore.js
217 lines (199 loc) · 5.42 KB
/
restore.js
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
/**
* ### Модуль чтения данных из файла *.json в couchdb
*
* @module restore
*
* Created by Evgeniy Malyarov on 16.06.2018.
*/
/**
* ### Переменные окружения
* DEBUG "wb:*,-not_this"
* DBPWD admin
* DBUSER admin
* COUCHPATH http://cou221:5984/wb_
*/
'use strict';
const debug = require('debug')('wb:restore');
const PouchDB = require('./pouchdb');
const path = require('path');
const fs = require('fs');
const JSZip = require('jszip');
debug('required');
const mb = 1024 * 1024;
let docs = 0;
let files;
let dst;
const yargs = require('yargs')
.demand(1)
.strict()
.version('v', 'Show version', '0.0.1').alias('v', 'version')
.help('h').alias('h', 'help')
.example('node restore from wb_21_doc.json', 'restore database from `wb_21_doc.json` file')
.command(
'from [from]',
'restore database',
(yargs) => yargs.positional('from', {type: 'string', describe: 'empty file name'}),
({from}) => {
if(from) {
// получаем параметры сеанса
const {DBUSER, DBPWD, COUCHPATH, ZONE} = process.env;
// подключаемся к базе данных
const prefix = 'wb_';
const url = from.includes('_meta') ? `${COUCHPATH}meta` : `${COUCHPATH}${ZONE}_${from.includes('_doc') ? 'doc' : 'ram'}`;
dst = new PouchDB(url, {
auth: {
username: DBUSER,
password: DBPWD
},
ajax: {timeout: 100000}
});
dst.info()
.then((info) => {
debug(`connected to ${info.host}, doc count: ${info.doc_count}`);
restore(from);
})
.catch(err => {
debug(err);
process.exit(1);
});
}
else {
yargs.showHelp();
process.exit(1);
}
})
.epilog('\nMore information about the library: https://github.com/oknosoft/windowbuilder');
function restore(from) {
// если from указывает на каталог, перебираем файлы архивов
try{
if(fs.lstatSync(from).isDirectory()) {
files = fs.readdirSync(from).filter((file) => {
const stat = fs.statSync(path.join(from, file));
return stat.isFile() && file.indexOf('.zip') !== -1;
});
fromFiles(from);
}
else {
// открываем файловый поток
const stream = fs.createReadStream(from, {highWaterMark: mb * 4});
fromFile(stream, from)
.then(() => {
debug('\nall done');
})
.catch(err => {
debug(err);
process.exit(1);
});
}
}catch(e){
debug(err);
process.exit(1);
}
}
function fromFiles(from) {
return new Promise((resolve, reject) => {
if(!files.length) {
return resolve();
}
// read a zip file
fs.readFile(path.join(from, files.splice(0, 1)[0]), (err, data) => {
if (err) {
reject(err);
}
resolve(JSZip.loadAsync(data));
});
})
.then((zip) => {
const file = Object.keys(zip.files)[0];
const content = zip.file(file);
return fromFile(content.nodeStream(), file);
})
.then(() => {
if(files.length) {
return fromFiles(from);
}
});
}
function fromFile(stream, name) {
return new Promise((resolve, reject) => {
// в tail (хвост) будем складывать неполные объекты
let tail = '';
let substr = '';
let finish;
stream.on('data', (chunk) => {
substr += chunk.toString();
if(!finish && substr.length < mb * 4) {
return;
}
// тормозим поток
stream.pause();
// делаем синтаксический разбор
const data = substr.replace(/,\n/g, '\n').split('\n');
substr = '';
if(data[0] === '{"new_edits":false,"docs":[' || data[0] === '') {
data.splice(0, 1);
}
if(tail) {
data[0] = tail + data[0];
tail = '';
}
let last = data[data.length - 1];
try {
while(last === '') {
data.splice(data.length - 1, 1);
last = data[data.length - 1];
finish = true;
}
if(last === ']}') {
data.splice(data.length - 1, 1);
finish = true;
}
else {
const test = JSON.parse(last);
}
}
catch(err) {
tail = last;
data.splice(data.length - 1, 1);
}
// отправляем данные в couchdb
try {
if(data.length){
dst.bulkDocs(data.map((str) => JSON.parse(str)), {new_edits: false})
.then((rows) => {
docs += data.length;
data.length = 0;
debug(`\u001b[2K\u001b[0E\tfile: ${name}, ${docs} docs written`);
//process.stdout.write(`\u001b[2K\u001b[0E\tfile: ${name}, ${docs} docs written`);
if(finish) {
resolve();
}
else {
stream.resume();
}
})
.catch((err) => {
reject(err);
});
}
else {
stream.resume();
}
}
catch(err) {
reject(err);
}
})
.on('end', () => {
finish = true;
if(substr) {
stream.emit('data', new Buffer([]));
}
});
});
}
const {argv} = yargs;
if(!argv._.length){
yargs.showHelp();
process.exit(1);
}