Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: BI SHUN KE <aionshun@livemail.tw>
- Loading branch information
Showing
11 changed files
with
253 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include "brotli/encode.h" | ||
#include "brotli/decode.h" | ||
#include "BrotliCompressor.h" | ||
#include "include/scope_guard.h" | ||
|
||
#define MAX_LEN (CEPH_PAGE_SIZE) | ||
|
||
int BrotliCompressor::compress(const bufferlist &in, bufferlist &out) | ||
{ | ||
BrotliEncoderState* s = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); | ||
auto sg = make_scope_guard([&s] { BrotliEncoderDestroyInstance(s); }); | ||
size_t available_in = 0; | ||
const uint8_t* next_in = nullptr; | ||
size_t available_out = 0; | ||
uint8_t* next_out = nullptr; | ||
if (!s) { | ||
return -1; | ||
} | ||
BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, (uint32_t)9); | ||
BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, 22); | ||
unsigned char* c_in; | ||
for (auto i = in.buffers().begin(); i != in.buffers().end();) { | ||
c_in = (unsigned char*) i->c_str(); | ||
size_t len = i->length(); | ||
available_in = len; | ||
next_in = c_in; | ||
++i; | ||
BrotliEncoderOperation finish = i != in.buffers().end() ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH; | ||
do { | ||
size_t max_comp_size = BrotliEncoderMaxCompressedSize(available_in); | ||
bufferptr ptr = buffer::create_page_aligned(max_comp_size); | ||
next_out = (unsigned char*)ptr.c_str(); | ||
available_out = max_comp_size; | ||
if (!BrotliEncoderCompressStream(s,finish,&available_in, &next_in, &available_out, &next_out, nullptr)) { | ||
return -1; | ||
} | ||
unsigned have = max_comp_size - available_out; | ||
out.append(ptr, 0, have); | ||
} while (available_out == 0); | ||
if (BrotliEncoderIsFinished(s)) { | ||
break; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
int BrotliCompressor::decompress(bufferlist::iterator &p, size_t compressed_size, bufferlist &out) | ||
{ | ||
const char* c_in; | ||
size_t available_in = 0; | ||
const uint8_t* next_in = nullptr; | ||
size_t available_out = 0; | ||
uint8_t* next_out = nullptr; | ||
BrotliDecoderState* s = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr); | ||
auto sg = make_scope_guard([&s] { BrotliDecoderDestroyInstance(s); }); | ||
size_t remaining = std::min<size_t>(p.get_remaining(), compressed_size); | ||
if (!s) { | ||
return -1; | ||
} | ||
while (remaining) { | ||
size_t len = p.get_ptr_and_advance(remaining, &c_in); | ||
remaining -= len; | ||
available_in = len; | ||
next_in = (unsigned char*)c_in; | ||
do { | ||
available_out = MAX_LEN; | ||
bufferptr ptr = buffer::create_page_aligned(MAX_LEN); | ||
next_out = (unsigned char*)ptr.c_str(); | ||
if (!BrotliDecoderDecompressStream(s, &available_in, &next_in, &available_out, &next_out, 0)) { | ||
return -1; | ||
} | ||
unsigned have = MAX_LEN - available_out; | ||
out.append(ptr, 0, have); | ||
} while (available_out == 0); | ||
if (BrotliDecoderIsFinished(s)) { | ||
break; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
int BrotliCompressor::decompress(const bufferlist &in, bufferlist &out) | ||
{ | ||
bufferlist::iterator i = const_cast<bufferlist&>(in).begin(); | ||
return decompress(i, in.length(), out); | ||
} |
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,31 @@ | ||
/* | ||
* Ceph - scalable distributed file system | ||
* | ||
* Copyright (C) 2017 BI SHUN KE <aionshun@livemail.tw> | ||
* | ||
* This is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License version 2.1, as published by the Free Software | ||
* Foundation. See file COPYING. | ||
* | ||
*/ | ||
|
||
#ifndef CEPH_BROTLICOMPRESSOR_H | ||
#define CEPH_BROTLICOMPRESSOR_H | ||
|
||
|
||
#include "include/buffer.h" | ||
#include "compressor/Compressor.h" | ||
|
||
class BrotliCompressor : public Compressor | ||
{ | ||
public: | ||
BrotliCompressor() : Compressor(COMP_ALG_BROTLI, "brotli") {} | ||
|
||
int compress(const bufferlist &in, bufferlist &out) override; | ||
int decompress(const bufferlist &in, bufferlist &out) override; | ||
int decompress(bufferlist::iterator &p, size_t compressed_len, bufferlist &out) override; | ||
}; | ||
|
||
#endif //CEPH_BROTLICOMPRESSOR_H | ||
|
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,45 @@ | ||
# brotli | ||
|
||
set(brotli_sources | ||
CompressionPluginBrotli.cc | ||
BrotliCompressor.cc | ||
) | ||
include(ExternalProject) | ||
ExternalProject_Add(brotli_ext | ||
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/src/ | ||
GIT_REPOSITORY "https://github.com/google/brotli.git" | ||
GIT_TAG "master" | ||
SOURCE_DIR ${CMAKE_BINARY_DIR}/src/brotli | ||
CONFIGURE_COMMAND ./configure-cmake --disable-debug | ||
INSTALL_COMMAND "" | ||
BUILD_COMMAND $(MAKE) | ||
BUILD_IN_SOURCE 1 | ||
INSTALL_COMMAND "") | ||
|
||
ExternalProject_Add_Step(brotli_ext forcebuild | ||
DEPENDEES configure | ||
DEPENDERS build | ||
COMMAND "true" | ||
ALWAYS 1) | ||
|
||
set(bortli_libs brotli::common brotli::dec brotli::enc) | ||
foreach(lib ${bortli_libs}) | ||
add_library(${lib} STATIC IMPORTED) | ||
add_dependencies(${lib} brotli_ext) | ||
STRING(REGEX REPLACE "::" "" static_name ${lib}) | ||
set_property(TARGET ${lib} PROPERTY IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/src/brotli/lib${static_name}-static.a") | ||
endforeach() | ||
|
||
add_library(ceph_brotli SHARED ${brotli_sources}) | ||
add_dependencies(ceph_brotli ${CMAKE_SOURCE_DIR}/src/ceph_ver.h) | ||
target_include_directories(ceph_brotli PRIVATE "${CMAKE_BINARY_DIR}/src/brotli/c/include") | ||
List(REVERSE bortli_libs) | ||
target_link_libraries(ceph_brotli ${bortli_libs}) | ||
set_target_properties(ceph_brotli PROPERTIES VERSION 2.0.0 SOVERSION 2) | ||
install(TARGETS ceph_brotli DESTINATION ${compressor_plugin_dir}) | ||
|
||
if(WITH_EMBEDDED) | ||
add_library(cephd_compressor_brotli STATIC ${brotli_sources}) | ||
target_include_directories(cephd_compressor_brotli PRIVATE "${CMAKE_BINARY_DIR}/src/brotli/c/include") | ||
set_target_properties(cephd_compressor_brotli PROPERTIES COMPILE_DEFINITIONS BUILDING_FOR_EMBEDDED) | ||
endif() |
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,18 @@ | ||
#include "acconfig.h" | ||
#include "ceph_ver.h" | ||
#include "CompressionPluginBrotli.h" | ||
|
||
#ifndef BUILDING_FOR_EMBEDDED | ||
|
||
const char *__ceph_plugin_version() | ||
{ | ||
return CEPH_GIT_NICE_VER; | ||
} | ||
|
||
int __ceph_plugin_init(CephContext *cct, const std::string& type, const std::string& name) | ||
{ | ||
PluginRegistry *instance = cct->get_plugin_registry(); | ||
return instance->add(type, name, new CompressionPluginBrotli(cct)); | ||
} | ||
|
||
#endif // !BUILDING_FOR_EMBEDDED |
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,36 @@ | ||
/* | ||
* Ceph - scalable distributed file system | ||
* | ||
* Copyright (C) 2017 BI SHUN KE <aionshun@livemail.tw> | ||
* | ||
* This is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License version 2.1, as published by the Free Software | ||
* Foundation. See file COPYING. | ||
* | ||
*/ | ||
|
||
#ifndef CEPH_COMPRESSION_PLUGIN_BROTLI_H | ||
#define CEPH_COMPRESSION_PLUGIN_BROTLI_H | ||
|
||
#include "ceph_ver.h" | ||
#include "compressor/CompressionPlugin.h" | ||
#include "BrotliCompressor.h" | ||
|
||
class CompressionPluginBrotli : public CompressionPlugin { | ||
public: | ||
explicit CompressionPluginBrotli(CephContext *cct) : CompressionPlugin(cct) | ||
{} | ||
|
||
virtual int factory(CompressorRef *cs, std::ostream *ss) | ||
{ | ||
if (compressor == 0) { | ||
BrotliCompressor *interface = new BrotliCompressor(); | ||
compressor = CompressorRef(interface); | ||
} | ||
*cs = compressor; | ||
return 0; | ||
} | ||
}; | ||
|
||
#endif |
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