|
| 1 | +/** |
| 2 | + * Copyright (c) 2017 Kibae Shin ([email protected]) |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * README.md file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +var EventEmitter = require('events').EventEmitter; |
| 9 | +var Client = require('./client'); |
| 10 | +var util = require('util'); |
| 11 | + |
| 12 | +var WalStream = function(config) { |
| 13 | + EventEmitter.call(this); |
| 14 | + |
| 15 | + var c = config || {}; |
| 16 | + c.replication = 'database'; |
| 17 | + |
| 18 | + var self = this; |
| 19 | + |
| 20 | + var client; |
| 21 | + var stoped = false; |
| 22 | + this.getChanges = function(slotName, uptoLsn, option, cb /*(err)*/) { |
| 23 | + option = option || {}; |
| 24 | + /* |
| 25 | + * includeXids : include xid on BEGIN and COMMIT, default false |
| 26 | + * includeTimestamp : include timestamp on COMMIT, default false |
| 27 | + * skipEmptyXacts : skip empty transaction like DDL, default true |
| 28 | + */ |
| 29 | + |
| 30 | + stoped = false; |
| 31 | + client = new Client(config); |
| 32 | + |
| 33 | + client.on('error', function(err) { |
| 34 | + self.emit('error', err); |
| 35 | + }); |
| 36 | + |
| 37 | + client.connect(function(err) { |
| 38 | + //error handling |
| 39 | + if (err) { |
| 40 | + self.emit('error', err); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + var sql = 'START_REPLICATION SLOT ' + slotName + ' LOGICAL ' + (uptoLsn ? uptoLsn : '0/00000000'); |
| 45 | + var opts = [ |
| 46 | + '"include-xids" \'' + (option.includeXids === true ? 'on' : 'off') + '\'', |
| 47 | + '"include-timestamp" \'' + (option.includeTimestamp === true ? 'on' : 'off') + '\'', |
| 48 | + '"skip-empty-xacts" \'' + (option.skipEmptyXacts !== false ? 'on' : 'off') + '\'', |
| 49 | + ]; |
| 50 | + sql += ' (' + (opts.join(' , ')) + ')'; |
| 51 | + |
| 52 | + client.query(sql, function(err) { |
| 53 | + if (err) { |
| 54 | + if (!stoped && cb) { |
| 55 | + cb(err); |
| 56 | + cb = null; |
| 57 | + } |
| 58 | + } |
| 59 | + cb = null; |
| 60 | + }); |
| 61 | + |
| 62 | + client.connection.once('replicationStart', function() { |
| 63 | + //start |
| 64 | + self.emit('start', self); |
| 65 | + client.connection.on('copyData', function(msg) { |
| 66 | + if (msg.chunk[0] != 0x77) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + var lsn = (msg.chunk.readUInt32BE(1).toString(16).toUpperCase()) + '/' + (msg.chunk.readUInt32BE(5).toString(16).toUpperCase()); |
| 71 | + self.emit('data', { |
| 72 | + lsn: lsn, |
| 73 | + log: msg.chunk.slice(25), |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + return self; |
| 79 | + }; |
| 80 | + |
| 81 | + this.stop = function() { |
| 82 | + stoped = true; |
| 83 | + if (client) { |
| 84 | + client.end(); |
| 85 | + client = null; |
| 86 | + } |
| 87 | + }; |
| 88 | +}; |
| 89 | + |
| 90 | +util.inherits(WalStream, EventEmitter); |
| 91 | + |
| 92 | +module.exports = WalStream; |
0 commit comments