-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Added paritial compression of data in the cache
- Values will be serialized and compressed before storing - Reverse will be applied during a Get call.
- Loading branch information
Showing
7 changed files
with
95 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef COMPRESSION_H | ||
#define COMPRESSION_H | ||
|
||
#include <string> | ||
#include <snappy.h> | ||
#include <sstream> | ||
#include <cstring> | ||
#include <type_traits> | ||
|
||
class Compressor { | ||
|
||
public: | ||
|
||
template<typename T> | ||
static std::string serialize(const T& data, std::true_type) { | ||
return std::to_string(data); | ||
} | ||
|
||
template<typename T> | ||
static T deserialize(const std::string& decompressed, std::true_type) { | ||
std::istringstream iss(decompressed); | ||
T data; | ||
iss >> data; | ||
return data; | ||
} | ||
|
||
template<typename T> | ||
static std::string serialize(const T& data, std::false_type) { | ||
// A byte-by-byte copy of the memory representation of the value object. | ||
const char* byte_seq = reinterpret_cast<const char*>(&data); | ||
return std::string(byte_seq, sizeof(T)); | ||
} | ||
|
||
template<typename T> | ||
static T deserialize(const std::string& decompressed, std::false_type) { | ||
T value; | ||
std::memcpy(&value, decompressed.data(), sizeof(T)); | ||
return value; | ||
} | ||
|
||
static std::string compress(const std::string& serialized){ | ||
std::string compressed; | ||
snappy::Compress(serialized.data(), serialized.size(), &compressed); | ||
return compressed; | ||
} | ||
|
||
static std::string uncompress(const std::string& compressed){ | ||
std::string decompressed; | ||
snappy::Uncompress(compressed.data(), compressed.size(), &decompressed); | ||
return decompressed; | ||
} | ||
|
||
}; | ||
|
||
#endif |