aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/yql_holding_file_storage.h
blob: 4eef6099aaf04edae2b376843fac340c60ec5620 (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
#pragma once

#include <yql/essentials/core/yql_user_data.h>
#include <yql/essentials/core/file_storage/file_storage.h>
#include <util/generic/map.h>

namespace NYql {

class THoldingFileStorage {
private:
    struct TDataLess {
        bool operator()(const TUserDataBlock& b1, const TUserDataBlock& b2) const {
            return std::tie(b1.Type, b1.Data, b1.UrlToken) < std::tie(b2.Type, b2.Data, b2.UrlToken);
        }
    };

public:
    explicit THoldingFileStorage(TFileStoragePtr fileStorage);
    TFileStoragePtr GetRawStorage() const;
    bool CanFreeze() const;
    // it is safe to call this method from multiple threads
    // method will return the same link for single path even in multi-threaded env
    // if we have single url but with different tokens we can potentially download several different versions of url
    TFileLinkPtr FreezeFile(const TUserDataBlock& block);

    // downloaded file will be registered in cache after function invocation
    NThreading::TFuture<std::function<TFileLinkPtr()>> FreezeFileAsync(const TUserDataBlock& block);

private:
    TFileLinkPtr PutDataAndRegister(const TUserDataBlock& block);
    TFileLinkPtr PutData(const TUserDataBlock& block);
    TFileLinkPtr FindOrPutData(const TUserDataBlock& block);

    NThreading::TFuture<std::function<TFileLinkPtr()>> PutDataAndRegisterAsync(const TUserDataBlock& block);
    NThreading::TFuture<TFileLinkPtr> PutDataAsync(const TUserDataBlock& block);
    NThreading::TFuture<std::function<TFileLinkPtr()>> FindOrPutDataAsync(const TUserDataBlock& block);

    TFileLinkPtr RegisterLink(const TUserDataBlock& block, TFileLinkPtr link);

private:
    TFileStoragePtr FileStorage_;
    TMap<TUserDataBlock, TFileLinkPtr, TDataLess> Links_;
};

}