Select   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 91
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 4 1
B getBody() 0 83 1
1
<?php
2
3
namespace Basis\Procedure;
4
5
use Tarantool\Mapper\Procedure;
6
7
class Select extends Procedure
8
{
9 4
    public function getParams() : array
10
    {
11 4
        return ['space', 'index', 'values'];
12
    }
13
14 4
    public function getBody(): string
15
    {
16
        return <<<LUA
17 4
        if box.space[space] == nil then
18
            return nil
19
        end
20
        if box.space[space].index[index] == nil then
21
            return nil
22
        end
23
24
        if #values == 0 then
25
            return {}
26
        end
27
28
        local pk = {}
29
        for i, t in pairs(box.space[space].index[0].parts) do
30
            table.insert(pk, t.fieldno)
31
        end
32
33
        local result = {}
34
        local keys = {}
35
36
        local parentField = nil
37
38
        for i, f in pairs(box.space[space]:format()) do
39
            if f.name == 'parent' and f.reference == box.space[space].name then
40
                parentField = i
41
            end
42
        end
43
44
        local childIndex = nil
45
46
        if parentField ~= nil then
47
            for i, candidate in pairs(box.space[space].index) do
48
                if #candidate.parts == 1 then
49
                    if candidate.parts[1].fieldno == parentField then
50
                        childIndex = candidate.name
51
                    end
52
                end
53
            end
54
        end
55
56
        local function getKey(tuple)
57
            return tuple[pk[1]]
58
        end
59
        
60
        if #pk > 1 then 
61
            getKey = function(tuple)
62
                local key = ""
63
                for i, f in pairs(pk) do
64
                    key = key .. tuple[f] .. '-'
65
                end
66
                return key
67
            end
68
        end
69
70
        local function selector(index, value)
71
            for j, tuple in box.space[space].index[index]:pairs(value) do
72
                local key = getKey(tuple)
73
                if keys[key] == nil then
74
                    keys[key] = true
75
                    table.insert(result, tuple)
76
                    if childIndex ~= nil then
77
                        selector(childIndex, tuple[1])
78
                    end
79
                end
80
            end
81
        end
82
83
        if fiber == nil then 
84
            fiber = require('fiber')
85
        end
86
87
        for i, value in pairs(values) do
88
            selector(index, value)
89
            if i % 10000 == 0 then
90
                fiber.yield()
91
            end
92
        end
93
94
        return result
95
LUA;
96
    }
97
}
98