1 | #define _GNU_SOURCE |
2 | #include "roaring/roaring.h" |
3 | #include "benchmark.h" |
4 | #include "numbersfromtextfiles.h" |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <unistd.h> |
8 | #include <errno.h> |
9 | #include <string.h> |
10 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
12 | #include <fcntl.h> |
13 | #include <sys/mman.h> |
14 | #ifdef __GLIBC__ |
15 | # include <malloc.h> |
16 | #endif |
17 | |
18 | #define FILENAME "/tmp/roaring.bin" |
19 | |
20 | void die(const char *func) { |
21 | fprintf(stderr, "%s(%s): %s\n" , func, FILENAME, strerror(errno)); |
22 | exit(1); |
23 | } |
24 | |
25 | void save(const char *buf, size_t len) { |
26 | int fd = open(FILENAME, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
27 | if (fd == -1) { |
28 | die("open" ); |
29 | } |
30 | while (len > 0) { |
31 | ssize_t res = write(fd, buf, len); |
32 | if (res == -1) { |
33 | die("write" ); |
34 | } |
35 | buf += res; |
36 | len -= res; |
37 | } |
38 | close(fd); |
39 | } |
40 | |
41 | void populate(roaring_bitmap_t *r, int n, char **paths) { |
42 | for (int i = 0; i < n; i++) { |
43 | size_t count = 0; |
44 | uint32_t *values = read_integer_file(paths[i], &count); |
45 | if (count == 0) { |
46 | fprintf(stderr, "No integers found in %s\n" , paths[i]); |
47 | exit(1); |
48 | } |
49 | for (size_t j = 0; j < count; j++) { |
50 | roaring_bitmap_add(r, values[j]); |
51 | } |
52 | free(values); |
53 | } |
54 | } |
55 | |
56 | int main(int argc, char* argv[]) { |
57 | (void)&read_all_integer_files; // suppress unused warning |
58 | |
59 | if (argc < 2) { |
60 | printf("Usage: %s <comma_separated_integers_file> ...\n" , argv[0]); |
61 | return 1; |
62 | } |
63 | |
64 | { |
65 | roaring_bitmap_t *r = roaring_bitmap_create(); |
66 | populate(r, argc-1, argv+1); |
67 | printf("Cardinality: %" PRId64"\n" , roaring_bitmap_get_cardinality(r)); |
68 | size_t len = roaring_bitmap_frozen_size_in_bytes(r); |
69 | printf("Serialized size [bytes]: %zu\n" , len); |
70 | char *buf = malloc(len); |
71 | roaring_bitmap_frozen_serialize(r, buf); |
72 | save(buf, len); |
73 | free(buf); |
74 | free(r); |
75 | } |
76 | { |
77 | int fd = open(FILENAME, O_RDONLY); |
78 | if (fd == -1) { |
79 | die("open" ); |
80 | } |
81 | |
82 | struct stat st; |
83 | if (fstat(fd, &st) == -1) { |
84 | die("fstat" ); |
85 | } |
86 | |
87 | char *ptr = (char *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); |
88 | if (!ptr) { |
89 | die("mmap" ); |
90 | } |
91 | const roaring_bitmap_t *r = roaring_bitmap_frozen_view(ptr, st.st_size); |
92 | #ifdef __GLIBC__ |
93 | printf("Allocation size [bytes]: %zu\n" , malloc_usable_size((void*)r) ); |
94 | #endif |
95 | roaring_bitmap_free(r); |
96 | munmap(ptr, st.st_size); |
97 | close(fd); |
98 | } |
99 | |
100 | return 0; |
101 | } |
102 | |
103 | |