Add a Tuple::makeTuple function to easily construct a tuple. Update Tuple to allow all types to be passed via .append() so they can be used with makeTuple.

This commit is contained in:
A.J. Beamon 2022-07-19 11:50:58 -07:00
parent 0e03fbd9e7
commit 1b81e72604
12 changed files with 77 additions and 25 deletions

View File

@ -1037,7 +1037,7 @@ TEST_CASE("tuple_support_versionstamp") {
// a random 12 bytes long StringRef as a versionstamp
StringRef str = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12"_sr;
Versionstamp vs(str);
const Tuple t = Tuple().append(prefix).append(RECORD).appendVersionstamp(vs).append("{K[3]}"_sr).append("{...}"_sr);
const Tuple t = Tuple().append(prefix).append(RECORD).append(vs).append("{K[3]}"_sr).append("{...}"_sr);
ASSERT(t.getVersionstamp(2) == vs);
// verify the round-way pack-unpack path for a Tuple containing a versionstamp

View File

@ -85,7 +85,7 @@ Values must always be encoded according to the :ref:`api-python-tuple-layer`.
const KeyRef myGlobalConfigKey = LiteralStringRef("config/key");
// When you want to set the value..
Tuple value = Tuple().appendDouble(1.5);
Tuple value = Tuple().append((double)1.5);
FDBTransaction* tr = ...;
tr->setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);

View File

@ -97,7 +97,7 @@ ACTOR Future<bool> profileCommandActor(Database db,
}
}
Tuple rate = Tuple().appendDouble(sampleRate);
Tuple rate = Tuple().append(sampleRate);
Tuple size = Tuple().append(sizeLimit);
tr->setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);
tr->set(GlobalConfig::prefixedKey(fdbClientInfoTxnSampleRate), rate.pack());

View File

@ -203,7 +203,7 @@ public:
return Tuple()
.append(version)
.append(StringRef(fileName))
.append(isRange)
.append((int)isRange)
.append(fileSize)
.append(blockSize)
.append(endVersion);

View File

@ -183,7 +183,7 @@ ACTOR Future<Void> GlobalConfig::migrate(GlobalConfig* self) {
if (sampleRate.present()) {
const double sampleRateDbl =
BinaryReader::fromStringRef<double>(sampleRate.get().contents(), Unversioned());
Tuple rate = Tuple().appendDouble(sampleRateDbl);
Tuple rate = Tuple().append(sampleRateDbl);
tr->set(GlobalConfig::prefixedKey(fdbClientInfoTxnSampleRate), rate.pack());
}
if (sizeLimit.present()) {

View File

@ -2005,7 +2005,7 @@ Future<Optional<std::string>> ClientProfilingImpl::commit(ReadYourWritesTransact
} else {
try {
double sampleRate = boost::lexical_cast<double>(sampleRateStr);
Tuple rate = Tuple().appendDouble(sampleRate);
Tuple rate = Tuple().append(sampleRate);
insertions.push_back_deep(insertions.arena(), KeyValueRef(fdbClientInfoTxnSampleRate, rate.pack()));
} catch (boost::bad_lexical_cast& e) {
return Optional<std::string>(ManagementAPIError::toJsonString(

View File

@ -142,10 +142,10 @@ bool ThrottleApi::TagQuotaValue::isValid() const {
Value ThrottleApi::TagQuotaValue::toValue() const {
Tuple tuple;
tuple.appendDouble(reservedReadQuota);
tuple.appendDouble(totalReadQuota);
tuple.appendDouble(reservedWriteQuota);
tuple.appendDouble(totalWriteQuota);
tuple.append(reservedReadQuota);
tuple.append(totalReadQuota);
tuple.append(reservedWriteQuota);
tuple.append(totalWriteQuota);
return tuple.pack();
}

View File

@ -19,6 +19,7 @@
*/
#include "fdbclient/Tuple.h"
#include "flow/UnitTest.h"
const uint8_t VERSIONSTAMP_96_CODE = 0x33;
@ -103,7 +104,7 @@ Tuple& Tuple::append(Tuple const& tuple) {
return *this;
}
Tuple& Tuple::appendVersionstamp(Versionstamp const& vs) {
Tuple& Tuple::append(Versionstamp const& vs) {
offsets.push_back(data.size());
data.push_back(data.arena(), VERSIONSTAMP_96_CODE);
@ -134,6 +135,10 @@ Tuple& Tuple::append(StringRef const& str, bool utf8) {
return *this;
}
Tuple& Tuple::append(UnicodeStr const& str) {
return append(str.str, true);
}
Tuple& Tuple::appendRaw(StringRef const& str) {
offsets.push_back(data.size());
@ -166,7 +171,11 @@ Tuple& Tuple::append(int64_t value) {
return *this;
}
Tuple& Tuple::appendBool(bool value) {
Tuple& Tuple::append(int32_t value) {
return append((int64_t)value);
}
Tuple& Tuple::append(bool value) {
offsets.push_back(data.size());
if (value) {
data.push_back(data.arena(), 0x27);
@ -176,7 +185,7 @@ Tuple& Tuple::appendBool(bool value) {
return *this;
}
Tuple& Tuple::appendFloat(float value) {
Tuple& Tuple::append(float value) {
offsets.push_back(data.size());
float swap = bigEndianFloat(value);
uint8_t* bytes = (uint8_t*)&swap;
@ -187,7 +196,7 @@ Tuple& Tuple::appendFloat(float value) {
return *this;
}
Tuple& Tuple::appendDouble(double value) {
Tuple& Tuple::append(double value) {
offsets.push_back(data.size());
double swap = value;
swap = bigEndianDouble(swap);
@ -199,12 +208,16 @@ Tuple& Tuple::appendDouble(double value) {
return *this;
}
Tuple& Tuple::appendNull() {
Tuple& Tuple::append(nullptr_t) {
offsets.push_back(data.size());
data.push_back(data.arena(), (uint8_t)'\x00');
return *this;
}
Tuple& Tuple::appendNull() {
return append(nullptr);
}
Tuple::ElementType Tuple::getType(size_t index) const {
if (index >= offsets.size()) {
throw invalid_tuple_index();
@ -426,3 +439,29 @@ StringRef Tuple::subTupleRawString(size_t index) const {
size_t endPos = end < offsets.size() ? offsets[end] : data.size();
return StringRef(data.begin() + offsets[index], endPos - offsets[index]);
}
TEST_CASE("fdbclient/Tuple/makeTuple") {
Tuple t1 = Tuple::makeTuple(
1, 1.0f, 1.0, false, "byteStr"_sr, Tuple::UnicodeStr("str"_sr), nullptr, Versionstamp("000000000000"_sr));
Tuple t2 = Tuple()
.append(1)
.append(1.0f)
.append(1.0)
.append(false)
.append("byteStr"_sr)
.append(Tuple::UnicodeStr("str"_sr))
.append(nullptr)
.append(Versionstamp("000000000000"_sr));
ASSERT(t1.pack() == t2.pack());
ASSERT(t1.getType(0) == Tuple::INT);
ASSERT(t1.getType(1) == Tuple::FLOAT);
ASSERT(t1.getType(2) == Tuple::DOUBLE);
ASSERT(t1.getType(3) == Tuple::BOOL);
ASSERT(t1.getType(4) == Tuple::BYTES);
ASSERT(t1.getType(5) == Tuple::UTF8);
ASSERT(t1.getType(6) == Tuple::NULL_TYPE);
ASSERT(t1.getType(7) == Tuple::VERSIONSTAMP);
return Void();
}

View File

@ -361,7 +361,7 @@ public:
template <>
inline Standalone<StringRef> TupleCodec<FileBackupAgent::ERestoreState>::pack(
FileBackupAgent::ERestoreState const& val) {
return Tuple().append(val).pack();
return Tuple().append(static_cast<int>(val)).pack();
}
template <>
inline FileBackupAgent::ERestoreState TupleCodec<FileBackupAgent::ERestoreState>::unpack(

View File

@ -28,6 +28,11 @@
#include "fdbclient/Versionstamp.h"
struct Tuple {
struct UnicodeStr {
StringRef str;
UnicodeStr(StringRef str) : str(str) {}
};
Tuple() {}
// Tuple parsing normally does not care of the final value is a numeric type and is incomplete.
@ -41,14 +46,15 @@ struct Tuple {
// the str needs to be a Tuple encoded string.
Tuple& appendRaw(StringRef const& str);
Tuple& append(StringRef const& str, bool utf8 = false);
Tuple& append(UnicodeStr const& str);
Tuple& append(int32_t);
Tuple& append(int64_t);
// There are some ambiguous append calls in fdbclient, so to make it easier
// to add append for floats and doubles, name them differently for now.
Tuple& appendBool(bool);
Tuple& appendFloat(float);
Tuple& appendDouble(double);
Tuple& append(bool);
Tuple& append(float);
Tuple& append(double);
Tuple& append(std::nullptr_t);
Tuple& appendNull();
Tuple& appendVersionstamp(Versionstamp const&);
Tuple& append(Versionstamp const&);
StringRef pack() const { return StringRef(data.begin(), data.size()); }
@ -84,6 +90,13 @@ struct Tuple {
Standalone<VectorRef<uint8_t>> getData() { return data; }
Standalone<StringRef> getDataAsStandalone() { return Standalone<StringRef>(pack(), data.arena()); }
template <class... Types>
static Tuple makeTuple(Types&&... args) {
Tuple t;
(t << ... << args);
return t;
}
private:
Tuple(const StringRef& data, bool exclude_incomplete = false);
Standalone<VectorRef<uint8_t>> data;

View File

@ -115,7 +115,7 @@ public:
return Tuple()
.append(version)
.append(StringRef(fileName))
.append(isRange)
.append((int)isRange)
.append(fileSize)
.append(blockSize)
.append(endVersion);
@ -193,7 +193,7 @@ struct RestoreFileFR {
return Tuple()
.append(version)
.append(StringRef(fileName))
.append(isRange)
.append((int)isRange)
.append(fileSize)
.append(blockSize)
.append(endVersion)

View File

@ -294,7 +294,7 @@ struct ClientTransactionProfileCorrectnessWorkload : TestWorkload {
wait(runRYWTransaction(cx, [=](Reference<ReadYourWritesTransaction> tr) -> Future<Void> {
tr->setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);
tr->setOption(FDBTransactionOptions::LOCK_AWARE);
Tuple rate = Tuple().appendDouble(sampleProbability);
Tuple rate = Tuple().append(sampleProbability);
Tuple size = Tuple().append(sizeLimit);
tr->set(GlobalConfig::prefixedKey(fdbClientInfoTxnSampleRate), rate.pack());
tr->set(GlobalConfig::prefixedKey(fdbClientInfoTxnSizeLimit), size.pack());