int mode_filter(int argc, char** argv) { uint argo = 0; uint negative = FALSE; if (strcmp(argv[2 + argo], "-n") == 0) { negative = TRUE; argo++; } uint update = FALSE; if (strcmp(argv[2 + argo], "-u") == 0) { update = TRUE; argo++; } char* bloom_file = argv[2 + argo]; bf_t* filter = bf_load(bloom_file, BLOOM_HASH_FUNC); if (!filter) { printf("File %s has wrong format or doesn't exist.\n", bloom_file); return 2; } char input_buffer[BF_KEY_BUFFER_SIZE] = { 0 }; uint actual_length = 0; while (gets_skip(input_buffer, BF_KEY_BUFFER_SIZE, &actual_length)) { uint has = bf_has(filter, (const char*)input_buffer, actual_length); if (negative) { if (!has) { printf("%s\n", input_buffer); fflush(stdout); if (update) { bf_add(filter, input_buffer, actual_length); } } } else { if (has) { printf("%s\n", input_buffer); fflush(stdout); } else { if (update) { bf_add(filter, input_buffer, actual_length); } } } } if (update) { bf_save(filter, bloom_file); } bf_destroy(filter); return 0; }
int mode_info(int argc, char** argv) { char* bloom_file = argv[2]; bf_t* filter = bf_load(bloom_file, BLOOM_HASH_FUNC); if (!filter) { printf("File %s has wrong format or doesn't exist.\n", bloom_file); return 2; } printf("capacity: %ld\n", filter->bits_count); printf("used: %ld\n", filter->bits_used); printf("error rate: %.3f%%\n", exp(- filter->bits_count / (float)filter->bits_used * BF_LOG2_2)); printf("hash/item: %d\n", filter->hashes_count); bf_destroy(filter); }
int mode_create(int argc, char** argv) { uint argo = 0; uint progress = FALSE; if (strcmp(argv[2], "-p") == 0) { argo++; progress = TRUE; } if (argc - argo < 5) { show_help(argv[0]); return 1; } char* bloom_file = argv[2 + argo]; double error_rate = atof(argv[3 + argo]); bf_index_t key_count = atol(argv[4 + argo]); bf_t* filter = bf_create(error_rate, key_count, BLOOM_HASH_FUNC); char input_buffer[BF_KEY_BUFFER_SIZE] = { 0 }; bf_index_t key = 0; uint actual_length; while (gets_skip(input_buffer, BF_KEY_BUFFER_SIZE, &actual_length)) { bf_add(filter, (const char*)input_buffer, actual_length); key = key + 1; if (progress && key % 1000000 == 0) { printf("%s: %s <- %ld\n", argv[0], bloom_file, key); } } bf_save(filter, bloom_file); bf_destroy(filter); return 0; }
int mode_merge(int argc, char** argv) { if (argc < 4) { show_help(argv[0]); return 1; } char* bloom_file = argv[3]; bf_t* filter = bf_load(bloom_file, BLOOM_HASH_FUNC); if (!filter) { printf("File %s has wrong format or doesn't exist.\n", bloom_file); return 2; } bf_t* merge_with; uint file_index; for (file_index = 4; file_index < argc; file_index++) { merge_with = bf_load(argv[file_index], BLOOM_HASH_FUNC); if (!merge_with) { printf("File %s has wrong format or doesn't exist.\n", argv[file_index]); return 2; } bf_merge(filter, merge_with); bf_destroy(merge_with); } char* merged_bloom_file = argv[2]; bf_save(filter, merged_bloom_file); return 0; }
int main( int argc, char ** argv ) { prf_model_t * model; bfile_t * bfile; int i; if ( argc <= 3 ) { printf( "Usage: %s <inputfile> <outputfile> <opcode> [<opcode> ...]\n", argv[0] ); return -1; } bfile = bf_create_r( argv[1] ); if ( ! bfile ) { printf( "Error: Could not open '%s' for loading.\n", argv[1] ); return -1; } prf_init(); model = prf_model_create(); assert( model != NULL ); prf_model_poolmem( model ); if ( ! prf_model_load( model, bfile ) ) { printf( "Error: Couldn't parse model.\n" ); bf_destroy( bfile ); prf_exit(); return -1; } bf_destroy( bfile ); for ( i = 3; i < argc; i++ ) { prf_nodeinfo_t * info; info = prf_nodeinfo_get( atoi( argv[i] ) ); if ( ! info ) { fprintf( stderr, "Warning: couldn't find nodeinfo for '%s'\n", argv[i] ); } else { info->save_f = dont_save; } } bfile = bf_create_w( argv[2] ); if ( ! bfile ) { printf( "Error: Could not open '%s' for writing.\n", argv[2] ); prf_model_destroy( model ); prf_exit(); return -1; } prf_model_save( model, bfile ); bf_destroy( bfile ); prf_model_destroy( model ); prf_exit(); return 0; } /* main() */