<commit_before>/**
 * Persistence layer, manages loading and saving game objects.
 * A lean persistence back-end module takes care of the actual
 * interaction with a specific storage facility (e.g. files on disk or
 * a database). It must implement the following API:
 * ```
 *     function init()
 *     function read(tsid)
 *     function write(obj, callback)
 *     function del(tsid)
 * ```
 *
 * Once loaded, game objects are kept in a cache data structure here,
 * to avoid having to reload them from the back-end for each access.
 * Game logic functions do not need to take care of saving modified
 * objects explicitly; this happens automatically at the end of each
 * request processed through {@link module:data/requestContext~run|
 * requestContext.run}, with the help of the {@link
 * module:data/persProxy|persProxy} wrapper.
 *
 * @module
 */

// public interface
module.exports = {
	init: init,
	get: get,
	add: add,
	processDirtyList: processDirtyList,
};


var gsjsBridge = require('model/gsjsBridge');
var orProxy = require('data/objrefProxy');
var persProxy = require('data/persProxy');
var rpc = require('data/rpc');
var reqContext = require('data/requestContext');


// live game object cache
var cache = {};

// persistence back-end
var pbe = null;


/**
 * (Re-)initializes the persistence layer by clearing the live object
 * cache and setting the supplied persistence back-end.
 *
 * @param {object} backEnd persistence back-end module; must implement
 * the API shown in the above module docs.
 */
function init(backEnd) {
	cache = {};
	pbe = backEnd;
	if (pbe && typeof pbe.init === 'function') {
		pbe.init();
	}
}


/**
 * Loads the game object with the given TSID from persistence.
 * Depending on whether the GS is "responsible" for this object, it
 * will be wrapped either in a {@link module:data/persProxy|persProxy}
 * or {@link module:data/rpcProxy|rpcProxy}.
 *
 * @param {string} tsid TSID of the object to load
 * @returns {GameObject} the requested object, undefined if not found
 */
function load(tsid) {
	log.debug('pers.load: %s', tsid);
	try {
		data = pbe.read(tsid);
	}
	catch (e) {
		log.error(e, 'could not load %s from persistence', tsid);
		return;
	}
	orProxy.proxify(data);<commit_msg>clearer error in case of missing persistence back-end<commit_after>/**
 * Persistence layer, manages loading and saving game objects.
 * A lean persistence back-end module takes care of the actual
 * interaction with a specific storage facility (e.g. files on disk or
 * a database). It must implement the following API:
 * ```
 *     function init()
 *     function read(tsid)
 *     function write(obj, callback)
 *     function del(tsid)
 * ```
 *
 * Once loaded, game objects are kept in a cache data structure here,
 * to avoid having to reload them from the back-end for each access.
 * Game logic functions do not need to take care of saving modified
 * objects explicitly; this happens automatically at the end of each
 * request processed through {@link module:data/requestContext~run|
 * requestContext.run}, with the help of the {@link
 * module:data/persProxy|persProxy} wrapper.
 *
 * @module
 */

// public interface
module.exports = {
	init: init,
	get: get,
	add: add,
	processDirtyList: processDirtyList,
};


var gsjsBridge = require('model/gsjsBridge');
var orProxy = require('data/objrefProxy');
var persProxy = require('data/persProxy');
var rpc = require('data/rpc');
var reqContext = require('data/requestContext');


// live game object cache
var cache = {};

// persistence back-end
var pbe = null;


/**
 * (Re-)initializes the persistence layer by clearing the live object
 * cache and setting the supplied persistence back-end.
 *
 * @param {object} backEnd persistence back-end module; must implement
 * the API shown in the above module docs.
 */
function init(backEnd) {
	cache = {};
	pbe = backEnd;
	if (pbe && typeof pbe.init === 'function') {
		pbe.init();
	}
}


/**
 * Loads the game object with the given TSID from persistence.
 * Depending on whether the GS is "responsible" for this object, it
 * will be wrapped either in a {@link module:data/persProxy|persProxy}
 * or {@link module:data/rpcProxy|rpcProxy}.
 *
 * @param {string} tsid TSID of the object to load
 * @returns {GameObject} the requested object, undefined if not found
 */
function load(tsid) {
	assert(pbe, 'persistence back-end not set');
	log.debug('pers.load: %s', tsid);
	try {
		data = pbe.read(tsid);
	}
	catch (e) {
		log.error(e, 'could not load %s from persistence', tsid);
		return;
	}
	orProxy.proxify(data);
{'fraction_non_alphanumeric': 0.11398509425690487, 'fraction_numerical': 0.0, 'mean_word_length': 3.938311688311688, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7510529', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1344, 'n_words': 527}