Replies: 3 comments
-
ClickHouse's code contains many libraries similar to BN (BigNumber), but since chDB or libchdb is positioned as an embedded data execution engine, we haven't exposed too much code from unrelated libraries. However, the example you provided is very interesting, but it needs to be clarified that since these symbols are not explicitly exported, they might be optimized by the compiler during some release process. |
Beta Was this translation helpful? Give feedback.
-
the functions I most needed are that register udfs, as I tested, the python udf is not fast, if I could call registerFunction to load c functions, it would be nice, |
Beta Was this translation helpful? Give feedback.
-
lz4 lib is also useful #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "lz4frame.h"
#define MAX_FILENAME_LEN 256
int compress_streaming_with_level(FILE* inFile, FILE* outFile, int compression_level) {
LZ4F_cctx* cctx = NULL;
const size_t in_chunk_size = 64 * 1024; // 64KB块大小
const size_t out_chunk_size = LZ4F_compressFrameBound(in_chunk_size, NULL) + 32;
char* in_buf = malloc(in_chunk_size);
char* out_buf = malloc(out_chunk_size);
size_t in_size, out_size;
// 设置压缩偏好(包含压缩级别)
LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
prefs.compressionLevel = compression_level;
prefs.frameInfo.blockSizeID = LZ4F_max64KB;
prefs.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled;
// 创建上下文
size_t ret = LZ4F_createCompressionContext(&cctx, LZ4F_VERSION);
if (LZ4F_isError(ret)) goto cleanup;
// 写入帧头
out_size = LZ4F_compressBegin(cctx, out_buf, out_chunk_size, &prefs);
if (LZ4F_isError(out_size)) goto cleanup;
fwrite(out_buf, 1, out_size, outFile);
// 流式压缩
while ((in_size = fread(in_buf, 1, in_chunk_size, inFile)) > 0) {
out_size = LZ4F_compressUpdate(cctx, out_buf, out_chunk_size, in_buf, in_size, NULL);
if (LZ4F_isError(out_size)) goto cleanup;
if (out_size > 0) fwrite(out_buf, 1, out_size, outFile);
}
// 写入帧尾
out_size = LZ4F_compressEnd(cctx, out_buf, out_chunk_size, NULL);
if (LZ4F_isError(out_size)) goto cleanup;
fwrite(out_buf, 1, out_size, outFile);
ret = 0;
cleanup:
free(in_buf);
free(out_buf);
LZ4F_freeCompressionContext(cctx);
return LZ4F_isError(ret) ? -1 : 0;
}
int compress_file(const char* input_path, const char* output_path, int level) {
FILE* fin = fopen(input_path, "rb");
FILE* fout = fopen(output_path, "wb");
if (!fin || !fout) {
if (fin) fclose(fin);
if (fout) fclose(fout);
return -1;
}
clock_t start = clock();
int result = compress_streaming_with_level(fin, fout, level);
clock_t end = clock();
fclose(fin);
fclose(fout);
if (result != 0) {
remove(output_path);
return -1;
}
return (int)((end - start) * 1000 / CLOCKS_PER_SEC);
}
void interactive_compression_test() {
char input_path[MAX_FILENAME_LEN];
char output_path[MAX_FILENAME_LEN];
int level;
printf("LZ4 Compression Test (Press Ctrl+C to exit)\n");
printf("------------------------------------------\n");
while (1) {
// 获取输入文件
printf("\nInput file path (or 'quit' to exit): ");
if (fgets(input_path, MAX_FILENAME_LEN, stdin) == NULL) break;
input_path[strcspn(input_path, "\n")] = '\0'; // 移除换行符
if (strcmp(input_path, "quit") == 0) break;
// 检查文件是否存在
FILE* test = fopen(input_path, "rb");
if (!test) {
printf("Error: File not found or inaccessible\n");
continue;
}
fclose(test);
// 获取压缩级别
printf("Compression level (1-12): ");
if (scanf("%d", &level) != 1 || level < 1 || level > 12) {
printf("Invalid level. Using default 6.\n");
level = 6;
}
while (getchar() != '\n'); // 清空输入缓冲区
// 生成输出文件名
snprintf(output_path, MAX_FILENAME_LEN, "%s.lz4", input_path);
// 执行压缩
printf("Compressing... ");
fflush(stdout);
int time_ms = compress_file(input_path, output_path, level);
if (time_ms < 0) {
printf("FAILED\n");
continue;
}
// 获取压缩结果
FILE* f = fopen(output_path, "rb");
fseek(f, 0, SEEK_END);
long compressed_size = ftell(f);
fclose(f);
f = fopen(input_path, "rb");
fseek(f, 0, SEEK_END);
long original_size = ftell(f);
fclose(f);
// 显示结果
printf("DONE\n");
printf("Original size: %.2f MB\n", original_size / (1024.0 * 1024.0));
printf("Compressed size: %.2f MB\n", compressed_size / (1024.0 * 1024.0));
printf("Compression ratio: %.2f%%\n", (compressed_size * 100.0) / original_size);
printf("Time taken: %d ms\n", time_ms);
}
printf("\nExiting...\n");
}
int main() {
interactive_compression_test();
return 0;
} both |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I use
nm -D libchdb.so | c++filt | grep " T " >libch2.h
, and found many useful functions.such as BN functions, but they lack of declaration in the head file. I ask deepseek to code them for me.
Beta Was this translation helpful? Give feedback.
All reactions