UNPKG

redis-smq

Version:

A high-performance, reliable, and scalable message queue for Node.js.

73 lines 3.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserStorageSortedSet = void 0; const redis_smq_common_1 = require("redis-smq-common"); const browser_storage_abstract_js_1 = require("./browser-storage-abstract.js"); const with_shared_pool_connection_js_1 = require("../../../common/redis/redis-connection-pool/with-shared-pool-connection.js"); class BrowserStorageSortedSet extends browser_storage_abstract_js_1.BrowserStorageAbstract { count(redisKey, cb) { this.logger.debug(`Counting items in ${redisKey}`); (0, with_shared_pool_connection_js_1.withSharedPoolConnection)((client, cb) => { this.logger.debug(`Executing ZCARD on ${redisKey}`); client.zcard(redisKey, (err, count) => { if (err) { this.logger.error(`ZCARD failed for ${redisKey}: ${err.message}`); cb(err); } else { this.logger.debug(`${redisKey} contains ${count} items`); cb(null, count); } }); }, cb); } fetchItems(redisKey, pageParams, cb) { const { offsetStart, offsetEnd } = pageParams; this.logger.debug(`Fetching items from ${redisKey} range [${offsetStart}:${offsetEnd}]`); (0, with_shared_pool_connection_js_1.withSharedPoolConnection)((client, cb) => { this.logger.debug(`Executing ZRANGE on ${redisKey} from ${offsetStart} to ${offsetEnd}`); client.zrange(redisKey, offsetStart, offsetEnd, (err, items) => { if (err) { this.logger.error(`ZRANGE failed for ${redisKey}: ${err.message}`); cb(err); } else { const itemCount = items ? items.length : 0; if (itemCount === 0) { this.logger.debug(`No items found in ${redisKey} range [${offsetStart}:${offsetEnd}]`); } else { this.logger.debug(`Retrieved ${itemCount} items from ${redisKey}`); } cb(null, items || []); } }); }, cb); } fetchAllItems(redisKey, cb) { this.logger.debug(`Fetching all items from ${redisKey} using ZSCAN`); (0, with_shared_pool_connection_js_1.withSharedPoolConnection)((client, cb) => { const allItems = []; let scanCount = 0; const scanRecursive = (cursor) => { scanCount++; this.logger.debug(`ZSCAN iteration ${scanCount} on ${redisKey}, cursor: ${cursor}`); redis_smq_common_1.async.withCallback((cb) => client.zscan(redisKey, cursor, { COUNT: 100 }, cb), (result, cb) => { const batchSize = result.items.length; allItems.push(...result.items); this.logger.debug(`ZSCAN batch #${scanCount} retrieved ${batchSize} items, total collected: ${allItems.length}`); if (result.cursor === '0') { this.logger.debug(`ZSCAN complete, collected ${allItems.length} items in ${scanCount} iterations`); return cb(null, allItems); } this.logger.debug(`ZSCAN continuing with cursor=${result.cursor}`); setImmediate(() => scanRecursive(result.cursor)); }, cb); }; this.logger.debug(`Starting ZSCAN with initial cursor=0`); scanRecursive('0'); }, cb); } } exports.BrowserStorageSortedSet = BrowserStorageSortedSet; //# sourceMappingURL=browser-storage-sorted-set.js.map