Created
April 2, 2014 17:40
-
-
Save anonymous/9939114 to your computer and use it in GitHub Desktop.
Blob compressor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Utility for compressing/decompressing blobs in/out of redis | |
*/ | |
function BlobCompressor(){ | |
this.compressData = function(data, callback){ | |
if(!data){ | |
return callback(false, null); | |
} | |
try{ | |
//GameLog.warn("Compressing data " + data); | |
var startTime = new Date().getTime(); | |
if(ServiceLocator.Constants.COMPRESS_BLOBS){ | |
ServiceLocator.Zlib.deflate(data, function (gzErr, result){ | |
if(gzErr){ | |
GameLog.warn("Caught an error deflating data " + gzErr); | |
return callback(true, null); | |
} | |
var buff = result.toString("binary"); | |
var savings = Math.floor(100 * (1 - (buff.length / data.length))); | |
var duration = new Date().getTime() - startTime; | |
//GameLog.warn("Compressed by " + savings + "% in " + duration + " MS"); | |
return callback(false, buff); | |
}); | |
} else{ | |
return callback(false, data); | |
} | |
}catch(err){ | |
GameLog.warn("Caught an exception deflating data " + err.message); | |
return callback(true, null); | |
} | |
} | |
this.compressAll = function(dataSet, callback){ | |
this.compressAllImpl(dataSet, Object.keys(dataSet), 0, 0, {}, callback); | |
} | |
this.compressAllImpl = function(dataSet, keys, index, numErrorsEncountered, results, callback){ | |
var that = this; | |
if(dataSet == null || index >= keys.length){ | |
return callback(numErrorsEncountered > 0, results); | |
} | |
var thisKey = keys[index]; | |
this.compressData(dataSet[thisKey], function(deflateError, compressed){ | |
if(deflateError){ | |
numErrorsEncountered++; | |
} else{ | |
results[thisKey] = compressed; | |
} | |
that.compressAllImpl(dataSet, keys, index+1, numErrorsEncountered, results, callback); | |
}); | |
} | |
this.decompressAll = function(dataSet, callback) { | |
this.decompressAllImpl(dataSet, Object.keys(dataSet), 0, 0, {}, callback); | |
} | |
this.decompressAllImpl = function(dataSet, keys, index, numErrorsEncountered, results, callback){ | |
var that = this; | |
if(dataSet == null || index >= keys.length){ | |
return callback(numErrorsEncountered > 0, results); | |
} | |
var thisKey = keys[index]; | |
this.decompressData(dataSet[thisKey], function(inflateError, decompressed){ | |
if(inflateError){ | |
numErrorsEncountered++; | |
} | |
results[thisKey] = decompressed; | |
that.decompressAllImpl(dataSet, keys, index+1, numErrorsEncountered, results, callback); | |
}); | |
} | |
this.decompressData = function(data, callback) { | |
if(!data){ | |
return callback(false, null); | |
} | |
try{ | |
//GameLog.warn("Inflating data " + data); | |
var buffData = new Buffer(data, "binary"); | |
ServiceLocator.Zlib.inflate(buffData, function (gzErr, uncompressed){ | |
if(gzErr){ | |
if(ServiceLocator.Constants.COMPRESS_BLOBS){ | |
//GameLog.warn("Caught an error inflating data " + gzErr); | |
} | |
return callback(true, data); | |
} | |
return callback(false, uncompressed); | |
}); | |
}catch(err){ | |
GameLog.warn("Caught an exception inflating data " + err.message); | |
return callback(true, data); | |
} | |
} | |
} | |
exports.instance = new BlobCompressor(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment