aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/on_disk/st_hash/static_hash_map.h
blob: 5dc50abd392f6e8160f0022f836b31a3d7693c1a (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
#pragma once

#include "static_hash.h"

#include <library/cpp/deprecated/mapped_file/mapped_file.h>

#include <util/system/filemap.h>

template <class SH>
struct sthash_mapped_c {
    typedef SH H;
    typedef typename H::const_iterator const_iterator;
    TMappedFile M;
    H* hsh;
    sthash_mapped_c()
        : M()
        , hsh(nullptr)
    {
    }
    sthash_mapped_c(const char* fname, bool precharge)
        : M()
        , hsh(nullptr)
    {
        Open(fname, precharge);
    }
    void Open(const char* fname, bool precharge) {
        M.init(fname);
        if (precharge)
            M.precharge();
        hsh = (H*)M.getData();
        if (M.getSize() < sizeof(H) || (ssize_t)M.getSize() != hsh->end().Data - (char*)hsh)
            ythrow yexception() << "Could not map hash: " << fname << " is damaged";
    }
    H* operator->() {
        return hsh;
    }
    const H* operator->() const {
        return hsh;
    }
    H* GetSthash() {
        return hsh;
    }
    const H* GetSthash() const {
        return hsh;
    }
};

template <class Key, class T, class Hash>
struct sthash_mapped: public sthash_mapped_c<sthash<Key, T, Hash>> {
    typedef sthash<Key, T, Hash> H;
    sthash_mapped(const char* fname, bool precharge)
        : sthash_mapped_c<H>(fname, precharge)
    {
    }
    sthash_mapped()
        : sthash_mapped_c<H>()
    {
    }
};