aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mock_spiller_ut.h
blob: 42846eab1f4e15e7dbb4ca917a65ec9a2029941e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include <unordered_map>

#include <library/cpp/threading/future/core/future.h>
#include <yql/essentials/minikql/computation/mkql_spiller.h>

namespace NKikimr::NMiniKQL {

//Dummy synchronous in-memory spiller
class TMockSpiller: public ISpiller{
public:
    TMockSpiller()
        : NextKey(0)
    {}

    NThreading::TFuture<TKey> Put(NYql::TChunkedBuffer&& blob) override {
        auto promise = NThreading::NewPromise<ISpiller::TKey>();

        auto key = NextKey;
        Storage[key] = std::move(blob);
        NextKey++;
        promise.SetValue(key);
        return promise.GetFuture();;
    }

    NThreading::TFuture<std::optional<NYql::TChunkedBuffer>> Get(TKey key) override {
        auto promise = NThreading::NewPromise<std::optional<NYql::TChunkedBuffer>>();
        if (auto it = Storage.find(key); it != Storage.end()) {
            promise.SetValue(it->second);
        } else {
            promise.SetValue(std::nullopt);
        }

        return promise.GetFuture();
    }

    NThreading::TFuture<std::optional<NYql::TChunkedBuffer>> Extract(TKey key) override {
        auto promise = NThreading::NewPromise<std::optional<NYql::TChunkedBuffer>>();
        if (auto it = Storage.find(key); it != Storage.end()) {
            promise.SetValue(std::move(it->second));
            Storage.erase(it);
        } else {
            promise.SetValue(std::nullopt);
        }

        return promise.GetFuture();
    }
    NThreading::TFuture<void> Delete(TKey key) override {
        auto promise = NThreading::NewPromise<void>();
        promise.SetValue();
        Storage.erase(key);
        return promise.GetFuture();
    }
private:
    ISpiller::TKey NextKey;
    std::unordered_map<ISpiller::TKey, NYql::TChunkedBuffer> Storage;
};
inline ISpiller::TPtr CreateMockSpiller() {
    return std::make_shared<TMockSpiller>();
}

} //namespace NKikimr::NMiniKQL