Skip to content

Commit

Permalink
fix: feat: implement the command SAdd (OpenAtomFoundation#106)
Browse files Browse the repository at this point in the history
* bug fix:sadd

* format

* fix: only the increased size is returned

fix:only the increased size is returned
  • Loading branch information
578223592 authored Jan 6, 2024
1 parent 07c6ab6 commit 761fb14
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/cmd_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ bool SAddCmd::DoInitial(PClient* client) {
client->SetKey(client->argv_[1]);
return true;
}
// Integer reply: the number of elements that were added to the set,
// not including all the elements already present in the set.
void SAddCmd::DoCmd(PClient* client) {
PObject* value = nullptr;
PError err = PSTORE.GetValueByType(client->Key(), value, kPTypeSet);
Expand All @@ -51,11 +53,11 @@ void SAddCmd::DoCmd(PClient* client) {
}
}
auto set = value->CastSet();
auto resPair = set->emplace(client->argv_[2]);
if (resPair.second) {
client->AppendInteger(1);
} else {
client->AppendInteger(0);
const auto oldSize = set->size();
for (int i = 2; i < client->argv_.size(); ++i) {
set->insert(client->argv_[i]);
}
// new size is bigger than old size , avoid the risk
client->AppendInteger(set->size() - oldSize);
}
} // namespace pikiwidb

0 comments on commit 761fb14

Please sign in to comment.