2022-02-24 00:06:39 +08:00
|
|
|
/*
|
|
|
|
* Tenant.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* Licensed 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fdbclient/SystemData.h"
|
|
|
|
#include "fdbclient/Tenant.h"
|
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
|
2022-06-23 06:49:44 +08:00
|
|
|
Key TenantMapEntry::idToPrefix(int64_t id) {
|
|
|
|
int64_t swapped = bigEndian64(id);
|
|
|
|
return StringRef(reinterpret_cast<const uint8_t*>(&swapped), 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t TenantMapEntry::prefixToId(KeyRef prefix) {
|
|
|
|
ASSERT(prefix.size() == 8);
|
|
|
|
int64_t id = *reinterpret_cast<const int64_t*>(prefix.begin());
|
|
|
|
id = bigEndian64(id);
|
|
|
|
ASSERT(id >= 0);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TenantMapEntry::initPrefix(KeyRef subspace) {
|
|
|
|
ASSERT(id >= 0);
|
|
|
|
prefix = makeString(8 + subspace.size());
|
|
|
|
uint8_t* data = mutateString(prefix);
|
|
|
|
if (subspace.size() > 0) {
|
|
|
|
memcpy(data, subspace.begin(), subspace.size());
|
|
|
|
}
|
|
|
|
int64_t swapped = bigEndian64(id);
|
|
|
|
memcpy(data + subspace.size(), &swapped, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
TenantMapEntry::TenantMapEntry() : id(-1) {}
|
|
|
|
TenantMapEntry::TenantMapEntry(int64_t id, KeyRef subspace) : id(id) {
|
|
|
|
initPrefix(subspace);
|
|
|
|
}
|
|
|
|
|
2022-02-24 00:06:39 +08:00
|
|
|
TEST_CASE("/fdbclient/TenantMapEntry/Serialization") {
|
|
|
|
TenantMapEntry entry1(1, ""_sr);
|
|
|
|
ASSERT(entry1.prefix == "\x00\x00\x00\x00\x00\x00\x00\x01"_sr);
|
2022-06-30 01:58:58 +08:00
|
|
|
TenantMapEntry entry2 = TenantMapEntry::decode(entry1.encode());
|
2022-02-24 00:06:39 +08:00
|
|
|
ASSERT(entry1.id == entry2.id && entry1.prefix == entry2.prefix);
|
|
|
|
|
|
|
|
TenantMapEntry entry3(std::numeric_limits<int64_t>::max(), "foo"_sr);
|
2022-05-25 05:58:47 +08:00
|
|
|
ASSERT(entry3.prefix == "foo\x7f\xff\xff\xff\xff\xff\xff\xff"_sr);
|
2022-06-30 01:58:58 +08:00
|
|
|
TenantMapEntry entry4 = TenantMapEntry::decode(entry3.encode());
|
2022-02-24 00:06:39 +08:00
|
|
|
ASSERT(entry3.id == entry4.id && entry3.prefix == entry4.prefix);
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
int bits = deterministicRandom()->randomInt(1, 64);
|
2022-05-25 05:58:47 +08:00
|
|
|
int64_t min = bits == 1 ? 0 : (UINT64_C(1) << (bits - 1));
|
|
|
|
int64_t maxPlusOne = std::min<uint64_t>(UINT64_C(1) << bits, std::numeric_limits<int64_t>::max());
|
2022-02-24 00:06:39 +08:00
|
|
|
int64_t id = deterministicRandom()->randomInt64(min, maxPlusOne);
|
|
|
|
|
|
|
|
int subspaceLength = deterministicRandom()->randomInt(0, 20);
|
|
|
|
Standalone<StringRef> subspace = makeString(subspaceLength);
|
|
|
|
generateRandomData(mutateString(subspace), subspaceLength);
|
|
|
|
|
|
|
|
TenantMapEntry entry(id, subspace);
|
|
|
|
int64_t bigEndianId = bigEndian64(id);
|
|
|
|
ASSERT(entry.id == id && entry.prefix.startsWith(subspace) &&
|
|
|
|
entry.prefix.endsWith(StringRef(reinterpret_cast<uint8_t*>(&bigEndianId), 8)) &&
|
|
|
|
entry.prefix.size() == subspaceLength + 8);
|
|
|
|
|
2022-06-30 01:58:58 +08:00
|
|
|
TenantMapEntry decodedEntry = TenantMapEntry::decode(entry.encode());
|
2022-05-25 05:58:47 +08:00
|
|
|
ASSERT(decodedEntry.id == entry.id && decodedEntry.prefix == entry.prefix);
|
2022-02-24 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|