#pragma once #include #include #include #include #include "../../data/logger.hpp" namespace world { struct read_ctx { ~read_ctx() { ZSTD_freeDCtx(ctx); } ZSTD_DCtx *ctx; ZSTD_DDict *dict; }; struct write_ctx { ~write_ctx() { ZSTD_freeCCtx(ctx); } ZSTD_CCtx *ctx; ZSTD_CDict *dict; }; class dict_set { public: dict_set(const std::string& path) { std::ifstream is(path, std::ios::in | std::ios::binary | std::ios::ate); if(!is.good()) { LOG_E("Missing dict " << path); exit(1); } const auto end = is.tellg(); is.seekg(0, std::ios::beg); std::vector dict(end - is.tellg()); is.read(dict.data(), dict.size()); is.close(); c = ZSTD_createCDict(dict.data(), dict.size(), ZSTD_CLEVEL_DEFAULT); assert(c != NULL); d = ZSTD_createDDict(dict.data(), dict.size()); assert(d != NULL); } ~dict_set() { ZSTD_freeCDict(c); ZSTD_freeDDict(d); } read_ctx make_reader() const { return read_ctx{ZSTD_createDCtx(), d}; } write_ctx make_writer() const { return write_ctx{ZSTD_createCCtx(), c}; } private: ZSTD_CDict *c; ZSTD_DDict *d; }; }