2012-11-08 12:25:23 -05:00
|
|
|
/* Bit operations.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-07-26 09:14:57 -04:00
|
|
|
#include "server.h"
|
2012-05-19 04:33:20 -04:00
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
2012-05-19 10:16:25 -04:00
|
|
|
* Helpers and low level bit functions.
|
2012-05-19 04:33:20 -04:00
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
2012-05-19 10:16:25 -04:00
|
|
|
/* Count number of bits set in the binary array pointed by 's' and long
|
|
|
|
* 'count' bytes. The implementation of this function is required to
|
|
|
|
* work with a input string length up to 512 MB. */
|
2013-06-26 09:19:06 -04:00
|
|
|
size_t redisPopcount(void *s, long count) {
|
2013-05-08 03:59:51 -04:00
|
|
|
size_t bits = 0;
|
2014-02-27 03:46:20 -05:00
|
|
|
unsigned char *p = s;
|
|
|
|
uint32_t *p4;
|
2012-05-19 10:16:25 -04:00
|
|
|
static const unsigned char bitsinbyte[256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
|
|
|
|
|
2014-02-27 03:46:20 -05:00
|
|
|
/* Count initial bytes not aligned to 32 bit. */
|
|
|
|
while((unsigned long)p & 3 && count) {
|
|
|
|
bits += bitsinbyte[*p++];
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
2014-12-02 01:46:15 -05:00
|
|
|
/* Count bits 28 bytes at a time */
|
2014-02-27 03:46:20 -05:00
|
|
|
p4 = (uint32_t*)p;
|
2014-12-02 01:46:15 -05:00
|
|
|
while(count>=28) {
|
|
|
|
uint32_t aux1, aux2, aux3, aux4, aux5, aux6, aux7;
|
2012-05-20 15:34:58 -04:00
|
|
|
|
|
|
|
aux1 = *p4++;
|
|
|
|
aux2 = *p4++;
|
|
|
|
aux3 = *p4++;
|
|
|
|
aux4 = *p4++;
|
2014-12-02 01:46:15 -05:00
|
|
|
aux5 = *p4++;
|
|
|
|
aux6 = *p4++;
|
|
|
|
aux7 = *p4++;
|
|
|
|
count -= 28;
|
2012-05-20 15:34:58 -04:00
|
|
|
|
|
|
|
aux1 = aux1 - ((aux1 >> 1) & 0x55555555);
|
|
|
|
aux1 = (aux1 & 0x33333333) + ((aux1 >> 2) & 0x33333333);
|
|
|
|
aux2 = aux2 - ((aux2 >> 1) & 0x55555555);
|
|
|
|
aux2 = (aux2 & 0x33333333) + ((aux2 >> 2) & 0x33333333);
|
|
|
|
aux3 = aux3 - ((aux3 >> 1) & 0x55555555);
|
|
|
|
aux3 = (aux3 & 0x33333333) + ((aux3 >> 2) & 0x33333333);
|
|
|
|
aux4 = aux4 - ((aux4 >> 1) & 0x55555555);
|
|
|
|
aux4 = (aux4 & 0x33333333) + ((aux4 >> 2) & 0x33333333);
|
2014-12-02 01:46:15 -05:00
|
|
|
aux5 = aux5 - ((aux5 >> 1) & 0x55555555);
|
|
|
|
aux5 = (aux5 & 0x33333333) + ((aux5 >> 2) & 0x33333333);
|
|
|
|
aux6 = aux6 - ((aux6 >> 1) & 0x55555555);
|
|
|
|
aux6 = (aux6 & 0x33333333) + ((aux6 >> 2) & 0x33333333);
|
|
|
|
aux7 = aux7 - ((aux7 >> 1) & 0x55555555);
|
|
|
|
aux7 = (aux7 & 0x33333333) + ((aux7 >> 2) & 0x33333333);
|
|
|
|
bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) +
|
|
|
|
((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) +
|
|
|
|
((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) +
|
|
|
|
((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) +
|
|
|
|
((aux5 + (aux5 >> 4)) & 0x0F0F0F0F) +
|
|
|
|
((aux6 + (aux6 >> 4)) & 0x0F0F0F0F) +
|
|
|
|
((aux7 + (aux7 >> 4)) & 0x0F0F0F0F))* 0x01010101) >> 24;
|
2012-05-19 18:49:35 -04:00
|
|
|
}
|
2014-02-27 06:40:58 -05:00
|
|
|
/* Count the remaining bytes. */
|
2012-05-19 18:49:35 -04:00
|
|
|
p = (unsigned char*)p4;
|
2012-05-19 10:16:25 -04:00
|
|
|
while(count--) bits += bitsinbyte[*p++];
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
2014-02-27 06:40:58 -05:00
|
|
|
/* Return the position of the first bit set to one (if 'bit' is 1) or
|
|
|
|
* zero (if 'bit' is 0) in the bitmap starting at 's' and long 'count' bytes.
|
|
|
|
*
|
|
|
|
* The function is guaranteed to return a value >= 0 if 'bit' is 0 since if
|
|
|
|
* no zero bit is found, it returns count*8 assuming the string is zero
|
|
|
|
* padded on the right. However if 'bit' is 1 it is possible that there is
|
|
|
|
* not a single set bit in the bitmap. In this special case -1 is returned. */
|
2014-08-13 05:44:38 -04:00
|
|
|
long redisBitpos(void *s, unsigned long count, int bit) {
|
2014-02-27 06:40:58 -05:00
|
|
|
unsigned long *l;
|
|
|
|
unsigned char *c;
|
|
|
|
unsigned long skipval, word = 0, one;
|
|
|
|
long pos = 0; /* Position of bit, to return to the caller. */
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned long j;
|
2017-02-23 09:38:44 -05:00
|
|
|
int found;
|
2014-02-27 06:40:58 -05:00
|
|
|
|
|
|
|
/* Process whole words first, seeking for first word that is not
|
|
|
|
* all ones or all zeros respectively if we are lookig for zeros
|
|
|
|
* or ones. This is much faster with large strings having contiguous
|
|
|
|
* blocks of 1 or 0 bits compared to the vanilla bit per bit processing.
|
|
|
|
*
|
|
|
|
* Note that if we start from an address that is not aligned
|
|
|
|
* to sizeof(unsigned long) we consume it byte by byte until it is
|
|
|
|
* aligned. */
|
|
|
|
|
|
|
|
/* Skip initial bits not aligned to sizeof(unsigned long) byte by byte. */
|
|
|
|
skipval = bit ? 0 : UCHAR_MAX;
|
|
|
|
c = (unsigned char*) s;
|
2017-02-23 09:38:44 -05:00
|
|
|
found = 0;
|
2014-02-27 06:40:58 -05:00
|
|
|
while((unsigned long)c & (sizeof(*l)-1) && count) {
|
2017-02-23 09:38:44 -05:00
|
|
|
if (*c != skipval) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-27 06:40:58 -05:00
|
|
|
c++;
|
|
|
|
count--;
|
|
|
|
pos += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip bits with full word step. */
|
|
|
|
l = (unsigned long*) c;
|
2017-02-23 09:38:44 -05:00
|
|
|
if (!found) {
|
|
|
|
skipval = bit ? 0 : ULONG_MAX;
|
|
|
|
while (count >= sizeof(*l)) {
|
|
|
|
if (*l != skipval) break;
|
|
|
|
l++;
|
|
|
|
count -= sizeof(*l);
|
|
|
|
pos += sizeof(*l)*8;
|
|
|
|
}
|
2014-02-27 06:40:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load bytes into "word" considering the first byte as the most significant
|
|
|
|
* (we basically consider it as written in big endian, since we consider the
|
|
|
|
* string as a set of bits from left to right, with the first bit at position
|
|
|
|
* zero.
|
|
|
|
*
|
|
|
|
* Note that the loading is designed to work even when the bytes left
|
|
|
|
* (count) are less than a full word. We pad it with zero on the right. */
|
|
|
|
c = (unsigned char*)l;
|
|
|
|
for (j = 0; j < sizeof(*l); j++) {
|
|
|
|
word <<= 8;
|
|
|
|
if (count) {
|
|
|
|
word |= *c;
|
|
|
|
c++;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Special case:
|
|
|
|
* If bits in the string are all zero and we are looking for one,
|
|
|
|
* return -1 to signal that there is not a single "1" in the whole
|
|
|
|
* string. This can't happen when we are looking for "0" as we assume
|
|
|
|
* that the right of the string is zero padded. */
|
|
|
|
if (bit == 1 && word == 0) return -1;
|
|
|
|
|
|
|
|
/* Last word left, scan bit by bit. The first thing we need is to
|
|
|
|
* have a single "1" set in the most significant position in an
|
|
|
|
* unsigned long. We don't know the size of the long so we use a
|
|
|
|
* simple trick. */
|
|
|
|
one = ULONG_MAX; /* All bits set to 1.*/
|
|
|
|
one >>= 1; /* All bits set to 1 but the MSB. */
|
|
|
|
one = ~one; /* All bits set to 0 but the MSB. */
|
|
|
|
|
|
|
|
while(one) {
|
|
|
|
if (((one & word) != 0) == bit) return pos;
|
|
|
|
pos++;
|
|
|
|
one >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we reached this point, there is a bug in the algorithm, since
|
|
|
|
* the case of no match is handled as a special case before. */
|
2015-07-27 03:41:48 -04:00
|
|
|
serverPanic("End of redisBitpos() reached.");
|
2014-02-27 07:17:23 -05:00
|
|
|
return 0; /* Just to avoid warnings. */
|
2014-02-27 06:40:58 -05:00
|
|
|
}
|
|
|
|
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
/* The following set.*Bitfield and get.*Bitfield functions implement setting
|
|
|
|
* and getting arbitrary size (up to 64 bits) signed and unsigned integers
|
|
|
|
* at arbitrary positions into a bitmap.
|
|
|
|
*
|
|
|
|
* The representation considers the bitmap as having the bit number 0 to be
|
|
|
|
* the most significant bit of the first byte, and so forth, so for example
|
|
|
|
* setting a 5 bits unsigned integer to value 23 at offset 7 into a bitmap
|
|
|
|
* previously set to all zeroes, will produce the following representation:
|
|
|
|
*
|
|
|
|
* +--------+--------+
|
|
|
|
* |00000001|01110000|
|
|
|
|
* +--------+--------+
|
|
|
|
*
|
|
|
|
* When offsets and integer sizes are aligned to bytes boundaries, this is the
|
|
|
|
* same as big endian, however when such alignment does not exist, its important
|
|
|
|
* to also understand how the bits inside a byte are ordered.
|
|
|
|
*
|
|
|
|
* Note that this format follows the same convention as SETBIT and related
|
|
|
|
* commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void setUnsignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits, uint64_t value) {
|
|
|
|
uint64_t byte, bit, byteval, bitval, j;
|
|
|
|
|
|
|
|
for (j = 0; j < bits; j++) {
|
2016-03-02 09:13:45 -05:00
|
|
|
bitval = (value & ((uint64_t)1<<(bits-1-j))) != 0;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
byte = offset >> 3;
|
|
|
|
bit = 7 - (offset & 0x7);
|
|
|
|
byteval = p[byte];
|
|
|
|
byteval &= ~(1 << bit);
|
|
|
|
byteval |= bitval << bit;
|
|
|
|
p[byte] = byteval & 0xff;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits, int64_t value) {
|
2016-05-31 05:52:07 -04:00
|
|
|
uint64_t uv = value; /* Casting will add UINT64_MAX + 1 if v is negative. */
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
setUnsignedBitfield(p,offset,bits,uv);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t getUnsignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits) {
|
|
|
|
uint64_t byte, bit, byteval, bitval, j, value = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < bits; j++) {
|
|
|
|
byte = offset >> 3;
|
|
|
|
bit = 7 - (offset & 0x7);
|
|
|
|
byteval = p[byte];
|
|
|
|
bitval = (byteval >> bit) & 1;
|
|
|
|
value = (value<<1) | bitval;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t getSignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits) {
|
2016-05-31 05:52:07 -04:00
|
|
|
int64_t value;
|
|
|
|
union {uint64_t u; int64_t i;} conv;
|
|
|
|
|
|
|
|
/* Converting from unsigned to signed is undefined when the value does
|
|
|
|
* not fit, however here we assume two's complement and the original value
|
|
|
|
* was obtained from signed -> unsigned conversion, so we'll find the
|
|
|
|
* most significant bit set if the original value was negative.
|
|
|
|
*
|
|
|
|
* Note that two's complement is mandatory for exact-width types
|
|
|
|
* according to the C99 standard. */
|
|
|
|
conv.u = getUnsignedBitfield(p,offset,bits);
|
|
|
|
value = conv.i;
|
|
|
|
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
/* If the top significant bit is 1, propagate it to all the
|
2016-05-31 05:52:07 -04:00
|
|
|
* higher bits for two's complement representation of signed
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
* integers. */
|
2020-06-22 05:41:19 -04:00
|
|
|
if (bits < 64 && (value & ((uint64_t)1 << (bits-1))))
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
value |= ((uint64_t)-1) << bits;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The following two functions detect overflow of a value in the context
|
|
|
|
* of storing it as an unsigned or signed integer with the specified
|
|
|
|
* number of bits. The functions both take the value and a possible increment.
|
|
|
|
* If no overflow could happen and the value+increment fit inside the limits,
|
|
|
|
* then zero is returned, otherwise in case of overflow, 1 is returned,
|
|
|
|
* otherwise in case of underflow, -1 is returned.
|
|
|
|
*
|
2020-04-08 06:20:32 -04:00
|
|
|
* When non-zero is returned (overflow or underflow), if not NULL, *limit is
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
* set to the value the operation should result when an overflow happens,
|
|
|
|
* depending on the specified overflow semantics:
|
|
|
|
*
|
|
|
|
* For BFOVERFLOW_SAT if 1 is returned, *limit it is set maximum value that
|
|
|
|
* you can store in that integer. when -1 is returned, *limit is set to the
|
|
|
|
* minimum value that an integer of that size can represent.
|
|
|
|
*
|
|
|
|
* For BFOVERFLOW_WRAP *limit is set by performing the operation in order to
|
|
|
|
* "wrap" around towards zero for unsigned integers, or towards the most
|
|
|
|
* negative number that is possible to represent for signed integers. */
|
|
|
|
|
|
|
|
#define BFOVERFLOW_WRAP 0
|
|
|
|
#define BFOVERFLOW_SAT 1
|
|
|
|
#define BFOVERFLOW_FAIL 2 /* Used by the BITFIELD command implementation. */
|
|
|
|
|
|
|
|
int checkUnsignedBitfieldOverflow(uint64_t value, int64_t incr, uint64_t bits, int owtype, uint64_t *limit) {
|
2016-03-02 09:13:45 -05:00
|
|
|
uint64_t max = (bits == 64) ? UINT64_MAX : (((uint64_t)1<<bits)-1);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
int64_t maxincr = max-value;
|
|
|
|
int64_t minincr = -value;
|
|
|
|
|
|
|
|
if (value > max || (incr > 0 && incr > maxincr)) {
|
|
|
|
if (limit) {
|
|
|
|
if (owtype == BFOVERFLOW_WRAP) {
|
|
|
|
goto handle_wrap;
|
|
|
|
} else if (owtype == BFOVERFLOW_SAT) {
|
|
|
|
*limit = max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else if (incr < 0 && incr < minincr) {
|
|
|
|
if (limit) {
|
|
|
|
if (owtype == BFOVERFLOW_WRAP) {
|
|
|
|
goto handle_wrap;
|
|
|
|
} else if (owtype == BFOVERFLOW_SAT) {
|
|
|
|
*limit = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
handle_wrap:
|
|
|
|
{
|
2016-06-05 09:34:43 -04:00
|
|
|
uint64_t mask = ((uint64_t)-1) << bits;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
uint64_t res = value+incr;
|
|
|
|
|
|
|
|
res &= ~mask;
|
|
|
|
*limit = res;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int checkSignedBitfieldOverflow(int64_t value, int64_t incr, uint64_t bits, int owtype, int64_t *limit) {
|
2016-03-02 09:13:45 -05:00
|
|
|
int64_t max = (bits == 64) ? INT64_MAX : (((int64_t)1<<(bits-1))-1);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
int64_t min = (-max)-1;
|
|
|
|
|
|
|
|
/* Note that maxincr and minincr could overflow, but we use the values
|
|
|
|
* only after checking 'value' range, so when we use it no overflow
|
|
|
|
* happens. */
|
|
|
|
int64_t maxincr = max-value;
|
|
|
|
int64_t minincr = min-value;
|
|
|
|
|
2016-03-02 09:13:45 -05:00
|
|
|
if (value > max || (bits != 64 && incr > maxincr) || (value >= 0 && incr > 0 && incr > maxincr))
|
|
|
|
{
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
if (limit) {
|
|
|
|
if (owtype == BFOVERFLOW_WRAP) {
|
|
|
|
goto handle_wrap;
|
|
|
|
} else if (owtype == BFOVERFLOW_SAT) {
|
|
|
|
*limit = max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2016-03-02 09:13:45 -05:00
|
|
|
} else if (value < min || (bits != 64 && incr < minincr) || (value < 0 && incr < 0 && incr < minincr)) {
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
if (limit) {
|
|
|
|
if (owtype == BFOVERFLOW_WRAP) {
|
|
|
|
goto handle_wrap;
|
|
|
|
} else if (owtype == BFOVERFLOW_SAT) {
|
|
|
|
*limit = min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
handle_wrap:
|
|
|
|
{
|
|
|
|
uint64_t msb = (uint64_t)1 << (bits-1);
|
|
|
|
uint64_t a = value, b = incr, c;
|
|
|
|
c = a+b; /* Perform addition as unsigned so that's defined. */
|
|
|
|
|
|
|
|
/* If the sign bit is set, propagate to all the higher order
|
|
|
|
* bits, to cap the negative value. If it's clear, mask to
|
|
|
|
* the positive integer limit. */
|
2020-06-22 05:41:19 -04:00
|
|
|
if (bits < 64) {
|
|
|
|
uint64_t mask = ((uint64_t)-1) << bits;
|
|
|
|
if (c & msb) {
|
|
|
|
c |= mask;
|
|
|
|
} else {
|
|
|
|
c &= ~mask;
|
|
|
|
}
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
}
|
|
|
|
*limit = c;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Debugging function. Just show bits in the specified bitmap. Not used
|
|
|
|
* but here for not having to rewrite it when debugging is needed. */
|
|
|
|
void printBits(unsigned char *p, unsigned long count) {
|
|
|
|
unsigned long j, i, byte;
|
|
|
|
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
byte = p[j];
|
|
|
|
for (i = 0x80; i > 0; i /= 2)
|
|
|
|
printf("%c", (byte & i) ? '1' : '0');
|
|
|
|
printf("|");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2012-05-19 10:16:25 -04:00
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* Bits related string commands: GETBIT, SETBIT, BITCOUNT, BITOP.
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define BITOP_AND 0
|
|
|
|
#define BITOP_OR 1
|
|
|
|
#define BITOP_XOR 2
|
|
|
|
#define BITOP_NOT 3
|
|
|
|
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
#define BITFIELDOP_GET 0
|
|
|
|
#define BITFIELDOP_SET 1
|
|
|
|
#define BITFIELDOP_INCRBY 2
|
|
|
|
|
|
|
|
/* This helper function used by GETBIT / SETBIT parses the bit offset argument
|
|
|
|
* making sure an error is returned if it is negative or if it overflows
|
2016-02-26 09:16:24 -05:00
|
|
|
* Redis 512 MB limit for the string value.
|
|
|
|
*
|
|
|
|
* If the 'hash' argument is true, and 'bits is positive, then the command
|
|
|
|
* will also parse bit offsets prefixed by "#". In such a case the offset
|
|
|
|
* is multiplied by 'bits'. This is useful for the BITFIELD command. */
|
|
|
|
int getBitOffsetFromArgument(client *c, robj *o, size_t *offset, int hash, int bits) {
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
long long loffset;
|
|
|
|
char *err = "bit offset is not an integer or out of range";
|
2016-02-26 09:16:24 -05:00
|
|
|
char *p = o->ptr;
|
|
|
|
size_t plen = sdslen(p);
|
2016-02-26 09:53:29 -05:00
|
|
|
int usehash = 0;
|
2016-02-26 09:16:24 -05:00
|
|
|
|
|
|
|
/* Handle #<offset> form. */
|
2016-02-26 09:53:29 -05:00
|
|
|
if (p[0] == '#' && hash && bits > 0) usehash = 1;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
|
2016-02-26 09:53:29 -05:00
|
|
|
if (string2ll(p+usehash,plen-usehash,&loffset) == 0) {
|
2016-02-26 09:16:24 -05:00
|
|
|
addReplyError(c,err);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
return C_ERR;
|
2016-02-26 09:16:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust the offset by 'bits' for #<offset> form. */
|
2016-02-26 09:53:29 -05:00
|
|
|
if (usehash) loffset *= bits;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
|
|
|
|
/* Limit offset to 512MB in bytes */
|
|
|
|
if ((loffset < 0) || ((unsigned long long)loffset >> 3) >= (512*1024*1024))
|
|
|
|
{
|
|
|
|
addReplyError(c,err);
|
|
|
|
return C_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*offset = (size_t)loffset;
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This helper function for BITFIELD parses a bitfield type in the form
|
|
|
|
* <sign><bits> where sign is 'u' or 'i' for unsigned and signed, and
|
|
|
|
* the bits is a value between 1 and 64. However 64 bits unsigned integers
|
|
|
|
* are reported as an error because of current limitations of Redis protocol
|
|
|
|
* to return unsigned integer values greater than INT64_MAX.
|
|
|
|
*
|
|
|
|
* On error C_ERR is returned and an error is sent to the client. */
|
|
|
|
int getBitfieldTypeFromArgument(client *c, robj *o, int *sign, int *bits) {
|
|
|
|
char *p = o->ptr;
|
|
|
|
char *err = "Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is.";
|
|
|
|
long long llbits;
|
|
|
|
|
|
|
|
if (p[0] == 'i') {
|
|
|
|
*sign = 1;
|
|
|
|
} else if (p[0] == 'u') {
|
|
|
|
*sign = 0;
|
|
|
|
} else {
|
|
|
|
addReplyError(c,err);
|
|
|
|
return C_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((string2ll(p+1,strlen(p+1),&llbits)) == 0 ||
|
|
|
|
llbits < 1 ||
|
2016-03-02 05:11:30 -05:00
|
|
|
(*sign == 1 && llbits > 64) ||
|
|
|
|
(*sign == 0 && llbits > 63))
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
{
|
|
|
|
addReplyError(c,err);
|
|
|
|
return C_ERR;
|
|
|
|
}
|
|
|
|
*bits = llbits;
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is an helper function for commands implementations that need to write
|
|
|
|
* bits to a string object. The command creates or pad with zeroes the string
|
|
|
|
* so that the 'maxbit' bit can be addressed. The object is finally
|
|
|
|
* returned. Otherwise if the key holds a wrong type NULL is returned and
|
|
|
|
* an error is sent to the client. */
|
|
|
|
robj *lookupStringForBitCommand(client *c, size_t maxbit) {
|
|
|
|
size_t byte = maxbit >> 3;
|
|
|
|
robj *o = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
|
|
|
|
if (o == NULL) {
|
|
|
|
o = createObject(OBJ_STRING,sdsnewlen(NULL, byte+1));
|
|
|
|
dbAdd(c->db,c->argv[1],o);
|
|
|
|
} else {
|
|
|
|
if (checkType(c,o,OBJ_STRING)) return NULL;
|
|
|
|
o = dbUnshareStringValue(c->db,c->argv[1],o);
|
|
|
|
o->ptr = sdsgrowzero(o->ptr,byte+1);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2016-05-18 09:35:17 -04:00
|
|
|
/* Return a pointer to the string object content, and stores its length
|
|
|
|
* in 'len'. The user is required to pass (likely stack allocated) buffer
|
|
|
|
* 'llbuf' of at least LONG_STR_SIZE bytes. Such a buffer is used in the case
|
|
|
|
* the object is integer encoded in order to provide the representation
|
|
|
|
* without usign heap allocation.
|
|
|
|
*
|
|
|
|
* The function returns the pointer to the object array of bytes representing
|
|
|
|
* the string it contains, that may be a pointer to 'llbuf' or to the
|
|
|
|
* internal object representation. As a side effect 'len' is filled with
|
|
|
|
* the length of such buffer.
|
|
|
|
*
|
|
|
|
* If the source object is NULL the function is guaranteed to return NULL
|
|
|
|
* and set 'len' to 0. */
|
|
|
|
unsigned char *getObjectReadOnlyString(robj *o, long *len, char *llbuf) {
|
|
|
|
serverAssert(o->type == OBJ_STRING);
|
|
|
|
unsigned char *p = NULL;
|
|
|
|
|
|
|
|
/* Set the 'p' pointer to the string, that can be just a stack allocated
|
|
|
|
* array if our string was integer encoded. */
|
|
|
|
if (o && o->encoding == OBJ_ENCODING_INT) {
|
|
|
|
p = (unsigned char*) llbuf;
|
|
|
|
if (len) *len = ll2string(llbuf,LONG_STR_SIZE,(long)o->ptr);
|
|
|
|
} else if (o) {
|
|
|
|
p = (unsigned char*) o->ptr;
|
|
|
|
if (len) *len = sdslen(o->ptr);
|
|
|
|
} else {
|
|
|
|
if (len) *len = 0;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-05-19 04:33:20 -04:00
|
|
|
/* SETBIT key offset bitvalue */
|
2015-07-26 09:20:46 -04:00
|
|
|
void setbitCommand(client *c) {
|
2012-05-19 04:33:20 -04:00
|
|
|
robj *o;
|
|
|
|
char *err = "bit is not an integer or out of range";
|
|
|
|
size_t bitoffset;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
ssize_t byte, bit;
|
2012-05-19 04:33:20 -04:00
|
|
|
int byteval, bitval;
|
|
|
|
long on;
|
|
|
|
|
2016-02-26 09:16:24 -05:00
|
|
|
if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset,0,0) != C_OK)
|
2012-05-19 04:33:20 -04:00
|
|
|
return;
|
|
|
|
|
2015-07-26 17:17:55 -04:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[3],&on,err) != C_OK)
|
2012-05-19 04:33:20 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Bits can only be set or cleared... */
|
|
|
|
if (on & ~1) {
|
|
|
|
addReplyError(c,err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
if ((o = lookupStringForBitCommand(c,bitoffset)) == NULL) return;
|
2012-05-19 04:33:20 -04:00
|
|
|
|
|
|
|
/* Get current values */
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
byte = bitoffset >> 3;
|
2012-07-18 06:01:43 -04:00
|
|
|
byteval = ((uint8_t*)o->ptr)[byte];
|
2012-05-19 04:33:20 -04:00
|
|
|
bit = 7 - (bitoffset & 0x7);
|
|
|
|
bitval = byteval & (1 << bit);
|
|
|
|
|
|
|
|
/* Update byte with new bit value and return original value */
|
|
|
|
byteval &= ~(1 << bit);
|
|
|
|
byteval |= ((on & 0x1) << bit);
|
2012-07-18 06:01:43 -04:00
|
|
|
((uint8_t*)o->ptr)[byte] = byteval;
|
2020-04-21 04:51:46 -04:00
|
|
|
signalModifiedKey(c,c->db,c->argv[1]);
|
2015-07-27 03:41:48 -04:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"setbit",c->argv[1],c->db->id);
|
2012-05-19 04:33:20 -04:00
|
|
|
server.dirty++;
|
|
|
|
addReply(c, bitval ? shared.cone : shared.czero);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GETBIT key offset */
|
2015-07-26 09:20:46 -04:00
|
|
|
void getbitCommand(client *c) {
|
2012-05-19 04:33:20 -04:00
|
|
|
robj *o;
|
|
|
|
char llbuf[32];
|
|
|
|
size_t bitoffset;
|
|
|
|
size_t byte, bit;
|
|
|
|
size_t bitval = 0;
|
|
|
|
|
2016-02-26 09:16:24 -05:00
|
|
|
if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset,0,0) != C_OK)
|
2012-05-19 04:33:20 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
2015-07-26 09:28:00 -04:00
|
|
|
checkType(c,o,OBJ_STRING)) return;
|
2012-05-19 04:33:20 -04:00
|
|
|
|
|
|
|
byte = bitoffset >> 3;
|
|
|
|
bit = 7 - (bitoffset & 0x7);
|
2012-06-05 15:50:10 -04:00
|
|
|
if (sdsEncodedObject(o)) {
|
2012-05-19 04:33:20 -04:00
|
|
|
if (byte < sdslen(o->ptr))
|
2012-07-18 06:01:43 -04:00
|
|
|
bitval = ((uint8_t*)o->ptr)[byte] & (1 << bit);
|
2012-06-05 15:50:10 -04:00
|
|
|
} else {
|
|
|
|
if (byte < (size_t)ll2string(llbuf,sizeof(llbuf),(long)o->ptr))
|
|
|
|
bitval = llbuf[byte] & (1 << bit);
|
2012-05-19 04:33:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addReply(c, bitval ? shared.cone : shared.czero);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */
|
2015-07-26 09:20:46 -04:00
|
|
|
void bitopCommand(client *c) {
|
2012-05-19 04:33:20 -04:00
|
|
|
char *opname = c->argv[1]->ptr;
|
|
|
|
robj *o, *targetkey = c->argv[2];
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned long op, j, numkeys;
|
2013-01-16 12:00:20 -05:00
|
|
|
robj **objects; /* Array of source objects. */
|
2012-05-19 04:33:20 -04:00
|
|
|
unsigned char **src; /* Array of source strings pointers. */
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned long *len, maxlen = 0; /* Array of length of src strings,
|
|
|
|
and max len. */
|
|
|
|
unsigned long minlen = 0; /* Min len among the input keys. */
|
2012-05-19 04:33:20 -04:00
|
|
|
unsigned char *res = NULL; /* Resulting string. */
|
|
|
|
|
|
|
|
/* Parse the operation name. */
|
|
|
|
if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,"and"))
|
|
|
|
op = BITOP_AND;
|
|
|
|
else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,"or"))
|
|
|
|
op = BITOP_OR;
|
|
|
|
else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,"xor"))
|
|
|
|
op = BITOP_XOR;
|
|
|
|
else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,"not"))
|
|
|
|
op = BITOP_NOT;
|
|
|
|
else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check: NOT accepts only a single key argument. */
|
|
|
|
if (op == BITOP_NOT && c->argc != 4) {
|
|
|
|
addReplyError(c,"BITOP NOT must be called with a single source key.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup keys, and store pointers to the string objects into an array. */
|
|
|
|
numkeys = c->argc - 3;
|
|
|
|
src = zmalloc(sizeof(unsigned char*) * numkeys);
|
|
|
|
len = zmalloc(sizeof(long) * numkeys);
|
2012-05-22 11:40:20 -04:00
|
|
|
objects = zmalloc(sizeof(robj*) * numkeys);
|
2012-05-19 04:33:20 -04:00
|
|
|
for (j = 0; j < numkeys; j++) {
|
|
|
|
o = lookupKeyRead(c->db,c->argv[j+3]);
|
|
|
|
/* Handle non-existing keys as empty strings. */
|
|
|
|
if (o == NULL) {
|
2012-05-22 11:40:20 -04:00
|
|
|
objects[j] = NULL;
|
2012-05-19 04:33:20 -04:00
|
|
|
src[j] = NULL;
|
|
|
|
len[j] = 0;
|
2012-05-31 15:45:39 -04:00
|
|
|
minlen = 0;
|
2012-05-19 04:33:20 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Return an error if one of the keys is not a string. */
|
2015-07-26 09:28:00 -04:00
|
|
|
if (checkType(c,o,OBJ_STRING)) {
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned long i;
|
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
if (objects[i])
|
|
|
|
decrRefCount(objects[i]);
|
2012-05-22 11:40:20 -04:00
|
|
|
}
|
2012-05-19 04:33:20 -04:00
|
|
|
zfree(src);
|
|
|
|
zfree(len);
|
2012-05-22 11:40:20 -04:00
|
|
|
zfree(objects);
|
2012-05-19 04:33:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-05-22 11:40:20 -04:00
|
|
|
objects[j] = getDecodedObject(o);
|
|
|
|
src[j] = objects[j]->ptr;
|
|
|
|
len[j] = sdslen(objects[j]->ptr);
|
2012-05-19 04:33:20 -04:00
|
|
|
if (len[j] > maxlen) maxlen = len[j];
|
BITOP command 10x speed improvement.
This commit adds a fast-path to the BITOP that can be used for all the
bytes from 0 to the minimal length of the string, and if there are
at max 16 input keys.
Often the intersected bitmaps are roughly the same size, so this
optimization can provide a 10x speed boost to most real world usages
of the command.
Bytes are processed four full words at a time, in loops specialized
for the specific BITOP sub-command, without the need to check for
length issues with the inputs (since we run this algorithm only as far
as there is data from all the keys at the same time).
The remaining part of the string is intersected in the usual way using
the slow but generic algorith.
It is possible to do better than this with inputs that are not roughly
the same size, sorting the input keys by length, by initializing the
result string in a smarter way, and noticing that the final part of the
output string composed of only data from the longest string does not
need any proecessing since AND, OR and XOR against an empty string does
not alter the output (zero in the first case, and the original string in
the other two cases).
More implementations will be implemented later likely, but this should
be enough to release Redis 2.6-RC4 with bitops merged in.
Note: this commit also adds better testing for BITOP NOT command, that
is currently the faster and hard to optimize further since it just
flips the bits of a single input string.
2012-05-23 16:12:50 -04:00
|
|
|
if (j == 0 || len[j] < minlen) minlen = len[j];
|
2012-05-19 04:33:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the bit operation, if at least one string is not empty. */
|
|
|
|
if (maxlen) {
|
|
|
|
res = (unsigned char*) sdsnewlen(NULL,maxlen);
|
|
|
|
unsigned char output, byte;
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned long i;
|
2012-05-19 04:33:20 -04:00
|
|
|
|
BITOP command 10x speed improvement.
This commit adds a fast-path to the BITOP that can be used for all the
bytes from 0 to the minimal length of the string, and if there are
at max 16 input keys.
Often the intersected bitmaps are roughly the same size, so this
optimization can provide a 10x speed boost to most real world usages
of the command.
Bytes are processed four full words at a time, in loops specialized
for the specific BITOP sub-command, without the need to check for
length issues with the inputs (since we run this algorithm only as far
as there is data from all the keys at the same time).
The remaining part of the string is intersected in the usual way using
the slow but generic algorith.
It is possible to do better than this with inputs that are not roughly
the same size, sorting the input keys by length, by initializing the
result string in a smarter way, and noticing that the final part of the
output string composed of only data from the longest string does not
need any proecessing since AND, OR and XOR against an empty string does
not alter the output (zero in the first case, and the original string in
the other two cases).
More implementations will be implemented later likely, but this should
be enough to release Redis 2.6-RC4 with bitops merged in.
Note: this commit also adds better testing for BITOP NOT command, that
is currently the faster and hard to optimize further since it just
flips the bits of a single input string.
2012-05-23 16:12:50 -04:00
|
|
|
/* Fast path: as far as we have data for all the input bitmaps we
|
|
|
|
* can take a fast path that performs much better than the
|
2017-02-19 10:07:08 -05:00
|
|
|
* vanilla algorithm. On ARM we skip the fast path since it will
|
|
|
|
* result in GCC compiling the code using multiple-words load/store
|
|
|
|
* operations that are not supported even in ARM >= v6. */
|
BITOP command 10x speed improvement.
This commit adds a fast-path to the BITOP that can be used for all the
bytes from 0 to the minimal length of the string, and if there are
at max 16 input keys.
Often the intersected bitmaps are roughly the same size, so this
optimization can provide a 10x speed boost to most real world usages
of the command.
Bytes are processed four full words at a time, in loops specialized
for the specific BITOP sub-command, without the need to check for
length issues with the inputs (since we run this algorithm only as far
as there is data from all the keys at the same time).
The remaining part of the string is intersected in the usual way using
the slow but generic algorith.
It is possible to do better than this with inputs that are not roughly
the same size, sorting the input keys by length, by initializing the
result string in a smarter way, and noticing that the final part of the
output string composed of only data from the longest string does not
need any proecessing since AND, OR and XOR against an empty string does
not alter the output (zero in the first case, and the original string in
the other two cases).
More implementations will be implemented later likely, but this should
be enough to release Redis 2.6-RC4 with bitops merged in.
Note: this commit also adds better testing for BITOP NOT command, that
is currently the faster and hard to optimize further since it just
flips the bits of a single input string.
2012-05-23 16:12:50 -04:00
|
|
|
j = 0;
|
2017-02-23 09:39:44 -05:00
|
|
|
#ifndef USE_ALIGNED_ACCESS
|
2014-12-02 21:07:58 -05:00
|
|
|
if (minlen >= sizeof(unsigned long)*4 && numkeys <= 16) {
|
BITOP command 10x speed improvement.
This commit adds a fast-path to the BITOP that can be used for all the
bytes from 0 to the minimal length of the string, and if there are
at max 16 input keys.
Often the intersected bitmaps are roughly the same size, so this
optimization can provide a 10x speed boost to most real world usages
of the command.
Bytes are processed four full words at a time, in loops specialized
for the specific BITOP sub-command, without the need to check for
length issues with the inputs (since we run this algorithm only as far
as there is data from all the keys at the same time).
The remaining part of the string is intersected in the usual way using
the slow but generic algorith.
It is possible to do better than this with inputs that are not roughly
the same size, sorting the input keys by length, by initializing the
result string in a smarter way, and noticing that the final part of the
output string composed of only data from the longest string does not
need any proecessing since AND, OR and XOR against an empty string does
not alter the output (zero in the first case, and the original string in
the other two cases).
More implementations will be implemented later likely, but this should
be enough to release Redis 2.6-RC4 with bitops merged in.
Note: this commit also adds better testing for BITOP NOT command, that
is currently the faster and hard to optimize further since it just
flips the bits of a single input string.
2012-05-23 16:12:50 -04:00
|
|
|
unsigned long *lp[16];
|
|
|
|
unsigned long *lres = (unsigned long*) res;
|
|
|
|
|
|
|
|
/* Note: sds pointer is always aligned to 8 byte boundary. */
|
|
|
|
memcpy(lp,src,sizeof(unsigned long*)*numkeys);
|
|
|
|
memcpy(res,src[0],minlen);
|
|
|
|
|
|
|
|
/* Different branches per different operations for speed (sorry). */
|
|
|
|
if (op == BITOP_AND) {
|
|
|
|
while(minlen >= sizeof(unsigned long)*4) {
|
|
|
|
for (i = 1; i < numkeys; i++) {
|
|
|
|
lres[0] &= lp[i][0];
|
|
|
|
lres[1] &= lp[i][1];
|
|
|
|
lres[2] &= lp[i][2];
|
|
|
|
lres[3] &= lp[i][3];
|
|
|
|
lp[i]+=4;
|
|
|
|
}
|
|
|
|
lres+=4;
|
|
|
|
j += sizeof(unsigned long)*4;
|
|
|
|
minlen -= sizeof(unsigned long)*4;
|
|
|
|
}
|
|
|
|
} else if (op == BITOP_OR) {
|
|
|
|
while(minlen >= sizeof(unsigned long)*4) {
|
|
|
|
for (i = 1; i < numkeys; i++) {
|
|
|
|
lres[0] |= lp[i][0];
|
|
|
|
lres[1] |= lp[i][1];
|
|
|
|
lres[2] |= lp[i][2];
|
|
|
|
lres[3] |= lp[i][3];
|
|
|
|
lp[i]+=4;
|
|
|
|
}
|
|
|
|
lres+=4;
|
|
|
|
j += sizeof(unsigned long)*4;
|
|
|
|
minlen -= sizeof(unsigned long)*4;
|
|
|
|
}
|
|
|
|
} else if (op == BITOP_XOR) {
|
|
|
|
while(minlen >= sizeof(unsigned long)*4) {
|
|
|
|
for (i = 1; i < numkeys; i++) {
|
|
|
|
lres[0] ^= lp[i][0];
|
|
|
|
lres[1] ^= lp[i][1];
|
|
|
|
lres[2] ^= lp[i][2];
|
|
|
|
lres[3] ^= lp[i][3];
|
|
|
|
lp[i]+=4;
|
|
|
|
}
|
|
|
|
lres+=4;
|
|
|
|
j += sizeof(unsigned long)*4;
|
|
|
|
minlen -= sizeof(unsigned long)*4;
|
|
|
|
}
|
|
|
|
} else if (op == BITOP_NOT) {
|
|
|
|
while(minlen >= sizeof(unsigned long)*4) {
|
|
|
|
lres[0] = ~lres[0];
|
|
|
|
lres[1] = ~lres[1];
|
|
|
|
lres[2] = ~lres[2];
|
|
|
|
lres[3] = ~lres[3];
|
|
|
|
lres+=4;
|
|
|
|
j += sizeof(unsigned long)*4;
|
|
|
|
minlen -= sizeof(unsigned long)*4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 10:07:08 -05:00
|
|
|
#endif
|
BITOP command 10x speed improvement.
This commit adds a fast-path to the BITOP that can be used for all the
bytes from 0 to the minimal length of the string, and if there are
at max 16 input keys.
Often the intersected bitmaps are roughly the same size, so this
optimization can provide a 10x speed boost to most real world usages
of the command.
Bytes are processed four full words at a time, in loops specialized
for the specific BITOP sub-command, without the need to check for
length issues with the inputs (since we run this algorithm only as far
as there is data from all the keys at the same time).
The remaining part of the string is intersected in the usual way using
the slow but generic algorith.
It is possible to do better than this with inputs that are not roughly
the same size, sorting the input keys by length, by initializing the
result string in a smarter way, and noticing that the final part of the
output string composed of only data from the longest string does not
need any proecessing since AND, OR and XOR against an empty string does
not alter the output (zero in the first case, and the original string in
the other two cases).
More implementations will be implemented later likely, but this should
be enough to release Redis 2.6-RC4 with bitops merged in.
Note: this commit also adds better testing for BITOP NOT command, that
is currently the faster and hard to optimize further since it just
flips the bits of a single input string.
2012-05-23 16:12:50 -04:00
|
|
|
|
|
|
|
/* j is set to the next byte to process by the previous loop. */
|
|
|
|
for (; j < maxlen; j++) {
|
2012-05-19 04:33:20 -04:00
|
|
|
output = (len[0] <= j) ? 0 : src[0][j];
|
|
|
|
if (op == BITOP_NOT) output = ~output;
|
|
|
|
for (i = 1; i < numkeys; i++) {
|
|
|
|
byte = (len[i] <= j) ? 0 : src[i][j];
|
|
|
|
switch(op) {
|
|
|
|
case BITOP_AND: output &= byte; break;
|
|
|
|
case BITOP_OR: output |= byte; break;
|
|
|
|
case BITOP_XOR: output ^= byte; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res[j] = output;
|
|
|
|
}
|
|
|
|
}
|
2012-05-22 11:40:20 -04:00
|
|
|
for (j = 0; j < numkeys; j++) {
|
|
|
|
if (objects[j])
|
|
|
|
decrRefCount(objects[j]);
|
|
|
|
}
|
2012-05-19 04:33:20 -04:00
|
|
|
zfree(src);
|
|
|
|
zfree(len);
|
2012-05-22 11:40:20 -04:00
|
|
|
zfree(objects);
|
2012-05-19 04:33:20 -04:00
|
|
|
|
|
|
|
/* Store the computed value into the target key */
|
|
|
|
if (maxlen) {
|
2015-07-26 09:28:00 -04:00
|
|
|
o = createObject(OBJ_STRING,res);
|
2020-04-21 04:51:46 -04:00
|
|
|
setKey(c,c->db,targetkey,o);
|
2015-07-27 03:41:48 -04:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"set",targetkey,c->db->id);
|
2012-05-19 04:33:20 -04:00
|
|
|
decrRefCount(o);
|
2020-07-10 01:20:27 -04:00
|
|
|
server.dirty++;
|
2012-05-19 04:33:20 -04:00
|
|
|
} else if (dbDelete(c->db,targetkey)) {
|
2020-04-21 04:51:46 -04:00
|
|
|
signalModifiedKey(c,c->db,targetkey);
|
2015-07-27 03:41:48 -04:00
|
|
|
notifyKeyspaceEvent(NOTIFY_GENERIC,"del",targetkey,c->db->id);
|
2020-07-10 01:20:27 -04:00
|
|
|
server.dirty++;
|
2012-05-19 04:33:20 -04:00
|
|
|
}
|
|
|
|
addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BITCOUNT key [start end] */
|
2015-07-26 09:20:46 -04:00
|
|
|
void bitcountCommand(client *c) {
|
2012-05-19 04:33:20 -04:00
|
|
|
robj *o;
|
2012-07-15 05:38:30 -04:00
|
|
|
long start, end, strlen;
|
2012-05-19 04:33:20 -04:00
|
|
|
unsigned char *p;
|
2016-05-18 09:35:17 -04:00
|
|
|
char llbuf[LONG_STR_SIZE];
|
2012-05-19 04:33:20 -04:00
|
|
|
|
|
|
|
/* Lookup, check for type, and return 0 for non existing keys. */
|
|
|
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
2015-07-26 09:28:00 -04:00
|
|
|
checkType(c,o,OBJ_STRING)) return;
|
2016-05-18 09:35:17 -04:00
|
|
|
p = getObjectReadOnlyString(o,&strlen,llbuf);
|
2012-05-19 04:33:20 -04:00
|
|
|
|
|
|
|
/* Parse start/end range if any. */
|
|
|
|
if (c->argc == 4) {
|
2015-07-26 17:17:55 -04:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK)
|
2012-05-19 04:33:20 -04:00
|
|
|
return;
|
2015-07-26 17:17:55 -04:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK)
|
2012-05-19 04:33:20 -04:00
|
|
|
return;
|
|
|
|
/* Convert negative indexes */
|
2016-06-15 06:16:39 -04:00
|
|
|
if (start < 0 && end < 0 && start > end) {
|
2016-05-30 04:21:08 -04:00
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-15 06:48:58 -04:00
|
|
|
if (start < 0) start = strlen+start;
|
|
|
|
if (end < 0) end = strlen+end;
|
2012-05-19 04:33:20 -04:00
|
|
|
if (start < 0) start = 0;
|
|
|
|
if (end < 0) end = 0;
|
2012-07-15 05:38:30 -04:00
|
|
|
if (end >= strlen) end = strlen-1;
|
2012-05-19 04:33:20 -04:00
|
|
|
} else if (c->argc == 2) {
|
|
|
|
/* The whole string. */
|
|
|
|
start = 0;
|
|
|
|
end = strlen-1;
|
|
|
|
} else {
|
|
|
|
/* Syntax error. */
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Precondition: end >= 0 && end < strlen, so the only condition where
|
|
|
|
* zero can be returned is: start > end. */
|
|
|
|
if (start > end) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
} else {
|
2012-05-19 10:16:25 -04:00
|
|
|
long bytes = end-start+1;
|
2012-05-19 04:33:20 -04:00
|
|
|
|
2013-06-26 09:19:06 -04:00
|
|
|
addReplyLongLong(c,redisPopcount(p+start,bytes));
|
2012-05-19 04:33:20 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-27 06:40:58 -05:00
|
|
|
|
2014-02-27 06:53:03 -05:00
|
|
|
/* BITPOS key bit [start [end]] */
|
2015-07-26 09:20:46 -04:00
|
|
|
void bitposCommand(client *c) {
|
2014-02-27 06:40:58 -05:00
|
|
|
robj *o;
|
|
|
|
long bit, start, end, strlen;
|
|
|
|
unsigned char *p;
|
2016-05-18 09:35:17 -04:00
|
|
|
char llbuf[LONG_STR_SIZE];
|
2014-02-27 06:53:03 -05:00
|
|
|
int end_given = 0;
|
2014-02-27 06:40:58 -05:00
|
|
|
|
|
|
|
/* Parse the bit argument to understand what we are looking for, set
|
|
|
|
* or clear bits. */
|
2015-07-26 17:17:55 -04:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[2],&bit,NULL) != C_OK)
|
2014-02-27 06:40:58 -05:00
|
|
|
return;
|
|
|
|
if (bit != 0 && bit != 1) {
|
|
|
|
addReplyError(c, "The bit argument must be 1 or 0.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the key does not exist, from our point of view it is an infinite
|
|
|
|
* array of 0 bits. If the user is looking for the fist clear bit return 0,
|
|
|
|
* If the user is looking for the first set bit, return -1. */
|
|
|
|
if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) {
|
|
|
|
addReplyLongLong(c, bit ? -1 : 0);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-26 09:28:00 -04:00
|
|
|
if (checkType(c,o,OBJ_STRING)) return;
|
2016-05-18 09:35:17 -04:00
|
|
|
p = getObjectReadOnlyString(o,&strlen,llbuf);
|
2014-02-27 06:40:58 -05:00
|
|
|
|
|
|
|
/* Parse start/end range if any. */
|
2014-02-27 06:53:03 -05:00
|
|
|
if (c->argc == 4 || c->argc == 5) {
|
2015-07-26 17:17:55 -04:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != C_OK)
|
2014-02-27 06:40:58 -05:00
|
|
|
return;
|
2014-02-27 06:53:03 -05:00
|
|
|
if (c->argc == 5) {
|
2015-07-26 17:17:55 -04:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != C_OK)
|
2014-02-27 06:53:03 -05:00
|
|
|
return;
|
|
|
|
end_given = 1;
|
|
|
|
} else {
|
|
|
|
end = strlen-1;
|
|
|
|
}
|
2014-02-27 06:40:58 -05:00
|
|
|
/* Convert negative indexes */
|
|
|
|
if (start < 0) start = strlen+start;
|
|
|
|
if (end < 0) end = strlen+end;
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
if (end < 0) end = 0;
|
|
|
|
if (end >= strlen) end = strlen-1;
|
|
|
|
} else if (c->argc == 3) {
|
|
|
|
/* The whole string. */
|
|
|
|
start = 0;
|
|
|
|
end = strlen-1;
|
|
|
|
} else {
|
|
|
|
/* Syntax error. */
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For empty ranges (start > end) we return -1 as an empty range does
|
|
|
|
* not contain a 0 nor a 1. */
|
|
|
|
if (start > end) {
|
|
|
|
addReplyLongLong(c, -1);
|
|
|
|
} else {
|
|
|
|
long bytes = end-start+1;
|
|
|
|
long pos = redisBitpos(p+start,bytes,bit);
|
|
|
|
|
2014-02-27 06:53:03 -05:00
|
|
|
/* If we are looking for clear bits, and the user specified an exact
|
|
|
|
* range with start-end, we can't consider the right of the range as
|
|
|
|
* zero padded (as we do when no explicit end is given).
|
2014-02-27 06:40:58 -05:00
|
|
|
*
|
2014-02-27 06:53:03 -05:00
|
|
|
* So if redisBitpos() returns the first bit outside the range,
|
2014-02-27 06:40:58 -05:00
|
|
|
* we return -1 to the caller, to mean, in the specified range there
|
|
|
|
* is not a single "0" bit. */
|
2014-02-27 06:53:03 -05:00
|
|
|
if (end_given && bit == 0 && pos == bytes*8) {
|
2014-02-27 06:40:58 -05:00
|
|
|
addReplyLongLong(c,-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pos != -1) pos += start*8; /* Adjust for the bytes we skipped. */
|
|
|
|
addReplyLongLong(c,pos);
|
|
|
|
}
|
|
|
|
}
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
|
|
|
|
/* BITFIELD key subcommmand-1 arg ... subcommand-2 arg ... subcommand-N ...
|
|
|
|
*
|
|
|
|
* Supported subcommands:
|
|
|
|
*
|
|
|
|
* GET <type> <offset>
|
|
|
|
* SET <type> <offset> <value>
|
|
|
|
* INCRBY <type> <offset> <increment>
|
|
|
|
* OVERFLOW [WRAP|SAT|FAIL]
|
|
|
|
*/
|
|
|
|
|
2020-03-23 06:28:09 -04:00
|
|
|
#define BITFIELD_FLAG_NONE 0
|
|
|
|
#define BITFIELD_FLAG_READONLY (1<<0)
|
2020-03-04 07:51:45 -05:00
|
|
|
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
struct bitfieldOp {
|
|
|
|
uint64_t offset; /* Bitfield offset. */
|
|
|
|
int64_t i64; /* Increment amount (INCRBY) or SET value */
|
|
|
|
int opcode; /* Operation id. */
|
|
|
|
int owtype; /* Overflow type to use. */
|
|
|
|
int bits; /* Integer bitfield bits width. */
|
|
|
|
int sign; /* True if signed, otherwise unsigned op. */
|
|
|
|
};
|
|
|
|
|
2020-03-23 06:28:09 -04:00
|
|
|
/* This implements both the BITFIELD command and the BITFIELD_RO command
|
|
|
|
* when flags is set to BITFIELD_FLAG_READONLY: in this case only the
|
|
|
|
* GET subcommand is allowed, other subcommands will return an error. */
|
2020-03-04 07:51:45 -05:00
|
|
|
void bitfieldGeneric(client *c, int flags) {
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
robj *o;
|
|
|
|
size_t bitoffset;
|
|
|
|
int j, numops = 0, changes = 0;
|
|
|
|
struct bitfieldOp *ops = NULL; /* Array of ops to execute at end. */
|
|
|
|
int owtype = BFOVERFLOW_WRAP; /* Overflow type. */
|
2016-05-24 16:31:36 -04:00
|
|
|
int readonly = 1;
|
2018-07-01 01:24:50 -04:00
|
|
|
size_t highest_write_offset = 0;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
|
|
|
|
for (j = 2; j < c->argc; j++) {
|
|
|
|
int remargs = c->argc-j-1; /* Remaining args other than current. */
|
|
|
|
char *subcmd = c->argv[j]->ptr; /* Current command name. */
|
|
|
|
int opcode; /* Current operation code. */
|
|
|
|
long long i64 = 0; /* Signed SET value. */
|
|
|
|
int sign = 0; /* Signed or unsigned type? */
|
|
|
|
int bits = 0; /* Bitfield width in bits. */
|
|
|
|
|
|
|
|
if (!strcasecmp(subcmd,"get") && remargs >= 2)
|
|
|
|
opcode = BITFIELDOP_GET;
|
|
|
|
else if (!strcasecmp(subcmd,"set") && remargs >= 3)
|
|
|
|
opcode = BITFIELDOP_SET;
|
|
|
|
else if (!strcasecmp(subcmd,"incrby") && remargs >= 3)
|
|
|
|
opcode = BITFIELDOP_INCRBY;
|
|
|
|
else if (!strcasecmp(subcmd,"overflow") && remargs >= 1) {
|
|
|
|
char *owtypename = c->argv[j+1]->ptr;
|
|
|
|
j++;
|
|
|
|
if (!strcasecmp(owtypename,"wrap"))
|
|
|
|
owtype = BFOVERFLOW_WRAP;
|
|
|
|
else if (!strcasecmp(owtypename,"sat"))
|
|
|
|
owtype = BFOVERFLOW_SAT;
|
|
|
|
else if (!strcasecmp(owtypename,"fail"))
|
|
|
|
owtype = BFOVERFLOW_FAIL;
|
|
|
|
else {
|
|
|
|
addReplyError(c,"Invalid OVERFLOW type specified");
|
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the type and offset arguments, common to all the ops. */
|
|
|
|
if (getBitfieldTypeFromArgument(c,c->argv[j+1],&sign,&bits) != C_OK) {
|
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-26 09:16:24 -05:00
|
|
|
if (getBitOffsetFromArgument(c,c->argv[j+2],&bitoffset,1,bits) != C_OK){
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode != BITFIELDOP_GET) {
|
2016-05-24 16:31:36 -04:00
|
|
|
readonly = 0;
|
2018-07-01 01:24:50 -04:00
|
|
|
if (highest_write_offset < bitoffset + bits - 1)
|
|
|
|
highest_write_offset = bitoffset + bits - 1;
|
2016-05-24 16:31:36 -04:00
|
|
|
/* INCRBY and SET require another argument. */
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
if (getLongLongFromObjectOrReply(c,c->argv[j+3],&i64,NULL) != C_OK){
|
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate the array of operations we'll process. */
|
|
|
|
ops = zrealloc(ops,sizeof(*ops)*(numops+1));
|
|
|
|
ops[numops].offset = bitoffset;
|
|
|
|
ops[numops].i64 = i64;
|
|
|
|
ops[numops].opcode = opcode;
|
|
|
|
ops[numops].owtype = owtype;
|
|
|
|
ops[numops].bits = bits;
|
|
|
|
ops[numops].sign = sign;
|
|
|
|
numops++;
|
|
|
|
|
|
|
|
j += 3 - (opcode == BITFIELDOP_GET);
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:36 -04:00
|
|
|
if (readonly) {
|
2016-06-16 06:54:33 -04:00
|
|
|
/* Lookup for read is ok if key doesn't exit, but errors
|
|
|
|
* if it's not a string. */
|
2016-05-24 16:31:36 -04:00
|
|
|
o = lookupKeyRead(c->db,c->argv[1]);
|
2019-04-08 21:24:22 -04:00
|
|
|
if (o != NULL && checkType(c,o,OBJ_STRING)) {
|
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-24 16:31:36 -04:00
|
|
|
} else {
|
2020-03-23 06:28:09 -04:00
|
|
|
if (flags & BITFIELD_FLAG_READONLY) {
|
2020-03-04 07:51:45 -05:00
|
|
|
zfree(ops);
|
2020-03-23 07:00:46 -04:00
|
|
|
addReplyError(c, "BITFIELD_RO only supports the GET subcommand");
|
2020-03-04 07:51:45 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:36 -04:00
|
|
|
/* Lookup by making room up to the farest bit reached by
|
|
|
|
* this operation. */
|
|
|
|
if ((o = lookupStringForBitCommand(c,
|
2019-04-08 21:24:22 -04:00
|
|
|
highest_write_offset)) == NULL) {
|
|
|
|
zfree(ops);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-24 16:31:36 -04:00
|
|
|
}
|
|
|
|
|
2018-11-30 04:40:54 -05:00
|
|
|
addReplyArrayLen(c,numops);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
|
|
|
|
/* Actually process the operations. */
|
|
|
|
for (j = 0; j < numops; j++) {
|
|
|
|
struct bitfieldOp *thisop = ops+j;
|
|
|
|
|
|
|
|
/* Execute the operation. */
|
|
|
|
if (thisop->opcode == BITFIELDOP_SET ||
|
|
|
|
thisop->opcode == BITFIELDOP_INCRBY)
|
|
|
|
{
|
|
|
|
/* SET and INCRBY: We handle both with the same code path
|
|
|
|
* for simplicity. SET return value is the previous value so
|
|
|
|
* we need fetch & store as well. */
|
|
|
|
|
2016-02-29 03:08:46 -05:00
|
|
|
/* We need two different but very similar code paths for signed
|
|
|
|
* and unsigned operations, since the set of functions to get/set
|
|
|
|
* the integers and the used variables types are different. */
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
if (thisop->sign) {
|
2016-02-29 03:08:46 -05:00
|
|
|
int64_t oldval, newval, wrapped, retval;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
int overflow;
|
|
|
|
|
|
|
|
oldval = getSignedBitfield(o->ptr,thisop->offset,
|
|
|
|
thisop->bits);
|
|
|
|
|
|
|
|
if (thisop->opcode == BITFIELDOP_INCRBY) {
|
|
|
|
newval = oldval + thisop->i64;
|
|
|
|
overflow = checkSignedBitfieldOverflow(oldval,
|
|
|
|
thisop->i64,thisop->bits,thisop->owtype,&wrapped);
|
|
|
|
if (overflow) newval = wrapped;
|
2016-02-29 03:08:46 -05:00
|
|
|
retval = newval;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
} else {
|
|
|
|
newval = thisop->i64;
|
|
|
|
overflow = checkSignedBitfieldOverflow(newval,
|
|
|
|
0,thisop->bits,thisop->owtype,&wrapped);
|
|
|
|
if (overflow) newval = wrapped;
|
2016-02-29 03:08:46 -05:00
|
|
|
retval = oldval;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
}
|
2016-02-29 03:08:46 -05:00
|
|
|
|
|
|
|
/* On overflow of type is "FAIL", don't write and return
|
|
|
|
* NULL to signal the condition. */
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
if (!(overflow && thisop->owtype == BFOVERFLOW_FAIL)) {
|
2016-02-29 03:08:46 -05:00
|
|
|
addReplyLongLong(c,retval);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
setSignedBitfield(o->ptr,thisop->offset,
|
|
|
|
thisop->bits,newval);
|
2016-02-29 03:08:46 -05:00
|
|
|
} else {
|
2018-11-30 04:40:54 -05:00
|
|
|
addReplyNull(c);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-29 03:08:46 -05:00
|
|
|
uint64_t oldval, newval, wrapped, retval;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
int overflow;
|
|
|
|
|
|
|
|
oldval = getUnsignedBitfield(o->ptr,thisop->offset,
|
|
|
|
thisop->bits);
|
|
|
|
|
|
|
|
if (thisop->opcode == BITFIELDOP_INCRBY) {
|
|
|
|
newval = oldval + thisop->i64;
|
|
|
|
overflow = checkUnsignedBitfieldOverflow(oldval,
|
|
|
|
thisop->i64,thisop->bits,thisop->owtype,&wrapped);
|
|
|
|
if (overflow) newval = wrapped;
|
2016-02-29 03:08:46 -05:00
|
|
|
retval = newval;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
} else {
|
|
|
|
newval = thisop->i64;
|
|
|
|
overflow = checkUnsignedBitfieldOverflow(newval,
|
|
|
|
0,thisop->bits,thisop->owtype,&wrapped);
|
|
|
|
if (overflow) newval = wrapped;
|
2016-02-29 03:08:46 -05:00
|
|
|
retval = oldval;
|
|
|
|
}
|
|
|
|
/* On overflow of type is "FAIL", don't write and return
|
|
|
|
* NULL to signal the condition. */
|
|
|
|
if (!(overflow && thisop->owtype == BFOVERFLOW_FAIL)) {
|
|
|
|
addReplyLongLong(c,retval);
|
|
|
|
setUnsignedBitfield(o->ptr,thisop->offset,
|
|
|
|
thisop->bits,newval);
|
|
|
|
} else {
|
2018-11-30 04:40:54 -05:00
|
|
|
addReplyNull(c);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
changes++;
|
|
|
|
} else {
|
|
|
|
/* GET */
|
|
|
|
unsigned char buf[9];
|
2016-05-24 07:52:43 -04:00
|
|
|
long strlen = 0;
|
2016-05-10 04:19:45 -04:00
|
|
|
unsigned char *src = NULL;
|
2016-05-18 09:35:17 -04:00
|
|
|
char llbuf[LONG_STR_SIZE];
|
2016-05-10 04:19:45 -04:00
|
|
|
|
2016-05-24 16:31:36 -04:00
|
|
|
if (o != NULL)
|
2016-05-24 07:52:43 -04:00
|
|
|
src = getObjectReadOnlyString(o,&strlen,llbuf);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
|
|
|
|
/* For GET we use a trick: before executing the operation
|
|
|
|
* copy up to 9 bytes to a local buffer, so that we can easily
|
|
|
|
* execute up to 64 bit operations that are at actual string
|
|
|
|
* object boundaries. */
|
|
|
|
memset(buf,0,9);
|
|
|
|
int i;
|
|
|
|
size_t byte = thisop->offset >> 3;
|
|
|
|
for (i = 0; i < 9; i++) {
|
2016-05-18 09:35:17 -04:00
|
|
|
if (src == NULL || i+byte >= (size_t)strlen) break;
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
buf[i] = src[i+byte];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now operate on the copied buffer which is guaranteed
|
|
|
|
* to be zero-padded. */
|
|
|
|
if (thisop->sign) {
|
|
|
|
int64_t val = getSignedBitfield(buf,thisop->offset-(byte*8),
|
|
|
|
thisop->bits);
|
|
|
|
addReplyLongLong(c,val);
|
|
|
|
} else {
|
|
|
|
uint64_t val = getUnsignedBitfield(buf,thisop->offset-(byte*8),
|
|
|
|
thisop->bits);
|
|
|
|
addReplyLongLong(c,val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changes) {
|
2020-04-21 04:51:46 -04:00
|
|
|
signalModifiedKey(c,c->db,c->argv[1]);
|
BITFIELD command initial implementation.
The new bitfield command is an extension to the Redis bit operations,
where not just single bit operations are performed, but the array of
bits composing a string, can be addressed at random, not aligned
offsets, with any width unsigned and signed integers like u8, s5, u10
(up to 64 bit signed integers and 63 bit unsigned integers).
The BITFIELD command supports subcommands that can SET, GET, or INCRBY
those arbitrary bit counters, with multiple overflow semantics.
Trivial and credits:
A similar command was imagined a few times in the past, but for
some reason looked a bit far fetched or not well specified.
Finally the command was proposed again in a clear form by
Yoav Steinberg from Redis Labs, that proposed a set of commands on
arbitrary sized integers stored at bit offsets.
Starting from this proposal I wrote an initial specification of a single
command with sub-commands similar to what Yoav envisioned, using short
names for types definitions, and adding control on the overflow.
This commit is the resulting implementation.
Examples:
BITFIELD mykey OVERFLOW wrap INCRBY i2 10 -1 GET i2 10
2016-02-25 17:31:45 -05:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"setbit",c->argv[1],c->db->id);
|
|
|
|
server.dirty += changes;
|
|
|
|
}
|
|
|
|
zfree(ops);
|
|
|
|
}
|
2020-03-04 07:51:45 -05:00
|
|
|
|
|
|
|
void bitfieldCommand(client *c) {
|
2020-03-23 06:28:09 -04:00
|
|
|
bitfieldGeneric(c, BITFIELD_FLAG_NONE);
|
2020-03-04 07:51:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void bitfieldroCommand(client *c) {
|
2020-03-23 06:28:09 -04:00
|
|
|
bitfieldGeneric(c, BITFIELD_FLAG_READONLY);
|
2020-03-04 07:51:45 -05:00
|
|
|
}
|