Test Failed
Push — master ( d80a54...b07f16 )
by Konrad
04:15
created

QueryHandler::getStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
5
 * the terms of the GPL-3 license.
6
 *
7
 * (c) Konrad Abicht <[email protected]>
8
 * (c) Benjamin Nowack
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace sweetrdf\InMemoryStoreSqlite\Store\QueryHandler;
15
16
use sweetrdf\InMemoryStoreSqlite\Log\Logger;
17
use sweetrdf\InMemoryStoreSqlite\NamespaceHelper;
18
use sweetrdf\InMemoryStoreSqlite\Store\InMemoryStoreSqlite;
19
20
abstract class QueryHandler
21
{
22
    protected Logger $logger;
23
24
    protected InMemoryStoreSqlite $store;
25
26
    protected array $term_id_cache;
27
28
    protected string $xsd = NamespaceHelper::NAMESPACE_XSD;
29
30 86
    public function __construct(InMemoryStoreSqlite $store, Logger $logger)
31
    {
32 86
        $this->logger = $logger;
33 86
        $this->store = $store;
34 86
    }
35
36 79
    public function getStore(): InMemoryStoreSqlite
37
    {
38
        return $this->store;
39 79
    }
40 79
41
    public function getTermID($val, $term = '')
42 79
    {
43 79
        /* mem cache */
44
        if (!isset($this->term_id_cache) || (\count(array_keys($this->term_id_cache)) > 100)) {
45
            $this->term_id_cache = [];
46 79
        }
47
        if (!isset($this->term_id_cache[$term])) {
48 79
            $this->term_id_cache[$term] = [];
49 51
        }
50
51
        $tbl = preg_match('/^(s|o)$/', $term) ? $term.'2val' : 'id2val';
52 79
        /* cached? */
53
        if ((\strlen($val) < 100) && isset($this->term_id_cache[$term][$val])) {
54 79
            return $this->term_id_cache[$term][$val];
55 23
        }
56 23
57 23
        $r = 0;
58
        /* via hash */
59 23
        if (preg_match('/^(s2val|o2val)$/', $tbl)) {
60 23
            $rows = $this->store->getDBObject()->fetchList(
61 23
                'SELECT id, val FROM '.$tbl.' WHERE val_hash = ? ORDER BY id',
62 23
                [$this->getValueHash($val)]
63 23
            );
64
            if (\is_array($rows) && 0 < \count($rows)) {
65
                foreach ($rows as $row) {
66
                    if ($row['val'] == $val) {
67
                        $r = $row['id'];
68
                        break;
69
                    }
70
                }
71 75
            }
72 75
        }
73
74 75
        /* exact match */
75 74
        else {
76
            $sql = 'SELECT id FROM '.$tbl.' WHERE val = ? LIMIT 1';
77
            $row = $this->store->getDBObject()->fetchRow($sql, [$val]);
78 79
79 78
            if (null !== $row && isset($row['id'])) {
80
                $r = $row['id'];
81
            }
82 79
        }
83
        if ($r && (\strlen($val) < 100)) {
84
            $this->term_id_cache[$term][$val] = $r;
85 85
        }
86
87 85
        return $r;
88
    }
89
90
    public function getValueHash(int | float | string $val): int | float
91
    {
92
        return abs(crc32($val));
93
    }
94
}
95