]>
Commit | Line | Data |
---|---|---|
61a7b982 | 1 | #include "test-tool.h" |
f52207a4 | 2 | #include "bloom.h" |
41771fa4 | 3 | #include "hex.h" |
ed591feb | 4 | #include "commit.h" |
d1cbe1e6 | 5 | #include "repository.h" |
e38da487 | 6 | #include "setup.h" |
f52207a4 | 7 | |
066b70ae | 8 | static struct bloom_filter_settings settings = DEFAULT_BLOOM_FILTER_SETTINGS; |
f1294eaf GS |
9 | |
10 | static void add_string_to_filter(const char *data, struct bloom_filter *filter) { | |
11 | struct bloom_key key; | |
12 | int i; | |
13 | ||
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]); | |
18 | } | |
19 | printf("\n"); | |
20 | add_key_to_filter(&key, filter, &settings); | |
9794633b | 21 | clear_bloom_key(&key); |
f1294eaf GS |
22 | } |
23 | ||
24 | static void print_bloom_filter(struct bloom_filter *filter) { | |
25 | int i; | |
26 | ||
27 | if (!filter) { | |
28 | printf("No filter.\n"); | |
29 | return; | |
30 | } | |
31 | printf("Filter_Length:%d\n", (int)filter->len); | |
32 | printf("Filter_Data:"); | |
24b7d1e7 | 33 | for (i = 0; i < filter->len; i++) { |
f1294eaf GS |
34 | printf("%02x|", filter->data[i]); |
35 | } | |
36 | printf("\n"); | |
37 | } | |
38 | ||
ed591feb GS |
39 | static void get_bloom_filter_for_commit(const struct object_id *commit_oid) |
40 | { | |
41 | struct commit *c; | |
42 | struct bloom_filter *filter; | |
ed591feb | 43 | c = lookup_commit(the_repository, commit_oid); |
312cff52 | 44 | filter = get_or_compute_bloom_filter(the_repository, c, 1, |
9a7a9ed1 | 45 | &settings, |
312cff52 | 46 | NULL); |
ed591feb GS |
47 | print_bloom_filter(filter); |
48 | } | |
49 | ||
1b4c57fa | 50 | static const char *bloom_usage = "\n" |
54c337be | 51 | " test-tool bloom get_murmur3 <string>\n" |
1b4c57fa | 52 | " test-tool bloom generate_filter <string> [<string>...]\n" |
dba94e3a | 53 | " test-tool bloom get_filter_for_commit <commit-hex>\n"; |
1b4c57fa | 54 | |
f52207a4 GS |
55 | int cmd__bloom(int argc, const char **argv) |
56 | { | |
094a685c | 57 | setup_git_directory(); |
58 | ||
1b4c57fa JK |
59 | if (argc < 2) |
60 | usage(bloom_usage); | |
61 | ||
f52207a4 | 62 | if (!strcmp(argv[1], "get_murmur3")) { |
1b4c57fa JK |
63 | uint32_t hashed; |
64 | if (argc < 3) | |
65 | usage(bloom_usage); | |
66 | hashed = murmur3_seeded(0, argv[2], strlen(argv[2])); | |
f52207a4 GS |
67 | printf("Murmur3 Hash with seed=0:0x%08x\n", hashed); |
68 | } | |
69 | ||
24b7d1e7 | 70 | if (!strcmp(argv[1], "generate_filter")) { |
f1294eaf GS |
71 | struct bloom_filter filter; |
72 | int i = 2; | |
73 | filter.len = (settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD; | |
486f4bd1 | 74 | CALLOC_ARRAY(filter.data, filter.len); |
f1294eaf | 75 | |
1b4c57fa JK |
76 | if (argc - 1 < i) |
77 | usage(bloom_usage); | |
f1294eaf GS |
78 | |
79 | while (argv[i]) { | |
80 | add_string_to_filter(argv[i], &filter); | |
81 | i++; | |
82 | } | |
83 | ||
84 | print_bloom_filter(&filter); | |
9794633b | 85 | free(filter.data); |
f1294eaf GS |
86 | } |
87 | ||
24b7d1e7 | 88 | if (!strcmp(argv[1], "get_filter_for_commit")) { |
ed591feb GS |
89 | struct object_id oid; |
90 | const char *end; | |
1b4c57fa JK |
91 | if (argc < 3) |
92 | usage(bloom_usage); | |
ed591feb GS |
93 | if (parse_oid_hex(argv[2], &oid, &end)) |
94 | die("cannot parse oid '%s'", argv[2]); | |
95 | init_bloom_filters(); | |
96 | get_bloom_filter_for_commit(&oid); | |
97 | } | |
98 | ||
f52207a4 | 99 | return 0; |
24b7d1e7 | 100 | } |