2014-01-04 04:52:22 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
|
|
|
#include <base/getPageSize.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/ReadBufferFromFile.h>
|
2014-01-04 04:52:22 +00:00
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
int main(int, char **)
|
2014-01-04 04:52:22 +00:00
|
|
|
{
|
|
|
|
using namespace DB;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
try
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
static const size_t N = 100000;
|
|
|
|
static const size_t BUF_SIZE = 1048576;
|
2020-10-30 21:24:16 +00:00
|
|
|
size_t page_size = static_cast<size_t>(::getPageSize());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
ReadBufferFromFile rand_in("/dev/urandom");
|
|
|
|
unsigned rand = 0;
|
|
|
|
readBinary(rand, rand_in);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
String test = "Hello, world! " + toString(rand);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-26 01:28:07 +00:00
|
|
|
/// Write to file as usual, read with O_DIRECT.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-04-02 07:14:40 +00:00
|
|
|
{
|
|
|
|
WriteBufferFromFile wb("test1", BUF_SIZE);
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
writeStringBinary(test, wb);
|
|
|
|
wb.next();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
{
|
2020-10-30 21:24:16 +00:00
|
|
|
ReadBufferFromFile rb("test1", BUF_SIZE, O_RDONLY | O_DIRECT, nullptr, page_size);
|
2014-04-02 07:14:40 +00:00
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
readStringBinary(res, rb);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-04-02 07:14:40 +00:00
|
|
|
std::cerr << "test: " << test << ", res: " << res << ", bytes: " << rb.count() << std::endl;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-03-26 01:28:07 +00:00
|
|
|
/// Write to file with O_DIRECT, read as usual.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-04-02 07:14:40 +00:00
|
|
|
{
|
2023-03-30 17:02:28 +00:00
|
|
|
WriteBufferFromFile wb("test2", BUF_SIZE, O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT, /* throttler= */ {}, 0666, nullptr, page_size);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-04-02 07:14:40 +00:00
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
writeStringBinary(test, wb);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-30 21:24:16 +00:00
|
|
|
if (wb.offset() % page_size != 0)
|
2014-04-02 07:14:40 +00:00
|
|
|
{
|
2020-10-30 21:24:16 +00:00
|
|
|
size_t pad = page_size - wb.offset() % page_size;
|
2014-04-02 07:14:40 +00:00
|
|
|
memset(wb.position(), 0, pad);
|
|
|
|
wb.position() += pad;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
wb.next();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
{
|
2014-04-02 07:14:40 +00:00
|
|
|
ReadBufferFromFile rb("test2", BUF_SIZE);
|
2014-01-04 04:52:22 +00:00
|
|
|
String res;
|
2014-04-02 07:14:40 +00:00
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
readStringBinary(res, rb);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-04-02 07:14:40 +00:00
|
|
|
std::cerr << "test: " << test << ", res: " << res << ", bytes: " << rb.count() << std::endl;
|
2014-01-04 04:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << ", " << e.displayText() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-01-04 04:52:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|