mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-27 17:16:14 +01:00 
			
		
		
		
	fix: redis tests
This commit is contained in:
		| @@ -17,3 +17,11 @@ helpers.resultsToBool = function (results) { | |||||||
| 	} | 	} | ||||||
| 	return results; | 	return results; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | helpers.zsetToObjectArray = function (data) { | ||||||
|  | 	const objects = new Array(data.length / 2); | ||||||
|  | 	for (let i = 0, k = 0; i < objects.length; i += 1, k += 2) { | ||||||
|  | 		objects[i] = { value: data[k], score: parseFloat(data[k + 1]) }; | ||||||
|  | 	} | ||||||
|  | 	return objects; | ||||||
|  | }; | ||||||
|   | |||||||
| @@ -1,9 +1,8 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| module.exports = function (module) { | module.exports = function (module) { | ||||||
| 	var _ = require('lodash'); | 	const utils = require('../../utils'); | ||||||
| 	var utils = require('../../utils'); | 	const helpers = require('./helpers'); | ||||||
| 	var helpers = require('./helpers'); |  | ||||||
|  |  | ||||||
| 	require('./sorted/add')(module); | 	require('./sorted/add')(module); | ||||||
| 	require('./sorted/remove')(module); | 	require('./sorted/remove')(module); | ||||||
| @@ -27,27 +26,40 @@ module.exports = function (module) { | |||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	async function sortedSetRange(method, key, start, stop, withScores) { | 	async function sortedSetRange(method, key, start, stop, withScores) { | ||||||
|  | 		function getFirst(mapped) { | ||||||
|  | 			let selectedArray = mapped[0]; | ||||||
|  | 			for (let i = 1; i < mapped.length; i++) { | ||||||
|  | 				if (mapped[i].length && ( | ||||||
|  | 					!selectedArray.length || | ||||||
|  | 					(method === 'zrange' && mapped[i][0].score < selectedArray[0].score) || | ||||||
|  | 					(method === 'zrevrange' && mapped[i][0].score > selectedArray[0].score) | ||||||
|  | 				)) { | ||||||
|  | 					selectedArray = mapped[i]; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			return selectedArray.length ? selectedArray.shift() : null; | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		if (Array.isArray(key)) { | 		if (Array.isArray(key)) { | ||||||
| 			if (!key.length) { | 			if (!key.length) { | ||||||
| 				return []; | 				return []; | ||||||
| 			} | 			} | ||||||
| 			const batch = module.client.batch(); | 			const batch = module.client.batch(); | ||||||
| 			key.forEach((key) => { | 			key.forEach(key => batch[method]([key, start, stop, 'WITHSCORES'])); | ||||||
| 				batch[method]([key, start, stop, 'WITHSCORES']); | 			const data = await helpers.execBatch(batch); | ||||||
| 			}); |  | ||||||
| 			let data = await helpers.execBatch(batch); |  | ||||||
| 			data = _.flatten(data); |  | ||||||
| 			let objects = []; |  | ||||||
| 			for (let i = 0; i < data.length; i += 2) { |  | ||||||
| 				objects.push({ value: data[i], score: parseFloat(data[i + 1]) }); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			objects.sort((a, b) => { | 			const mapped = data.map(setData => helpers.zsetToObjectArray(setData)); | ||||||
| 				if (method === 'zrange') { |  | ||||||
| 					return a.score - b.score; | 			let objects = []; | ||||||
|  | 			const count = stop - start + 1; | ||||||
|  | 			let item = null; | ||||||
|  | 			do { | ||||||
|  | 				item = getFirst(mapped); | ||||||
|  | 				if (item) { | ||||||
|  | 					objects.push(item); | ||||||
| 				} | 				} | ||||||
| 				return b.score - a.score; | 			} while (item && (objects.length < count || stop === -1)); | ||||||
| 			}); |  | ||||||
| 			if (!withScores) { | 			if (!withScores) { | ||||||
| 				objects = objects.map(item => item.value); | 				objects = objects.map(item => item.value); | ||||||
| 			} | 			} | ||||||
| @@ -63,10 +75,7 @@ module.exports = function (module) { | |||||||
| 		if (!withScores) { | 		if (!withScores) { | ||||||
| 			return data; | 			return data; | ||||||
| 		} | 		} | ||||||
| 		const objects = []; | 		const objects = helpers.zsetToObjectArray(data); | ||||||
| 		for (var i = 0; i < data.length; i += 2) { |  | ||||||
| 			objects.push({ value: data[i], score: parseFloat(data[i + 1]) }); |  | ||||||
| 		} |  | ||||||
| 		return objects; | 		return objects; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -88,11 +97,7 @@ module.exports = function (module) { | |||||||
|  |  | ||||||
| 	async function sortedSetRangeByScoreWithScores(method, key, start, count, min, max) { | 	async function sortedSetRangeByScoreWithScores(method, key, start, count, min, max) { | ||||||
| 		const data = await module.client.async[method]([key, min, max, 'WITHSCORES', 'LIMIT', start, count]); | 		const data = await module.client.async[method]([key, min, max, 'WITHSCORES', 'LIMIT', start, count]); | ||||||
| 		const objects = []; | 		return helpers.zsetToObjectArray(data); | ||||||
| 		for (var i = 0; i < data.length; i += 2) { |  | ||||||
| 			objects.push({ value: data[i], score: parseFloat(data[i + 1]) }); |  | ||||||
| 		} |  | ||||||
| 		return objects; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	module.sortedSetCount = async function (key, min, max) { | 	module.sortedSetCount = async function (key, min, max) { | ||||||
|   | |||||||
| @@ -61,10 +61,6 @@ module.exports = function (module) { | |||||||
| 			return results ? results[1] : null; | 			return results ? results[1] : null; | ||||||
| 		} | 		} | ||||||
| 		results = results[1] || []; | 		results = results[1] || []; | ||||||
| 		var objects = []; | 		return helpers.zsetToObjectArray(results); | ||||||
| 		for (var i = 0; i < results.length; i += 2) { |  | ||||||
| 			objects.push({ value: results[i], score: parseFloat(results[i + 1]) }); |  | ||||||
| 		} |  | ||||||
| 		return objects; |  | ||||||
| 	} | 	} | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -47,10 +47,6 @@ module.exports = function (module) { | |||||||
| 			return results ? results[1] : null; | 			return results ? results[1] : null; | ||||||
| 		} | 		} | ||||||
| 		results = results[1] || []; | 		results = results[1] || []; | ||||||
| 		var objects = []; | 		return helpers.zsetToObjectArray(results); | ||||||
| 		for (var i = 0; i < results.length; i += 2) { |  | ||||||
| 			objects.push({ value: results[i], score: parseFloat(results[i + 1]) }); |  | ||||||
| 		} |  | ||||||
| 		return objects; |  | ||||||
| 	}; | 	}; | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user