aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/microbdb/utility.h
diff options
context:
space:
mode:
authorvvvv <vvvv@ydb.tech>2023-07-31 18:21:04 +0300
committervvvv <vvvv@ydb.tech>2023-07-31 18:21:04 +0300
commitdec41c40e51aa407edef81a3c566a5a15780fc49 (patch)
tree4f197b596b32f35eca368121f0dff913419da9af /library/cpp/microbdb/utility.h
parent3ca8b54c96e09eb2b65be7f09675623438d559c7 (diff)
downloadydb-dec41c40e51aa407edef81a3c566a5a15780fc49.tar.gz
YQL-16239 Move purecalc to public
Diffstat (limited to 'library/cpp/microbdb/utility.h')
-rw-r--r--library/cpp/microbdb/utility.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/library/cpp/microbdb/utility.h b/library/cpp/microbdb/utility.h
new file mode 100644
index 00000000000..5c86061bca0
--- /dev/null
+++ b/library/cpp/microbdb/utility.h
@@ -0,0 +1,75 @@
+#pragma once
+
+#include "microbdb.h"
+
+template <class TRecord, template <class T> class TCompare>
+int SortData(const TFile& ifile, const TFile& ofile, const TDatMetaPage* meta, size_t memory, const char* tmpDir = nullptr) {
+ char templ[FILENAME_MAX];
+ TInDatFileImpl<TRecord> datin;
+ TOutDatFileImpl<TRecord> datout;
+ TDatSorterImpl<TRecord, TCompare<TRecord>, TFakeCompression, TFakeSieve<TRecord>> sorter;
+ const TRecord* u;
+ int ret;
+
+ const size_t minMemory = (2u << 20);
+ memory = Max(memory, minMemory + minMemory / 2);
+ if (datin.Open(ifile, meta, memory - minMemory, 0))
+ err(1, "can't read input file");
+
+ size_t outpages = Max((size_t)2u, minMemory / datin.GetPageSize());
+ memory -= outpages * datin.GetPageSize();
+
+ if (ret = MakeSorterTempl(templ, tmpDir))
+ err(1, "can't create tempdir in \"%s\"; error: %d\n", templ, ret);
+
+ if (sorter.Open(templ, datin.GetPageSize(), outpages)) {
+ *strrchr(templ, LOCSLASH_C) = 0;
+ RemoveDirWithContents(templ);
+ err(1, "can't open sorter");
+ }
+
+ while (1) {
+ datin.Freeze();
+ while ((u = datin.Next()))
+ sorter.PushWithExtInfo(u);
+ sorter.NextPortion();
+ if (datin.GetError() || datin.IsEof())
+ break;
+ }
+
+ if (datin.GetError()) {
+ *strrchr(templ, LOCSLASH_C) = 0;
+ RemoveDirWithContents(templ);
+ err(1, "in data file error %d", datin.GetError());
+ }
+ if (datin.Close()) {
+ *strrchr(templ, LOCSLASH_C) = 0;
+ RemoveDirWithContents(templ);
+ err(1, "can't close in data file");
+ }
+
+ sorter.Sort(memory);
+
+ if (datout.Open(ofile, datin.GetPageSize(), outpages)) {
+ *strrchr(templ, LOCSLASH_C) = 0;
+ RemoveDirWithContents(templ);
+ err(1, "can't write out file");
+ }
+
+ while ((u = sorter.Next()))
+ datout.PushWithExtInfo(u);
+
+ if (sorter.GetError())
+ err(1, "sorter error %d", sorter.GetError());
+ if (sorter.Close())
+ err(1, "can't close sorter");
+
+ *strrchr(templ, LOCSLASH_C) = 0;
+ RemoveDirWithContents(templ);
+
+ if (datout.GetError())
+ err(1, "out data file error %d", datout.GetError());
+ if (datout.Close())
+ err(1, "can't close out data file");
+ return 0;
+}