/Users/deen/code/yugabyte-db/src/yb/common/partition-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <stdint.h> |
34 | | |
35 | | #include <iterator> |
36 | | #include <string> |
37 | | #include <vector> |
38 | | |
39 | | #include <gtest/gtest.h> |
40 | | |
41 | | #include "yb/common/common.pb.h" |
42 | | #include "yb/common/crc16.h" |
43 | | #include "yb/common/partial_row.h" |
44 | | #include "yb/common/partition.h" |
45 | | #include "yb/common/schema.h" |
46 | | |
47 | | #include "yb/util/monotime.h" |
48 | | #include "yb/util/test_macros.h" |
49 | | |
50 | | #include "yb/yql/redis/redisserver/redis_constants.h" |
51 | | |
52 | | using std::vector; |
53 | | using std::string; |
54 | | |
55 | | namespace yb { |
56 | | |
57 | 8 | string EncodeRedisKey(const Slice& key) { |
58 | 8 | return PartitionSchema::EncodeMultiColumnHashValue( |
59 | 8 | crc16(key.data(), key.size()) % kRedisClusterSlots); |
60 | 8 | } |
61 | | |
62 | 1 | TEST(PartitionTest, TestRedisEncoding) { |
63 | 1 | Schema schema({ ColumnSchema("key", STRING, false, true) }, { ColumnId(0) }, 1); |
64 | | |
65 | 1 | PartitionSchema partition_schema; |
66 | 1 | ASSERT_OK(PartitionSchema::FromPB(PartitionSchemaPB(), schema, &partition_schema)); |
67 | | |
68 | 1 | YBPartialRow split1(&schema); |
69 | 1 | ASSERT_OK(split1.SetStringCopy("key", "{user1000}.following")); |
70 | 1 | string pk1; |
71 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
72 | 1 | YBPartialRow split2(&schema); |
73 | 1 | ASSERT_OK(split2.SetStringCopy("key", "{user1000}.followers")); |
74 | 1 | string pk2; |
75 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split2, &pk2)); |
76 | 1 | ASSERT_EQ(pk1, pk2); |
77 | 1 | pk2 = EncodeRedisKey(Slice("user1000")); |
78 | 1 | ASSERT_EQ(pk1, pk2); |
79 | | |
80 | 1 | ASSERT_OK(split1.SetStringCopy("key", "foo{}{bar}")); |
81 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
82 | 1 | pk2 = EncodeRedisKey(Slice("foo{}{bar}")); |
83 | 1 | ASSERT_EQ(pk1, pk2); |
84 | | |
85 | 1 | ASSERT_OK(split1.SetStringCopy("key", "foo{{bar}}zap")); |
86 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
87 | 1 | pk2 = EncodeRedisKey(Slice("{bar")); |
88 | 1 | ASSERT_EQ(pk1, pk2); |
89 | | |
90 | 1 | ASSERT_OK(split1.SetStringCopy("key", "foo{bar}{zap}")); |
91 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
92 | 1 | pk2 = EncodeRedisKey(Slice("bar")); |
93 | 1 | ASSERT_EQ(pk1, pk2); |
94 | | |
95 | 1 | ASSERT_OK(split1.SetStringCopy("key", "{}foobar")); |
96 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
97 | 1 | pk2 = EncodeRedisKey(Slice("{}foobar")); |
98 | 1 | ASSERT_EQ(pk1, pk2); |
99 | | |
100 | 1 | ASSERT_OK(split1.SetStringCopy("key", "foobar{}")); |
101 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
102 | 1 | pk2 = EncodeRedisKey(Slice("foobar{}")); |
103 | 1 | ASSERT_EQ(pk1, pk2); |
104 | | |
105 | 1 | ASSERT_OK(split1.SetStringCopy("key", "foobar{z}")); |
106 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
107 | 1 | pk2 = EncodeRedisKey(Slice("z")); |
108 | 1 | ASSERT_EQ(pk1, pk2); |
109 | | |
110 | 1 | ASSERT_OK(split1.SetStringCopy("key", "foobar")); |
111 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
112 | 1 | pk2 = EncodeRedisKey(Slice("foobar")); |
113 | 1 | ASSERT_EQ(pk1, pk2); |
114 | | |
115 | 1 | ASSERT_OK(split1.SetStringCopy("key", "a")); |
116 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk1)); |
117 | | |
118 | 1 | ASSERT_OK(split1.SetStringCopy("key", "{a}")); |
119 | 1 | ASSERT_OK(partition_schema.EncodeRedisKey(split1, &pk2)); |
120 | 1 | ASSERT_EQ(pk1, pk2); |
121 | 1 | } |
122 | | |
123 | | } // namespace yb |