forked from votem/mongo-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionManager.js
More file actions
140 lines (121 loc) · 4.07 KB
/
CollectionManager.js
File metadata and controls
140 lines (121 loc) · 4.07 KB
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
const BSON = require('bson');
const bson = new BSON();
const fs = require('fs');
class CollectionManager {
constructor(db, collection, elasticManager) {
this.db = db;
this.elasticManager = elasticManager;
this.collection = collection;
this.elasticManager.setMappings(collection);
this.resumeToken = this.getResumeToken();
this.changeStream;
}
async dumpCollection() {
const cursor = this.db.collection(this.collection).find({}, {});
const count = await this.db.collection(this.collection).count();
let requestCount = 0;
let bulkOp = [];
let nextObject;
for (let i = 0; i < count; i++) {
if (bulkOp.length !== 0 && bulkOp.length % (this.elasticManager.bulkSize * 2) === 0) {
requestCount += (bulkOp.length/2);
console.log(`${this.collection} request progress: ${requestCount}/${count}`);
await this.elasticManager.sendBulkRequest(bulkOp);
bulkOp = [];
}
nextObject = await cursor.next().catch(err => console.log('next object error', err));
if (nextObject === null) {
break;
}
const _id = nextObject._id;
delete nextObject._id;
bulkOp.push({
index: {
_index: this.elasticManager.mappings[this.collection].index,
_type: this.elasticManager.mappings[this.collection].type,
_id: _id
}
});
bulkOp.push(nextObject);
}
requestCount += (bulkOp.length/2);
console.log(`${this.collection} FINAL request progress: ${requestCount}/${count}`);
await this.elasticManager.sendBulkRequest(bulkOp); // last bits
console.log('done');
}
watch(ignoreResumeToken = false) {
console.log('new watcher for collection', this.collection);
if (ignoreResumeToken) this.resumeToken = null;
this.changeStream = this.db.collection(this.collection).watch({resumeAfter: this.resumeToken, fullDocument: 'updateLookup'});
this._addChangeListener();
this._addCloseListener();
this._addErrorListener();
}
_addChangeListener() {
this.changeStream.on('change', (change) => {
console.log('change event', change.operationType);
if (change.operationType === 'invalidate') {
this.resetChangeStream(true);
return;
}
this.resumeToken = change._id;
this.elasticManager.replicate(change);
});
}
_addCloseListener() {
this.changeStream.on('close', () => {
console.log('close event');
});
}
_addErrorListener() {
this.changeStream.on('error', (error) => {
console.log(this.collection,' changeStream error', error);
// resume of change stream was not possible, as the resume token was not found
if (error.code === 40585 || error.code === 40615) {
this.resetChangeStream();
}
});
}
async resetChangeStream(dump = false) {
delete this.changeStream;
this.resumeToken = null;
if (dump) {
await this.elasticManager.deleteElasticCollection(this.collection);
await this.dumpCollection().catch(err => console.log(err));
}
this.watch();
}
removeChangeStream() {
if (!this.changeStream) { return; }
const listeners = this.changeStream.eventNames();
listeners.forEach(listener => {
this.changeStream.removeAllListeners(listener);
});
delete this.changeStream;
this.resumeToken = null;
}
getResumeToken() {
if (!this.resumeToken) {
try {
const base64Buffer = fs.readFileSync(`./resumeTokens/${this.collection}`);
this.resumeToken = bson.deserialize(base64Buffer);
} catch (err) {
this.resumeToken = null;
}
}
return this.resumeToken;
}
hasResumeToken() {
return !!this.resumeToken;
}
writeResumeToken() {
if (!this.resumeToken) return;
const b64String = bson.serialize(this.resumeToken).toString('base64');
fs.writeFileSync(`./resumeTokens/${this.collection}`, b64String, 'base64');
}
removeResumeToken() {
this.resumeToken = null;
if(fs.existsSync(`./resumeTokens/${this.collection}`)) fs.unlink(`./resumeTokens/${this.collection}`);
}
}
module.exports = CollectionManager;