@ -56,34 +56,32 @@ ZSTD_customMem GetJeZstdAllocationOverrides();
// If, in the future we have more than one native context to
// If, in the future we have more than one native context to
// cache we can arrange this as a tuple
// cache we can arrange this as a tuple
class ZSTDUncompressCachedData {
class ZSTDUncompressCachedData {
public :
public :
using ZSTDNativeContext = ZSTD_DCtx * ;
using ZSTDNativeContext = ZSTD_DCtx * ;
ZSTDUncompressCachedData ( ) { }
ZSTDUncompressCachedData ( ) { }
// Init from cache
// Init from cache
ZSTDUncompressCachedData ( const ZSTDUncompressCachedData & o ) = delete ;
ZSTDUncompressCachedData ( const ZSTDUncompressCachedData & o ) = delete ;
ZSTDUncompressCachedData & operator = ( const ZSTDUncompressCachedData & ) = delete ;
ZSTDUncompressCachedData & operator = ( const ZSTDUncompressCachedData & ) = delete ;
ZSTDUncompressCachedData ( ZSTDUncompressCachedData & & o ) ROCKSDB_NOEXCEPT :
ZSTDUncompressCachedData ( ZSTDUncompressCachedData & & o ) ROCKSDB_NOEXCEPT
ZSTDUncompressCachedData ( ) {
: ZSTDUncompressCachedData ( ) {
* this = std : : move ( o ) ;
* this = std : : move ( o ) ;
}
}
ZSTDUncompressCachedData & operator = ( ZSTDUncompressCachedData & & o ) ROCKSDB_NOEXCEPT {
ZSTDUncompressCachedData & operator = ( ZSTDUncompressCachedData & & o )
ROCKSDB_NOEXCEPT {
assert ( zstd_ctx_ = = nullptr ) ;
assert ( zstd_ctx_ = = nullptr ) ;
std : : swap ( zstd_ctx_ , o . zstd_ctx_ ) ;
std : : swap ( zstd_ctx_ , o . zstd_ctx_ ) ;
std : : swap ( cache_idx_ , o . cache_idx_ ) ;
std : : swap ( cache_idx_ , o . cache_idx_ ) ;
return * this ;
return * this ;
}
}
ZSTDNativeContext Get ( ) const {
ZSTDNativeContext Get ( ) const { return zstd_ctx_ ; }
return zstd_ctx_ ;
int64_t GetCacheIndex ( ) const { return cache_idx_ ; }
}
int64_t GetCacheIndex ( ) const {
return cache_idx_ ;
}
void CreateIfNeeded ( ) {
void CreateIfNeeded ( ) {
if ( zstd_ctx_ = = nullptr ) {
if ( zstd_ctx_ = = nullptr ) {
# ifdef ROCKSDB_ZSTD_CUSTOM_MEM
# ifdef ROCKSDB_ZSTD_CUSTOM_MEM
zstd_ctx_ = ZSTD_createDCtx_advanced ( port : : GetJeZstdAllocationOverrides ( ) ) ;
zstd_ctx_ =
ZSTD_createDCtx_advanced ( port : : GetJeZstdAllocationOverrides ( ) ) ;
# else // ROCKSDB_ZSTD_CUSTOM_MEM
# else // ROCKSDB_ZSTD_CUSTOM_MEM
zstd_ctx_ = ZSTD_createDCtx ( ) ;
zstd_ctx_ = ZSTD_createDCtx ( ) ;
# endif // ROCKSDB_ZSTD_CUSTOM_MEM
# endif // ROCKSDB_ZSTD_CUSTOM_MEM
cache_idx_ = - 1 ;
cache_idx_ = - 1 ;
}
}
@ -97,31 +95,30 @@ public:
ZSTD_freeDCtx ( zstd_ctx_ ) ;
ZSTD_freeDCtx ( zstd_ctx_ ) ;
}
}
}
}
private :
ZSTDNativeContext zstd_ctx_ = nullptr ;
private :
int64_t cache_idx_ = - 1 ; // -1 means this instance owns the context
ZSTDNativeContext zstd_ctx_ = nullptr ;
int64_t cache_idx_ = - 1 ; // -1 means this instance owns the context
} ;
} ;
# endif // (ZSTD_VERSION_NUMBER >= 500)
# endif // (ZSTD_VERSION_NUMBER >= 500)
} // namespace rocksdb
} // namespace rocksdb
# endif // ZSTD
# endif // ZSTD
# if !(defined ZSTD) || !(ZSTD_VERSION_NUMBER >= 500)
# if !(defined ZSTD) || !(ZSTD_VERSION_NUMBER >= 500)
namespace rocksdb {
namespace rocksdb {
class ZSTDUncompressCachedData {
class ZSTDUncompressCachedData {
void * padding ; // unused
void * padding ; // unused
public :
public :
using ZSTDNativeContext = void * ;
using ZSTDNativeContext = void * ;
ZSTDUncompressCachedData ( ) { }
ZSTDUncompressCachedData ( ) { }
ZSTDUncompressCachedData ( const ZSTDUncompressCachedData & ) { }
ZSTDUncompressCachedData ( const ZSTDUncompressCachedData & ) { }
ZSTDUncompressCachedData & operator = ( const ZSTDUncompressCachedData & ) = delete ;
ZSTDUncompressCachedData & operator = ( const ZSTDUncompressCachedData & ) = delete ;
ZSTDUncompressCachedData ( ZSTDUncompressCachedData & & ) ROCKSDB_NOEXCEPT = default ;
ZSTDUncompressCachedData ( ZSTDUncompressCachedData & & )
ZSTDUncompressCachedData & operator = ( ZSTDUncompressCachedData & & ) ROCKSDB_NOEXCEPT = default ;
ROCKSDB_NOEXCEPT = default ;
ZSTDNativeContext Get ( ) const {
ZSTDUncompressCachedData & operator = ( ZSTDUncompressCachedData & & )
return nullptr ;
ROCKSDB_NOEXCEPT = default ;
}
ZSTDNativeContext Get ( ) const { return nullptr ; }
int64_t GetCacheIndex ( ) const {
int64_t GetCacheIndex ( ) const { return - 1 ; }
return - 1 ;
}
void CreateIfNeeded ( ) { }
void CreateIfNeeded ( ) { }
void InitFromCache ( const ZSTDUncompressCachedData & , int64_t ) { }
void InitFromCache ( const ZSTDUncompressCachedData & , int64_t ) { }
} ;
} ;
@ -136,18 +133,19 @@ namespace rocksdb {
// Instantiate this class and pass it to the uncompression API below
// Instantiate this class and pass it to the uncompression API below
class CompressionContext {
class CompressionContext {
private :
private :
const CompressionType type_ ;
const CompressionType type_ ;
const CompressionOptions opts_ ;
const CompressionOptions opts_ ;
Slice dict_ ;
Slice dict_ ;
# if defined(ZSTD) && (ZSTD_VERSION_NUMBER >= 500)
# if defined(ZSTD) && (ZSTD_VERSION_NUMBER >= 500)
ZSTD_CCtx * zstd_ctx_ = nullptr ;
ZSTD_CCtx * zstd_ctx_ = nullptr ;
void CreateNativeContext ( ) {
void CreateNativeContext ( ) {
if ( type_ = = kZSTD ) {
if ( type_ = = kZSTD ) {
# ifdef ROCKSDB_ZSTD_CUSTOM_MEM
# ifdef ROCKSDB_ZSTD_CUSTOM_MEM
zstd_ctx_ = ZSTD_createCCtx_advanced ( port : : GetJeZstdAllocationOverrides ( ) ) ;
zstd_ctx_ =
ZSTD_createCCtx_advanced ( port : : GetJeZstdAllocationOverrides ( ) ) ;
# else // ROCKSDB_ZSTD_CUSTOM_MEM
# else // ROCKSDB_ZSTD_CUSTOM_MEM
zstd_ctx_ = ZSTD_createCCtx ( ) ;
zstd_ctx_ = ZSTD_createCCtx ( ) ;
# endif // ROCKSDB_ZSTD_CUSTOM_MEM
# endif // ROCKSDB_ZSTD_CUSTOM_MEM
}
}
}
}
@ -156,79 +154,63 @@ private:
ZSTD_freeCCtx ( zstd_ctx_ ) ;
ZSTD_freeCCtx ( zstd_ctx_ ) ;
}
}
}
}
public :
public :
// callable inside ZSTD_Compress
// callable inside ZSTD_Compress
ZSTD_CCtx * ZSTDPreallocCtx ( ) const {
ZSTD_CCtx * ZSTDPreallocCtx ( ) const {
assert ( type_ = = kZSTD ) ;
assert ( type_ = = kZSTD ) ;
return zstd_ctx_ ;
return zstd_ctx_ ;
}
}
# else // ZSTD && (ZSTD_VERSION_NUMBER >= 500)
# else // ZSTD && (ZSTD_VERSION_NUMBER >= 500)
private :
private :
void CreateNativeContext ( ) { }
void CreateNativeContext ( ) { }
void DestroyNativeContext ( ) { }
void DestroyNativeContext ( ) { }
# endif //ZSTD && (ZSTD_VERSION_NUMBER >= 500)
# endif // ZSTD && (ZSTD_VERSION_NUMBER >= 500)
public :
public :
explicit CompressionContext ( CompressionType comp_type ) :
explicit CompressionContext ( CompressionType comp_type ) : type_ ( comp_type ) {
type_ ( comp_type ) {
CreateNativeContext ( ) ;
CreateNativeContext ( ) ;
}
}
CompressionContext ( CompressionType comp_type ,
CompressionContext ( CompressionType comp_type , const CompressionOptions & opts ,
const CompressionOptions & opts ,
const Slice & comp_dict = Slice ( ) )
const Slice & comp_dict = Slice ( ) ) :
: type_ ( comp_type ) , opts_ ( opts ) , dict_ ( comp_dict ) {
type_ ( comp_type ) ,
opts_ ( opts ) ,
dict_ ( comp_dict ) {
CreateNativeContext ( ) ;
CreateNativeContext ( ) ;
}
}
~ CompressionContext ( ) {
~ CompressionContext ( ) { DestroyNativeContext ( ) ; }
DestroyNativeContext ( ) ;
}
CompressionContext ( const CompressionContext & ) = delete ;
CompressionContext ( const CompressionContext & ) = delete ;
CompressionContext & operator = ( const CompressionContext & ) = delete ;
CompressionContext & operator = ( const CompressionContext & ) = delete ;
const CompressionOptions & options ( ) const {
const CompressionOptions & options ( ) const { return opts_ ; }
return opts_ ;
CompressionType type ( ) const { return type_ ; }
}
const Slice & dict ( ) const { return dict_ ; }
CompressionType type ( ) const {
Slice & dict ( ) { return dict_ ; }
return type_ ;
}
const Slice & dict ( ) const {
return dict_ ;
}
Slice & dict ( ) {
return dict_ ;
}
} ;
} ;
// Instantiate this class and pass it to the uncompression API below
// Instantiate this class and pass it to the uncompression API below
class UncompressionContext {
class UncompressionContext {
private :
private :
CompressionType type_ ;
CompressionType type_ ;
Slice dict_ ;
Slice dict_ ;
CompressionContextCache * ctx_cache_ = nullptr ;
CompressionContextCache * ctx_cache_ = nullptr ;
ZSTDUncompressCachedData uncomp_cached_data_ ;
ZSTDUncompressCachedData uncomp_cached_data_ ;
public :
public :
struct NoCache { } ;
struct NoCache { } ;
// Do not use context cache, used by TableBuilder
// Do not use context cache, used by TableBuilder
UncompressionContext ( NoCache , CompressionType comp_type ) :
UncompressionContext ( NoCache , CompressionType comp_type ) : type_ ( comp_type ) { }
type_ ( comp_type ) {
explicit UncompressionContext ( CompressionType comp_type )
}
: UncompressionContext ( comp_type , Slice ( ) ) { }
explicit UncompressionContext ( CompressionType comp_type ) :
UncompressionContext ( CompressionType comp_type , const Slice & comp_dict )
UncompressionContext ( comp_type , Slice ( ) ) {
: type_ ( comp_type ) , dict_ ( comp_dict ) {
}
UncompressionContext ( CompressionType comp_type , const Slice & comp_dict ) :
type_ ( comp_type ) , dict_ ( comp_dict ) {
if ( type_ = = kZSTD ) {
if ( type_ = = kZSTD ) {
ctx_cache_ = CompressionContextCache : : Instance ( ) ;
ctx_cache_ = CompressionContextCache : : Instance ( ) ;
uncomp_cached_data_ = ctx_cache_ - > GetCachedZSTDUncompressData ( ) ;
uncomp_cached_data_ = ctx_cache_ - > GetCachedZSTDUncompressData ( ) ;
}
}
}
}
~ UncompressionContext ( ) {
~ UncompressionContext ( ) {
if ( type_ = = kZSTD & &
if ( type_ = = kZSTD & & uncomp_cached_data_ . GetCacheIndex ( ) ! = - 1 ) {
uncomp_cached_data_ . GetCacheIndex ( ) ! = - 1 ) {
assert ( ctx_cache_ ! = nullptr ) ;
assert ( ctx_cache_ ! = nullptr ) ;
ctx_cache_ - > ReturnCachedZSTDUncompressData (
ctx_cache_ - > ReturnCachedZSTDUncompressData (
uncomp_cached_data_ . GetCacheIndex ( ) ) ;
uncomp_cached_data_ . GetCacheIndex ( ) ) ;
}
}
}
}
UncompressionContext ( const UncompressionContext & ) = delete ;
UncompressionContext ( const UncompressionContext & ) = delete ;
@ -237,15 +219,9 @@ public:
ZSTDUncompressCachedData : : ZSTDNativeContext GetZSTDContext ( ) const {
ZSTDUncompressCachedData : : ZSTDNativeContext GetZSTDContext ( ) const {
return uncomp_cached_data_ . Get ( ) ;
return uncomp_cached_data_ . Get ( ) ;
}
}
CompressionType type ( ) const {
CompressionType type ( ) const { return type_ ; }
return type_ ;
const Slice & dict ( ) const { return dict_ ; }
}
Slice & dict ( ) { return dict_ ; }
const Slice & dict ( ) const {
return dict_ ;
}
Slice & dict ( ) {
return dict_ ;
}
} ;
} ;
inline bool Snappy_Supported ( ) {
inline bool Snappy_Supported ( ) {
@ -471,9 +447,9 @@ inline bool Zlib_Compress(const CompressionContext& ctx,
if ( ctx . dict ( ) . size ( ) ) {
if ( ctx . dict ( ) . size ( ) ) {
// Initialize the compression library's dictionary
// Initialize the compression library's dictionary
st = deflateSetDictionary (
st = deflateSetDictionary ( & _stream ,
& _stream , reinterpret_cast < const Bytef * > ( ctx . dict ( ) . data ( ) ) ,
reinterpret_cast < const Bytef * > ( ctx . dict ( ) . data ( ) ) ,
static_cast < unsigned int > ( ctx . dict ( ) . size ( ) ) ) ;
static_cast < unsigned int > ( ctx . dict ( ) . size ( ) ) ) ;
if ( st ! = Z_OK ) {
if ( st ! = Z_OK ) {
deflateEnd ( & _stream ) ;
deflateEnd ( & _stream ) ;
return false ;
return false ;
@ -516,8 +492,8 @@ inline bool Zlib_Compress(const CompressionContext& ctx,
// header in varint32 format
// header in varint32 format
// @param compression_dict Data for presetting the compression library's
// @param compression_dict Data for presetting the compression library's
// dictionary.
// dictionary.
inline char * Zlib_Uncompress ( const UncompressionContext & ctx , const char * input_data ,
inline char * Zlib_Uncompress ( const UncompressionContext & ctx ,
size_t input_length ,
const char * input_data , size_t input_length ,
int * decompress_size ,
int * decompress_size ,
uint32_t compress_format_version ,
uint32_t compress_format_version ,
int windowBits = - 14 ) {
int windowBits = - 14 ) {
@ -551,9 +527,9 @@ inline char* Zlib_Uncompress(const UncompressionContext& ctx, const char* input_
if ( ctx . dict ( ) . size ( ) ) {
if ( ctx . dict ( ) . size ( ) ) {
// Initialize the compression library's dictionary
// Initialize the compression library's dictionary
st = inflateSetDictionary (
st = inflateSetDictionary ( & _stream ,
& _stream , reinterpret_cast < const Bytef * > ( ctx . dict ( ) . data ( ) ) ,
reinterpret_cast < const Bytef * > ( ctx . dict ( ) . data ( ) ) ,
static_cast < unsigned int > ( ctx . dict ( ) . size ( ) ) ) ;
static_cast < unsigned int > ( ctx . dict ( ) . size ( ) ) ) ;
if ( st ! = Z_OK ) {
if ( st ! = Z_OK ) {
return nullptr ;
return nullptr ;
}
}
@ -835,8 +811,8 @@ inline bool LZ4_Compress(const CompressionContext& ctx,
// header in varint32 format
// header in varint32 format
// @param compression_dict Data for presetting the compression library's
// @param compression_dict Data for presetting the compression library's
// dictionary.
// dictionary.
inline char * LZ4_Uncompress ( const UncompressionContext & ctx , const char * input_data ,
inline char * LZ4_Uncompress ( const UncompressionContext & ctx ,
size_t input_length ,
const char * input_data , size_t input_length ,
int * decompress_size ,
int * decompress_size ,
uint32_t compress_format_version ) {
uint32_t compress_format_version ) {
# ifdef LZ4
# ifdef LZ4
@ -1026,9 +1002,9 @@ inline bool ZSTD_Compress(const CompressionContext& ctx, const char* input,
# if ZSTD_VERSION_NUMBER >= 500 // v0.5.0+
# if ZSTD_VERSION_NUMBER >= 500 // v0.5.0+
ZSTD_CCtx * context = ctx . ZSTDPreallocCtx ( ) ;
ZSTD_CCtx * context = ctx . ZSTDPreallocCtx ( ) ;
assert ( context ! = nullptr ) ;
assert ( context ! = nullptr ) ;
outlen = ZSTD_compress_usingDict (
outlen = ZSTD_compress_usingDict ( context , & ( * output ) [ output_header_len ] ,
context , & ( * output ) [ output_header_len ] , compressBound , input , length ,
compressBound , input , length ,
ctx . dict ( ) . data ( ) , ctx . dict ( ) . size ( ) , level ) ;
ctx . dict ( ) . data ( ) , ctx . dict ( ) . size ( ) , level ) ;
# else // up to v0.4.x
# else // up to v0.4.x
outlen = ZSTD_compress ( & ( * output ) [ output_header_len ] , compressBound , input ,
outlen = ZSTD_compress ( & ( * output ) [ output_header_len ] , compressBound , input ,
length , level ) ;
length , level ) ;
@ -1049,8 +1025,8 @@ inline bool ZSTD_Compress(const CompressionContext& ctx, const char* input,
// @param compression_dict Data for presetting the compression library's
// @param compression_dict Data for presetting the compression library's
// dictionary.
// dictionary.
inline char * ZSTD_Uncompress ( const UncompressionContext & ctx , const char * input_data ,
inline char * ZSTD_Uncompress ( const UncompressionContext & ctx ,
size_t input_length ,
const char * input_data , size_t input_length ,
int * decompress_size ) {
int * decompress_size ) {
# ifdef ZSTD
# ifdef ZSTD
uint32_t output_len = 0 ;
uint32_t output_len = 0 ;
@ -1065,8 +1041,8 @@ inline char* ZSTD_Uncompress(const UncompressionContext& ctx, const char* input_
ZSTD_DCtx * context = ctx . GetZSTDContext ( ) ;
ZSTD_DCtx * context = ctx . GetZSTDContext ( ) ;
assert ( context ! = nullptr ) ;
assert ( context ! = nullptr ) ;
actual_output_length = ZSTD_decompress_usingDict (
actual_output_length = ZSTD_decompress_usingDict (
context , output , output_len , input_data , input_length ,
context , output , output_len , input_data , input_length , ctx . dict ( ) . data ( ) ,
ctx . dict ( ) . data ( ) , ctx . dict ( ) . size ( ) ) ;
ctx . dict ( ) . size ( ) ) ;
# else // up to v0.4.x
# else // up to v0.4.x
actual_output_length =
actual_output_length =
ZSTD_decompress ( output , output_len , input_data , input_length ) ;
ZSTD_decompress ( output , output_len , input_data , input_length ) ;