• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / crud / 21364862659

26 Jan 2026 04:13PM UTC coverage: 73.492% (-15.0%) from 88.463%
21364862659

push

github

web-flow
Merge f981517ee into a84e19f3e

4253 of 5787 relevant lines covered (73.49%)

55.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.0
/crud/common/sharding/storage_metadata_cache.lua
1
local stash = require('crud.common.stash')
19✔
2
local utils = require('crud.common.sharding.utils')
19✔
3

4
local storage_metadata_cache = {}
19✔
5

6
local FUNC = 1
19✔
7
local KEY = 2
19✔
8

9
local cache_data = {
19✔
10
    [FUNC] = nil,
19✔
11
    [KEY] = nil,
19✔
12
}
13

14
local ddl_space = {
19✔
15
    [FUNC] = '_ddl_sharding_func',
19✔
16
    [KEY] = '_ddl_sharding_key',
19✔
17
}
18

19
local trigger_stash = stash.get(stash.name.ddl_triggers)
19✔
20

21
local function update_sharding_func_hash(old, new)
22
    if new ~= nil then
3✔
23
        local space_name = new[utils.SPACE_NAME_FIELDNO]
3✔
24
        local sharding_func_def = utils.extract_sharding_func_def(new)
3✔
25
        cache_data[FUNC][space_name] = utils.compute_hash(sharding_func_def)
6✔
26
    else
27
        local space_name = old[utils.SPACE_NAME_FIELDNO]
×
28
        cache_data[FUNC][space_name] = nil
×
29
    end
30
end
31

32
local function update_sharding_key_hash(old, new)
33
    if new ~= nil then
10✔
34
        local space_name = new[utils.SPACE_NAME_FIELDNO]
10✔
35
        local sharding_key_def = new[utils.SPACE_SHARDING_KEY_FIELDNO]
10✔
36
        cache_data[KEY][space_name] = utils.compute_hash(sharding_key_def)
20✔
37
    else
38
        local space_name = old[utils.SPACE_NAME_FIELDNO]
×
39
        cache_data[KEY][space_name] = nil
×
40
    end
41
end
42

43
local update_hash = {
19✔
44
    [FUNC] = update_sharding_func_hash,
19✔
45
    [KEY] = update_sharding_key_hash,
19✔
46
}
47

48
local function init_cache(section)
49
    cache_data[section] = {}
21✔
50

51
    local space = box.space[ddl_space[section]]
21✔
52

53
    local update_hash_func = update_hash[section]
21✔
54

55
    -- Remove old trigger if there was some code reload.
56
    -- It is possible that ddl space was dropped and created again,
57
    -- so removing non-existing trigger will cause fail;
58
    -- thus we use pcall.
59
    pcall(space.on_replace, space, nil, trigger_stash[section])
21✔
60

61
    trigger_stash[section] = space:on_replace(
42✔
62
        function(old, new)
63
            return update_hash_func(old, new)
×
64
        end
65
    )
21✔
66

67
    for _, tuple in space:pairs() do
89✔
68
        local space_name = tuple[utils.SPACE_NAME_FIELDNO]
13✔
69
        -- If the cache record for a space is not nil, it means
70
        -- that it was already set to up-to-date value with trigger.
71
        -- It is more like an overcautiousness since the cycle
72
        -- isn't expected to yield, but let it be here.
73
        if cache_data[section][space_name] == nil then
13✔
74
            update_hash_func(nil, tuple)
13✔
75
        end
76
    end
77
end
78

79
local function get_sharding_hash(space_name, section)
80
    if box.space[ddl_space[section]] == nil then
185✔
81
        return nil
×
82
    end
83

84
    -- If one would drop and rebuild ddl spaces fom scratch manually,
85
    -- caching is likely to break.
86
    if cache_data[section] == nil then
185✔
87
        init_cache(section)
21✔
88
    end
89

90
    return cache_data[section][space_name]
185✔
91
end
92

93
function storage_metadata_cache.get_sharding_func_hash(space_name)
19✔
94
    return get_sharding_hash(space_name, FUNC)
91✔
95
end
96

97
function storage_metadata_cache.get_sharding_key_hash(space_name)
19✔
98
    return get_sharding_hash(space_name, KEY)
94✔
99
end
100

101
function storage_metadata_cache.drop_caches()
19✔
102
    cache_data = {}
29✔
103
end
104

105
return storage_metadata_cache
19✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc