File: Freezer.php

package info (click to toggle)
dojo 1.10.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 97,980 kB
  • ctags: 10,348
  • sloc: php: 10,616; xml: 3,429; java: 3,098; sql: 928; sh: 484; pascal: 205; perl: 182; makefile: 77; python: 45; sed: 37; ruby: 10
file content (133 lines) | stat: -rw-r--r-- 3,542 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

final class Freezer
{
  protected $key_delimeter = '%%%';

  private $length = 99999;
  private $nodes;
  private $nodes_location = '';
  private $queue;
  private $limit = 500;

  public static function clean($directory, $suffix) {
    $loc = $directory . '/' . $suffix;
    file_exists($loc) && unlink($loc);
  }

  public function __construct($directory, $suffix) {
    $this->queue = array();
    $this->nodes_location = $directory . '/' . $suffix;
    touch($this->nodes_location);
    $this->nodes = fopen($this->nodes_location, 'r');
  }

  public function __destruct() {
    $this->flush();

    fclose($this->nodes);
  }

  public function ids($flush = TRUE) {
    if ($flush) {
      $this->flush();
    }

    $ids = array();

    rewind($this->nodes);
    while (!feof($this->nodes)) {
      $line = stream_get_line($this->nodes, $this->length, "\n");
      list($key,) = explode($this->key_delimeter, $line);
      if (trim($key)) {
        $ids[] = $key;
      }
    }

    return $ids;
  }

  public function open($key, $default) {
    foreach (array_reverse($this->queue) as $queue) {
      list($queue_key, $content) = $queue;
      if ($queue_key == $key) {
        return $content;
      }
    }

    $key .= $this->key_delimeter;
    rewind($this->nodes);
    while (!feof($this->nodes)) {
      $line = stream_get_line($this->nodes, $this->length, "\n");
      if (strlen($line) > strlen($key) && substr($line, 0, strlen($key)) == $key) {
        $line = substr($line, strlen($key));
        return unserialize(str_replace("\\n", "\n", $line));
      }
    }
    return $default;
  }

  public function save($key, $content) {
    $this->queue[] = array($key, $content);
    if (count($this->queue) > $this->limit) {
      $this->flush();
    }
  }

  private function flush() {
    $deferred = array();
    $keys = $this->ids(FALSE);

    foreach ($this->queue as $queue) {
      list($key, $content) = $queue;
      $serialized = $key . $this->key_delimeter . str_replace("\n", "\\n", serialize($content));
      if (!in_array($key, $keys)) {
        $deferred[$key] = $serialized;
        continue;
      }

      $key = $key . $this->key_delimeter;
      $found = false;
      $tmp = fopen($this->nodes_location . '_tmp', 'w');
      rewind($this->nodes);
      while (!feof($this->nodes)) {
        if ($line = stream_get_line($this->nodes, $this->length, "\n")) {
          if (strlen($line) > strlen($key) && substr($line, 0, strlen($key)) == $key) {
            $found = true;
            $line = $serialized;
          }
          if (strlen($line) > $this->length-2) {
            die('Line too long: ' . $line . "\n");
          }
          fwrite($tmp, $line . "\n");
        }
      }
      if (!$found) {
        fwrite($tmp, $key . str_replace("\n", "\\n", serialize($content)) . "\n");
      }
      fclose($tmp);
      fclose($this->nodes);

      unlink($this->nodes_location);
      rename($this->nodes_location . '_tmp', $this->nodes_location);
      $this->nodes = fopen($this->nodes_location, 'r');
    }

    if (!empty($deferred)) {
      fclose($this->nodes);
      copy($this->nodes_location, $this->nodes_location . '_tmp');
      $tmp = fopen($this->nodes_location . '_tmp', 'a');

      foreach ($deferred as $line) {
        fwrite($tmp, $line . "\n");
      }
      fclose($tmp);

      unlink($this->nodes_location);
      rename($this->nodes_location . '_tmp', $this->nodes_location);
      $this->nodes = fopen($this->nodes_location, 'r');
    }

    $this->queue = array();
  }
}