1 | <?php |
||
14 | class GzDumpReader implements SeekableDumpReader { |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $dumpFile; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $initialPosition; |
||
25 | |||
26 | /** |
||
27 | * @var resource|null |
||
28 | */ |
||
29 | private $handle = null; |
||
30 | |||
31 | /** |
||
32 | * @param string $dumpFilePath |
||
33 | * @param int $initialPosition |
||
34 | */ |
||
35 | 5 | public function __construct( $dumpFilePath, $initialPosition = 0 ) { |
|
36 | 5 | $this->dumpFile = $dumpFilePath; |
|
37 | 5 | $this->initialPosition = $initialPosition; |
|
38 | 5 | } |
|
39 | |||
40 | 5 | public function __destruct() { |
|
41 | 5 | $this->closeReader(); |
|
42 | 5 | } |
|
43 | |||
44 | 5 | private function closeReader() { |
|
45 | 5 | if ( is_resource( $this->handle ) ) { |
|
46 | 4 | gzclose( $this->handle ); |
|
47 | 4 | $this->handle = null; |
|
48 | } |
||
49 | 5 | } |
|
50 | |||
51 | 1 | public function rewind() { |
|
52 | 1 | $this->closeReader(); |
|
53 | 1 | $this->initReader(); |
|
54 | 1 | } |
|
55 | |||
56 | 5 | private function initReader() { |
|
57 | 5 | if ( $this->handle === null ) { |
|
58 | 5 | $this->handle = @gzopen( $this->dumpFile, 'r' ); |
|
59 | |||
60 | 5 | if ( $this->handle === false ) { |
|
61 | 1 | throw new DumpReadingException( 'Could not open file: ' . $this->dumpFile ); |
|
62 | } |
||
63 | |||
64 | 4 | $this->seekToPosition( $this->initialPosition ); |
|
65 | } |
||
66 | 4 | } |
|
67 | |||
68 | /** |
||
69 | * @return string|null |
||
70 | * @throws DumpReadingException |
||
71 | */ |
||
72 | 5 | public function nextJsonLine() { |
|
73 | 5 | $this->initReader(); |
|
74 | |||
75 | do { |
||
76 | 4 | if ( gzeof( $this->handle ) ) { |
|
77 | 2 | return null; |
|
78 | } |
||
79 | |||
80 | 4 | $line = @gzgets( $this->handle ); |
|
81 | |||
82 | 4 | if ( $line === false ) { |
|
83 | return null; |
||
84 | } |
||
85 | } |
||
86 | 4 | while ( $line === '' || $line{0} !== '{' ); |
|
87 | |||
88 | 3 | return rtrim( $line, ",\n\r" ); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return int |
||
93 | * @throws DumpReadingException |
||
94 | */ |
||
95 | 1 | public function getPosition() { |
|
109 | |||
110 | /** |
||
111 | * @param int $position |
||
112 | * @throws DumpReadingException |
||
113 | */ |
||
114 | 4 | public function seekToPosition( $position ) { |
|
122 | |||
123 | } |
||
124 |