Passed
Push — master ( 16ac55...a1819b )
by Teye
05:04
created

JdbcLookup::toArray()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 48
ccs 31
cts 31
cp 1
rs 6.5222
cc 9
nc 256
nop 0
crap 9
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Lookups;
5
6
/**
7
 * @see https://druid.apache.org/docs/latest/querying/lookups-cached-global#jdbc-lookup
8
 * @internal
9
 */
10
class JdbcLookup implements LookupInterface
11
{
12
    /**
13
     * The JDBC lookups will poll a database to populate its local cache. If the tsColumn is set it must be able to
14
     * accept comparisons in the format '2015-01-01 00:00:00'. For example, the following must be valid SQL for the
15
     * table SELECT * FROM some_lookup_table WHERE timestamp_column > '2015-01-01 00:00:00'. If tsColumn is set, the
16
     * caching service will attempt to only poll values that were written after the last sync. If tsColumn is not set,
17
     * the entire table is pulled every time.
18
     *
19
     * @see https://druid.apache.org/docs/latest/configuration/#jdbc-connections-to-external-databases
20
     *
21
     * @param string          $connectUri          The JDBC connect Uri. You can selectively allow JDBC properties in
22
     *                                             connectURI. See JDBC connections security config for more details.
23
     * @param string|null     $username
24
     * @param string|null     $password
25
     * @param string          $table               The table which contains the key value pairs
26
     * @param string          $keyColumn           The column in table which contains the keys
27
     * @param string          $valueColumn         The column in table which contains the values
28
     * @param string|null     $filter              The filter to use when selecting lookups, this is used to create a
29
     *                                             where clause on lookup population. For example "age >= 18"
30
     * @param string|null     $tsColumn            The column in table which contains when the key was updated
31
     * @param int|null        $jitterSeconds       How much jitter to add (in seconds) up to maximum as a delay (actual
32
     *                                             value will be used as random from 0 to jitterSeconds), used to
33
     *                                             distribute db load more evenly
34
     * @param int|null        $loadTimeoutSeconds  How much time (in seconds) it can take to query and populate lookup
35
     *                                             values. It will be helpful in lookup updates. On lookup update, it
36
     *                                             will wait maximum of loadTimeoutSeconds for new lookup to come up
37
     *                                             and continue serving from old lookup until new lookup successfully
38
     *                                             loads.
39
     * @param string|int|null $pollPeriod          The pollPeriod value specifies the period in ISO 8601 format between
40
     *                                             checks for replacement data for the lookup. For example PT15M. When
41
     *                                             not given, it is only once.
42
     * @param int|null        $maxHeapPercentage   The maximum percentage of heap size that the lookup should consume.
43
     *                                             If the lookup grows beyond this size, warning messages will be
44
     *                                             logged in the  respective service logs.
45
     * @param bool            $injective           If the underlying map is injective (keys and values are unique) then
46
     *                                             optimizations can occur internally by setting this to true
47
     * @param int             $firstCacheTimeoutMs How long to wait (in ms) for the first run of the cache to populate.
48
     *                                             0 indicates to not  wait
49
     */
50 2
    public function __construct(
51
        protected string $connectUri,
52
        protected string|null $username,
53
        protected string|null $password,
54
        protected string $table,
55
        protected string $keyColumn,
56
        protected string $valueColumn,
57
        protected ?string $filter = null,
58
        protected ?string $tsColumn = null,
59
        protected ?int $jitterSeconds = null,
60
        protected ?int $loadTimeoutSeconds = null,
61
        protected null|int|string $pollPeriod = null,
62
        protected ?int $maxHeapPercentage = null,
63
        protected bool $injective = false,
64
        protected int $firstCacheTimeoutMs = 0
65
    ) {
66
67 2
    }
68
69 2
    public function toArray(): array
70
    {
71 2
        $response = [
72 2
            'type'            => 'jdbc',
73 2
            'connectorConfig' => [
74 2
                'connectURI' => $this->connectUri,
75 2
            ],
76 2
            'table'           => $this->table,
77 2
            'keyColumn'       => $this->keyColumn,
78 2
            'valueColumn'     => $this->valueColumn,
79 2
        ];
80
81 2
        if ($this->username !== null) {
82 2
            $response['connectorConfig']['user'] = $this->username;
83
        }
84 2
        if ($this->password !== null) {
85 2
            $response['connectorConfig']['password'] = $this->password;
86
        }
87
88 2
        if ($this->filter !== null) {
89 2
            $response['filter'] = $this->filter;
90
        }
91
92 2
        if ($this->tsColumn) {
93 2
            $response['tsColumn'] = $this->tsColumn;
94
        }
95
96 2
        if ($this->pollPeriod !== null) {
97 2
            $response['pollPeriod'] = $this->pollPeriod;
98
        }
99
100 2
        if ($this->jitterSeconds !== null) {
101 1
            $response['jitterSeconds'] = $this->jitterSeconds;
102
        }
103
104 2
        if ($this->loadTimeoutSeconds !== null) {
105 1
            $response['loadTimeoutSeconds'] = $this->loadTimeoutSeconds;
106
        }
107
108 2
        if ($this->maxHeapPercentage !== null) {
109 2
            $response['maxHeapPercentage'] = $this->maxHeapPercentage;
110
        }
111
112 2
        return [
113 2
            'type'                => 'cachedNamespace',
114 2
            'extractionNamespace' => $response,
115 2
            'injective'           => $this->injective,
116 2
            'firstCacheTimeout'   => $this->firstCacheTimeoutMs,
117 2
        ];
118
    }
119
}