Mosca lib/persistence/levelup.js

    Description

    Copyright (c) 2013-2014 Matteo Collina, http://matteocollina.com

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

    Source

    "use strict";
    
    var levelup = require("levelup");
    var sublevel = require("level-sublevel");
    var AbstractPersistence = require("./abstract");
    var JSONB = require("json-buffer");
    var util = require("util");
    var ttl = require('level-ttl');
    var Matcher = require("./matcher");
    var async = require("async");
    var extend = require("extend");
    var defaults = {
      ttl: {
        // TTL for subscriptions is 1 hour
        subscriptions: 60 * 60 * 1000,
    
        // TTL for packets is 1 hour
        packets: 60 * 60 * 1000,
    
        // the checkFrequency is 1 minute
        checkFrequency: 60 * 1000
      }
    };

    LevelUpPersistence

    function
    LevelUpPersistence()
    • @param: {Object}optionsThe options to create this persistance
    • @param: {Function}callbackCalled when ready.

    Description

    A LevelUp-based persistance.

    The current options include:
    - path, the path to the database
    - ttl, an object containing three values:
    * checkFrequency, the frequency at which the
    the expiration will be checked. It defaults to 1 minute.
    * subscriptions, the time (ms) after which subscriptions
    will expire. It defaults to 1 hour.
    * packets, the time (ms) after which packets will expire.
    It defaults to 1 hour.
    - db, the AbstractLevelDown implementation.
    - all other levelup otions.

    Source

    function LevelUpPersistence(options, callback) {
      if (!(this instanceof LevelUpPersistence)) {
        return new LevelUpPersistence(options, callback);
      }
    
      this.options = extend(true, {}, defaults, options);
      this.options.valueEncoding = {
        encode : function (val) { return JSONB.stringify(val); },
        decode : function (val) { return JSONB.parse(val); },
        buffer : false,
        type   : "JSON-Buffers"
      };
    
      this.db = ttl(levelup(this.options.path, this.options), options.ttl);
      this._retained = this.db.sublevel("retained");
      this._clientSubscriptions = this.db.sublevel("clientSubscriptions");
      this._subscriptions = this.db.sublevel("subscriptions");
      this._offlinePackets = this.db.sublevel("offlinePackets");
      this._subMatcher = new Matcher();
      this._packetCounter = 0;
      this._lastStoredPacketTime = Date.now();
    
      var that = this;
      var stream = this._subscriptions.createReadStream();
      stream.on("data", function(data) {
        that._subMatcher.add(data.value.topic, data.key);
      });
      stream.on("end", function() {
        if (callback) {
          callback(null, that);
        }
      });
    }
    
    util.inherits(LevelUpPersistence, AbstractPersistence);

    exports

    property
    module.exports

      Description

      Export it as a module

      Source

      module.exports = LevelUpPersistence;