1 | #include <string> |
2 | |
3 | #include <iostream> |
4 | |
5 | #include <Core/Types.h> |
6 | #include <IO/WriteHelpers.h> |
7 | #include <IO/ReadHelpers.h> |
8 | #include <IO/WriteBufferFromFile.h> |
9 | #include <IO/ReadBufferFromFile.h> |
10 | |
11 | int main(int, char **) |
12 | { |
13 | using namespace DB; |
14 | |
15 | try |
16 | { |
17 | const size_t N = 100000; |
18 | const size_t BUF_SIZE = 1048576; |
19 | |
20 | ReadBufferFromFile rand_in("/dev/urandom" ); |
21 | unsigned rand = 0; |
22 | readBinary(rand, rand_in); |
23 | |
24 | String test = "Hello, world! " + toString(rand); |
25 | |
26 | /// Write to file as usual, read with O_DIRECT. |
27 | |
28 | { |
29 | WriteBufferFromFile wb("test1" , BUF_SIZE); |
30 | for (size_t i = 0; i < N; ++i) |
31 | writeStringBinary(test, wb); |
32 | wb.next(); |
33 | } |
34 | |
35 | { |
36 | ReadBufferFromFile rb("test1" , BUF_SIZE, O_RDONLY | O_DIRECT, nullptr, 4096); |
37 | String res; |
38 | for (size_t i = 0; i < N; ++i) |
39 | readStringBinary(res, rb); |
40 | |
41 | std::cerr << "test: " << test << ", res: " << res << ", bytes: " << rb.count() << std::endl; |
42 | } |
43 | |
44 | /// Write to file with O_DIRECT, read as usual. |
45 | |
46 | { |
47 | WriteBufferFromFile wb("test2" , BUF_SIZE, O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT, 0666, nullptr, 4096); |
48 | |
49 | for (size_t i = 0; i < N; ++i) |
50 | writeStringBinary(test, wb); |
51 | |
52 | if (wb.offset() % 4096 != 0) |
53 | { |
54 | size_t pad = 4096 - wb.offset() % 4096; |
55 | memset(wb.position(), 0, pad); |
56 | wb.position() += pad; |
57 | } |
58 | |
59 | wb.next(); |
60 | } |
61 | |
62 | { |
63 | ReadBufferFromFile rb("test2" , BUF_SIZE); |
64 | String res; |
65 | for (size_t i = 0; i < N; ++i) |
66 | readStringBinary(res, rb); |
67 | |
68 | std::cerr << "test: " << test << ", res: " << res << ", bytes: " << rb.count() << std::endl; |
69 | } |
70 | } |
71 | catch (const Exception & e) |
72 | { |
73 | std::cerr << e.what() << ", " << e.displayText() << std::endl; |
74 | return 1; |
75 | } |
76 | |
77 | return 0; |
78 | } |
79 | |