aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/file.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/stream/file.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/stream/file.h')
-rw-r--r--util/stream/file.h108
1 files changed, 108 insertions, 0 deletions
diff --git a/util/stream/file.h b/util/stream/file.h
new file mode 100644
index 0000000000..c1cf4f591d
--- /dev/null
+++ b/util/stream/file.h
@@ -0,0 +1,108 @@
+#pragma once
+
+#include "fwd.h"
+#include "input.h"
+#include "output.h"
+#include "buffered.h"
+#include "mem.h"
+
+#include <util/system/file.h>
+#include <utility>
+
+/**
+ * @addtogroup Streams_Files
+ * @{
+ */
+
+/**
+ * Unbuffered file input stream.
+ *
+ * Note that the input is not buffered, which means that `ReadLine` calls will
+ * be _very_ slow.
+ */
+class TUnbufferedFileInput: public IInputStream {
+public:
+ TUnbufferedFileInput(const TFile& file);
+ TUnbufferedFileInput(const TString& path);
+
+private:
+ size_t DoRead(void* buf, size_t len) override;
+ size_t DoSkip(size_t len) override;
+
+private:
+ TFile File_;
+};
+
+/**
+ * Memory-mapped file input stream.
+ */
+class TMappedFileInput: public TMemoryInput {
+public:
+ TMappedFileInput(const TFile& file);
+ TMappedFileInput(const TString& path);
+ ~TMappedFileInput() override;
+
+private:
+ class TImpl;
+ THolder<TImpl> Impl_;
+};
+
+/**
+ * File output stream.
+ *
+ * Note that the output is unbuffered, thus writing in many small chunks is
+ * likely to be quite slow.
+ */
+class TUnbufferedFileOutput: public IOutputStream {
+public:
+ TUnbufferedFileOutput(const TString& path);
+ TUnbufferedFileOutput(const TFile& file);
+ ~TUnbufferedFileOutput() override;
+
+ TUnbufferedFileOutput(TUnbufferedFileOutput&&) noexcept = default;
+ TUnbufferedFileOutput& operator=(TUnbufferedFileOutput&&) noexcept = default;
+
+private:
+ void DoWrite(const void* buf, size_t len) override;
+ void DoFlush() override;
+
+private:
+ TFile File_;
+};
+
+/**
+ * Buffered file input stream.
+ *
+ * @see TBuffered
+ */
+class TFileInput: public TBuffered<TUnbufferedFileInput> {
+public:
+ template <class T>
+ inline TFileInput(T&& t, size_t buf = 1 << 13)
+ : TBuffered<TUnbufferedFileInput>(buf, std::forward<T>(t))
+ {
+ }
+
+ ~TFileInput() override = default;
+};
+
+/**
+ * Buffered file output stream.
+ *
+ * Currently deprecated, please use TFileOutput in new code.
+ *
+ * @deprecated
+ * @see TBuffered
+ */
+class TFixedBufferFileOutput: public TBuffered<TUnbufferedFileOutput> {
+public:
+ template <class T>
+ inline TFixedBufferFileOutput(T&& t, size_t buf = 1 << 13)
+ : TBuffered<TUnbufferedFileOutput>(buf, std::forward<T>(t))
+ {
+ }
+
+ ~TFixedBufferFileOutput() override = default;
+};
+
+/** @} */