<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
var redis = require("redis");
var async = require('async');

function RedisStorage(options, settings) {
  this._settings = settings;
  this._client = redis.createClient(
    options.port,
    options.host,
    options.options
  );
  if (options.db) {
    this._client.select(options.db);
  }
}

RedisStorage.prototype = {
  addUserSimplePushURL: function(userMac, simplepushURL, callback) {
    // delete the SP url if it exists
    var self = this;
    self._client.lrem('spurl.' + userMac, 0, simplepushURL,
      function(err, deleted) {
        if (err) {
          callback(err);
          return;
        }
        // And add it back.
        self._client.lpush('spurl.' + userMac, simplepushURL,
          function(err, size) {
            // Keep the X most recent URLs.
            if (size > self._settings.maxSimplePushUrls) {
              self._client.ltrim(
                'spurl.' + userMac,
                0,
                self._settings.maxSimplePushUrls - 1,
                callback);
            } else {
              callback(null);
            }
          });
      });
  },

  getUserSimplePushURLs: function(userMac, callback) {
    this._client.lrange('spurl.' + userMac,
      0, this._settings.maxSimplePushUrls, callback);
  },

  removeSimplePushURL: function(userMac, simplepushURL, callback) {
    this._client.lrem('spurl.' + userMac, 0, simplepushURL, callback);
  },

  addUserCallUrlData: function(userMac, callUrlId, urlData, callback) {
    if (userMac === undefined) {
      callback(new Error("userMac should be defined."));
      return;
    } else if (urlData.timestamp === undefined) {
      callback(new Error("urlData should have a timestamp property."));
      return;
    }
    var self = this;
    // In that case use setex to add the metadata of the url.
    this._client.setex(
      'callurl.' + callUrlId,
      urlData.expires - urlData.timestamp,
      JSON.stringify(urlData),
      function(err) {
        if (err) {
          callback(err);
          return;
        }
        self._client.sadd(
          'userUrls.' + userMac,
          'callurl.' + callUrlId, callback
        );
      });
  },

  /**
   * Update a call url data.
   *
   * If the call-url doesn't belong to the given user, returns an
   * authentication error.
   **/
  updateUserCallUrlData: function(userMac, callUrlId, newData, callback) {
    var self = this;
    self._client.sismember(
      'userUrls.' + userMac,
      'callurl.' + callUrlId,
      function(err, res) {
        if (err){
          callback(err);
          return;
        }
        if (res === 0) {
          var error = new Error("Doesn't exist");
          error.notFound = true;
          callback(error);
          return;
        }
        // Get and update the existing data.
        self.getCallUrlData(callUrlId, function(err, data) {
          if (err) {
            callback(err);
            return;
          }
          Object.keys(newData).forEach(function(key) {
            data[key] = newData[key];
          });
          self._client.setex(
            'callurl.' + callUrlId,
            data.expires - data.timestamp,
            JSON.stringify(data),
            callback<commit_msg>Don't trust the timestamp value to set the expiration timer.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
var redis = require("redis");
var async = require('async');

function RedisStorage(options, settings) {
  this._settings = settings;
  this._client = redis.createClient(
    options.port,
    options.host,
    options.options
  );
  if (options.db) {
    this._client.select(options.db);
  }
}

RedisStorage.prototype = {
  addUserSimplePushURL: function(userMac, simplepushURL, callback) {
    // delete the SP url if it exists
    var self = this;
    self._client.lrem('spurl.' + userMac, 0, simplepushURL,
      function(err, deleted) {
        if (err) {
          callback(err);
          return;
        }
        // And add it back.
        self._client.lpush('spurl.' + userMac, simplepushURL,
          function(err, size) {
            // Keep the X most recent URLs.
            if (size > self._settings.maxSimplePushUrls) {
              self._client.ltrim(
                'spurl.' + userMac,
                0,
                self._settings.maxSimplePushUrls - 1,
                callback);
            } else {
              callback(null);
            }
          });
      });
  },

  getUserSimplePushURLs: function(userMac, callback) {
    this._client.lrange('spurl.' + userMac,
      0, this._settings.maxSimplePushUrls, callback);
  },

  removeSimplePushURL: function(userMac, simplepushURL, callback) {
    this._client.lrem('spurl.' + userMac, 0, simplepushURL, callback);
  },

  addUserCallUrlData: function(userMac, callUrlId, urlData, callback) {
    if (userMac === undefined) {
      callback(new Error("userMac should be defined."));
      return;
    } else if (urlData.timestamp === undefined) {
      callback(new Error("urlData should have a timestamp property."));
      return;
    }
    var self = this;
    // In that case use setex to add the metadata of the url.
    this._client.setex(
      'callurl.' + callUrlId,
      urlData.expires - parseInt(Date.now() / 1000),
      JSON.stringify(urlData),
      function(err) {
        if (err) {
          callback(err);
          return;
        }
        self._client.sadd(
          'userUrls.' + userMac,
          'callurl.' + callUrlId, callback
        );
      });
  },

  /**
   * Update a call url data.
   *
   * If the call-url doesn't belong to the given user, returns an
   * authentication error.
   **/
  updateUserCallUrlData: function(userMac, callUrlId, newData, callback) {
    var self = this;
    self._client.sismember(
      'userUrls.' + userMac,
      'callurl.' + callUrlId,
      function(err, res) {
        if (err){
          callback(err);
          return;
        }
        if (res === 0) {
          var error = new Error("Doesn't exist");
          error.notFound = true;
          callback(error);
          return;
        }
        // Get and update the existing data.
        self.getCallUrlData(callUrlId, function(err, data) {
          if (err) {
            callback(err);
            return;
          }
          Object.keys(newData).forEach(function(key) {
            data[key] = newData[key];
          });
          self._client.setex(
            'callurl.' + callUrlId,
            data.expires - parseInt(Date.now() / 1000),
            JSON.stringify(data),
            callback
{'fraction_non_alphanumeric': 0.11975875933371626, 'fraction_numerical': 0.00402067777139575, 'mean_word_length': 1.950021177467175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6920815', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2047, 'n_words': 520}