QueryHandler::getStore()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
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 2
cts 2
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-2 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 100
    public function __construct(InMemoryStoreSqlite $store, Logger $logger)
31
    {
32 100
        $this->logger = $logger;
33 100
        $this->store = $store;
34
    }
35
36 1
    public function getStore(): InMemoryStoreSqlite
37
    {
38 1
        return $this->store;
39
    }
40
41 77
    public function getTermID($val, $term = '')
42
    {
43
        /* mem cache */
44 77
        if (!isset($this->term_id_cache) || (\count(array_keys($this->term_id_cache)) > 100)) {
45 77
            $this->term_id_cache = [];
46
        }
47 77
        if (!isset($this->term_id_cache[$term])) {
48 77
            $this->term_id_cache[$term] = [];
49
        }
50
51 77
        $tbl = preg_match('/^(s|o)$/', $term) ? $term.'2val' : 'id2val';
52
        /* cached? */
53 77
        if ((\strlen($val) < 100) && isset($this->term_id_cache[$term][$val])) {
54 47
            return $this->term_id_cache[$term][$val];
55
        }
56
57 77
        $r = 0;
58
        /* via hash */
59 77
        if (preg_match('/^(s2val|o2val)$/', $tbl)) {
60 16
            $rows = $this->store->getDBObject()->fetchList(
61 16
                'SELECT id, val FROM '.$tbl.' WHERE val_hash = ? ORDER BY id',
62 16
                [$this->getValueHash($val)]
63 16
            );
64 16
            if (\is_array($rows) && 0 < \count($rows)) {
65 16
                foreach ($rows as $row) {
66 16
                    if ($row['val'] == $val) {
67 16
                        $r = $row['id'];
68 16
                        break;
69
                    }
70
                }
71
            }
72
        } else {
73
            /* exact match */
74 73
            $sql = 'SELECT id FROM '.$tbl.' WHERE val = ? LIMIT 1';
75 73
            $row = $this->store->getDBObject()->fetchRow($sql, [$val]);
76
77 73
            if (null !== $row && isset($row['id'])) {
78 71
                $r = $row['id'];
79
            }
80
        }
81 77
        if ($r && (\strlen($val) < 100)) {
82 75
            $this->term_id_cache[$term][$val] = $r;
83
        }
84
85 77
        return $r;
86
    }
87
88 94
    public function getValueHash(int | float | string $val): int | float
89
    {
90 94
        return abs(crc32($val));
91
    }
92
}
93