Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "test-tool.h"
4 #include "bloom.h"
5 #include "hex.h"
6 #include "commit.h"
7 #include "repository.h"
8 #include "setup.h"
9
10 static struct bloom_filter_settings settings = DEFAULT_BLOOM_FILTER_SETTINGS;
11
12 static void add_string_to_filter(const char *data, struct bloom_filter *filter) {
13 struct bloom_key key;
14
15 bloom_key_fill(&key, data, strlen(data), &settings);
16 printf("Hashes:");
17 for (size_t i = 0; i < settings.num_hashes; i++)
18 printf("0x%08x|", key.hashes[i]);
19 printf("\n");
20 add_key_to_filter(&key, filter, &settings);
21 bloom_key_clear(&key);
22 }
23
24 static void print_bloom_filter(struct bloom_filter *filter) {
25 if (!filter) {
26 printf("No filter.\n");
27 return;
28 }
29 printf("Filter_Length:%d\n", (int)filter->len);
30 printf("Filter_Data:");
31 for (size_t i = 0; i < filter->len; i++)
32 printf("%02x|", filter->data[i]);
33 printf("\n");
34 }
35
36 static void get_bloom_filter_for_commit(const struct object_id *commit_oid)
37 {
38 struct commit *c;
39 struct bloom_filter *filter;
40 c = lookup_commit(the_repository, commit_oid);
41 filter = get_or_compute_bloom_filter(the_repository, c, 1,
42 &settings,
43 NULL);
44 print_bloom_filter(filter);
45 }
46
47 static const char *const bloom_usage = "\n"
48 " test-tool bloom get_murmur3 <string>\n"
49 " test-tool bloom get_murmur3_seven_highbit\n"
50 " test-tool bloom generate_filter <string> [<string>...]\n"
51 " test-tool bloom get_filter_for_commit <commit-hex>\n";
52
53 int cmd__bloom(int argc, const char **argv)
54 {
55 setup_git_directory(the_repository);
56
57 if (argc < 2)
58 usage(bloom_usage);
59
60 if (!strcmp(argv[1], "get_murmur3")) {
61 uint32_t hashed;
62 if (argc < 3)
63 usage(bloom_usage);
64 hashed = test_bloom_murmur3_seeded(0, argv[2], strlen(argv[2]), 2);
65 printf("Murmur3 Hash with seed=0:0x%08x\n", hashed);
66 }
67
68 if (!strcmp(argv[1], "get_murmur3_seven_highbit")) {
69 uint32_t hashed;
70 hashed = test_bloom_murmur3_seeded(0, "\x99\xaa\xbb\xcc\xdd\xee\xff", 7, 2);
71 printf("Murmur3 Hash with seed=0:0x%08x\n", hashed);
72 }
73
74 if (!strcmp(argv[1], "generate_filter")) {
75 struct bloom_filter filter;
76 int i = 2;
77 filter.len = (settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD;
78 CALLOC_ARRAY(filter.data, filter.len);
79
80 if (argc - 1 < i)
81 usage(bloom_usage);
82
83 while (argv[i]) {
84 add_string_to_filter(argv[i], &filter);
85 i++;
86 }
87
88 print_bloom_filter(&filter);
89 free(filter.data);
90 }
91
92 if (!strcmp(argv[1], "get_filter_for_commit")) {
93 struct object_id oid;
94 const char *end;
95 if (argc < 3)
96 usage(bloom_usage);
97 if (parse_oid_hex(argv[2], &oid, &end))
98 die("cannot parse oid '%s'", argv[2]);
99 init_bloom_filters();
100 get_bloom_filter_for_commit(&oid);
101 }
102
103 return 0;
104 }