|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Toflar\FastSet; |
| 6 | + |
| 7 | +final class SetBuilder |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param string $sourcePath the source path must be a file with all entries separated by new lines |
| 11 | + * @param string $outputPath If your output path ends on ".gz", the set will also get compressed on top. |
| 12 | + */ |
| 13 | + public static function buildSet(string $sourcePath, string $outputPath): void |
| 14 | + { |
| 15 | + $inputHandle = fopen($sourcePath, 'r'); |
| 16 | + if (false === $inputHandle) { |
| 17 | + throw new \RuntimeException('Unable to open file: '.$sourcePath); |
| 18 | + } |
| 19 | + |
| 20 | + $terms = []; |
| 21 | + |
| 22 | + while (($line = fgets($inputHandle)) !== false) { |
| 23 | + $line = trim($line); |
| 24 | + if ('' !== $line) { |
| 25 | + $terms[] = $line; |
| 26 | + } |
| 27 | + } |
| 28 | + fclose($inputHandle); |
| 29 | + |
| 30 | + sort($terms, SORT_STRING); |
| 31 | + |
| 32 | + $outputHandle = self::openForWritingPossiblyGzip($outputPath); |
| 33 | + |
| 34 | + $previousTerm = ''; |
| 35 | + |
| 36 | + foreach ($terms as $term) { |
| 37 | + $commonPrefixLength = self::commonPrefixByteLength($previousTerm, $term); |
| 38 | + $suffix = substr($term, $commonPrefixLength); |
| 39 | + |
| 40 | + // <prefixLen>\t<suffix>\n |
| 41 | + fwrite($outputHandle, (string) $commonPrefixLength); |
| 42 | + fwrite($outputHandle, "\t"); |
| 43 | + fwrite($outputHandle, $suffix); |
| 44 | + fwrite($outputHandle, "\n"); |
| 45 | + |
| 46 | + $previousTerm = $term; |
| 47 | + } |
| 48 | + |
| 49 | + self::closePossiblyGzip($outputHandle, $outputPath); |
| 50 | + } |
| 51 | + |
| 52 | + public static function readSet(string $setPath, callable $callable): void |
| 53 | + { |
| 54 | + $handle = self::openForReadingPossiblyGzip($setPath); |
| 55 | + |
| 56 | + $previousTerm = ''; |
| 57 | + |
| 58 | + while (($line = self::readLinePossiblyGzip($handle, $setPath)) !== false) { |
| 59 | + $line = rtrim($line, "\r\n"); |
| 60 | + if ('' === $line) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $tabPosition = strpos($line, "\t"); |
| 65 | + if (false === $tabPosition) { |
| 66 | + throw new \UnexpectedValueException('Invalid file format. Ensure you have built it using the SetBuilder class!'); |
| 67 | + } |
| 68 | + |
| 69 | + $prefixLengthText = substr($line, 0, $tabPosition); |
| 70 | + $suffix = substr($line, $tabPosition + 1); |
| 71 | + |
| 72 | + $prefixLength = (int) $prefixLengthText; |
| 73 | + |
| 74 | + $term = substr($previousTerm, 0, $prefixLength).$suffix; |
| 75 | + |
| 76 | + $callable($term); |
| 77 | + |
| 78 | + $previousTerm = $term; |
| 79 | + } |
| 80 | + |
| 81 | + self::closePossiblyGzip($handle, $setPath); |
| 82 | + } |
| 83 | + |
| 84 | + private static function commonPrefixByteLength(string $left, string $right): int |
| 85 | + { |
| 86 | + $limit = min(\strlen($left), \strlen($right)); |
| 87 | + $index = 0; |
| 88 | + |
| 89 | + // Byte-wise common prefix |
| 90 | + while ($index < $limit && $left[$index] === $right[$index]) { |
| 91 | + ++$index; |
| 92 | + } |
| 93 | + |
| 94 | + return $index; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return resource |
| 99 | + */ |
| 100 | + private static function openForWritingPossiblyGzip(string $path) |
| 101 | + { |
| 102 | + if (str_ends_with($path, '.gz')) { |
| 103 | + if (!\function_exists('gzopen')) { |
| 104 | + throw new \RuntimeException('Cannot open for gzip write (gzopen not available): '.$path); |
| 105 | + } |
| 106 | + |
| 107 | + $handle = gzopen($path, 'wb9'); |
| 108 | + if (false === $handle) { |
| 109 | + throw new \RuntimeException('Cannot open for gzip write: '.$path); |
| 110 | + } |
| 111 | + |
| 112 | + return $handle; |
| 113 | + } |
| 114 | + |
| 115 | + $handle = fopen($path, 'w'); |
| 116 | + if (false === $handle) { |
| 117 | + throw new \RuntimeException('Cannot open for write: '.$path); |
| 118 | + } |
| 119 | + |
| 120 | + return $handle; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @return resource |
| 125 | + */ |
| 126 | + private static function openForReadingPossiblyGzip(string $path) |
| 127 | + { |
| 128 | + if (str_ends_with($path, '.gz')) { |
| 129 | + if (!\function_exists('gzopen')) { |
| 130 | + throw new \RuntimeException('Cannot open for reading gzip reading (gzopen not available): '.$path); |
| 131 | + } |
| 132 | + |
| 133 | + $handle = gzopen($path, 'rb'); |
| 134 | + if (false === $handle) { |
| 135 | + throw new \RuntimeException('Cannot open for reading gzip reading: '.$path); |
| 136 | + } |
| 137 | + |
| 138 | + return $handle; |
| 139 | + } |
| 140 | + |
| 141 | + $handle = fopen($path, 'r'); |
| 142 | + if (false === $handle) { |
| 143 | + throw new \RuntimeException('Cannot open for reading: '.$path); |
| 144 | + } |
| 145 | + |
| 146 | + return $handle; |
| 147 | + } |
| 148 | + |
| 149 | + private static function readLinePossiblyGzip($handle, string $path): string|false |
| 150 | + { |
| 151 | + if (str_ends_with($path, '.gz')) { |
| 152 | + if (!\function_exists('gzgets')) { |
| 153 | + throw new \RuntimeException('Cannot read gzip: '.$path); |
| 154 | + } |
| 155 | + |
| 156 | + return gzgets($handle); |
| 157 | + } |
| 158 | + |
| 159 | + return fgets($handle); |
| 160 | + } |
| 161 | + |
| 162 | + private static function closePossiblyGzip($handle, string $path): void |
| 163 | + { |
| 164 | + if (str_ends_with($path, '.gz')) { |
| 165 | + if (!\function_exists('gzclose')) { |
| 166 | + throw new \RuntimeException('Cannot write gzip: '.$path); |
| 167 | + } |
| 168 | + |
| 169 | + gzclose($handle); |
| 170 | + |
| 171 | + return; |
| 172 | + } |
| 173 | + fclose($handle); |
| 174 | + } |
| 175 | +} |
0 commit comments