Commit 2f1cd44469abd421ee45a1184caf82de8c41bdcf

Authored by Benjamin Renard
1 parent 2a24088c

Apply convention naming for classes used for TT and Catalog cache

php/classes/CatIntervalCacheObject.php renamed to php/classes/CatalogCacheIntervalObject.php
1 1 <?php
2 2  
3   -class CatIntervalCacheObject extends IntervalCacheObject
  3 +class CatalogCacheIntervalObject extends TimeTableCacheIntervalObject
4 4 {
5 5 // for catalog
6 6 private $params = array();
... ...
php/classes/CatalogCacheObject.php
... ... @@ -8,7 +8,7 @@ class CatalogCacheObject extends TimeTableCacheObject
8 8  
9 9 public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1)
10 10 {
11   - $interval = new CatIntervalCacheObject($this->lastId, count($this->intervals));
  11 + $interval = new CatalogCacheIntervalObject($this->lastId, count($this->intervals));
12 12 ++$this->lastId;
13 13 $interval->setStartFromISO($startIso);
14 14 $interval->setStopFromISO($stopIso);
... ... @@ -150,7 +150,7 @@ class CatalogCacheObject extends TimeTableCacheObject
150 150  
151 151 for ($i = 0; $i < $nbIntervals; ++$i)
152 152 {
153   - $interval = new CatIntervalCacheObject(-1);
  153 + $interval = new CatalogCacheIntervalObject(-1);
154 154 $interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);
155 155 array_push($this->intervals, $interval);
156 156 }
... ...
php/classes/FilterCacheObject.php renamed to php/classes/TimeTableCacheFilterObject.php
1 1 <?php
2 2  
3   -class FilterPartCacheObject
  3 +class TimeTableCacheFilterPartObject
4 4 {
5 5 public static $TYPE_UNKNOWN = 0;
6 6 public static $TYPE_START = 1;
... ... @@ -156,7 +156,7 @@ class FilterPartCacheObject
156 156 }
157 157  
158 158 public function dump() {
159   - echo " => FilterPartCacheObject : type = ";
  159 + echo " => TimeTableCacheFilterPartObject : type = ";
160 160 switch ($this->type)
161 161 {
162 162 case self::$TYPE_START :
... ... @@ -196,7 +196,7 @@ class FilterPartCacheObject
196 196 }
197 197 }
198 198  
199   -class FilterCacheObject
  199 +class TimeTableCacheFilterObject
200 200 {
201 201 protected $parts = array();
202 202  
... ... @@ -222,7 +222,7 @@ class FilterCacheObject
222 222  
223 223 foreach ($filter_obj as $filter_part)
224 224 {
225   - $part = new FilterPartCacheObject();
  225 + $part = new TimeTableCacheFilterPartObject();
226 226 $part->loadFromObject($filter_part);
227 227 array_push($this->parts, $part);
228 228 }
... ... @@ -245,7 +245,7 @@ class FilterCacheObject
245 245 }
246 246  
247 247 public function isSameFromJSON($filter_json) {
248   - $filter = new FilterCacheObject();
  248 + $filter = new TimeTableCacheFilterObject();
249 249 $filter->loadFromJSON($filter_json);
250 250 return $this->isSame($filter);
251 251 }
... ... @@ -270,14 +270,14 @@ class FilterCacheObject
270 270 $res = unpack('Lcount',fread($handle,4));
271 271 for ($i = 0; $i < $res['count']; ++$i)
272 272 {
273   - $part = new FilterPartCacheObject();
  273 + $part = new TimeTableCacheFilterPartObject();
274 274 $part->loadBin($handle);
275 275 array_push($this->parts, $part);
276 276 }
277 277 }
278 278  
279 279 public function dump() {
280   - echo " => FilterCacheObject : number of parts = ".count($this->parts).PHP_EOL;
  280 + echo " => TimeTableCacheFilterObject : number of parts = ".count($this->parts).PHP_EOL;
281 281 foreach ($this->parts as $part)
282 282 $part->dump();
283 283 }
... ...
php/classes/IntervalCacheObject.php renamed to php/classes/TimeTableCacheIntervalObject.php
1 1 <?php
2   -class IntervalCacheObject
  2 +class TimeTableCacheIntervalObject
3 3 {
4 4 protected $id = -1;
5 5 protected $index = -1;
... ...
php/classes/TimeTableCacheObject.php
... ... @@ -4,24 +4,24 @@ class TimeTableCacheObject
4 4 {
5 5 protected static $format_version = 1;
6 6 protected static $token_len = 8;
7   -
  7 +
8 8 protected $token = "";
9   -
  9 +
10 10 protected $lastId = 0;
11   -
  11 +
12 12 protected $intervals = array();
13 13 protected $indexes = array();
14   -
  14 +
15 15 protected $isModified = false;
16   -
  16 +
17 17 protected $filter = null;
18   -
  18 +
19 19 protected $sort = null;
20 20  
21 21 function __construct() {
22 22 $this->token = $this->getRandomToken();
23   - $this->filter = new FilterCacheObject();
24   - $this->sort = new SortCacheObject();
  23 + $this->filter = new TimeTableCacheFilterObject();
  24 + $this->sort = new TimeTableCacheSortObject();
25 25 }
26 26  
27 27 public function reset() {
... ... @@ -30,17 +30,17 @@ class TimeTableCacheObject
30 30 $this->intervals = array();
31 31 $this->indexes = array();
32 32 unset($this->filter);
33   - $this->filter = new FilterCacheObject();;
  33 + $this->filter = new TimeTableCacheFilterObject();;
34 34 unset($this->sort);
35   - $this->sort = new SortCacheObject();
  35 + $this->sort = new TimeTableCacheSortObject();
36 36 }
37   -
  37 +
38 38 public function setIsModified($isModified) {
39 39 $this->isModified = $isModified;
40 40 }
41   -
  41 +
42 42 public function addInterval($startIso, $stopIso, $isNew = false, $index = -1) {
43   - $interval = new IntervalCacheObject($this->lastId, count($this->intervals));
  43 + $interval = new TimeTableCacheIntervalObject($this->lastId, count($this->intervals));
44 44 ++$this->lastId;
45 45 $interval->setStartFromISO($startIso);
46 46 $interval->setStopFromISO($stopIso);
... ... @@ -54,7 +54,7 @@ class TimeTableCacheObject
54 54 $this->isModified = true;
55 55 return $interval;
56 56 }
57   -
  57 +
58 58 public function removeIntervalFromId($id) {
59 59 for ($i = 0; $i < count($this->intervals); ++$i)
60 60 {
... ... @@ -81,10 +81,10 @@ class TimeTableCacheObject
81 81 return true;
82 82 }
83 83 }
84   -
  84 +
85 85 return false;
86 86 }
87   -
  87 +
88 88 public function modifyIntervalFromId($id, $start, $stop) {
89 89 foreach ($this->intervals as $interval)
90 90 {
... ... @@ -99,46 +99,46 @@ class TimeTableCacheObject
99 99 return true;
100 100 }
101 101 }
102   -
  102 +
103 103 return false;
104 104 }
105   -
  105 +
106 106 public function operationIntervals($extendTime, $shiftTime) {
107 107 if (($extendTime == 0) && ($shiftTime == 0))
108 108 //Nothing to do
109 109 return true;
110   -
  110 +
111 111 for ($i = 0; $i < count($this->indexes); ++$i) {
112 112 $start = $this->intervals[$this->indexes[$i]]->getStartToStamp();
113 113 $start -= $extendTime;
114 114 $start += $shiftTime;
115 115 $this->intervals[$this->indexes[$i]]->setStartFromStamp($start);
116   -
  116 +
117 117 $stop = $this->intervals[$this->indexes[$i]]->getStopToStamp();
118 118 $stop += $extendTime;
119 119 $stop += $shiftTime;
120 120 $this->intervals[$this->indexes[$i]]->setStopFromStamp($stop);
121   -
  121 +
122 122 $this->intervals[$this->indexes[$i]]->setIsModified(true);
123 123 $this->isModified = true;
124 124 }
125   -
  125 +
126 126 return true;
127 127 }
128   -
  128 +
129 129 public function mergeIntervals() {
130 130 $this->sort->reset();
131   -
  131 +
132 132 $this->sort->loadFromObject(
133 133 array(
134 134 (object)array("property" => "start", "direction" => "DESC")
135   - )
  135 + )
136 136 );
137   -
  137 +
138 138 $this->updateIndexes();
139   -
  139 +
140 140 $merged_intervals = array();
141   -
  141 +
142 142 for ($i = 0; $i < count($this->indexes); ++$i) {
143 143 if (count($merged_intervals) == 0)
144 144 {
... ... @@ -162,11 +162,11 @@ class TimeTableCacheObject
162 162 "mod" => FALSE)
163 163 );
164 164 }
165   -
  165 +
166 166 $this->reset();
167   -
  167 +
168 168 foreach ($merged_intervals as $merged_interval) {
169   - $interval = new IntervalCacheObject($this->lastId, count($this->intervals));
  169 + $interval = new TimeTableCacheIntervalObject($this->lastId, count($this->intervals));
170 170 ++$this->lastId;
171 171 $interval->setStartFromStamp($merged_interval["start"]);
172 172 $interval->setStopFromStamp($merged_interval["stop"]);
... ... @@ -176,10 +176,10 @@ class TimeTableCacheObject
176 176 array_push($this->intervals, $interval);
177 177 array_push($this->indexes, count($this->intervals) - 1);
178 178 }
179   -
  179 +
180 180 return true;
181 181 }
182   -
  182 +
183 183 public function getStatistics() {
184 184 $minTime = NULL;
185 185 $maxTime = NULL;
... ... @@ -187,38 +187,38 @@ class TimeTableCacheObject
187 187 $maxDuration = NULL;
188 188 $indexMinDuration = -1;
189 189 $indexMaxDuration = -1;
190   -
  190 +
191 191 $nbValid = 0;
192 192 $durationTotal = 0;
193   -
  193 +
194 194 //Min & Max
195 195 for ($i = 0; $i < count($this->indexes); ++$i) {
196 196 if ($this->intervals[$this->indexes[$i]]->getDuration() <= 0)
197 197 //Invalid interval
198 198 continue;
199   -
  199 +
200 200 ++$nbValid;
201 201 $durationTotal += $this->intervals[$this->indexes[$i]]->getDuration();
202   -
  202 +
203 203 if (!isset($minTime) || ($minTime > $this->intervals[$this->indexes[$i]]->getStartToStamp()))
204 204 $minTime = $this->intervals[$this->indexes[$i]]->getStartToStamp();
205   -
  205 +
206 206 if (!isset($maxTime) || ($maxTime < $this->intervals[$this->indexes[$i]]->getStopToStamp()))
207 207 $maxTime = $this->intervals[$this->indexes[$i]]->getStopToStamp();
208   -
  208 +
209 209 if (!isset($minDuration) || ($minDuration > $this->intervals[$this->indexes[$i]]->getDuration()))
210 210 {
211 211 $minDuration = $this->intervals[$this->indexes[$i]]->getDuration();
212 212 $indexMinDuration = $i;
213 213 }
214   -
  214 +
215 215 if (!isset($maxDuration) || ($maxDuration < $this->intervals[$this->indexes[$i]]->getDuration()))
216 216 {
217 217 $maxDuration = $this->intervals[$this->indexes[$i]]->getDuration();
218 218 $indexMaxDuration = $i;
219 219 }
220 220 }
221   -
  221 +
222 222 if (!isset($minTime))
223 223 $minTime = 0;
224 224 if (!isset($maxTime))
... ... @@ -227,21 +227,21 @@ class TimeTableCacheObject
227 227 $minDuration = 0;
228 228 if (!isset($maxDuration))
229 229 $maxDuration = 0;
230   -
231   -
  230 +
  231 +
232 232 //Mean
233 233 if ($nbValid > 0)
234 234 $mean = $durationTotal / $nbValid;
235 235 else
236 236 $mean = 0;
237   -
  237 +
238 238 //Standard deviation
239 239 $pow = 0;
240 240 for ($i = 0; $i < count($this->indexes); ++$i) {
241 241 if ($this->intervals[$this->indexes[$i]]->getDuration() <= 0)
242 242 //Invalid interval
243 243 continue;
244   -
  244 +
245 245 $pow += pow($this->intervals[$this->indexes[$i]]->getDuration()-$mean,2);
246 246 }
247 247 if ($nbValid > 0)
... ... @@ -249,27 +249,27 @@ class TimeTableCacheObject
249 249 else
250 250 $variance = 0;
251 251 $stdev = sqrt($variance);
252   -
  252 +
253 253 //Sort by duration to get median
254 254 $this->sort->reset();
255   -
  255 +
256 256 $this->sort->loadFromObject(
257 257 array(
258 258 (object)array("property" => "durationSec", "direction" => "DESC")
259 259 )
260 260 );
261   -
  261 +
262 262 $this->updateIndexes();
263   -
  263 +
264 264 $durations = array();
265 265 for ($i = 0; $i < count($this->indexes); ++$i) {
266 266 if ($this->intervals[$this->indexes[$i]]->getDuration() <= 0)
267 267 //Invalid interval
268 268 continue;
269   -
  269 +
270 270 array_push($durations, $this->intervals[$this->indexes[$i]]->getDuration());
271 271 }
272   -
  272 +
273 273 if (count($durations) > 0)
274 274 {
275 275 if (count($durations)%2 > 0) {
... ... @@ -280,24 +280,24 @@ class TimeTableCacheObject
280 280 }
281 281 else
282 282 $median = 0;
283   -
  283 +
284 284 //Merge intervals to get density
285 285 $this->mergeIntervals();
286   -
  286 +
287 287 $durationMergedTotal = 0;
288 288 for ($i = 0; $i < count($this->indexes); ++$i) {
289 289 if ($this->intervals[$this->indexes[$i]]->getDuration() <= 0)
290 290 //Invalid interval
291 291 continue;
292   -
  292 +
293 293 $durationMergedTotal += $this->intervals[$this->indexes[$i]]->getDuration();
294 294 }
295   -
  295 +
296 296 if (($maxTime-$minTime) > 0)
297 297 $density = (($durationMergedTotal/($maxTime-$minTime)));
298 298 else
299 299 $density = 0;
300   -
  300 +
301 301 return array(
302 302 "minDuration" => $minDuration,
303 303 "minDurationIndex"=> $indexMinDuration,
... ... @@ -308,10 +308,10 @@ class TimeTableCacheObject
308 308 "median" => $median,
309 309 "density" => $density);
310 310 }
311   -
  311 +
312 312 public function getStatus() {
313 313 $nbFiltered = count($this->intervals) - count($this->indexes);
314   -
  314 +
315 315 $nbModified = 0;
316 316 $nbNew = 0;
317 317 $nbInvalid = 0;
... ... @@ -326,7 +326,7 @@ class TimeTableCacheObject
326 326 if ($this->intervals[$this->indexes[$i]]->isNew())
327 327 ++$nbNew;
328 328 }
329   -
  329 +
330 330 return array(
331 331 "nbFiltered" => $nbFiltered,
332 332 "nbModified" => $nbModified,
... ... @@ -336,16 +336,16 @@ class TimeTableCacheObject
336 336 "isModified" => $this->isModified
337 337 );
338 338 }
339   -
  339 +
340 340 public function getIntervalsArray($startIndex, $limit,$skipInvalid = false) {
341 341 $intervals = array();
342   -
  342 +
343 343 if (!isset($startIndex))
344 344 $startIndex = 0;
345   -
  345 +
346 346 if (!isset($limit))
347 347 $limit = count($this->indexes);
348   -
  348 +
349 349 for ($i = 0; $i < $limit; ++$i) {
350 350 if ($startIndex+$i >= count($this->indexes))
351 351 break;
... ... @@ -355,32 +355,32 @@ class TimeTableCacheObject
355 355 }
356 356 return $intervals;
357 357 }
358   -
  358 +
359 359 public function getLength() {
360 360 return count($this->indexes);
361 361 }
362   -
  362 +
363 363 public function getToken() {
364 364 return $this->token;
365 365 }
366   -
  366 +
367 367 public function getFilter() {
368 368 return $this->filter;
369 369 }
370   -
  370 +
371 371 public function getSort() {
372 372 return $this->sort;
373 373 }
374   -
  374 +
375 375 public function updateIndexes() {
376 376 $this->indexes = array();
377   -
  377 +
378 378 for ($i = 0; $i < count($this->intervals); ++$i)
379 379 $this->intervals[$i]->setIndex($i);
380   -
  380 +
381 381 //Apply sort
382 382 $sort_result = $this->sort->apply($this->intervals);
383   -
  383 +
384 384 //Apply filter
385 385 for ($i = 0; $i < count($sort_result); ++$i)
386 386 {
... ... @@ -388,24 +388,24 @@ class TimeTableCacheObject
388 388 array_push($this->indexes,$this->intervals[$sort_result[$i]]->getIndex());
389 389 }
390 390 }
391   -
  391 +
392 392 public function writeBin($handle) {
393 393 //Magic key ("TTC")
394 394 fwrite($handle,pack('C3',ord('T'),ord('T'),ord('C')));
395   -
  395 +
396 396 //Version
397 397 fwrite($handle,pack('L',TimeTableCacheObject::$format_version));
398   -
  398 +
399 399 //Token
400 400 for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
401 401 fwrite($handle,pack('C',ord($this->token[$i])));
402   -
  402 +
403 403 //Modified
404 404 fwrite($handle,pack('L',$this->isModified));
405   -
  405 +
406 406 //Filter
407 407 $this->filter->writeBin($handle);
408   -
  408 +
409 409 //Sort
410 410 $this->sort->writeBin($handle);
411 411  
... ... @@ -413,21 +413,21 @@ class TimeTableCacheObject
413 413 fwrite($handle,pack('L2',count($this->intervals), $this->lastId));
414 414 foreach($this->intervals as $interval)
415 415 $interval->writeBin($handle);
416   -
  416 +
417 417 //Indexes
418 418 fwrite($handle,pack('L',count($this->indexes)));
419 419 foreach($this->indexes as $index)
420 420 fwrite($handle,pack('L',$index));
421 421 }
422   -
  422 +
423 423 public function loadBin($handle) {
424 424 //Magic key ("TTC")
425 425 if (!$res = unpack('C3key',fread($handle,3)))
426 426 return false;
427   -
  427 +
428 428 if (($res['key1'] != ord('T')) || ($res['key2'] != ord('T')) || ($res['key3'] != ord('C')))
429 429 return false;
430   -
  430 +
431 431 //Version
432 432 if (!$res = unpack('Lversion',fread($handle,4)))
433 433 return false;
... ... @@ -443,29 +443,29 @@ class TimeTableCacheObject
443 443 $token .= chr($res['token']);
444 444 }
445 445 $this->token = $token;
446   -
  446 +
447 447 //Modified
448 448 if (!$res = unpack('Lmodified',fread($handle,4)))
449 449 return false;
450 450 $this->isModified = $res['modified'];
451   -
  451 +
452 452 //Filter
453 453 $this->filter->loadBin($handle);
454   -
  454 +
455 455 //Sort
456 456 $this->sort->loadBin($handle);
457   -
  457 +
458 458 //Intervals
459 459 $res = unpack('L2data',fread($handle,2*4));
460 460 $nbIntervals = $res['data1'];
461 461 $this->lastId = $res['data2'];
462 462 for ($i = 0; $i < $nbIntervals; ++$i)
463 463 {
464   - $interval = new IntervalCacheObject(-1);
  464 + $interval = new TimeTableCacheIntervalObject(-1);
465 465 $interval->loadBin($handle);
466 466 array_push($this->intervals, $interval);
467 467 }
468   -
  468 +
469 469 //Indexes
470 470 $res = unpack('Ldata',fread($handle,4));
471 471 $nbIndexes = $res['data'];
... ... @@ -474,29 +474,29 @@ class TimeTableCacheObject
474 474 $res = unpack('Lindex',fread($handle,4));
475 475 array_push($this->indexes, $res['index']);
476 476 }
477   -
  477 +
478 478 return true;
479 479 }
480   -
  480 +
481 481 protected function getRandomToken() {
482 482 $letters = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
483 483 return substr(str_shuffle($letters), 0, TimeTableCacheObject::$token_len);
484 484 }
485   -
  485 +
486 486 public function dump() {
487 487 echo " => TimeTableCacheObject : token = ".$this->token.", nb intervals = ".count($this->intervals).", last id = ".$this->lastId.", nb indexes = ".count($this->indexes).PHP_EOL;
488 488 echo PHP_EOL;
489   -
  489 +
490 490 $this->filter->dump();
491 491 echo PHP_EOL;
492   -
  492 +
493 493 $this->sort->dump();
494 494 echo PHP_EOL;
495   -
  495 +
496 496 foreach ($this->intervals as $interval)
497 497 $interval->dump();
498 498 echo PHP_EOL;
499   -
  499 +
500 500 echo " => Indexes list : ";
501 501 foreach ($this->indexes as $index)
502 502 {
... ... @@ -506,4 +506,4 @@ class TimeTableCacheObject
506 506 }
507 507 }
508 508  
509   -?>
510 509 \ No newline at end of file
  510 +?>
... ...
php/classes/SortCacheObject.php renamed to php/classes/TimeTableCacheSortObject.php
1 1 <?php
2 2  
3   -class SortPartCacheObject
  3 +class TimeTableCacheSortPartObject
4 4 {
5 5 public static $TYPE_UNKNOWN = 0;
6 6 public static $TYPE_START = 1;
... ... @@ -119,7 +119,7 @@ class SortPartCacheObject
119 119 }
120 120  
121 121 public function dump() {
122   - echo " => SortPartCacheObject : type = ";
  122 + echo " => TimeTableCacheSortPartObject : type = ";
123 123 switch ($this->type)
124 124 {
125 125 case self::$TYPE_START :
... ... @@ -156,7 +156,7 @@ class SortPartCacheObject
156 156 }
157 157 }
158 158  
159   -class SortCacheObject
  159 +class TimeTableCacheSortObject
160 160 {
161 161 protected $parts = array();
162 162  
... ... @@ -179,14 +179,14 @@ class SortCacheObject
179 179 $this->reset();
180 180 foreach ($sort_obj as $sort_part)
181 181 {
182   - $part = new SortPartCacheObject();
  182 + $part = new TimeTableCacheSortPartObject();
183 183 $part->loadFromObject($sort_part);
184 184 array_push($this->parts, $part);
185 185 }
186 186 }
187 187  
188 188 public function isSameFromObject($sort_obj) {
189   - $sort = new SortCacheObject();
  189 + $sort = new TimeTableCacheSortObject();
190 190 $sort->loadFromObject($sort_obj);
191 191 return $this->isSame($sort);
192 192 }
... ... @@ -245,14 +245,14 @@ class SortCacheObject
245 245 $res = unpack('Lcount',fread($handle,4));
246 246 for ($i = 0; $i < $res['count']; ++$i)
247 247 {
248   - $part = new SortPartCacheObject();
  248 + $part = new TimeTableCacheSortPartObject();
249 249 $part->loadBin($handle);
250 250 array_push($this->parts, $part);
251 251 }
252 252 }
253 253  
254 254 public function dump() {
255   - echo " => SortCacheObject : number of parts = ".count($this->parts).PHP_EOL;
  255 + echo " => TimeTableCacheSortObject : number of parts = ".count($this->parts).PHP_EOL;
256 256 foreach ($this->parts as $part)
257 257 $part->dump();
258 258 }
... ...