summaryrefslogtreecommitdiffstats
path: root/library/cpp/deprecated/datafile
diff options
context:
space:
mode:
authorvvvv <[email protected]>2023-07-31 18:21:04 +0300
committervvvv <[email protected]>2023-07-31 18:21:04 +0300
commitdec41c40e51aa407edef81a3c566a5a15780fc49 (patch)
tree4f197b596b32f35eca368121f0dff913419da9af /library/cpp/deprecated/datafile
parent3ca8b54c96e09eb2b65be7f09675623438d559c7 (diff)
YQL-16239 Move purecalc to public
Diffstat (limited to 'library/cpp/deprecated/datafile')
-rw-r--r--library/cpp/deprecated/datafile/CMakeLists.darwin-x86_64.txt19
-rw-r--r--library/cpp/deprecated/datafile/CMakeLists.linux-aarch64.txt20
-rw-r--r--library/cpp/deprecated/datafile/CMakeLists.linux-x86_64.txt20
-rw-r--r--library/cpp/deprecated/datafile/CMakeLists.txt17
-rw-r--r--library/cpp/deprecated/datafile/CMakeLists.windows-x86_64.txt19
-rw-r--r--library/cpp/deprecated/datafile/README.md3
-rw-r--r--library/cpp/deprecated/datafile/datafile.cpp42
-rw-r--r--library/cpp/deprecated/datafile/datafile.h88
-rw-r--r--library/cpp/deprecated/datafile/loadmode.cpp1
-rw-r--r--library/cpp/deprecated/datafile/loadmode.h20
-rw-r--r--library/cpp/deprecated/datafile/ya.make12
11 files changed, 261 insertions, 0 deletions
diff --git a/library/cpp/deprecated/datafile/CMakeLists.darwin-x86_64.txt b/library/cpp/deprecated/datafile/CMakeLists.darwin-x86_64.txt
new file mode 100644
index 00000000000..3f88f788da7
--- /dev/null
+++ b/library/cpp/deprecated/datafile/CMakeLists.darwin-x86_64.txt
@@ -0,0 +1,19 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+
+add_library(cpp-deprecated-datafile)
+target_link_libraries(cpp-deprecated-datafile PUBLIC
+ contrib-libs-cxxsupp
+ yutil
+ cpp-deprecated-mapped_file
+)
+target_sources(cpp-deprecated-datafile PRIVATE
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/datafile.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/loadmode.cpp
+)
diff --git a/library/cpp/deprecated/datafile/CMakeLists.linux-aarch64.txt b/library/cpp/deprecated/datafile/CMakeLists.linux-aarch64.txt
new file mode 100644
index 00000000000..43da9ae45a1
--- /dev/null
+++ b/library/cpp/deprecated/datafile/CMakeLists.linux-aarch64.txt
@@ -0,0 +1,20 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+
+add_library(cpp-deprecated-datafile)
+target_link_libraries(cpp-deprecated-datafile PUBLIC
+ contrib-libs-linux-headers
+ contrib-libs-cxxsupp
+ yutil
+ cpp-deprecated-mapped_file
+)
+target_sources(cpp-deprecated-datafile PRIVATE
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/datafile.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/loadmode.cpp
+)
diff --git a/library/cpp/deprecated/datafile/CMakeLists.linux-x86_64.txt b/library/cpp/deprecated/datafile/CMakeLists.linux-x86_64.txt
new file mode 100644
index 00000000000..43da9ae45a1
--- /dev/null
+++ b/library/cpp/deprecated/datafile/CMakeLists.linux-x86_64.txt
@@ -0,0 +1,20 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+
+add_library(cpp-deprecated-datafile)
+target_link_libraries(cpp-deprecated-datafile PUBLIC
+ contrib-libs-linux-headers
+ contrib-libs-cxxsupp
+ yutil
+ cpp-deprecated-mapped_file
+)
+target_sources(cpp-deprecated-datafile PRIVATE
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/datafile.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/loadmode.cpp
+)
diff --git a/library/cpp/deprecated/datafile/CMakeLists.txt b/library/cpp/deprecated/datafile/CMakeLists.txt
new file mode 100644
index 00000000000..f8b31df0c11
--- /dev/null
+++ b/library/cpp/deprecated/datafile/CMakeLists.txt
@@ -0,0 +1,17 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+if (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" AND NOT HAVE_CUDA)
+ include(CMakeLists.linux-aarch64.txt)
+elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
+ include(CMakeLists.darwin-x86_64.txt)
+elseif (WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" AND NOT HAVE_CUDA)
+ include(CMakeLists.windows-x86_64.txt)
+elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND NOT HAVE_CUDA)
+ include(CMakeLists.linux-x86_64.txt)
+endif()
diff --git a/library/cpp/deprecated/datafile/CMakeLists.windows-x86_64.txt b/library/cpp/deprecated/datafile/CMakeLists.windows-x86_64.txt
new file mode 100644
index 00000000000..3f88f788da7
--- /dev/null
+++ b/library/cpp/deprecated/datafile/CMakeLists.windows-x86_64.txt
@@ -0,0 +1,19 @@
+
+# This file was generated by the build system used internally in the Yandex monorepo.
+# Only simple modifications are allowed (adding source-files to targets, adding simple properties
+# like target_include_directories). These modifications will be ported to original
+# ya.make files by maintainers. Any complex modifications which can't be ported back to the
+# original buildsystem will not be accepted.
+
+
+
+add_library(cpp-deprecated-datafile)
+target_link_libraries(cpp-deprecated-datafile PUBLIC
+ contrib-libs-cxxsupp
+ yutil
+ cpp-deprecated-mapped_file
+)
+target_sources(cpp-deprecated-datafile PRIVATE
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/datafile.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/deprecated/datafile/loadmode.cpp
+)
diff --git a/library/cpp/deprecated/datafile/README.md b/library/cpp/deprecated/datafile/README.md
new file mode 100644
index 00000000000..7f8547108e8
--- /dev/null
+++ b/library/cpp/deprecated/datafile/README.md
@@ -0,0 +1,3 @@
+A wrapper on top of some user-defined custom file format.
+
+Just write your own if you need it. It's going to be way easier than figuring out how to use this one.
diff --git a/library/cpp/deprecated/datafile/datafile.cpp b/library/cpp/deprecated/datafile/datafile.cpp
new file mode 100644
index 00000000000..ff93f11c6b7
--- /dev/null
+++ b/library/cpp/deprecated/datafile/datafile.cpp
@@ -0,0 +1,42 @@
+#include "datafile.h"
+
+void TDataFileBase::DoLoad(const char* fname, int loadMode) {
+ Destroy();
+ TFile f(fname, RdOnly);
+ DoLoad(f, loadMode, nullptr, 0);
+}
+
+void TDataFileBase::DoLoad(TFile& f, int loadMode, void* hdrPtr, size_t hdrSize) {
+ if (hdrPtr) {
+ if (loadMode & DLM_EXACT_SIZE && f.GetLength() != (i64)Length)
+ throw yexception() << f.GetName() << " size does not match its header value";
+ } else {
+ Length = f.GetLength();
+ hdrSize = 0;
+ }
+ if ((loadMode & DLM_LD_TYPE_MASK) == DLM_READ) {
+ MemData = TVector<char>(Length);
+ memcpy(MemData.begin(), hdrPtr, hdrSize);
+ f.Load(MemData.begin() + hdrSize, Length - hdrSize);
+ Start = MemData.begin();
+ } else {
+ FileData.init(f);
+ if (FileData.getSize() < Length)
+ throw yexception() << f.GetName() << " is smaller than what its header value says";
+ if ((loadMode & DLM_LD_TYPE_MASK) == DLM_MMAP_PRC)
+ FileData.precharge();
+ Start = (const char*)FileData.getData();
+ }
+}
+
+void TDataFileBase::Destroy() {
+ TVector<char>().swap(MemData);
+ FileData.term();
+ Start = nullptr;
+ Length = 0;
+}
+
+void TDataFileBase::Precharge() const {
+ if (Length && Start == (char*)FileData.getData())
+ FileData.precharge();
+}
diff --git a/library/cpp/deprecated/datafile/datafile.h b/library/cpp/deprecated/datafile/datafile.h
new file mode 100644
index 00000000000..a438baceca3
--- /dev/null
+++ b/library/cpp/deprecated/datafile/datafile.h
@@ -0,0 +1,88 @@
+#pragma once
+
+#include "loadmode.h"
+
+#include <library/cpp/deprecated/mapped_file/mapped_file.h>
+
+#include <util/generic/vector.h>
+#include <util/system/file.h>
+#include <util/system/filemap.h>
+
+/** Simple helper that allows a file to be either mapped or read into malloc'ed memory.
+ This behaviour is controlled by EDataLoadMode enum defined in loadmode.h.
+ Unlike TBlob it provides Precharge() function and simple file size - based integrity check.
+
+ To use this code, inherit your class from TDataFile<TFileHeader>.
+ TFileHeader must be a pod-type structure with byte layout of the file header.
+ File must start with that header.
+ TFileHeader must have FileSize() member function that determines expected file size or
+ length of data that need to be read from the beginning of file.
+ */
+
+class TDataFileBase {
+protected:
+ TVector<char> MemData;
+ TMappedFile FileData;
+
+ const char* Start;
+ size_t Length;
+
+ TDataFileBase()
+ : Start(nullptr)
+ , Length(0)
+ {
+ }
+
+ void DoLoad(TFile& f, int loadMode, void* hdrPtr, size_t hdrSize);
+ void DoLoad(const char* fname, int loadMode); // just whole file
+ void Destroy();
+ void swap(TDataFileBase& with) {
+ MemData.swap(with.MemData);
+ FileData.swap(with.FileData);
+ DoSwap(Start, with.Start);
+ DoSwap(Length, with.Length);
+ }
+
+public:
+ void Precharge() const;
+};
+
+template <class TFileHeader>
+class TDataFile: public TDataFileBase {
+protected:
+ void Load(const char* fname, EDataLoadMode loadMode) {
+ Destroy();
+ TFile f(fname, RdOnly | Seq);
+ TFileHeader hdr;
+ f.Load(&hdr, sizeof(hdr));
+ Length = hdr.FileSize();
+ DoLoad(f, (int)loadMode, &hdr, sizeof(hdr));
+ }
+ const TFileHeader& Hdr() const {
+ return *(TFileHeader*)Start;
+ }
+};
+
+// Use: class TFoo: public TDataFileEx<Foo> {...};
+// Additional requrement: TFileHeader must have Validate(fname) function that throws exception.
+// Class TUser itself must have Init(fname) function
+// Adds Load() function to your class (TUser)
+template <class TUser, class TFileHeader>
+class TDataFileEx: public TDataFile<TFileHeader> {
+private:
+ using TBase = TDataFile<TFileHeader>;
+ TUser& User() const {
+ return *(TUser*)this;
+ }
+
+public:
+ TDataFileEx(const char* fname, EDataLoadMode loadMode = DLM_DEFAULT) {
+ if (fname)
+ Load(fname, loadMode);
+ }
+ void Load(const char* fname, EDataLoadMode loadMode = DLM_DEFAULT) {
+ TBase::Load(fname, loadMode);
+ TBase::Hdr().Validate(fname);
+ User().Init(fname);
+ }
+};
diff --git a/library/cpp/deprecated/datafile/loadmode.cpp b/library/cpp/deprecated/datafile/loadmode.cpp
new file mode 100644
index 00000000000..a857830326e
--- /dev/null
+++ b/library/cpp/deprecated/datafile/loadmode.cpp
@@ -0,0 +1 @@
+#include "loadmode.h"
diff --git a/library/cpp/deprecated/datafile/loadmode.h b/library/cpp/deprecated/datafile/loadmode.h
new file mode 100644
index 00000000000..f04054dd645
--- /dev/null
+++ b/library/cpp/deprecated/datafile/loadmode.h
@@ -0,0 +1,20 @@
+#pragma once
+
+// It is recommended to support all reasonal value combinations via this enum,
+// to let Load() function argument be of EDataLoadMode type, not just int type
+
+enum EDataLoadMode {
+ DLM_READ = 0,
+ DLM_MMAP_PRC = 1, // precharge
+ DLM_MMAP = 2, // w/o precharge
+ DLM_MMAP_AUTO_PRC = 3, // precharge automatically (same as DLM_MMAP unless specifically supported)
+ DLM_LD_TYPE_MASK = 15,
+ DLM_EXACT_SIZE = 16, // fail if input file is larger than what header says
+
+ DLM_READ_ESZ = DLM_READ | DLM_EXACT_SIZE,
+ DLM_MMAP_PRC_ESZ = DLM_MMAP_PRC | DLM_EXACT_SIZE,
+ DLM_MMAP_ESZ = DLM_MMAP | DLM_EXACT_SIZE,
+ DLM_MMAP_APRC_ESZ = DLM_MMAP_AUTO_PRC | DLM_EXACT_SIZE,
+
+ DLM_DEFAULT = DLM_MMAP_PRC_ESZ,
+};
diff --git a/library/cpp/deprecated/datafile/ya.make b/library/cpp/deprecated/datafile/ya.make
new file mode 100644
index 00000000000..1ad4fe9bc74
--- /dev/null
+++ b/library/cpp/deprecated/datafile/ya.make
@@ -0,0 +1,12 @@
+LIBRARY()
+
+SRCS(
+ datafile.cpp
+ loadmode.cpp
+)
+
+PEERDIR(
+ library/cpp/deprecated/mapped_file
+)
+
+END()