hash-ll.h: split out of hash.h to remove dependency on repository.h
[alt-git.git] / t / helper / test-bloom.c
blobaabe31d724b675e4f69c9f3760bcd0a6e8e9bbad
1 #include "test-tool.h"
2 #include "bloom.h"
3 #include "hex.h"
4 #include "commit.h"
5 #include "repository.h"
6 #include "setup.h"
8 static struct bloom_filter_settings settings = DEFAULT_BLOOM_FILTER_SETTINGS;
10 static void add_string_to_filter(const char *data, struct bloom_filter *filter) {
11 struct bloom_key key;
12 int i;
14 fill_bloom_key(data, strlen(data), &key, &settings);
15 printf("Hashes:");
16 for (i = 0; i < settings.num_hashes; i++){
17 printf("0x%08x|", key.hashes[i]);
19 printf("\n");
20 add_key_to_filter(&key, filter, &settings);
21 clear_bloom_key(&key);
24 static void print_bloom_filter(struct bloom_filter *filter) {
25 int i;
27 if (!filter) {
28 printf("No filter.\n");
29 return;
31 printf("Filter_Length:%d\n", (int)filter->len);
32 printf("Filter_Data:");
33 for (i = 0; i < filter->len; i++) {
34 printf("%02x|", filter->data[i]);
36 printf("\n");
39 static void get_bloom_filter_for_commit(const struct object_id *commit_oid)
41 struct commit *c;
42 struct bloom_filter *filter;
43 setup_git_directory();
44 c = lookup_commit(the_repository, commit_oid);
45 filter = get_or_compute_bloom_filter(the_repository, c, 1,
46 &settings,
47 NULL);
48 print_bloom_filter(filter);
51 static const char *bloom_usage = "\n"
52 " test-tool bloom get_murmur3 <string>\n"
53 " test-tool bloom generate_filter <string> [<string>...]\n"
54 " test-tool bloom get_filter_for_commit <commit-hex>\n";
56 int cmd__bloom(int argc, const char **argv)
58 setup_git_directory();
60 if (argc < 2)
61 usage(bloom_usage);
63 if (!strcmp(argv[1], "get_murmur3")) {
64 uint32_t hashed;
65 if (argc < 3)
66 usage(bloom_usage);
67 hashed = murmur3_seeded(0, argv[2], strlen(argv[2]));
68 printf("Murmur3 Hash with seed=0:0x%08x\n", hashed);
71 if (!strcmp(argv[1], "generate_filter")) {
72 struct bloom_filter filter;
73 int i = 2;
74 filter.len = (settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD;
75 CALLOC_ARRAY(filter.data, filter.len);
77 if (argc - 1 < i)
78 usage(bloom_usage);
80 while (argv[i]) {
81 add_string_to_filter(argv[i], &filter);
82 i++;
85 print_bloom_filter(&filter);
86 free(filter.data);
89 if (!strcmp(argv[1], "get_filter_for_commit")) {
90 struct object_id oid;
91 const char *end;
92 if (argc < 3)
93 usage(bloom_usage);
94 if (parse_oid_hex(argv[2], &oid, &end))
95 die("cannot parse oid '%s'", argv[2]);
96 init_bloom_filters();
97 get_bloom_filter_for_commit(&oid);
100 return 0;