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

chore(hyperloglog): add go test cases and minor optimizations/bugfixes #2463

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Bugfix and add basic go tests
mapleFU committed Aug 4, 2024
commit 45717f5334055ad82e4de98bb68bfbb0fef6da43
23 changes: 13 additions & 10 deletions src/commands/cmd_hll.cc
Original file line number Diff line number Diff line change
@@ -35,12 +35,13 @@ class CommandPfAdd final : public Commander {
public:
Status Execute(Server *srv, Connection *conn, std::string *output) override {
redis::HyperLogLog hll(srv->storage, conn->GetNamespace());
std::vector<uint64_t> hashes(args_.size() - 1);
for (size_t i = 1; i < args_.size(); i++) {
hashes[i - 1] = redis::HyperLogLog::HllHash(args_[i]);
DCHECK_GE(args_.size(), 2);
std::vector<uint64_t> hashes(args_.size() - 2);
for (size_t i = 2; i < args_.size(); i++) {
hashes[i - 2] = redis::HyperLogLog::HllHash(args_[i]);
}
uint64_t ret{};
auto s = hll.Add(args_[0], hashes, &ret);
auto s = hll.Add(args_[1], hashes, &ret);
if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
}
@@ -58,11 +59,13 @@ class CommandPfCount final : public Commander {
redis::HyperLogLog hll(srv->storage, conn->GetNamespace());
uint64_t ret{};
rocksdb::Status s;
if (args_.size() > 1) {
std::vector<Slice> keys(args_.begin(), args_.end());
// The first argument is the command name, so we need to skip it.
DCHECK_GE(args_.size(), 1);
if (args_.size() > 2) {
std::vector<Slice> keys(args_.begin() + 2, args_.end());
s = hll.CountMultiple(keys, &ret);
} else {
s = hll.Count(args_[0], &ret);
s = hll.Count(args_[1], &ret);
}
if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
@@ -81,9 +84,9 @@ class CommandPfCount final : public Commander {
class CommandPfMerge final : public Commander {
Status Execute(Server *srv, Connection *conn, std::string *output) override {
redis::HyperLogLog hll(srv->storage, conn->GetNamespace());
DCHECK_GT(args_.size(), 0);
std::vector<Slice> src_user_keys(args_.begin() + 1, args_.end());
auto s = hll.Merge(/*dest_user_key=*/args_[0], src_user_keys);
DCHECK_GT(args_.size(), 1);
std::vector<Slice> src_user_keys(args_.begin() + 2, args_.end());
auto s = hll.Merge(/*dest_user_key=*/args_[1], src_user_keys);
if (!s.ok() && !s.IsNotFound()) {
return {Status::RedisExecErr, s.ToString()};
}
12 changes: 9 additions & 3 deletions src/types/redis_hyperloglog.cc
Original file line number Diff line number Diff line change
@@ -115,7 +115,9 @@ rocksdb::Status HyperLogLog::Add(const Slice &user_key, const std::vector<uint64
LockGuard guard(storage_->GetLockManager(), ns_key);
HyperLogLogMetadata metadata{};
rocksdb::Status s = GetMetadata(GetOptions(), ns_key, &metadata);
if (!s.ok() && !s.IsNotFound()) return s;
if (!s.ok() && !s.IsNotFound()) {
return s;
}

auto batch = storage_->GetWriteBatchBase();
WriteBatchLogData log_data(kRedisHyperLogLog);
@@ -148,7 +150,9 @@ rocksdb::Status HyperLogLog::Add(const Slice &user_key, const std::vector<uint64
}
}
// Nothing changed, no need to flush the segments
if (*ret == 0) return rocksdb::Status::OK();
if (*ret == 0) {
return rocksdb::Status::OK();
}

// Flush dirty segments
// Release memory after batch is written
@@ -179,7 +183,9 @@ rocksdb::Status HyperLogLog::Count(const Slice &user_key, uint64_t *ret) {
LatestSnapShot ss(storage_);
Database::GetOptions get_options(ss.GetSnapShot());
auto s = getRegisters(get_options, ns_key, &registers);
if (!s.ok()) return s;
if (!s.ok()) {
return s;
}
}
DCHECK_EQ(kHyperLogLogSegmentCount, registers.size());
std::vector<nonstd::span<const uint8_t>> register_segments = TransformToSpan(registers);
101 changes: 101 additions & 0 deletions tests/gocase/unit/hyperloglog/hyperloglog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package hyperloglog

import (
"context"
"testing"

"github.com/apache/kvrocks/tests/gocase/util"
"github.com/stretchr/testify/require"
)

func TestHyperLogLog(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
defer srv.Close()

ctx := context.Background()
rdb := srv.NewClient()
defer func() { require.NoError(t, rdb.Close()) }()

t.Run("basic add", func(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "DEL", "hll").Err())

card, err := rdb.PFCount(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 0, card)
addCnt, err := rdb.PFAdd(ctx, "hll", "foo").Result()
require.NoError(t, err)
require.EqualValues(t, 1, addCnt)
card, err = rdb.PFCount(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 1, card)
})

t.Run("duplicate add", func(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "DEL", "hll").Err())

card, err := rdb.PFCount(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 0, card)
addCnt, err := rdb.PFAdd(ctx, "hll", "foo").Result()
require.NoError(t, err)
require.EqualValues(t, 1, addCnt)
addCnt, err = rdb.PFAdd(ctx, "hll", "foo").Result()
require.NoError(t, err)
require.EqualValues(t, 0, addCnt)
})

t.Run("empty add", func(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "DEL", "hll").Err())

addCnt, err := rdb.PFAdd(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 0, addCnt)

card, err := rdb.PFCount(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 0, card)
})

t.Run("multiple add", func(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "DEL", "hll").Err())

addCnt, err := rdb.PFAdd(ctx, "hll", "a", "b", "c", "d").Result()
require.NoError(t, err)
require.EqualValues(t, 1, addCnt)

card, err := rdb.PFCount(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 4, card)

addCnt, err = rdb.PFAdd(ctx, "hll", "a", "b", "c").Result()
require.NoError(t, err)
require.EqualValues(t, 0, addCnt)

addCnt, err = rdb.PFAdd(ctx, "hll", "a", "f", "c").Result()
require.NoError(t, err)
require.EqualValues(t, 1, addCnt)

card, err = rdb.PFCount(ctx, "hll").Result()
require.NoError(t, err)
require.EqualValues(t, 5, card)
})
}