2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "util/logging.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
static std::string IKey(const std::string& user_key,
|
|
|
|
uint64_t seq,
|
|
|
|
ValueType vt) {
|
|
|
|
std::string encoded;
|
|
|
|
AppendInternalKey(&encoded, ParsedInternalKey(user_key, seq, vt));
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string Shorten(const std::string& s, const std::string& l) {
|
|
|
|
std::string result = s;
|
|
|
|
InternalKeyComparator(BytewiseComparator()).FindShortestSeparator(&result, l);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string ShortSuccessor(const std::string& s) {
|
|
|
|
std::string result = s;
|
|
|
|
InternalKeyComparator(BytewiseComparator()).FindShortSuccessor(&result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestKey(const std::string& key,
|
|
|
|
uint64_t seq,
|
|
|
|
ValueType vt) {
|
|
|
|
std::string encoded = IKey(key, seq, vt);
|
|
|
|
|
|
|
|
Slice in(encoded);
|
|
|
|
ParsedInternalKey decoded("", 0, kTypeValue);
|
|
|
|
|
|
|
|
ASSERT_TRUE(ParseInternalKey(in, &decoded));
|
|
|
|
ASSERT_EQ(key, decoded.user_key.ToString());
|
|
|
|
ASSERT_EQ(seq, decoded.sequence);
|
|
|
|
ASSERT_EQ(vt, decoded.type);
|
|
|
|
|
|
|
|
ASSERT_TRUE(!ParseInternalKey(Slice("bar"), &decoded));
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class FormatTest : public testing::Test {};
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(FormatTest, InternalKey_EncodeDecode) {
|
2011-03-18 23:37:00 +01:00
|
|
|
const char* keys[] = { "", "k", "hello", "longggggggggggggggggggggg" };
|
|
|
|
const uint64_t seq[] = {
|
|
|
|
1, 2, 3,
|
|
|
|
(1ull << 8) - 1, 1ull << 8, (1ull << 8) + 1,
|
|
|
|
(1ull << 16) - 1, 1ull << 16, (1ull << 16) + 1,
|
|
|
|
(1ull << 32) - 1, 1ull << 32, (1ull << 32) + 1
|
|
|
|
};
|
2012-11-06 21:02:18 +01:00
|
|
|
for (unsigned int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
|
|
|
|
for (unsigned int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
TestKey(keys[k], seq[s], kTypeValue);
|
|
|
|
TestKey("hello", 1, kTypeDeletion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(FormatTest, InternalKeyShortSeparator) {
|
2011-03-18 23:37:00 +01:00
|
|
|
// When user keys are same
|
|
|
|
ASSERT_EQ(IKey("foo", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("foo", 99, kTypeValue)));
|
|
|
|
ASSERT_EQ(IKey("foo", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("foo", 101, kTypeValue)));
|
|
|
|
ASSERT_EQ(IKey("foo", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("foo", 100, kTypeValue)));
|
|
|
|
ASSERT_EQ(IKey("foo", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("foo", 100, kTypeDeletion)));
|
|
|
|
|
|
|
|
// When user keys are misordered
|
|
|
|
ASSERT_EQ(IKey("foo", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("bar", 99, kTypeValue)));
|
|
|
|
|
|
|
|
// When user keys are different, but correctly ordered
|
|
|
|
ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("hello", 200, kTypeValue)));
|
|
|
|
|
2016-04-26 08:02:14 +02:00
|
|
|
ASSERT_EQ(IKey("ABC2", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
Shorten(IKey("ABC1AAAAA", 100, kTypeValue),
|
|
|
|
IKey("ABC2ABB", 200, kTypeValue)));
|
|
|
|
|
|
|
|
ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
Shorten(IKey("AAA1AAA", 100, kTypeValue),
|
|
|
|
IKey("AAA2AA", 200, kTypeValue)));
|
|
|
|
|
|
|
|
ASSERT_EQ(
|
|
|
|
IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA4", 200, kTypeValue)));
|
|
|
|
|
|
|
|
ASSERT_EQ(
|
|
|
|
IKey("AAA1B", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));
|
|
|
|
|
|
|
|
ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
Shorten(IKey("AAA1AAA", 100, kTypeValue),
|
|
|
|
IKey("AAA2A", 200, kTypeValue)));
|
|
|
|
|
|
|
|
ASSERT_EQ(
|
|
|
|
IKey("AAA1", 100, kTypeValue),
|
|
|
|
Shorten(IKey("AAA1", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// When start user key is prefix of limit user key
|
|
|
|
ASSERT_EQ(IKey("foo", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foo", 100, kTypeValue),
|
|
|
|
IKey("foobar", 200, kTypeValue)));
|
|
|
|
|
|
|
|
// When limit user key is prefix of start user key
|
|
|
|
ASSERT_EQ(IKey("foobar", 100, kTypeValue),
|
|
|
|
Shorten(IKey("foobar", 100, kTypeValue),
|
|
|
|
IKey("foo", 200, kTypeValue)));
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(FormatTest, InternalKeyShortestSuccessor) {
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
|
|
|
|
ShortSuccessor(IKey("foo", 100, kTypeValue)));
|
|
|
|
ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),
|
|
|
|
ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(FormatTest, IterKeyOperation) {
|
2014-07-23 21:31:11 +02:00
|
|
|
IterKey k;
|
|
|
|
const char p[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
const char q[] = "0123456789";
|
|
|
|
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string(""));
|
|
|
|
|
|
|
|
k.TrimAppend(0, p, 3);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("abc"));
|
|
|
|
|
|
|
|
k.TrimAppend(1, p, 3);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("aabc"));
|
|
|
|
|
|
|
|
k.TrimAppend(0, p, 26);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("abcdefghijklmnopqrstuvwxyz"));
|
|
|
|
|
|
|
|
k.TrimAppend(26, q, 10);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("abcdefghijklmnopqrstuvwxyz0123456789"));
|
|
|
|
|
|
|
|
k.TrimAppend(36, q, 1);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("abcdefghijklmnopqrstuvwxyz01234567890"));
|
|
|
|
|
|
|
|
k.TrimAppend(26, q, 1);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("abcdefghijklmnopqrstuvwxyz0"));
|
|
|
|
|
|
|
|
// Size going up, memory allocation is triggered
|
|
|
|
k.TrimAppend(27, p, 26);
|
2017-04-04 23:17:16 +02:00
|
|
|
ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
|
2014-07-23 21:31:11 +02:00
|
|
|
std::string("abcdefghijklmnopqrstuvwxyz0"
|
2017-04-04 23:17:16 +02:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"));
|
2014-07-23 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 09:21:41 +02:00
|
|
|
TEST_F(FormatTest, UpdateInternalKey) {
|
|
|
|
std::string user_key("abcdefghijklmnopqrstuvwxyz");
|
|
|
|
uint64_t new_seq = 0x123456;
|
|
|
|
ValueType new_val_type = kTypeDeletion;
|
|
|
|
|
|
|
|
std::string ikey;
|
|
|
|
AppendInternalKey(&ikey, ParsedInternalKey(user_key, 100U, kTypeValue));
|
|
|
|
size_t ikey_size = ikey.size();
|
|
|
|
UpdateInternalKey(&ikey, new_seq, new_val_type);
|
|
|
|
ASSERT_EQ(ikey_size, ikey.size());
|
|
|
|
|
|
|
|
Slice in(ikey);
|
|
|
|
ParsedInternalKey decoded;
|
|
|
|
ASSERT_TRUE(ParseInternalKey(in, &decoded));
|
|
|
|
ASSERT_EQ(user_key, decoded.user_key.ToString());
|
|
|
|
ASSERT_EQ(new_seq, decoded.sequence);
|
|
|
|
ASSERT_EQ(new_val_type, decoded.type);
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:34:54 +02:00
|
|
|
TEST_F(FormatTest, RangeTombstoneSerializeEndKey) {
|
|
|
|
RangeTombstone t("a", "b", 2);
|
|
|
|
InternalKey k("b", 3, kTypeValue);
|
|
|
|
const InternalKeyComparator cmp(BytewiseComparator());
|
|
|
|
ASSERT_LT(cmp.Compare(t.SerializeEndKey(), k), 0);
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|