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