all files / src/init/ ve.init.ListStorage.js

100% Statements 30/30
100% Branches 4/4
100% Functions 10/10
100% Lines 30/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                10×           47×           23×           44×                                                           27×                         13×       13× 16×   13×               10×     10×   10×    
/**
 * Efficient append-only list storage based on a ve.init.SafeStorage instance
 *
 * @class ve.init.ListStorage
 *
 * @constructor
 * @param {ve.init.SafeStorage} storage The SafeStorage instance to wrap around
 */
ve.init.ListStorage = function ( storage ) {
	this.storage = storage;
};
 
/**
 * @inheritdoc ve.init.SafeStorage
 */
ve.init.ListStorage.prototype.get = function ( key ) {
	return this.storage.get( key );
};
 
/**
 * @inheritdoc ve.init.SafeStorage
 */
ve.init.ListStorage.prototype.set = function ( key, value ) {
	return this.storage.set( key, value );
};
 
/**
 * @inheritdoc ve.init.SafeStorage
 */
ve.init.ListStorage.prototype.remove = function ( key ) {
	return this.storage.remove( key );
};
 
/**
 * @inheritdoc ve.init.SafeStorage
 */
ve.init.ListStorage.prototype.getObject = function ( key ) {
	return this.storage.getObject( key );
};
 
/**
 * @inheritdoc ve.init.SafeStorage
 */
ve.init.ListStorage.prototype.setObject = function ( key, value ) {
	return this.storage.setObject( key, value );
};
 
/**
 * Append a value to a list stored in storage
 *
 * @param {string} key Key of list to set value for
 * @param {string} value Value to set
 * @return {boolean} The value was set
 */
ve.init.ListStorage.prototype.appendToList = function ( key, value ) {
	var length = this.getListLength( key );
 
	if ( this.set( key + '__' + length, value ) ) {
		length++;
		return this.set( key + '__length', length.toString() );
	}
	return false;
};
 
/**
 * Get the length of a list in storage
 *
 * @param {string} key Key of list
 * @return {number} List length, 0 if the list doesn't exist
 */
ve.init.ListStorage.prototype.getListLength = function ( key ) {
	return +this.get( key + '__length' ) || 0;
};
 
/**
 * Get a list stored in storage
 *
 * Internally this will use items with the keys:
 *  - key__length
 *  - key__0 … key__N
 *
 * @param {string} key Key of list
 * @return {string[]} List
 */
ve.init.ListStorage.prototype.getList = function ( key ) {
	var i,
		list = [],
		length = this.getListLength( key );
 
	for ( i = 0; i < length; i++ ) {
		list.push( this.get( key + '__' + i ) );
	}
	return list;
};
 
/**
 * Remove a list stored in storage
 *
 * @param {string} key Key of list
 */
ve.init.ListStorage.prototype.removeList = function ( key ) {
	var i,
		length = this.getListLength( key );
 
	for ( i = 0; i < length; i++ ) {
		this.remove( key + '__' + i );
	}
	this.remove( key + '__length' );
};