简体   繁体   中英

redis bitset — how to upload an existing bitset array

I have huge data of bitset, stored in db. I want to upload the same to redis bitset, so I can perform bit operations on it. Is there a way to upload this data from either redis-cli or javascript code? I am using bitset.js npm module to load the bitset in my program from db.

One obvious way is to iterate my bitset array within my javascript code and keep calling redis.setbit(...) multiple times. Is there a way to upload all of them at once? If so how?

A bitset in Redis is actually just a string, so you can assign to it directly all at once. The bits in the string are the bits of the bitfield, set in left-to-right order. Ie setting bit number 0 to 1 yields the binary number 10000000 , or a single byte with the value 128. This looks like "\\x80" when Redis prints it, which you can see for yourself by running setbit foo 0 1 and then get foo in Redis.

So to construct the right string to send to Redis, we just need to read the bits out of your BitSet and construct a buffer, one byte at a time, with the appropriate bits set.

Below is code that uses bitset.js and the redis npm module to transfer a BitSet in JavaScript into a Redis key. Note that this code assumes that the bitfield fits comfortably in memory.

let redis = require('redis'),
    BitSet = require('./bitset');

let client = redis.createClient();

// create some data
let bs = new BitSet;    
bs.set(0, 1);
bs.set(31, 1);

// calculate how many bytes we'll need
var numBytes = Math.ceil(bs.msb()/8);
// construct a buffer with that much space
var buffer = new Buffer(numBytes);

// for each byte    
for (var i = 0; i < numBytes; i++) {
    var byte = 0;

    // iterate over each bit
    for (var j = 0; j < 8; j++) {
        // slide previous bits to the left
        byte <<= 1;
        // and set the rightmost bit
        byte |= bs.get(i*8+j);
    }

    // put this byte in the buffer
    buffer[i] = byte;
}

// now we have a complete buffer to use as our value in Redis    
client.set('bitset', buffer, function (err, result) {
    client.getbit('bitset', 31, function (err, result) {
        console.log('Bit 31 = ' + result);
        client.del('bitset', function () {
            client.quit();
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM