YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/integration-tests/redis_table_test_base.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/integration-tests/redis_table_test_base.h"
15
16
#include <glog/logging.h>
17
18
#include "yb/client/client.h"
19
#include "yb/client/session.h"
20
#include "yb/client/table.h"
21
#include "yb/client/yb_op.h"
22
#include "yb/client/yb_table_name.h"
23
24
#include "yb/common/partition.h"
25
#include "yb/common/redis_constants_common.h"
26
#include "yb/common/redis_protocol.pb.h"
27
28
#include "yb/integration-tests/yb_table_test_base.h"
29
30
#include "yb/util/monotime.h"
31
32
#include "yb/yql/redis/redisserver/redis_parser.h"
33
34
using std::string;
35
using std::vector;
36
using std::unique_ptr;
37
38
namespace yb {
39
namespace integration_tests {
40
41
using client::YBRedisWriteOp;
42
using client::YBRedisReadOp;
43
using client::YBColumnSchema;
44
using client::YBTableCreator;
45
using client::YBSchemaBuilder;
46
using client::YBColumnSchema;
47
using client::YBTableType;
48
using client::YBTableName;
49
using client::YBSession;
50
51
using redisserver::RedisClientCommand;
52
using redisserver::ParseSet;
53
using redisserver::ParseGet;
54
55
0
YBTableName RedisTableTestBase::table_name() {
56
0
  return YBTableName(YQL_DATABASE_REDIS, common::kRedisKeyspaceName, common::kRedisTableName);
57
0
}
58
59
0
void RedisTableTestBase::CreateTable() {
60
0
  if (!table_exists_) {
61
0
    CreateRedisTable(table_name());
62
0
    client::YBSchema schema;
63
0
    PartitionSchema partition_schema;
64
0
    CHECK_OK(client_->GetTableSchema(RedisTableTestBase::table_name(), &schema, &partition_schema));
65
0
    ASSERT_EQ(partition_schema.hash_schema(), YBHashSchema::kRedisHash);
66
0
    table_exists_ = true;
67
0
  }
68
0
}
69
70
0
RedisClientCommand SlicesFromString(const vector<string>& args) {
71
0
  RedisClientCommand vector_slice;
72
0
  for (const string& s : args) {
73
0
    vector_slice.emplace_back(s);
74
0
  }
75
0
  return vector_slice;
76
0
}
77
78
0
void RedisTableTestBase::PutKeyValue(string key, string value) {
79
0
  auto set_op = std::make_shared<YBRedisWriteOp>(table_->shared_from_this());
80
0
  ASSERT_OK(ParseSet(set_op.get(), SlicesFromString({"set", key, value})));
81
0
  ASSERT_OK(session_->ApplyAndFlush(set_op));
82
0
}
83
84
0
void RedisTableTestBase::PutKeyValueWithTtlNoFlush(string key, string value, int64_t ttl_msec) {
85
0
  auto set_op = std::make_shared<YBRedisWriteOp>(table_->shared_from_this());
86
0
  ASSERT_OK(ParseSet(set_op.get(),
87
0
      SlicesFromString({"set", key, value, "PX", std::to_string(ttl_msec)})));
88
0
  session_->Apply(set_op);
89
0
}
90
91
void RedisTableTestBase::GetKeyValue(
92
0
    const string& key, const string& value, bool expect_not_found) {
93
0
  auto get_op = std::make_shared<YBRedisReadOp>(table_->shared_from_this());
94
0
  ASSERT_OK(ParseGet(get_op.get(), SlicesFromString({"get", key})));
95
0
  ASSERT_OK(session_->ReadSync(get_op));
96
0
  if (expect_not_found) {
97
0
    ASSERT_EQ(RedisResponsePB_RedisStatusCode_NIL, get_op->response().code());
98
0
  } else {
99
0
    ASSERT_EQ(RedisResponsePB_RedisStatusCode_OK, get_op->response().code());
100
0
    ASSERT_EQ(value, get_op->response().string_response());
101
0
  }
102
0
}
103
104
0
void RedisTableTestBase::RedisSimpleSetCommands() {
105
0
  session_ = NewSession();
106
0
  PutKeyValue("key123", "value123");
107
0
  PutKeyValue("key200", "value200");
108
0
  PutKeyValue("key300", "value300");
109
0
}
110
111
0
void RedisTableTestBase::RedisSimpleGetCommands() {
112
0
  session_ = NewSession();
113
0
  GetKeyValue("key123", "value123");
114
0
  GetKeyValue("key200", "value200");
115
0
  GetKeyValue("key300", "value300");
116
0
  GetKeyValue("key400", "value400", true);
117
0
}
118
119
0
void RedisTableTestBase::RedisTtlSetCommands() {
120
0
  session_ = NewSession();
121
0
  PutKeyValueWithTtlNoFlush("key456", "value456", 10000);
122
0
  PutKeyValueWithTtlNoFlush("key567", "value567", 500);
123
0
  PutKeyValue("key678", "value678");
124
  // The first two commands do not flush the session, but the last command does.
125
0
}
126
127
0
void RedisTableTestBase::RedisTtlGetCommands() {
128
0
  session_ = NewSession();
129
0
  GetKeyValue("key456", "value456", false);
130
0
  GetKeyValue("key567", "value567", true);
131
0
  GetKeyValue("key678", "value678", false);
132
0
}
133
134
}  // namespace integration_tests
135
}  // namespace yb