CatalogCacheObject.php
4.79 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
class CatalogCacheObject extends TimeTableCacheObject
{
private $paramsNumber;
private $paramsSizes = array();
private $paramsTypes = array();
public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1)
{
$interval = new CatIntervalCacheObject($this->lastId, count($this->intervals));
++$this->lastId;
$interval->setStartFromISO($startIso);
$interval->setStopFromISO($stopIso);
// for catalog
$interval->setParams($params);
$interval->setIsNew($isNew);
array_push($this->intervals, $interval);
if ($index < 0)
array_push($this->indexes, count($this->intervals) - 1);
else
array_splice($this->indexes, $index, 0, array(count($this->intervals) - 1));
if ($isNew)
$this->isModified = true;
return $interval;
}
public function setParamsNumber($number)
{
$this->paramsNumber = $number;
}
public function setParamsSizes($params)
{
for ($i = 0; $i < $this->paramsNumber; $i++)
$this->paramsSizes[$i] = $params[$i]['size'];
}
public function setParamsTypes($params)
{
for ($i = 0; $i < $this->paramsNumber; $i++)
$this->paramsTypes[$i] = $params[$i]['type'];
}
public function writeBin($handle)
{
//Magic key ("TTC")
fwrite($handle,pack('C3',ord('T'),ord('T'),ord('C')));
//Version
fwrite($handle,pack('L',TimeTableCacheObject::$format_version));
//Token
for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
fwrite($handle,pack('C',ord($this->token[$i])));
//Modified
fwrite($handle,pack('L',$this->isModified));
//Filter
$this->filter->writeBin($handle);
//Sort
$this->sort->writeBin($handle);
//Params Number
fwrite($handle,pack('L',$this->paramsNumber));
//Params Sizes
for ($i = 0; $i < $this->paramsNumber; $i++)
fwrite($handle,pack('L',$this->paramsSizes[$i]));
//Params Types
for ($i = 0; $i < $this->paramsNumber; $i++)
fwrite($handle,pack('L',$this->paramsTypes[$i]));
//Intervals
fwrite($handle,pack('L2', count($this->intervals), $this->lastId));
foreach($this->intervals as $interval)
$interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes, $this->paramsTypes);
//Indexes
fwrite($handle,pack('L',count($this->indexes)));
foreach($this->indexes as $index)
fwrite($handle,pack('L',$index));
}
public function loadBin($handle) {
//Magic key ("TTC")
if (!$res = unpack('C3key',fread($handle,3)))
return false;
if (($res['key1'] != ord('T')) || ($res['key2'] != ord('T')) || ($res['key3'] != ord('C')))
return false;
//Version
if (!$res = unpack('Lversion',fread($handle,4)))
return false;
if (($res['version'] != TimeTableCacheObject::$format_version))
return false;
//Token
$token = "";
for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
{
if (!$res = unpack('Ctoken',fread($handle,1)))
return false;
$token .= chr($res['token']);
}
$this->token = $token;
//Modified
if (!$res = unpack('Lmodified',fread($handle,4)))
return false;
$this->isModified = $res['modified'];
//Filter
$this->filter->loadBin($handle);
//Sort
$this->sort->loadBin($handle);
//ParamsNumber
if (!$res = unpack('Lnumber',fread($handle,4)))
return false;
$this->paramsNumber = $res['number'];
//ParamsSizes
for ($i = 0; $i < $this->paramsNumber; $i++) {
if (!$res = unpack('Lsize',fread($handle,4)))
return false;
$this->paramsSizes[$i] = $res['size'];
}
//ParamsTypes
for ($i = 0; $i < $this->paramsNumber; $i++) {
if (!$res = unpack('Ltype',fread($handle,4)))
return false;
$this->paramsTypes[$i] = $res['type'];
}
//Intervals
$res = unpack('L2data',fread($handle,2*4));
$nbIntervals = $res['data1'];
$this->lastId = $res['data2'];
for ($i = 0; $i < $nbIntervals; ++$i)
{
$interval = new CatIntervalCacheObject(-1);
$interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);
array_push($this->intervals, $interval);
}
//Indexes
$res = unpack('Ldata',fread($handle,4));
$nbIndexes = $res['data'];
for ($i = 0; $i < $nbIndexes; ++$i)
{
$res = unpack('Lindex',fread($handle,4));
array_push($this->indexes, $res['index']);
}
return true;
}
public function modifyIntervalFromId($obj) {
foreach ($this->intervals as $interval)
{
if ($interval->getId() == $obj->cacheId)
{
foreach((array)$obj as $key => $val) {
if ($key == 'start')
$interval->setStartFromISO($val);
else if ($key == 'stop')
$interval->setStopFromISO($val);
else {
if (strpos($key, 'param') === false)
continue;
$params = $interval->getParams();
$paramIndex = (int)substr($key,5);
$params[$paramIndex-2] = $val;
$interval->setParams($params);
}
}
$interval->setIsModified(true);
$this->isModified = true;
return true;
}
}
return false;
}
}
?>