2014-01-04 04:52:22 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-09-15 09:55:57 +00:00
|
|
|
#include <common/types.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
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using namespace DB;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
static const size_t N = 100000;
|
|
|
|
static const size_t BUF_SIZE = 1048576;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ReadBufferFromFile rand_in("/dev/urandom");
|
|
|
|
unsigned rand = 0;
|
|
|
|
readBinary(rand, rand_in);
|
|
|
|
|
|
|
|
String test = "Hello, world! " + toString(rand);
|
|
|
|
|
|
|
|
/// Write to file as usual, read with O_DIRECT.
|
|
|
|
|
|
|
|
{
|
|
|
|
WriteBufferFromFile wb("test1", BUF_SIZE);
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
writeStringBinary(test, wb);
|
|
|
|
wb.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ReadBufferFromFile rb("test1", BUF_SIZE, O_RDONLY | O_DIRECT, nullptr, 4096);
|
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
readStringBinary(res, rb);
|
|
|
|
|
|
|
|
std::cerr << "test: " << test << ", res: " << res << ", bytes: " << rb.count() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write to file with O_DIRECT, read as usual.
|
|
|
|
|
|
|
|
{
|
|
|
|
WriteBufferFromFile wb("test2", BUF_SIZE, O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT, 0666, nullptr, 4096);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
writeStringBinary(test, wb);
|
|
|
|
|
|
|
|
if (wb.offset() % 4096 != 0)
|
|
|
|
{
|
|
|
|
size_t pad = 4096 - wb.offset() % 4096;
|
|
|
|
memset(wb.position(), 0, pad);
|
|
|
|
wb.position() += pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ReadBufferFromFile rb("test2", BUF_SIZE);
|
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
readStringBinary(res, rb);
|
|
|
|
|
|
|
|
std::cerr << "test: " << test << ", res: " << res << ", bytes: " << rb.count() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << ", " << e.displayText() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-01-04 04:52:22 +00:00
|
|
|
}
|