mock_spiller_ut.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include <unordered_map>
  3. #include <library/cpp/threading/future/core/future.h>
  4. #include <yql/essentials/minikql/computation/mkql_spiller.h>
  5. namespace NKikimr::NMiniKQL {
  6. //Dummy synchronous in-memory spiller
  7. class TMockSpiller: public ISpiller{
  8. public:
  9. TMockSpiller()
  10. : NextKey_(0)
  11. {}
  12. NThreading::TFuture<TKey> Put(NYql::TChunkedBuffer&& blob) override {
  13. auto promise = NThreading::NewPromise<ISpiller::TKey>();
  14. auto key = NextKey_;
  15. Storage_[key] = std::move(blob);
  16. PutSizes_.push_back(Storage_[key].Size());
  17. NextKey_++;
  18. promise.SetValue(key);
  19. return promise.GetFuture();;
  20. }
  21. NThreading::TFuture<std::optional<NYql::TChunkedBuffer>> Get(TKey key) override {
  22. auto promise = NThreading::NewPromise<std::optional<NYql::TChunkedBuffer>>();
  23. if (auto it = Storage_.find(key); it != Storage_.end()) {
  24. promise.SetValue(it->second);
  25. } else {
  26. promise.SetValue(std::nullopt);
  27. }
  28. return promise.GetFuture();
  29. }
  30. NThreading::TFuture<std::optional<NYql::TChunkedBuffer>> Extract(TKey key) override {
  31. auto promise = NThreading::NewPromise<std::optional<NYql::TChunkedBuffer>>();
  32. if (auto it = Storage_.find(key); it != Storage_.end()) {
  33. promise.SetValue(std::move(it->second));
  34. Storage_.erase(it);
  35. } else {
  36. promise.SetValue(std::nullopt);
  37. }
  38. return promise.GetFuture();
  39. }
  40. NThreading::TFuture<void> Delete(TKey key) override {
  41. auto promise = NThreading::NewPromise<void>();
  42. promise.SetValue();
  43. Storage_.erase(key);
  44. return promise.GetFuture();
  45. }
  46. const std::vector<size_t>& GetPutSizes() const {
  47. return PutSizes_;
  48. }
  49. private:
  50. ISpiller::TKey NextKey_;
  51. std::unordered_map<ISpiller::TKey, NYql::TChunkedBuffer> Storage_;
  52. std::vector<size_t> PutSizes_;
  53. };
  54. inline ISpiller::TPtr CreateMockSpiller() {
  55. return std::make_shared<TMockSpiller>();
  56. }
  57. } //namespace NKikimr::NMiniKQL