<commit_before>var stream = require('stream');
var util = require('util');
var _ = require('underscore');
var Transform = stream.Transform || require('readable-stream').Transform;
var PropertiesReader = require('properties-reader');
var path = require('path');


var configuration = PropertiesReader(path.resolve(__dirname, '../', 'config', 'config.properties'));

function Cache(options) {

  if (!(this instanceof Cache)) {
    return new Cache(options);
  }

  this.array = [];
  this.limit = options.limit || 100;

  // init Transform
  Transform.call(this, options);

}
util.inherits(Cache, Transform);

Cache.prototype._write = function(chunk, enc, callback) {
  var data = JSON.parse(chunk);
  this.add(_.extend({
    ts: new Date(data['http://www.w3.org/ns/prov#generatedAtTime']).getTime()
  }, _.pick(data, ['http://www.w3.org/ns/prov#generatedAtTime', '@id', '@graph'])));

  callback();
};

Cache.prototype.add = function(element) {
  if (this.array.length === this.limit) {
    this.array.pop();
}  this.array.unshift(element);
};

Cache.prototype.find = function(ts) {

  var id = 'http://' + configuration.get('hostname');

  if (configuration.get('port')) {
    id += ':' + configuration.get('port');
  }

  id += configuration.get('path') + '/';

  id += ts;
  console.log(id);
  for (var i = this.array.length - 1; i >= 0; i--) {
    var e = this.array[i];

    console.log(e['@id']);
    console.log(id);

    if (e['@id'] === id) {
      return e;
    }
  }
  return null;
};

Cache.prototype.getAll = function() {

  var array = _.clone(this.array);

  /*if (array.length === 0) {
    return {};
  }*/

  var cache = {
    "@context": {
      "sld": "http://streamreasoning.org/ontologies/SLD4TripleWave#",
      "generatedAt": {
        "@id": "http://www.w3.org/ns/prov#generatedAtTime",
        "@type": "http://www.w3.org/2001/XMLSchema#dateTime"
      }
    },
    "@id": "tr:sGraph",
    "sld:contains": {
      "@list": []
    }
  };

  cache['sld:streamLocation'] = 'ws://' + configuration.get('hostname') + ':' + configuration.get('ws_port') + configuration.get('ws_stream_location');
  cache['sld:tBoxLocation'] = configuration.get('tbox_stream_location');

  for (var i = array.length - 1; i >= 0; i--) {
    var e = array[i];
    cache['sld:contains']["@list"].push({
      generatedAt: e['http://www.w3.org/ns/prov#generatedAtTime'],
      "@id": e["@id"]
    });
  }

  if (array.length > 0) {

    cache["sld:lastUpdated"] = array[0]['http://www.w3.org/ns/prov#generatedAtTime'];
  }

  return cache;
};

exports = module.exports = Cache;<commit_msg>workaround to fix the bug that prevented elements to be found in sgraph if port was not present<commit_after>var stream = require('stream');
var util = require('util');
var _ = require('underscore');
var Transform = stream.Transform || require('readable-stream').Transform;
var PropertiesReader = require('properties-reader');
var path = require('path');


var configuration = PropertiesReader(path.resolve(__dirname, '../', 'config', 'config.properties'));

function Cache(options) {

  if (!(this instanceof Cache)) {
    return new Cache(options);
  }

  this.array = [];
  this.limit = options.limit || 100;

  // init Transform
  Transform.call(this, options);

}
util.inherits(Cache, Transform);

Cache.prototype._write = function(chunk, enc, callback) {
  var data = JSON.parse(chunk);
  this.add(_.extend({
    ts: new Date(data['http://www.w3.org/ns/prov#generatedAtTime']).getTime()
  }, _.pick(data, ['http://www.w3.org/ns/prov#generatedAtTime', '@id', '@graph'])));

  callback();
};

Cache.prototype.add = function(element) {
  if (this.array.length === this.limit) {
    this.array.pop();
  }
  this.array.unshift(element);
};

Cache.prototype.find = function(ts) {

  var id = 'http://' + configuration.get('hostname');

  if (configuration.get('port')) {
    id += ':' + configuration.get('port');
  }

  id += configuration.get('path') + '/';

  id += ts;
  console.log(id);
  for (var i = this.array.length - 1; i >= 0; i--) {
    var e = this.array[i];

    console.log(e['@id']);
    console.log(id);

    if (e['@id'] === id) {
      return e;
    }
  }

  id = 'http://' + configuration.get('hostname');

  id += configuration.get('path') + '/';

  id += ts;
  for (var i = this.array.length - 1; i >= 0; i--) {
    var e = this.array[i];

    console.log(e['@id']);
    console.log(id);

    if (e['@id'] === id) {
      return e;
    }
  }
  return null;
};

Cache.prototype.getAll = function() {

  var array = _.clone(this.array);

  /*if (array.length === 0) {
    return {};
  }*/

  var cache = {
    "@context": {
      "sld": "http://streamreasoning.org/ontologies/SLD4TripleWave#",
      "generatedAt": {
        "@id": "http://www.w3.org/ns/prov#generatedAtTime",
        "@type": "http://www.w3.org/2001/XMLSchema#dateTime"
      }
    },
    "@id": "tr:sGraph",
    "sld:contains": {
      "@list": []
    }
  };

  cache['sld:streamLocation'] = 'ws://' + configuration.get('hostname') + ':' + configuration.get('ws_port') + configuration.get('ws_stream_location');
  cache['sld:tBoxLocation'] = configuration.get('tbox_stream_location');

  for (var i = array.length - 1; i >= 0; i--) {
    var e = array[i];
    cache['sld:contains']["@list"].push({
      generatedAt: e['http://www.w3.org/ns/prov#generatedAtTime'],
      "@id": e["@id"]
    });
  }

  if (array.length > 0) {

    cache["sld:lastUpdated"] = array[0]['http://www.w3.org/ns/prov#generatedAtTime'];
  }

  return cache;
};

exports = module.exports = Cache;
{'fraction_non_alphanumeric': 0.21078254597908402, 'fraction_numerical': 0.007933645870897944, 'mean_word_length': 3.330210772833724, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 12, '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': '2590267', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1904, 'n_words': 353}