diff --git a/lib/persistence.js b/lib/persistence.js index 5ee5181..e2e32b0 100644 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -102,6 +102,30 @@ Persistence.prototype.persistCachedDatabase = function (cb) { }; +/** + * Set automatic compaction every interval ms + * @param {Number} interval in milliseconds, with an enforced minimum of 5 seconds + */ +Persistence.prototype.setAutocompactionInterval = function (interval) { + var self = this; + + if (interval < 5000) { interval = 5000; } + this.stopAutocompaction(); + + this.autocompactionIntervalId = setInterval(function () { + self.db.executor.push({ this: self, fn: self.persistCachedDatabase, arguments: [] }); + }, interval); +}; + + +/** + * Stop autocompaction (do nothing if autocompaction was not running) + */ +Persistence.prototype.stopAutocompaction = function () { + if (this.autocompactionIntervalId) { clearInterval(this.autocompactionIntervalId); } +}; + + /** * Persist new state for the given newDocs (can be insertion, update or removal) * Use an append-only format