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