diff --git a/antipersistence.js b/antipersistence.js index d2639fa..9458653 100644 --- a/antipersistence.js +++ b/antipersistence.js @@ -2,6 +2,7 @@ const fs = require('fs'); const fsp = require('fs').promises; const zlib = require('zlib'); +const stableStringify = require('./stable-stringify.js'); const EtcTree = require('./etctree.js'); const { de64, runCallbacks } = require('./common.js'); @@ -52,10 +53,10 @@ class AntiPersistence let changed = false; for (const ev of msg.events) { - if (ev.kv.lease) + if (ev.lease) { // Values with lease are never persisted - const key = de64(ev.kv.key); + const key = de64(ev.key); if (this.prev_value[key] !== undefined) { delete this.prev_value[key]; @@ -64,15 +65,14 @@ class AntiPersistence } else { - const key = de64(ev.kv.key); - const filtered = this.cfg.persist_filter(key, ev.type === 'DELETE' ? undefined : de64(ev.kv.value)); + const key = de64(ev.key); + const filtered = this.cfg.persist_filter(key, ev.value == null ? undefined : de64(ev.value)); if (!EtcTree.eq(filtered, this.prev_value[key])) { this.prev_value[key] = filtered; changed = true; } } - changed = true; } if (!changed) { @@ -114,7 +114,7 @@ class AntiPersistence { let dump = this.antietcd.etctree.dump(true); dump['term'] = this.antietcd.stored_term; - dump = JSON.stringify(dump); + dump = stableStringify(dump); dump = await new Promise((ok, no) => zlib.gzip(dump, (err, res) => err ? no(err) : ok(res))); const fh = await fsp.open(this.cfg.data+'.tmp', 'w'); await fh.writeFile(dump); @@ -124,7 +124,7 @@ class AntiPersistence } catch (e) { - console.error(e); + console.error('Error persisting data to disk: '+e); process.exit(1); } runCallbacks(this, 'wait_persist', null);