|
| 1 | +#include "key_value.h" |
| 2 | + |
| 3 | +#include <util/string/printf.h> |
| 4 | + |
| 5 | +using namespace NLastGetopt; |
| 6 | +using namespace NYdb; |
| 7 | +using namespace NYdb::NTable; |
| 8 | + |
| 9 | + |
| 10 | +TGenerateInitialContentJob::TGenerateInitialContentJob(const TCreateOptions& createOpts, std::uint32_t maxId) |
| 11 | + : TThreadJob(createOpts.CommonOptions) |
| 12 | + , Executor(createOpts.CommonOptions, Stats, TExecutor::ModeBlocking) |
| 13 | + , PackGenerator( |
| 14 | + createOpts.CommonOptions |
| 15 | + , createOpts.PackSize |
| 16 | + , [](const TKeyValueRecordData& recordData) { return BuildValueFromRecord(recordData); } |
| 17 | + , createOpts.Count |
| 18 | + , maxId |
| 19 | + ) |
| 20 | + , Total(createOpts.Count) |
| 21 | +{} |
| 22 | + |
| 23 | +void TGenerateInitialContentJob::ShowProgress(TStringBuilder& report) { |
| 24 | + report << Endl << "======- GenerateInitialContentJob report -======" << Endl; |
| 25 | + Executor.Report(report); |
| 26 | + TDuration timePassed = TInstant::Now() - Stats.GetStartTime(); |
| 27 | + std::uint64_t rps = (Total - PackGenerator.GetRemain()) * 1000000 / timePassed.MicroSeconds(); |
| 28 | + report << "Generated " << Total - PackGenerator.GetRemain() << " new elements." << Endl |
| 29 | + << "With pack_size=" << PackGenerator.GetPackSize() << " its " << rps << " rows/sec" << Endl |
| 30 | + << "Generator compute time: " << PackGenerator.GetComputeTime() << Endl; |
| 31 | + Stats.PrintStatistics(report); |
| 32 | + report << "========================================" << Endl; |
| 33 | +} |
| 34 | + |
| 35 | +void TGenerateInitialContentJob::DoJob() { |
| 36 | + std::vector<TValue> pack; |
| 37 | + while (!ShouldStop.load() && PackGenerator.GetNextPack(pack)) { |
| 38 | + auto upload = [pack{ std::move(pack) }, this](TSession session)->TAsyncStatus { |
| 39 | + static const TString query = Sprintf(R"( |
| 40 | +--!syntax_v1 |
| 41 | +PRAGMA TablePathPrefix("%s"); |
| 42 | +
|
| 43 | +DECLARE $items AS |
| 44 | + List<Struct< |
| 45 | + `object_id_key`: Uint32, |
| 46 | + `object_id`: Uint32, |
| 47 | + `timestamp`: Uint64, |
| 48 | + `payload`: Utf8>>; |
| 49 | +
|
| 50 | +UPSERT INTO `%s` SELECT * FROM AS_TABLE($items); |
| 51 | +
|
| 52 | +)", Prefix.c_str(), TableName.c_str()); |
| 53 | + auto promise = NThreading::NewPromise<TStatus>(); |
| 54 | + auto params = PackValuesToParamsAsList(pack); |
| 55 | + |
| 56 | + auto resultFuture = session.ExecuteDataQuery( |
| 57 | + query |
| 58 | + , TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx() |
| 59 | + , std::move(params) |
| 60 | + , TExecDataQuerySettings() |
| 61 | + .KeepInQueryCache(true) |
| 62 | + .OperationTimeout(MaxDelay + ReactionTimeDelay) |
| 63 | + .ClientTimeout(MaxDelay + ReactionTimeDelay) |
| 64 | + ); |
| 65 | + |
| 66 | + resultFuture.Subscribe([promise](TAsyncDataQueryResult queryFuture) mutable { |
| 67 | + Y_ABORT_UNLESS(queryFuture.HasValue()); |
| 68 | + TDataQueryResult queryResult = queryFuture.GetValue(); |
| 69 | + promise.SetValue(std::move(queryResult)); |
| 70 | + }); |
| 71 | + |
| 72 | + return promise.GetFuture(); |
| 73 | + }; |
| 74 | + |
| 75 | + if (!Executor.Execute(upload)) { |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void TGenerateInitialContentJob::OnFinish() { |
| 82 | + Executor.Finish(); |
| 83 | + Executor.Wait(); |
| 84 | + Stats.Flush(); |
| 85 | +} |
0 commit comments