Skip to content

Commit

Permalink
Sandbox c-blosc
Browse files Browse the repository at this point in the history
  • Loading branch information
oshogbo committed Feb 11, 2022
1 parent 0f78eb4 commit 9e1515f
Show file tree
Hide file tree
Showing 17 changed files with 875 additions and 5 deletions.
11 changes: 6 additions & 5 deletions contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ libraries.

## Projects Sandboxed

Directory | Project | Home Page | Integration
----------- | ------------------------------------------------- | -------------------------------------------------------------------- | -----------
`jsonnet/` | Jsonnet - The Data Templating Language | [github.com/google/jsonnet](https://github.com/google/jsonnet) | CMake
`hunspell/` | Hunspell - The most popular spellchecking library | [github.com/hunspell/hunspell](https://github.com/hunspell/hunspell) | CMake
`zstd/` | Zstandard - Fast real-time compression algorithm | [github.com/facebook/zstd](https://github.com/facebook/zstd) | CMake
Directory | Project | Home Page | Integration
----------- | --------------------------------------------------------------- | -------------------------------------------------------------------- | -----------
`c-blosc/` | c-blosc A blocking, shuffling and loss-less compression library | [github.com/Blosc/c-blosc](https://github.com/Blosc/c-blosc) | CMake
`jsonnet/` | Jsonnet - The Data Templating Language | [github.com/google/jsonnet](https://github.com/google/jsonnet) | CMake
`hunspell/` | Hunspell - The most popular spellchecking library | [github.com/hunspell/hunspell](https://github.com/hunspell/hunspell) | CMake
`zstd/` | Zstandard - Fast real-time compression algorithm | [github.com/facebook/zstd](https://github.com/facebook/zstd) | CMake

## Projects Shipping with Sandboxed API Sandboxes

Expand Down
90 changes: 90 additions & 0 deletions contrib/c-blosc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.13..3.22)

project(sapi_blosc CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if(NOT TARGET sapi::sapi)
set(SAPI_ROOT "../.." CACHE PATH "Path to the Sandboxed API source tree")
add_subdirectory(
"${SAPI_ROOT}"
"${CMAKE_BINARY_DIR}/sandboxed-api-build"
EXCLUDE_FROM_ALL
)
endif()

set(HIDE_SYMBOLS off)
FetchContent_Declare(
libblosc

GIT_REPOSITORY https://github.com/Blosc/c-blosc.git
GIT_TAG a0e5c18d37db8e6f1003254a574c8062c5b45e00
PATCH_COMMAND patch < "${CMAKE_SOURCE_DIR}/patches/c-blosc.cmake.patch" && cd blosc && patch < "${CMAKE_SOURCE_DIR}/patches/c-blosc.blosc.cmake.patch"
)
FetchContent_MakeAvailable(libblosc)

add_sapi_library(
sapi_blosc

FUNCTIONS
blosc_init
blosc_destroy

blosc_compress
blosc_decompress

blosc_get_nthreads
blosc_set_nthreads

blosc_get_compressor
blosc_set_compressor

blosc_list_compressors

blosc_get_version_string

blosc_get_blocksize
blosc_set_blocksize

blosc_set_splitmode

blosc_cbuffer_sizes
blosc_cbuffer_validate
blosc_cbuffer_versions

INPUTS
"${libblosc_SOURCE_DIR}/blosc/blosc.h"

LIBRARY blosc_static
LIBRARY_NAME Cblosc
NAMESPACE ""
)

add_library(sapi_contrib::blosc ALIAS sapi_blosc)

target_include_directories(sapi_blosc INTERFACE
"${PROJECT_BINARY_DIR}"
)

if (SAPI_ENABLE_EXAMPLES)
add_subdirectory(example)
endif()

if (SAPI_ENABLE_TESTS)
add_subdirectory(test)
endif()
32 changes: 32 additions & 0 deletions contrib/c-blosc/example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

add_executable(
sapi_miniblosc

"main.cc"
"../utils/utils_blosc.cc"
)

target_include_directories(sapi_miniblosc INTERFACE
"${SAPI_SOURCE_DIR}"
)

target_link_libraries(
sapi_miniblosc PRIVATE

sapi_blosc
sapi::sapi
absl::flags_parse
)
94 changes: 94 additions & 0 deletions contrib/c-blosc/example/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <absl/flags/flag.h>
#include <absl/flags/parse.h>
#include <unistd.h>

#include <fstream>
#include <iostream>
#include <string>

#include "contrib/c-blosc/sandboxed.h"
#include "contrib/c-blosc/utils/utils_blosc.h"

ABSL_FLAG(bool, decompress, false, "decompress");
ABSL_FLAG(int, clevel, 5, "compression level");
ABSL_FLAG(uint32_t, nthreads, 5, "number of threads");
ABSL_FLAG(std::string, compressor, "blosclz",
"compressor engine. Available: blosclz, lz4, lz4hc, zlib, zstd");

absl::Status Stream(CbloscApi& api, std::string& infile_s,
std::string& outfile_s) {
std::ifstream infile(infile_s, std::ios::binary);
if (!infile.is_open()) {
return absl::UnavailableError(absl::StrCat("Unable to open ", infile_s));
}
std::ofstream outfile(outfile_s, std::ios::binary);
if (!outfile.is_open()) {
return absl::UnavailableError(absl::StrCat("Unable to open ", outfile_s));
}

std::string compressor(absl::GetFlag(FLAGS_compressor));

if (absl::GetFlag(FLAGS_decompress)) {
return Decompress(api, infile, outfile, 5);
}

return Compress(api, infile, outfile, absl::GetFlag(FLAGS_clevel), compressor,
absl::GetFlag(FLAGS_nthreads));
}

int main(int argc, char* argv[]) {
std::string prog_name(argv[0]);
google::InitGoogleLogging(argv[0]);
std::vector<char*> args = absl::ParseCommandLine(argc, argv);

if (args.size() != 3) {
std::cerr << "Usage:\n " << prog_name << " INPUT OUTPUT\n";
return EXIT_FAILURE;
}

CbloscSapiSandbox sandbox;
if (!sandbox.Init().ok()) {
std::cerr << "Unable to start sandbox\n";
return EXIT_FAILURE;
}
CbloscApi api(&sandbox);

if (absl::Status status = api.blosc_init(); !status.ok()) {
std::cerr << "Unable to init library\n";
std::cerr << status << std::endl;
return EXIT_FAILURE;
}

std::string infile_s(args[1]);
std::string outfile_s(args[2]);

if (absl::Status status = Stream(api, infile_s, outfile_s); !status.ok()) {
std::cerr << "Unable to ";
std::cerr << (absl::GetFlag(FLAGS_decompress) ? "de" : "");
std::cerr << "compress file\n";
std::cerr << status << std::endl;
return EXIT_FAILURE;
}

if (absl::Status status = api.blosc_destroy(); !status.ok()) {
std::cerr << "Unable to uninitialize library\n";
std::cerr << status << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Loading

0 comments on commit 9e1515f

Please sign in to comment.