<commit_before>/*

No persistence or access control for now.

"Why not just index the objects by number?"

Because then you couldn't replace something by renaming the old one.

"Why are you preserving so much of the ColdMUD semantics?"

_"I have no idea..."_

*/

var EngineObject = require('engine-obj');

module.exports = function ObjectDB(worldGen, path) {
  if (!(this instanceof ObjectDB))
    return new ObjectDB(worldGen, path);

  if (!path) {
    path = worldGen;
    worldGen = undefined;
  }

  // EngineID -> EngineObject
  this.db = [];

  // lookup by name
  this.names = {};

  // Bootstrapping framework
  initMinimal(this);

  // this.initWorld(path, worldGen)
}

ObjectDB.prototype = {
  get: function (id) { return this.db[id] },

  lookup: function (name) {
    if (!this.names[name])
      throw "Object name not found";

    return this.names[name] 
  },

  create: function () {
    var id = this.db.length;
    var o = new EngineObject(this, id);

    this.db.push(o);

    return o;
  },

  destroy: function (id) {
    // Give the object a chance to clean itself up.
    try { this.db[id].sendMessage('dbDestroy'); }

    // Deleting hash elements is contra-indicated for performance reasons.
    this.db[id] = undefined;
  },

  addName: function (o, name) {
    if (this.names[name])
      throw "Name already in use";

    this.names[name] = o;
  },

  delName: function (name) {
    if (!this.names[name])
      throw "Name not found";

    this.names[name] = undefined;
  },
};

// Private functions below

function initMinimal (db) {
  require('minimal-db')(db);
};<commit_msg>Minor edit<commit_after>/*

No persistence or access control for now.

"Why not just index the objects by number?"

Because then you couldn't replace something by renaming the old one.

"Why are you preserving so much of the ColdMUD semantics?"

_"I have no idea..."_

*/

var EngineObject = require('engine-obj');

module.exports = function ObjectDB(worldGen, path) {
  if (!(this instanceof ObjectDB))
    return new ObjectDB(worldGen, path);

  if (!path) {
    path = worldGen;
    worldGen = undefined;
  }

  // EngineID -> EngineObject
  this.db = [];

  // lookup by name
  this.names = {};

  // Bootstrapping framework
  initMinimal(this);

  // this.initWorld(path, worldGen)
}

ObjectDB.prototype = {
  get: function (id) { return this.db[id] },

  lookup: function (name) {
    if (!this.names[name])
      throw "Object name not found";

    return this.names[name] 
  },

  create: function () {
    var id = this.db.length;
    var o = new EngineObject(this, id);

    this.db.push(o);

    return o;
  },

  destroy: function (id) {
    var o = this.db[id], names;

    if (o && o.names) {
      o.names.forEach(function (name) { this.delName(name) });

      // Give the object a chance to clean itself up.
      try {
        o.sendMessage('dbDestroy');
      } catch (e) {
        console.log("Error notifying object of destruction: \n", e);
      }
    }

    // Deleting hash elements is contra-indicated for performance reasons.
    this.db[id] = undefined;
  },

  addName: function (o, name) {
    if (this.names[name])
      throw "Name already in use";

    this.names[name] = o;
  },

  delName: function (name) {
    if (!this.names[name])
      throw "Name not found";

    this.names[name] = undefined;
  },
};

function initMinimal (db) {
  require('minimal-db')(db);
};
{'fraction_non_alphanumeric': 0.13690999706830842, 'fraction_numerical': 0.0, 'mean_word_length': 2.799554565701559, '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': '646002', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1084, 'n_words': 337}