1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Service\Json; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Service\IndexService; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Reads records one by one. |
19
|
|
|
* |
20
|
|
|
* Sample input: |
21
|
|
|
* <p> |
22
|
|
|
* [ |
23
|
|
|
* {"count":2}, |
24
|
|
|
* {"_id":"doc1","title":"Document 1"}, |
25
|
|
|
* {"_id":"doc2","title":"Document 2"} |
26
|
|
|
* ] |
27
|
|
|
* </p> |
28
|
|
|
*/ |
29
|
|
|
class JsonReader implements \Countable, \Iterator |
30
|
|
|
{ |
31
|
|
|
private $filename; |
32
|
|
|
private $handle; |
33
|
|
|
private $key = 0; |
34
|
|
|
private $currentLine; |
35
|
|
|
private $metadata; |
36
|
|
|
private $index; |
37
|
|
|
private $optionsResolver; |
38
|
|
|
private $options; |
39
|
|
|
|
40
|
|
|
public function __construct(IndexService $index, string $filename, array $options = []) |
41
|
|
|
{ |
42
|
|
|
$this->index = $index; |
43
|
|
|
$this->filename = $filename; |
44
|
|
|
$this->options = $options; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Destructor. Closes file handler if open. |
49
|
|
|
*/ |
50
|
|
|
public function __destruct() |
51
|
|
|
{ |
52
|
|
|
if ($this->handle !== null) { |
53
|
|
|
@fclose($this->handle); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getIndex(): IndexService |
58
|
|
|
{ |
59
|
|
|
return $this->index; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getFileHandler() |
63
|
|
|
{ |
64
|
|
|
//Make sure the gzip option is resolved from a filename. |
65
|
|
|
if ($this->handle === null) { |
66
|
|
|
$isGzip = array_key_exists('gzip', $this->options); |
67
|
|
|
|
68
|
|
|
$filename = !$isGzip? |
69
|
|
|
$this->filename: |
70
|
|
|
sprintf('compress.zlib://%s', $this->filename); |
71
|
|
|
$fileHandler = @fopen($filename, 'r'); |
72
|
|
|
|
73
|
|
|
if ($fileHandler === false) { |
74
|
|
|
throw new \LogicException('Can not open file.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->handle = $fileHandler; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->handle; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function readMetadata() |
84
|
|
|
{ |
85
|
|
|
if ($this->metadata !== null) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$line = fgets($this->getFileHandler()); |
90
|
|
|
|
91
|
|
|
if (trim($line) !== '[') { |
92
|
|
|
throw new \InvalidArgumentException('Given file does not match expected pattern.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$line = trim(fgets($this->getFileHandler())); |
96
|
|
|
$this->metadata = json_decode(rtrim($line, ','), true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function readLine() |
100
|
|
|
{ |
101
|
|
|
$buffer = ''; |
102
|
|
|
|
103
|
|
|
while ($buffer === '') { |
104
|
|
|
$buffer = fgets($this->getFileHandler()); |
105
|
|
|
|
106
|
|
|
if ($buffer === false) { |
107
|
|
|
$this->currentLine = null; |
108
|
|
|
|
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$buffer = trim($buffer); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($buffer === ']') { |
116
|
|
|
$this->currentLine = null; |
117
|
|
|
|
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$data = json_decode(rtrim($buffer, ','), true); |
122
|
|
|
$this->currentLine = $this->getOptionsResolver()->resolve($data); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function configureResolver(OptionsResolver $resolver) |
126
|
|
|
{ |
127
|
|
|
$resolver |
128
|
|
|
->setRequired(['_id', '_source']) |
129
|
|
|
->setDefaults(['_score' => null, 'fields' => []]) |
130
|
|
|
->addAllowedTypes('_id', ['integer', 'string']) |
131
|
|
|
->addAllowedTypes('_source', 'array') |
132
|
|
|
->addAllowedTypes('fields', 'array'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function current() |
136
|
|
|
{ |
137
|
|
|
if ($this->currentLine === null) { |
138
|
|
|
$this->readLine(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->currentLine; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function next() |
145
|
|
|
{ |
146
|
|
|
$this->readLine(); |
147
|
|
|
|
148
|
|
|
$this->key++; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function key() |
152
|
|
|
{ |
153
|
|
|
return $this->key; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function valid() |
157
|
|
|
{ |
158
|
|
|
return !feof($this->getFileHandler()) && $this->currentLine; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function rewind() |
162
|
|
|
{ |
163
|
|
|
rewind($this->getFileHandler()); |
164
|
|
|
$this->metadata = null; |
165
|
|
|
$this->readMetadata(); |
166
|
|
|
$this->readLine(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function count() |
170
|
|
|
{ |
171
|
|
|
$metadata = $this->getMetadata(); |
|
|
|
|
172
|
|
|
|
173
|
|
|
if (!isset($metadata['count'])) { |
174
|
|
|
throw new \LogicException('Given file does not contain count of documents.'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $metadata['count']; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getMetadata() |
181
|
|
|
{ |
182
|
|
|
$this->readMetadata(); |
183
|
|
|
|
184
|
|
|
return $this->metadata; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function getOptionsResolver(): OptionsResolver |
188
|
|
|
{ |
189
|
|
|
if (!$this->optionsResolver) { |
190
|
|
|
$this->optionsResolver = new OptionsResolver(); |
191
|
|
|
$this->configureResolver($this->optionsResolver); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this->optionsResolver; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: