Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flip a bitmap by range #77

Merged
merged 3 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ core
vgcore*
dist/
cmake-build-debug/
src/roaring.c
src/roaring.h
5 changes: 5 additions & 0 deletions src/data-structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ Bitmap* bitmap_not_array(uint32_t unused, const Bitmap** bitmaps) {
return roaring_bitmap_flip(bitmaps[0], 0, last + 1);
}

Bitmap* bitmap_flip(const Bitmap* bitmap, uint32_t end) {
return roaring_bitmap_flip(bitmap, 0, end);
}


Bitmap* bitmap_not(const Bitmap* bitmap) {
const Bitmap* bitmaps[] = {bitmap};
return bitmap_not_array(1, bitmaps);
Expand Down
1 change: 1 addition & 0 deletions src/data-structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int64_t bitmap_get_nth_element_not_present_slow(const Bitmap* bitmap, uint64_t n
Bitmap* bitmap_or(uint32_t n, const Bitmap** bitmaps);
Bitmap* bitmap_and(uint32_t n, const Bitmap** bitmaps);
Bitmap* bitmap_xor(uint32_t n, const Bitmap** bitmaps);
Bitmap* bitmap_flip(const Bitmap* bitmap, uint32_t end);
/**
* Returns a bitmap equals to the inverted set of first bitmap of the `bitmaps` array,
* ignoring the first parameter of the function. * The purpose of this function is to
Expand Down
100 changes: 87 additions & 13 deletions src/redis-roaring.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@ int RGetBitArrayCommand(RedisModuleCtx* ctx, RedisModuleString** argv, int argc)


int RBitOp(RedisModuleCtx* ctx, RedisModuleString** argv, int argc, Bitmap* (* operation)(uint32_t, const Bitmap**)) {
if (RedisModule_IsKeysPositionRequest(ctx) > 0) {
if (argc > 4) {
for (int i = 2; i < argc; i++) {
RedisModule_KeyAtPos(ctx, i);
}
}
return REDISMODULE_OK;
}
if (argc == 4) return RedisModule_WrongArity(ctx);

// open destkey for writing
RedisModuleKey* destkey = RedisModule_OpenKey(ctx, argv[2], REDISMODULE_READ | REDISMODULE_WRITE);
int desttype = RedisModule_KeyType(destkey);
Expand Down Expand Up @@ -515,6 +525,71 @@ int RBitOp(RedisModuleCtx* ctx, RedisModuleString** argv, int argc, Bitmap* (* o
return REDISMODULE_OK;
}


int RBitFlip(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
if (RedisModule_IsKeysPositionRequest(ctx) > 0) {
if (argc <= 5) {
RedisModule_KeyAtPos(ctx, 2);
RedisModule_KeyAtPos(ctx, 3);
}
return REDISMODULE_OK;
}

long long last = 0;
if (argc == 5) {
if ((RedisModule_StringToLongLong(argv[4], &last) != REDISMODULE_OK)) {
return RedisModule_ReplyWithError(ctx, "ERR invalid last: must be an unsigned 32 bit integer");
}
} else if (argc > 5) {
return RedisModule_WrongArity(ctx);
}

// open destkey for writing
RedisModuleKey* destkey = RedisModule_OpenKey(ctx, argv[2], REDISMODULE_READ | REDISMODULE_WRITE);
int desttype = RedisModule_KeyType(destkey);
if (desttype != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(destkey) != BitmapType) {
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
}

// checks for srckey types
RedisModuleKey* srckey = RedisModule_OpenKey(ctx, argv[3], REDISMODULE_READ);
int srctype = RedisModule_KeyType(srckey);
if (srctype != REDISMODULE_KEYTYPE_EMPTY && RedisModule_ModuleTypeGetType(srckey) != BitmapType) {
return RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE);
}

bool should_free = false;
Bitmap* bitmap;
if (srctype == REDISMODULE_KEYTYPE_EMPTY) {
// "non-existent keys [...] are considered as a stream of zero bytes up to the length of the longest string"
bitmap = bitmap_alloc();
should_free = true;
} else if (RedisModule_ModuleTypeGetType(srckey) == BitmapType) {
bitmap = RedisModule_ModuleTypeGetValue(srckey);
should_free = false;
}

if (argc == 4) {
last = bitmap_max(bitmap);
}
// calculate destkey bitmap
Bitmap* result = bitmap_flip(bitmap, last+1);
RedisModule_ModuleTypeSetValue(destkey, BitmapType, result);

if (should_free) {
bitmap_free(bitmap);
}

RedisModule_ReplicateVerbatim(ctx);

// Integer reply: The size of the string stored in the destination key
// (adapted to cardinality)
uint64_t cardinality = bitmap_get_cardinality(result);
RedisModule_ReplyWithLongLong(ctx, (long long) cardinality);

return REDISMODULE_OK;
}

/**
* BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyN
* BITOP OR destkey srckey1 srckey2 srckey3 ... srckeyN
Expand All @@ -524,30 +599,29 @@ int RBitOp(RedisModuleCtx* ctx, RedisModuleString** argv, int argc, Bitmap* (* o
* */
int RBitOpCommand(RedisModuleCtx* ctx, RedisModuleString** argv, int argc) {
if (argc < 4) {
return RedisModule_WrongArity(ctx);
return (RedisModule_IsKeysPositionRequest(ctx) > 0) ? REDISMODULE_OK : RedisModule_WrongArity(ctx);
}

RedisModule_AutoMemory(ctx);
size_t len;
const char* operation = RedisModule_StringPtrLen(argv[1], &len);

if (strcmp(operation, "NOT") == 0) {
if (argc != 4) return RedisModule_WrongArity(ctx);
else return RBitOp(ctx, argv, argc, bitmap_not_array);
return RBitFlip(ctx, argv, argc);
} else if (strcmp(operation, "AND") == 0) {
if (argc == 4) return RedisModule_WrongArity(ctx);
else return RBitOp(ctx, argv, argc, bitmap_and);
return RBitOp(ctx, argv, argc, bitmap_and);
} else if (strcmp(operation, "OR") == 0) {
if (argc == 4) return RedisModule_WrongArity(ctx);
else return RBitOp(ctx, argv, argc, bitmap_or);
return RBitOp(ctx, argv, argc, bitmap_or);
} else if (strcmp(operation, "XOR") == 0) {
if (argc == 4) return RedisModule_WrongArity(ctx);
else return RBitOp(ctx, argv, argc, bitmap_xor);
return RBitOp(ctx, argv, argc, bitmap_xor);
} else {
RedisModule_ReplyWithSimpleString(ctx, "ERR syntax error");
return REDISMODULE_ERR;
if (RedisModule_IsKeysPositionRequest(ctx) > 0) {
return REDISMODULE_OK;
} else {
RedisModule_ReplyWithSimpleString(ctx, "ERR syntax error");
return REDISMODULE_ERR;
}
}

return REDISMODULE_OK;
}

Expand Down Expand Up @@ -723,7 +797,7 @@ int RedisModule_OnLoad(RedisModuleCtx* ctx) {
if (RedisModule_CreateCommand(ctx, "R.GETBITARRAY", RGetBitArrayCommand, "readonly", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "R.BITOP", RBitOpCommand, "write", 2, -1, 1) == REDISMODULE_ERR) {
if (RedisModule_CreateCommand(ctx, "R.BITOP", RBitOpCommand, "write getkeys-api", 0, 0, 0) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "R.BITCOUNT", RBitCountCommand, "readonly", 1, 1, 1) == REDISMODULE_ERR) {
Expand Down
14 changes: 12 additions & 2 deletions tests/integration_1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,24 @@ function test_bitop()
function not()
{
FOUND=$(echo "R.BITOP NOT test_bitop_dest_3 test_bitop_4" | ./deps/redis/src/redis-cli)
EXPECTED="2"
EXPECTED="1"
[ "$FOUND" == "$EXPECTED" ]
FOUND=$(echo "R.GETBIT test_bitop_dest_3 0" | ./deps/redis/src/redis-cli)
EXPECTED="1"
[ "$FOUND" == "$EXPECTED" ]
FOUND=$(echo "R.GETBIT test_bitop_dest_3 1" | ./deps/redis/src/redis-cli)
FOUND=$(echo "R.MAX test_bitop_dest_3" | ./deps/redis/src/redis-cli)
EXPECTED="0"
[ "$FOUND" == "$EXPECTED" ]

FOUND=$(echo "R.BITOP NOT test_bitop_dest_3 test_bitop_4 3" | ./deps/redis/src/redis-cli)
EXPECTED="2"
[ "$FOUND" == "$EXPECTED" ]
FOUND=$(echo "R.GETBIT test_bitop_dest_3 0" | ./deps/redis/src/redis-cli)
EXPECTED="1"
[ "$FOUND" == "$EXPECTED" ]
FOUND=$(echo "R.MAX test_bitop_dest_3" | ./deps/redis/src/redis-cli)
EXPECTED="3"
[ "$FOUND" == "$EXPECTED" ]
}

function collision()
Expand Down