| 1 | <?php |
| 2 | |
| 3 | final class Freezer |
| 4 | { |
| 5 | protected $key_delimeter = '%%%'; |
| 6 | |
| 7 | private $length = 99999; |
| 8 | private $nodes; |
| 9 | private $nodes_location = ''; |
| 10 | private $queue; |
| 11 | private $limit = 500; |
| 12 | |
| 13 | public static function clean($directory, $suffix) { |
| 14 | $loc = $directory . '/' . $suffix; |
| 15 | file_exists($loc) && unlink($loc); |
| 16 | } |
| 17 | |
| 18 | public function __construct($directory, $suffix) { |
| 19 | $this->queue = array(); |
| 20 | $this->nodes_location = $directory . '/' . $suffix; |
| 21 | touch($this->nodes_location); |
| 22 | $this->nodes = fopen($this->nodes_location, 'r'); |
| 23 | } |
| 24 | |
| 25 | public function __destruct() { |
| 26 | $this->flush(); |
| 27 | |
| 28 | fclose($this->nodes); |
| 29 | } |
| 30 | |
| 31 | public function ids($flush = TRUE) { |
| 32 | if ($flush) { |
| 33 | $this->flush(); |
| 34 | } |
| 35 | |
| 36 | $ids = array(); |
| 37 | |
| 38 | rewind($this->nodes); |
| 39 | while (!feof($this->nodes)) { |
| 40 | $line = stream_get_line($this->nodes, $this->length, "\n"); |
| 41 | list($key,) = explode($this->key_delimeter, $line); |
| 42 | if (trim($key)) { |
| 43 | $ids[] = $key; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return $ids; |
| 48 | } |
| 49 | |
| 50 | public function open($key, $default) { |
| 51 | foreach (array_reverse($this->queue) as $queue) { |
| 52 | list($queue_key, $content) = $queue; |
| 53 | if ($queue_key == $key) { |
| 54 | return $content; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | $key .= $this->key_delimeter; |
| 59 | rewind($this->nodes); |
| 60 | while (!feof($this->nodes)) { |
| 61 | $line = stream_get_line($this->nodes, $this->length, "\n"); |
| 62 | if (strlen($line) > strlen($key) && substr($line, 0, strlen($key)) == $key) { |
| 63 | $line = substr($line, strlen($key)); |
| 64 | return unserialize(str_replace("\\n", "\n", $line)); |
| 65 | } |
| 66 | } |
| 67 | return $default; |
| 68 | } |
| 69 | |
| 70 | public function save($key, $content) { |
| 71 | $this->queue[] = array($key, $content); |
| 72 | if (count($this->queue) > $this->limit) { |
| 73 | $this->flush(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private function flush() { |
| 78 | $deferred = array(); |
| 79 | $keys = $this->ids(FALSE); |
| 80 | |
| 81 | foreach ($this->queue as $queue) { |
| 82 | list($key, $content) = $queue; |
| 83 | $serialized = $key . $this->key_delimeter . str_replace("\n", "\\n", serialize($content)); |
| 84 | if (!in_array($key, $keys)) { |
| 85 | $deferred[$key] = $serialized; |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | $key = $key . $this->key_delimeter; |
| 90 | $found = false; |
| 91 | $tmp = fopen($this->nodes_location . '_tmp', 'w'); |
| 92 | rewind($this->nodes); |
| 93 | while (!feof($this->nodes)) { |
| 94 | if ($line = stream_get_line($this->nodes, $this->length, "\n")) { |
| 95 | if (strlen($line) > strlen($key) && substr($line, 0, strlen($key)) == $key) { |
| 96 | $found = true; |
| 97 | $line = $serialized; |
| 98 | } |
| 99 | if (strlen($line) > $this->length-2) { |
| 100 | die('Line too long: ' . $line . "\n"); |
| 101 | } |
| 102 | fwrite($tmp, $line . "\n"); |
| 103 | } |
| 104 | } |
| 105 | if (!$found) { |
| 106 | fwrite($tmp, $key . str_replace("\n", "\\n", serialize($content)) . "\n"); |
| 107 | } |
| 108 | fclose($tmp); |
| 109 | fclose($this->nodes); |
| 110 | |
| 111 | unlink($this->nodes_location); |
| 112 | rename($this->nodes_location . '_tmp', $this->nodes_location); |
| 113 | $this->nodes = fopen($this->nodes_location, 'r'); |
| 114 | } |
| 115 | |
| 116 | if (!empty($deferred)) { |
| 117 | fclose($this->nodes); |
| 118 | copy($this->nodes_location, $this->nodes_location . '_tmp'); |
| 119 | $tmp = fopen($this->nodes_location . '_tmp', 'a'); |
| 120 | |
| 121 | foreach ($deferred as $line) { |
| 122 | fwrite($tmp, $line . "\n"); |
| 123 | } |
| 124 | fclose($tmp); |
| 125 | |
| 126 | unlink($this->nodes_location); |
| 127 | rename($this->nodes_location . '_tmp', $this->nodes_location); |
| 128 | $this->nodes = fopen($this->nodes_location, 'r'); |
| 129 | } |
| 130 | |
| 131 | $this->queue = array(); |
| 132 | } |
| 133 | } |