Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/commands/cmd_bit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ class CommandBitOp : public Commander {
op_flag_ = kBitOpXor;
else if (opname == "not")
op_flag_ = kBitOpNot;
else if (opname == "diff")
op_flag_ = kBitOpDiff;
else if (opname == "diff1")
op_flag_ = kBitOpDiff1;
else if (opname == "andor")
op_flag_ = kBitOpAndOr;
else if (opname == "one")
op_flag_ = kBitOpOne;
else
return {Status::RedisInvalidCmd, errInvalidSyntax};
if (op_flag_ == kBitOpNot && args.size() != 4) {
Expand Down
129 changes: 129 additions & 0 deletions src/types/redis_bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,80 @@ rocksdb::Status Bitmap::BitOp(engine::Context &ctx, BitOpFlags op_flag, const st
apply_fast_path_op([](uint64_t &a, uint64_t b) { a |= b; });
} else if (op_flag == kBitOpXor) {
apply_fast_path_op([](uint64_t &a, uint64_t b) { a ^= b; });
} else if (op_flag == kBitOpDiff) {
// X & (~(Y1 | Y2 | ...))
uint64_t others_or[4];
while (frag_minlen >= sizeof(uint64_t) * 4) {
for (uint64_t k = 0; k < 4; k++) {
others_or[k] = 0;
for (uint64_t i = 1; i < frag_numkeys; i++) {
others_or[k] |= lp[i][k];
}
lres[k] = lres[k] & ~others_or[k];
}
for (uint64_t i = 1; i < frag_numkeys; i++) {
lp[i] += 4;
}
lres += 4;
j += sizeof(uint64_t) * 4;
frag_minlen -= sizeof(uint64_t) * 4;
}
} else if (op_flag == kBitOpDiff1) {
// (~X) & (Y1 | Y2 | ...)
uint64_t others_or[4];
while (frag_minlen >= sizeof(uint64_t) * 4) {
for (uint64_t k = 0; k < 4; k++) {
others_or[k] = 0;
for (uint64_t i = 1; i < frag_numkeys; i++) {
others_or[k] |= lp[i][k];
}
lres[k] = ~lres[k] & others_or[k];
}
for (uint64_t i = 1; i < frag_numkeys; i++) {
lp[i] += 4;
}
lres += 4;
j += sizeof(uint64_t) * 4;
frag_minlen -= sizeof(uint64_t) * 4;
}
} else if (op_flag == kBitOpAndOr) {
// X & (Y1 | Y2 | ...)
uint64_t others_or[4];
while (frag_minlen >= sizeof(uint64_t) * 4) {
for (uint64_t k = 0; k < 4; k++) {
others_or[k] = 0;
for (uint64_t i = 1; i < frag_numkeys; i++) {
others_or[k] |= lp[i][k];
}
lres[k] = lres[k] & others_or[k];
}
for (uint64_t i = 1; i < frag_numkeys; i++) {
lp[i] += 4;
}
lres += 4;
j += sizeof(uint64_t) * 4;
frag_minlen -= sizeof(uint64_t) * 4;
}
} else if (op_flag == kBitOpOne) {
// (X1 ^ X2 ^ ...) & (~(X1 & X2 & ...))
uint64_t all_and[4], all_xor[4];
while (frag_minlen >= sizeof(uint64_t) * 4) {
for (uint64_t k = 0; k < 4; k++) {
all_and[k] = lp[0][k];
all_xor[k] = lp[0][k];
for (uint64_t i = 1; i < frag_numkeys; i++) {
all_and[k] &= lp[i][k];
all_xor[k] ^= lp[i][k];
}
lres[k] = all_xor[k] & ~all_and[k];
}
for (uint64_t i = 0; i < frag_numkeys; i++) {
lp[i] += 4;
}
lres += 4;
j += sizeof(uint64_t) * 4;
frag_minlen -= sizeof(uint64_t) * 4;
}
} else if (op_flag == kBitOpNot) {
while (frag_minlen >= sizeof(uint64_t) * 4) {
lres[0] = ~lres[0];
Expand All @@ -597,6 +671,46 @@ rocksdb::Status Bitmap::BitOp(engine::Context &ctx, BitOpFlags op_flag, const st
for (; j < frag_maxlen; j++) {
output = (fragments[0].size() <= j) ? 0 : fragments[0][j];
if (op_flag == kBitOpNot) output = ~output;

// For DIFF, DIFF1, ANDOR, and ONE operations, we need special handling
if (op_flag == kBitOpDiff || op_flag == kBitOpDiff1 || op_flag == kBitOpAndOr) {
// Calculate OR of all keys except the first one
uint8_t others_or = 0;
for (uint64_t i = 1; i < frag_numkeys; i++) {
byte = (fragments[i].size() <= j) ? 0 : fragments[i][j];
others_or |= byte;
}

if (op_flag == kBitOpDiff) {
// X & (~(Y1 | Y2 | ...))
output = output & ~others_or;
} else if (op_flag == kBitOpDiff1) {
// (~X) & (Y1 | Y2 | ...)
output = ~output & others_or;
} else if (op_flag == kBitOpAndOr) {
// X & (Y1 | Y2 | ...)
output = output & others_or;
}
frag_res[j] = output;
continue;
}

if (op_flag == kBitOpOne) {
// Calculate AND and XOR of all keys
uint8_t all_and = (fragments[0].size() <= j) ? 0 : fragments[0][j];
uint8_t all_xor = all_and;
for (uint64_t i = 1; i < frag_numkeys; i++) {
byte = (fragments[i].size() <= j) ? 0 : fragments[i][j];
all_and &= byte;
all_xor ^= byte;
}
// (X1 ^ X2 ^ ...) & (~(X1 & X2 & ...))
output = all_xor & ~all_and;
frag_res[j] = output;
continue;
}

// Standard operations: AND, OR, XOR, NOT
for (uint64_t i = 1; i < frag_numkeys; i++) {
byte = (fragments[i].size() <= j) ? 0 : fragments[i][j];
switch (op_flag) {
Expand All @@ -609,6 +723,8 @@ rocksdb::Status Bitmap::BitOp(engine::Context &ctx, BitOpFlags op_flag, const st
case kBitOpXor:
output ^= byte;
break;
case kBitOpNot:
// NOT operation ignores other keys after the first one
default:
break;
}
Expand All @@ -631,6 +747,19 @@ rocksdb::Status Bitmap::BitOp(engine::Context &ctx, BitOpFlags op_flag, const st
} else {
frag_maxlen = kBitmapSegmentBytes;
}
} else if (op_flag == kBitOpDiff || op_flag == kBitOpDiff1 || op_flag == kBitOpAndOr ||
op_flag == kBitOpOne) {
// For DIFF, DIFF1, ANDOR, and ONE operations, we need to ensure
// frag_maxlen is correctly set for the last segment
if (frag_index == stop_index) {
if (max_bitmap_size == (frag_index + 1) * kBitmapSegmentBytes) {
frag_maxlen = kBitmapSegmentBytes;
} else {
frag_maxlen = max_bitmap_size % kBitmapSegmentBytes;
}
} else {
frag_maxlen = kBitmapSegmentBytes;
}
}
std::string sub_key = InternalKey(ns_key, std::to_string(frag_index * kBitmapSegmentBytes),
res_metadata.version, storage_->IsSlotIdEncoded())
Expand Down
4 changes: 4 additions & 0 deletions src/types/redis_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ enum BitOpFlags {
kBitOpOr,
kBitOpXor,
kBitOpNot,
kBitOpDiff,
kBitOpDiff1,
kBitOpAndOr,
kBitOpOne,
};

namespace redis {
Expand Down
Loading
Loading