<commit_before>'use strict';

/**
 * Caching server: protocol implementation.
 * (C) 2013 <NAME>.
 */


// requires
require('prototypes');
var memory = require('./memory.js');
var Log = require('log');
var testing = require('testing');


// globals
exports.SET_COMMANDS = {
	set: setCommand,
};
exports.GET_COMMANDS = {
	get: getCommand,
};
exports.DELETE_COMMANDS = {
	delete: deleteCommand,
};
var commands = {};
commands.overwriteWith(exports.SET_COMMANDS);
commands.overwriteWith(exports.GET_COMMANDS);
commands.overwriteWith(exports.DELETE_COMMANDS);
var log = new Log('info');


/**
 * Interpret a command sent over the wire. Params:
 *	- command: the command to interpret.
 *	- options: passed after the command.
 *	- data: optional contents after the line, as string.
 * Returns the response to a command as a string.
 */
exports.interpret = function(command, options, data)
{
	if (!(command in commands))
	{
		return 'ERROR';
	}
	var commandFunction = commands[command];
	return commandFunction(options, data);
};

/**
 * Command to set a value in the cache. Params:
 *	- options: passed after the command.
 *	- data: for the value.
 * Returns the response.
 */
function setCommand(options, data)
{
	if (memory.set(options.key, options.exptime, data))
	{
		return 'STORED';
	}
	return 'NOT_STORED';
}

/**
 * Command to get a value from the cache. Params:
 *	- options: passed after the command.
 * Returns the response.
 */
function getCommand(options)
{
	var value = memory.get(options.key);
	if (!value)
	{
		return 'END';
	}
	var data = value;
	if (typeof value != 'string')
	{
		data = JSON.stringify(value);
	}
	var bytes = data.length;
	var result = 'VALUE ' + options.key + ' 0 ' + bytes + '\r\n';
	return result + data + '\r\nEND';
}

/**
 * Command to delete a value from the cache. Params:
 *	- options: passed after the command.
 * Returns the response.
 */
function deleteCommand(options)
{
	if (memory.delete(options.key))
	{
		return 'DELETED';
	}
	return 'NOT_FOUND';
}

/**
 * Test to get, set and delete a test value.
 */
function testGetSetDelete(callback)
{
	var key = 'test#';
	var value = '{}';
	var options = {
		key: key,
	};
	var deleteResult = exports.interpret('delete', options);
	testing.assertEquals(deleteResult, 'NOT_FOUND', 'Could delete command before setting', callback);
	var getResult = exports.interpret('get', options);
	testing.assertEquals(getResult, 'END', 'Could get command before setting', callback);
	options.exptime = 0;
	var setResult = exports.interpret('set', options, value);
	testing.assertEquals(setResult, 'STORED', 'Could not set command', callback);
	getResult = exports.interpret('get', options);
	var expected = 'VALUE ' + key + ' 0 2\r\n{}\r\nEND';
	testing.assertEquals(getResult, expected, 'Could not get command after setting', callback);
	deleteResult = exports.interpret('delete', options);
	testing.assertEquals(deleteResult, 'DELETED', 'Could not delete command', callback);
	getResult = exports.interpret('get', options);
	testing.assertEquals(getResult, 'END', 'Could get command after deleting', callback);
	testing.success(callback);
}

/**
 * Run all tests.
 */
exports.test = function(callback)
{
	log.debug('Running tests');
	testing.run({
		getSet: testGetSetDelete,
	}, callback);
};

// run tests if invoked directly
if (__filename == process.argv[1])
{
	exports.test(testing.show);
}

<commit_msg>De-export everything in the protocol library.<commit_after>'use strict';

/**
 * Caching server: protocol implementation.
 * (C) 2013 <NAME>.
 */


// requires
require('prototypes');
var memory = require('./memory.js');
var Log = require('log');
var testing = require('testing');


// globals
var SET_COMMANDS = {
	set: setCommand,
};
var GET_COMMANDS = {
	get: getCommand,
};
var DELETE_COMMANDS = {
	delete: deleteCommand,
};
var commands = {};
commands.overwriteWith(SET_COMMANDS);
commands.overwriteWith(GET_COMMANDS);
commands.overwriteWith(DELETE_COMMANDS);
var log = new Log('info');


/**
 * Interpret a command sent over the wire. Params:
 *	- command: the command to interpret.
 *	- options: passed after the command.
 *	- data: optional contents after the line, as string.
 * Returns the response to a command as a string.
 */
function interpret(command, options, data)
{
	if (!(command in commands))
	{
		return 'ERROR';
	}
	var commandFunction = commands[command];
	return commandFunction(options, data);
}

/**
 * Command to set a value in the cache. Params:
 *	- options: passed after the command.
 *	- data: for the value.
 * Returns the response.
 */
function setCommand(options, data)
{
	if (memory.set(options.key, options.exptime, data))
	{
		return 'STORED';
	}
	return 'NOT_STORED';
}

/**
 * Command to get a value from the cache. Params:
 *	- options: passed after the command.
 * Returns the response.
 */
function getCommand(options)
{
	var value = memory.get(options.key);
	if (!value)
	{
		return 'END';
	}
	var data = value;
	if (typeof value != 'string')
	{
		data = JSON.stringify(value);
	}
	var bytes = data.length;
	var result = 'VALUE ' + options.key + ' 0 ' + bytes + '\r\n';
	return result + data + '\r\nEND';
}

/**
 * Command to delete a value from the cache. Params:
 *	- options: passed after the command.
 * Returns the response.
 */
function deleteCommand(options)
{
	if (memory.delete(options.key))
	{
		return 'DELETED';
	}
	return 'NOT_FOUND';
}

/**
 * Test to interpret a few commands.
 */
function testInterpretCommands(callback)
{
	var key = 'test#';
	var value = '{}';
	var options = {
		key: key,
	};
	var deleteResult = interpret('delete', options);
	testing.assertEquals(deleteResult, 'NOT_FOUND', 'Could delete command before setting', callback);
	var getResult = interpret('get', options);
	testing.assertEquals(getResult, 'END', 'Could get command before setting', callback);
	options.exptime = 0;
	var setResult = interpret('set', options, value);
	testing.assertEquals(setResult, 'STORED', 'Could not set command', callback);
	getResult = interpret('get', options);
	var expected = 'VALUE ' + key + ' 0 2\r\n{}\r\nEND';
	testing.assertEquals(getResult, expected, 'Could not get command after setting', callback);
	deleteResult = interpret('delete', options);
	testing.assertEquals(deleteResult, 'DELETED', 'Could not delete command', callback);
	getResult = interpret('get', options);
	testing.assertEquals(getResult, 'END', 'Could get command after deleting', callback);
	testing.success(callback);
}

/**
 * Run all tests.
 */
exports.test = function(callback)
{
	log.debug('Running tests');
	testing.run({
		interpret: testInterpretCommands,
	}, callback);
};

// run tests if invoked directly
if (__filename == process.argv[1])
{
	exports.test(testing.show);
}

{'fraction_non_alphanumeric': 0.1464141941255405, 'fraction_numerical': 0.0026837632324437154, 'mean_word_length': 4.311163895486936, 'pattern_counts': {'":': 0, '<': 5, '<?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': '865115', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2110, 'n_words': 633}