2017-07-28 20:55:19 +00:00
|
|
|
// Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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).
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper functions which serialize and deserialize integers
|
|
|
|
* into bytes in big endian.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-10 16:59:05 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-06-16 21:12:52 +00:00
|
|
|
namespace cassandra {
|
|
|
|
namespace {
|
|
|
|
const int64_t kCharMask = 0xFFLL;
|
|
|
|
const int32_t kBitsPerByte = 8;
|
2022-10-24 23:38:09 +00:00
|
|
|
} // namespace
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <typename T>
|
2017-06-16 21:12:52 +00:00
|
|
|
void Serialize(T val, std::string* dest);
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <typename T>
|
|
|
|
T Deserialize(const char* src, std::size_t offset = 0);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
// Specializations
|
2022-10-24 23:38:09 +00:00
|
|
|
template <>
|
2017-06-16 21:12:52 +00:00
|
|
|
inline void Serialize<int8_t>(int8_t t, std::string* dest) {
|
|
|
|
dest->append(1, static_cast<char>(t & kCharMask));
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <>
|
2017-06-16 21:12:52 +00:00
|
|
|
inline void Serialize<int32_t>(int32_t t, std::string* dest) {
|
|
|
|
for (unsigned long i = 0; i < sizeof(int32_t); i++) {
|
2022-10-24 23:38:09 +00:00
|
|
|
dest->append(
|
|
|
|
1, static_cast<char>((t >> (sizeof(int32_t) - 1 - i) * kBitsPerByte) &
|
|
|
|
kCharMask));
|
2017-06-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <>
|
2017-06-16 21:12:52 +00:00
|
|
|
inline void Serialize<int64_t>(int64_t t, std::string* dest) {
|
|
|
|
for (unsigned long i = 0; i < sizeof(int64_t); i++) {
|
2022-10-24 23:38:09 +00:00
|
|
|
dest->append(
|
|
|
|
1, static_cast<char>((t >> (sizeof(int64_t) - 1 - i) * kBitsPerByte) &
|
|
|
|
kCharMask));
|
2017-06-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <>
|
2017-06-16 21:12:52 +00:00
|
|
|
inline int8_t Deserialize<int8_t>(const char* src, std::size_t offset) {
|
|
|
|
return static_cast<int8_t>(src[offset]);
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <>
|
2017-06-16 21:12:52 +00:00
|
|
|
inline int32_t Deserialize<int32_t>(const char* src, std::size_t offset) {
|
|
|
|
int32_t result = 0;
|
|
|
|
for (unsigned long i = 0; i < sizeof(int32_t); i++) {
|
|
|
|
result |= static_cast<int32_t>(static_cast<unsigned char>(src[offset + i]))
|
2022-10-24 23:38:09 +00:00
|
|
|
<< ((sizeof(int32_t) - 1 - i) * kBitsPerByte);
|
2017-06-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
template <>
|
2017-06-16 21:12:52 +00:00
|
|
|
inline int64_t Deserialize<int64_t>(const char* src, std::size_t offset) {
|
|
|
|
int64_t result = 0;
|
|
|
|
for (unsigned long i = 0; i < sizeof(int64_t); i++) {
|
|
|
|
result |= static_cast<int64_t>(static_cast<unsigned char>(src[offset + i]))
|
2022-10-24 23:38:09 +00:00
|
|
|
<< ((sizeof(int64_t) - 1 - i) * kBitsPerByte);
|
2017-06-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:38:09 +00:00
|
|
|
} // namespace cassandra
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|