Completed
Pull Request — master (#20)
by Teye
07:49 queued 01:45
created

ScanQuery::setOffset()   A

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
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Queries;
5
6
use Level23\Druid\Context\QueryContext;
7
use Level23\Druid\Types\OrderByDirection;
8
use Level23\Druid\Filters\FilterInterface;
9
use Level23\Druid\Types\ScanQueryResultFormat;
10
use Level23\Druid\Responses\ScanQueryResponse;
11
use Level23\Druid\Collections\IntervalCollection;
12
13
/**
14
 * Class ScanQuery
15
 *
16
 * @see     https://druid.apache.org/docs/latest/querying/scan-query.html
17
 * @package Level23\Druid\Queries
18
 */
19
class ScanQuery implements QueryInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $dataSource;
25
26
    /**
27
     * @var \Level23\Druid\Collections\IntervalCollection
28
     */
29
    protected $intervals;
30
31
    /**
32
     * @var string
33
     */
34
    protected $resultFormat = ScanQueryResultFormat::NORMAL_LIST;
35
36
    /**
37
     * How many rows buffered before return to client. Default is 20480
38
     *
39
     * @var int
40
     */
41
    protected $batchSize;
42
43
    /**
44
     * Return results consistent with the legacy "scan-query" contrib extension. Defaults to the value set by
45
     * druid.query.scan.legacy, which in turn defaults to false. See Legacy mode for details.
46
     *
47
     * @var bool
48
     */
49
    protected $legacy;
50
51
    /**
52
     * The ordering of returned rows based on timestamp. "ascending", "descending" are supported. When not supplied,
53
     * "none" is used. Currently, "ascending" and "descending" are only supported for queries where the __time column
54
     * is included in the columns field and the requirements outlined in the time ordering section are met.
55
     *
56
     * @var string
57
     */
58
    protected $order;
59
60
    /**
61
     * How many rows to return. If not specified, all rows will be returned.
62
     *
63
     * @var int
64
     */
65
    protected $limit;
66
67
    /**
68
     * Skip this many rows when returning results.
69
     *
70
     * @var int
71
     */
72
    protected $offset;
73
74
    /**
75
     * @var \Level23\Druid\Context\QueryContext|null
76
     */
77
    protected $context;
78
79
    /**
80
     * @var \Level23\Druid\Filters\FilterInterface|null
81
     */
82
    protected $filter;
83
84
    /**
85
     * A String array of dimensions and metrics to scan. If left empty, all dimensions and metrics are returned.
86
     *
87
     * @var array|string[]
88
     */
89
    protected $columns = [];
90
91 7
    public function __construct(string $dataSource, IntervalCollection $intervals)
92
    {
93 7
        $this->dataSource = $dataSource;
94 7
        $this->intervals  = $intervals;
95 7
    }
96
97
    /**
98
     * Return the query in array format so we can fire it to druid.
99
     *
100
     * @return array
101
     */
102 3
    public function toArray(): array
103
    {
104
        $result = [
105 3
            'queryType'    => 'scan',
106 3
            'dataSource'   => $this->dataSource,
107 3
            'intervals'    => $this->intervals->toArray(),
108 3
            'resultFormat' => $this->resultFormat,
109 3
            'columns'      => $this->columns,
110
        ];
111
112 3
        if ($this->filter) {
113 3
            $result['filter'] = $this->filter->toArray();
114
        }
115
116 3
        if ($this->batchSize !== null) {
117 2
            $result['batchSize'] = $this->batchSize;
118
        }
119
120 3
        if ($this->limit !== null) {
121 2
            $result['limit'] = $this->limit;
122
        }
123
124 3
        if ($this->offset !== null) {
125 2
            $result['offset'] = $this->offset;
126
        }
127
128 3
        if ($this->legacy !== null) {
129 3
            $result['legacy'] = $this->legacy;
130
        }
131
132 3
        if ($this->context) {
133 2
            $result['context'] = $this->context->toArray();
134
        }
135
136 3
        if ($this->order) {
137 2
            $result['order'] = $this->order;
138
        }
139
140 3
        return $result;
141
    }
142
143
    /**
144
     * Parse the response into something we can return to the user.
145
     *
146
     * @param array $response
147
     *
148
     * @return ScanQueryResponse
149
     */
150 2
    public function parseResponse(array $response): ScanQueryResponse
151
    {
152 2
        return new ScanQueryResponse($response);
153
    }
154
155
    /**
156
     * How the results are represented. Use one of the ScanQueryResultFormat constants
157
     *
158
     * @param string $resultFormat
159
     */
160 4
    public function setResultFormat(string $resultFormat): void
161
    {
162 4
        $this->resultFormat = ScanQueryResultFormat::validate($resultFormat);
163 3
    }
164
165
    /**
166
     * How many rows buffered before return to client. Default is 20480
167
     *
168
     * @param int $batchSize
169
     */
170 2
    public function setBatchSize(int $batchSize): void
171
    {
172 2
        $this->batchSize = $batchSize;
173 2
    }
174
175
    /**
176
     * How many rows to return. If not specified, all rows will be returned.
177
     *
178
     * @param int $limit
179
     */
180 2
    public function setLimit(int $limit): void
181
    {
182 2
        $this->limit = $limit;
183 2
    }
184
185
    /**
186
     * Skip this many rows when returning results. Skipped rows will still need to be generated internally and then
187
     * discarded, meaning that raising offsets to high values can cause queries to use additional resources.
188
     *
189
     * Together, "limit" and "offset" can be used to implement pagination. However, note that if the underlying
190
     * datasource is modified in between page fetches in ways that affect overall query results, then the
191
     * different pages will not necessarily align with each other.
192
     *
193
     * @param int $offset
194
     */
195 2
    public function setOffset(int $offset): void
196
    {
197 2
        $this->offset = $offset;
198 2
    }
199
200
    /**
201
     * @param bool $legacy
202
     */
203 3
    public function setLegacy(bool $legacy): void
204
    {
205 3
        $this->legacy = $legacy;
206 3
    }
207
208
    /**
209
     * @param \Level23\Druid\Context\QueryContext $context
210
     */
211 2
    public function setContext(QueryContext $context): void
212
    {
213 2
        $this->context = $context;
214 2
    }
215
216
    /**
217
     * @param \Level23\Druid\Filters\FilterInterface $filter
218
     */
219 3
    public function setFilter(FilterInterface $filter): void
220
    {
221 3
        $this->filter = $filter;
222 3
    }
223
224
    /**
225
     * The ordering of returned rows based on timestamp. "ascending", "descending", and "none" (default) are supported.
226
     * Currently, "ascending" and "descending" are only supported for queries where the __time column is included in
227
     * the columns field and the requirements outlined in the time ordering section are met.
228
     *
229
     * @param string $order
230
     */
231 2
    public function setOrder(string $order): void
232
    {
233 2
        $this->order = OrderByDirection::validate($order);
234 2
    }
235
236
    /**
237
     * A String array of dimensions and metrics to scan. If left empty, all dimensions and metrics are returned.
238
     *
239
     * @param array|string[] $columns
240
     */
241 2
    public function setColumns(array $columns): void
242
    {
243 2
        $this->columns = $columns;
244
    }
245
}