aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/microbdb/utility.h
diff options
context:
space:
mode:
authorqrort <qrort@yandex-team.com>2022-11-30 23:47:12 +0300
committerqrort <qrort@yandex-team.com>2022-11-30 23:47:12 +0300
commit22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch)
treebffa27765faf54126ad44bcafa89fadecb7a73d7 /library/cpp/microbdb/utility.h
parent332b99e2173f0425444abb759eebcb2fafaa9209 (diff)
downloadydb-22f8ae0e3f5d68b92aecccdf96c1d841a0334311.tar.gz
validate canons without yatest_common
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 0000000000..5c86061bca
--- /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;
+}