From 2a24088cb94976e35dc48e85d9d5c7115e18d8b3 Mon Sep 17 00:00:00 2001 From: Benjamin Renard <benjamin.renard@akka.eu> Date: Thu, 18 Jul 2019 09:38:06 +0200 Subject: [PATCH] Split some source files --- php/classes/CatIntervalCacheObject.php | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ php/classes/CatalogCacheMgr.php | 301 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- php/classes/CatalogCacheObject.php | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ php/classes/FilterCacheObject.php | 286 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ php/classes/SortCacheObject.php | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ php/classes/TimeTableCacheMgr.php | 541 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 6 files changed, 853 insertions(+), 842 deletions(-) create mode 100644 php/classes/CatIntervalCacheObject.php create mode 100644 php/classes/CatalogCacheObject.php create mode 100644 php/classes/FilterCacheObject.php create mode 100644 php/classes/SortCacheObject.php diff --git a/php/classes/CatIntervalCacheObject.php b/php/classes/CatIntervalCacheObject.php new file mode 100644 index 0000000..67d581a --- /dev/null +++ b/php/classes/CatIntervalCacheObject.php @@ -0,0 +1,105 @@ +<?php + +class CatIntervalCacheObject extends IntervalCacheObject +{ + // for catalog + private $params = array(); + + public function toArray() + { + $result = array( + "cacheId" => $this->id, + "start" => $this->getStartToISO(), + "stop" => $this->getStopToISO() + ); + if ($this->isNew) + $result["isNew"] = true; + + if ($this->isModified) + $result["isModified"] = true; + + for ($i = 0; $i < count($this->params); $i++) + { + $paramObject = array(); + $index = 'param'.sprintf("%d",$i+2); + $result[$index] = $this->params[$i]; + } + return $result; + } + + // for catalog + public function setParams($params) + { + $this->params = $params; + } + + public function getParams() + { + return $this->params; + } + + public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) + { + fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified)); + for ($i = 0; $i < $paramsNumber; $i++) + { + if ($paramsTypes[$i] == '2') // string + { + fwrite($handle,pack('d', strlen($this->params[$i]))); + fwrite($handle, $this->params[$i],strlen($this->params[$i])); + } + else + { + if ($paramsTypes[$i] == '1') + $paramString = TimeUtils::iso2stamp($this->params[$i]); + else + $paramString = $this->params[$i]; + + $paramArray = explode(',',$paramString); + for ($j = 0; $j < $paramsSizes[$i]; $j++) fwrite($handle,pack('d', $paramArray[$j])); + } + } + } + + public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) + { + $array = unpack('L6int',fread($handle,6*4)); + $this->id = $array['int1']; + $this->index = $array['int2']; + $this->start = $array['int3']; + $this->stop = $array['int4']; + $this->isNew = $array['int5']; + $this->isModified = $array['int6']; + + for ($i = 0; $i < $paramsNumber; $i++) { + $this->params[$i] = null; + + for ($j = 0; $j < $paramsSizes[$i]; $j++) + { + $val = unpack('dval',fread($handle,8)); + + if ($paramsTypes[$i] == '2') // string + { + $this->params[$i] = fread($handle,$val['val']); + } + else + { + if ($paramsTypes[$i] == '1') + $this->params[$i] .= TimeUtils::stamp2iso($val['val']); + else + $this->params[$i] .= $val['val']; + + if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ','; + } + } + } + + } + + public function dump() + { + echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".$this->start.", stop = ".$this->stop.", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL; + } +} + + ?> diff --git a/php/classes/CatalogCacheMgr.php b/php/classes/CatalogCacheMgr.php index 670972d..225e4af 100644 --- a/php/classes/CatalogCacheMgr.php +++ b/php/classes/CatalogCacheMgr.php @@ -4,307 +4,6 @@ * @class CatalogCacheMgr */ - -class CatIntervalCacheObject extends IntervalCacheObject -{ - // for catalog - private $params = array(); - - public function toArray() - { - $result = array( - "cacheId" => $this->id, - "start" => $this->getStartToISO(), - "stop" => $this->getStopToISO() - ); - if ($this->isNew) - $result["isNew"] = true; - - if ($this->isModified) - $result["isModified"] = true; - - for ($i = 0; $i < count($this->params); $i++) - { - $paramObject = array(); - $index = 'param'.sprintf("%d",$i+2); - $result[$index] = $this->params[$i]; - } - return $result; - } - - // for catalog - public function setParams($params) - { - $this->params = $params; - } - - public function getParams() - { - return $this->params; - } - - public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) - { - fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified)); - for ($i = 0; $i < $paramsNumber; $i++) - { - if ($paramsTypes[$i] == '2') // string - { - fwrite($handle,pack('d', strlen($this->params[$i]))); - fwrite($handle, $this->params[$i],strlen($this->params[$i])); - } - else - { - if ($paramsTypes[$i] == '1') - $paramString = TimeUtils::iso2stamp($this->params[$i]); - else - $paramString = $this->params[$i]; - - $paramArray = explode(',',$paramString); - for ($j = 0; $j < $paramsSizes[$i]; $j++) fwrite($handle,pack('d', $paramArray[$j])); - } - } - } - - public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) - { - $array = unpack('L6int',fread($handle,6*4)); - $this->id = $array['int1']; - $this->index = $array['int2']; - $this->start = $array['int3']; - $this->stop = $array['int4']; - $this->isNew = $array['int5']; - $this->isModified = $array['int6']; - - for ($i = 0; $i < $paramsNumber; $i++) { - $this->params[$i] = null; - - for ($j = 0; $j < $paramsSizes[$i]; $j++) - { - $val = unpack('dval',fread($handle,8)); - - if ($paramsTypes[$i] == '2') // string - { - $this->params[$i] = fread($handle,$val['val']); - } - else - { - if ($paramsTypes[$i] == '1') - $this->params[$i] .= TimeUtils::stamp2iso($val['val']); - else - $this->params[$i] .= $val['val']; - - if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ','; - } - } - } - - } - - public function dump() - { - echo " => Interval : id = ".$this->id.", index = ".$this->index.", start = ".$this->start.", stop = ".$this->stop.", isNew = ".$this->isNew.", isModified = ".$this->isModified.PHP_EOL; - } -} - -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; - } -} - class CatalogCacheMgr extends TimeTableCacheMgr { diff --git a/php/classes/CatalogCacheObject.php b/php/classes/CatalogCacheObject.php new file mode 100644 index 0000000..082df0e --- /dev/null +++ b/php/classes/CatalogCacheObject.php @@ -0,0 +1,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; + } +} + + ?> diff --git a/php/classes/FilterCacheObject.php b/php/classes/FilterCacheObject.php new file mode 100644 index 0000000..cb6d958 --- /dev/null +++ b/php/classes/FilterCacheObject.php @@ -0,0 +1,286 @@ +<?php + +class FilterPartCacheObject +{ + public static $TYPE_UNKNOWN = 0; + public static $TYPE_START = 1; + public static $TYPE_STOP = 2; + public static $TYPE_DURATION_SEC = 3; + public static $TYPE_DURATION_MIN = 4; + public static $TYPE_DURATION_HOUR = 5; + + public static $OPERATION_UNKNOWN = 0; + public static $OPERATION_LT = 1; + public static $OPERATION_GT = 2; + public static $OPERATION_EQ = 3; + + protected $type; + protected $op; + protected $value; + + function __construct() { + $this->type = self::$TYPE_UNKNOWN; + $this->op = self::$OPERATION_UNKNOWN; + $this->value = 0.; + } + + public function getType() { + return $this->type; + } + + public function getOp() { + return $this->op; + } + + public function getValue() { + return $this->value; + } + + public function isSame($part) { + return (($this->type == $part->getType()) && ($this->op == $part->getOp()) && ($this->value == $part->getValue())); + } + + public function toFiltered($interval) { + switch ($this->type) { + case self::$TYPE_START : + { + switch ($this->op) { + case self::$OPERATION_LT : + return ($interval->getStartToStamp() < $this->value); + case self::$OPERATION_GT : + return ($interval->getStartToStamp() > $this->value); + case self::$OPERATION_EQ : + return (!(($interval->getStartToStamp() >= $this->value) && ($interval->getStartToStamp() <= $this->value+86400))); + default : + return false; + } + } + break; + case self::$TYPE_STOP : + { + switch ($this->op) { + case self::$OPERATION_LT : + return ($interval->getStopToStamp() < $this->value); + case self::$OPERATION_GT : + return ($interval->getStopToStamp() > $this->value); + case self::$OPERATION_EQ : + return (!(($interval->getStopToStamp() >= $this->value) && ($interval->getStopToStamp() <= $this->value+86400))); + default : + return false; + } + } + break; + case self::$TYPE_DURATION_SEC : + case self::$TYPE_DURATION_MIN : + case self::$TYPE_DURATION_HOUR : + { + $value = $this->value; + if ($this->type == self::$TYPE_DURATION_MIN) + $value *= 60; + else if ($this->type == self::$TYPE_DURATION_HOUR) + $value *= 3600; + switch ($this->op) { + case self::$OPERATION_LT : + return ($interval->getDuration() < $value); + case self::$OPERATION_GT : + return ($interval->getDuration() > $value); + case self::$OPERATION_EQ : + return ($interval->getDuration() != $value); + default : + return false; + } + } + break; + default: + return false; + } + } + + public function loadFromObject($part_obj) { + $this->value = 0.; + switch ($part_obj->field) + { + case 'start' : + $this->value = TimeUtils::iso2stamp($part_obj->value); + $this->type = self::$TYPE_START; + break; + case 'stop' : + $this->value = TimeUtils::iso2stamp($part_obj->value); + $this->type = self::$TYPE_STOP; + break; + case 'durationMin' : + $this->value = $part_obj->value; + $this->type = self::$TYPE_DURATION_MIN; + break; + case 'durationHour' : + $this->value = $part_obj->value; + $this->type = self::$TYPE_DURATION_HOUR; + break; + case 'durationSec' : + $this->value = $part_obj->value; + $this->type = self::$TYPE_DURATION_SEC; + break; + default: + $this->value = 0.; + $this->type = self::$TYPE_UNKNOWN; + } + + switch ($part_obj->comparison) + { + case 'lt' : + $this->op = self::$OPERATION_LT; + break; + case 'gt' : + $this->op = self::$OPERATION_GT; + break; + case 'eq' : + $this->op = self::$OPERATION_EQ; + break; + default: + $this->op = self::$OPERATION_UNKNOWN; + } + } + + public function writeBin($handle) { + fwrite($handle,pack('L2',$this->type,$this->op)); + fwrite($handle,pack('f',$this->value)); + } + + public function loadBin($handle) { + $res = unpack('L2data',fread($handle,4*2)); + $this->type = $res['data1']; + $this->op = $res['data2']; + + $res = unpack('fvalue',fread($handle,4)); + $this->value = $res['value']; + } + + public function dump() { + echo " => FilterPartCacheObject : type = "; + switch ($this->type) + { + case self::$TYPE_START : + echo "start"; + break; + case self::$TYPE_STOP : + echo "stop"; + break; + case self::$TYPE_DURATION_SEC : + echo "duration seconde"; + break; + case self::$TYPE_DURATION_MIN : + echo "duration minute"; + break; + case self::$TYPE_DURATION_HOUR : + echo "duration hour"; + break; + default: + echo "unknown"; + } + echo ", operation = "; + switch ($this->op) + { + case self::$OPERATION_LT : + echo "lt"; + break; + case self::$OPERATION_GT : + echo "gt"; + break; + case self::$OPERATION_EQ : + echo "eq"; + break; + default: + echo "unknown"; + } + echo ", value = ".$this->value.PHP_EOL; + } +} + +class FilterCacheObject +{ + protected $parts = array(); + + function __construct() { + + } + + public function getParts() { + return $this->parts; + } + + public function reset() { + $this->parts = array(); + } + + public function isEmpty() { + return (count($this->parts) == 0); + } + + public function loadFromJSON($filter_json) { + $this->reset(); + $filter_obj = json_decode($filter_json); + + foreach ($filter_obj as $filter_part) + { + $part = new FilterPartCacheObject(); + $part->loadFromObject($filter_part); + array_push($this->parts, $part); + } + } + + public function isSame($filter) { + if (count($this->parts) != count($filter->getParts())) + return false; + + $identique = true; + for ($i = 0; $i < count($this->parts); ++$i) + { + if (!$this->parts[$i]->isSame($filter->getParts()[$i])) + { + return false; + } + } + + return true; + } + + public function isSameFromJSON($filter_json) { + $filter = new FilterCacheObject(); + $filter->loadFromJSON($filter_json); + return $this->isSame($filter); + } + + public function toFiltered($interval) { + foreach ($this->parts as $part) + { + if ($part->toFiltered($interval)) + return true; + } + return false; + } + + public function writeBin($handle) { + fwrite($handle,pack('L',count($this->parts))); + foreach ($this->parts as $part) + $part->writeBin($handle); + } + + public function loadBin($handle) { + $this->reset(); + $res = unpack('Lcount',fread($handle,4)); + for ($i = 0; $i < $res['count']; ++$i) + { + $part = new FilterPartCacheObject(); + $part->loadBin($handle); + array_push($this->parts, $part); + } + } + + public function dump() { + echo " => FilterCacheObject : number of parts = ".count($this->parts).PHP_EOL; + foreach ($this->parts as $part) + $part->dump(); + } +} + + ?> diff --git a/php/classes/SortCacheObject.php b/php/classes/SortCacheObject.php new file mode 100644 index 0000000..d837d12 --- /dev/null +++ b/php/classes/SortCacheObject.php @@ -0,0 +1,261 @@ +<?php + +class SortPartCacheObject +{ + public static $TYPE_UNKNOWN = 0; + public static $TYPE_START = 1; + public static $TYPE_STOP = 2; + public static $TYPE_DURATION_SEC = 3; + public static $TYPE_DURATION_MIN = 4; + public static $TYPE_DURATION_HOUR = 5; + + public static $DIRECTION_UNKNOWN = 0; + public static $DIRECTION_ASC = 1; + public static $DIRECTION_DES = 2; + + protected $type; + protected $dir; + + function __construct() { + $this->type = self::$TYPE_UNKNOWN; + $this->dir = self::$DIRECTION_UNKNOWN; + } + + public function getType() { + return $this->type; + } + + public function getDir() { + return $this->dir; + } + + public function isSame($part) { + return (($this->type == $part->getType()) && ($this->dir == $part->getDir())); + } + + public function compare($interval_a, $interval_b) { + switch ($this->type) { + case self::$TYPE_START : + { + switch ($this->dir) { + case self::$DIRECTION_ASC : + return ($interval_b->getStartToStamp() - $interval_a->getStartToStamp()); + default : + return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp()); + } + } + break; + case self::$TYPE_STOP : + { + switch ($this->dir) { + case self::$DIRECTION_ASC : + return ($interval_b->getStopToStamp() - $interval_a->getStopToStamp()); + default : + return ($interval_a->getStopToStamp() - $interval_b->getStopToStamp()); + } + } + break; + case self::$TYPE_DURATION_SEC : + case self::$TYPE_DURATION_MIN : + case self::$TYPE_DURATION_HOUR : + { + switch ($this->dir) { + case self::$DIRECTION_ASC : + return ($interval_b->getDuration() - $interval_a->getDuration()); + default : + return ($interval_a->getDuration() - $interval_b->getDuration()); + } + } + break; + default : + return 0; + } + return 0; + } + + public function loadFromObject($part_obj) { + switch ($part_obj->property) + { + case 'start' : + $this->type = self::$TYPE_START; + break; + case 'stop' : + $this->type = self::$TYPE_STOP; + break; + case 'durationMin' : + $this->type = self::$TYPE_DURATION_MIN; + break; + case 'durationHour' : + $this->type = self::$TYPE_DURATION_HOUR; + break; + case 'durationSec' : + $this->type = self::$TYPE_DURATION_SEC; + break; + default: + $this->type = self::$TYPE_UNKNOWN; + } + + switch ($part_obj->direction) + { + case 'ASC' : + $this->dir = self::$DIRECTION_ASC; + break; + case 'DESC' : + $this->dir = self::$DIRECTION_DES; + break; + default: + $this->dir = self::$DIRECTION_UNKNOWN; + } + } + + public function writeBin($handle) { + fwrite($handle,pack('L2',$this->type,$this->dir)); + } + + public function loadBin($handle) { + $res = unpack('L2data',fread($handle,4*2)); + $this->type = $res['data1']; + $this->dir = $res['data2']; + } + + public function dump() { + echo " => SortPartCacheObject : type = "; + switch ($this->type) + { + case self::$TYPE_START : + echo "start"; + break; + case self::$TYPE_STOP : + echo "stop"; + break; + case self::$TYPE_DURATION_SEC : + echo "duration seconde"; + break; + case self::$TYPE_DURATION_MIN : + echo "duration minute"; + break; + case self::$TYPE_DURATION_HOUR : + echo "duration hour"; + break; + default: + echo "unknown"; + } + echo ", direction = "; + switch ($this->dir) + { + case self::$DIRECTION_ASC : + echo "ASC"; + break; + case self::$DIRECTION_DES : + echo "DESC"; + break; + default: + echo "unknown"; + } + echo PHP_EOL; + } +} + +class SortCacheObject +{ + protected $parts = array(); + + function __construct() { + } + + public function getParts() { + return $this->parts; + } + + public function reset() { + $this->parts = array(); + } + + public function isEmpty() { + return (count($this->parts) == 0); + } + + public function loadFromObject($sort_obj) { + $this->reset(); + foreach ($sort_obj as $sort_part) + { + $part = new SortPartCacheObject(); + $part->loadFromObject($sort_part); + array_push($this->parts, $part); + } + } + + public function isSameFromObject($sort_obj) { + $sort = new SortCacheObject(); + $sort->loadFromObject($sort_obj); + return $this->isSame($sort); + } + + public function isSame($sort) { + if (count($this->parts) != count($sort->getParts())) + return false; + + $identique = true; + for ($i = 0; $i < count($this->parts); ++$i) + { + if (!$this->parts[$i]->isSame($sort->getParts()[$i])) + { + return false; + } + } + + return true; + } + + public function apply($intervals) { + $sorted_indexes = array(); + + global $global_parts, $global_intervals; + $global_parts = $this->parts; + $global_intervals = $intervals; + + foreach ($intervals as $interval) + array_push($sorted_indexes, $interval->getIndex()); + + if (count($global_parts) == 0) + return $sorted_indexes; + + usort($sorted_indexes, function ($index_a, $index_b) { + global $global_parts, $global_intervals; + foreach ($global_parts as $part) + { + $res = $part->compare($global_intervals[$index_a], $global_intervals[$index_b]); + if ($res != 0) + return $res; + } + return $index_a-$index_b; + }); + + return $sorted_indexes; + } + + public function writeBin($handle) { + fwrite($handle,pack('L',count($this->parts))); + foreach ($this->parts as $part) + $part->writeBin($handle); + } + + public function loadBin($handle) { + $this->reset(); + $res = unpack('Lcount',fread($handle,4)); + for ($i = 0; $i < $res['count']; ++$i) + { + $part = new SortPartCacheObject(); + $part->loadBin($handle); + array_push($this->parts, $part); + } + } + + public function dump() { + echo " => SortCacheObject : number of parts = ".count($this->parts).PHP_EOL; + foreach ($this->parts as $part) + $part->dump(); + } +} + + ?> diff --git a/php/classes/TimeTableCacheMgr.php b/php/classes/TimeTableCacheMgr.php index af9b262..0ab5ed9 100644 --- a/php/classes/TimeTableCacheMgr.php +++ b/php/classes/TimeTableCacheMgr.php @@ -1,546 +1,5 @@ <?php -class SortPartCacheObject -{ - public static $TYPE_UNKNOWN = 0; - public static $TYPE_START = 1; - public static $TYPE_STOP = 2; - public static $TYPE_DURATION_SEC = 3; - public static $TYPE_DURATION_MIN = 4; - public static $TYPE_DURATION_HOUR = 5; - - public static $DIRECTION_UNKNOWN = 0; - public static $DIRECTION_ASC = 1; - public static $DIRECTION_DES = 2; - - protected $type; - protected $dir; - - function __construct() { - $this->type = self::$TYPE_UNKNOWN; - $this->dir = self::$DIRECTION_UNKNOWN; - } - - public function getType() { - return $this->type; - } - - public function getDir() { - return $this->dir; - } - - public function isSame($part) { - return (($this->type == $part->getType()) && ($this->dir == $part->getDir())); - } - - public function compare($interval_a, $interval_b) { - switch ($this->type) { - case self::$TYPE_START : - { - switch ($this->dir) { - case self::$DIRECTION_ASC : - return ($interval_b->getStartToStamp() - $interval_a->getStartToStamp()); - default : - return ($interval_a->getStartToStamp() - $interval_b->getStartToStamp()); - } - } - break; - case self::$TYPE_STOP : - { - switch ($this->dir) { - case self::$DIRECTION_ASC : - return ($interval_b->getStopToStamp() - $interval_a->getStopToStamp()); - default : - return ($interval_a->getStopToStamp() - $interval_b->getStopToStamp()); - } - } - break; - case self::$TYPE_DURATION_SEC : - case self::$TYPE_DURATION_MIN : - case self::$TYPE_DURATION_HOUR : - { - switch ($this->dir) { - case self::$DIRECTION_ASC : - return ($interval_b->getDuration() - $interval_a->getDuration()); - default : - return ($interval_a->getDuration() - $interval_b->getDuration()); - } - } - break; - default : - return 0; - } - return 0; - } - - public function loadFromObject($part_obj) { - switch ($part_obj->property) - { - case 'start' : - $this->type = self::$TYPE_START; - break; - case 'stop' : - $this->type = self::$TYPE_STOP; - break; - case 'durationMin' : - $this->type = self::$TYPE_DURATION_MIN; - break; - case 'durationHour' : - $this->type = self::$TYPE_DURATION_HOUR; - break; - case 'durationSec' : - $this->type = self::$TYPE_DURATION_SEC; - break; - default: - $this->type = self::$TYPE_UNKNOWN; - } - - switch ($part_obj->direction) - { - case 'ASC' : - $this->dir = self::$DIRECTION_ASC; - break; - case 'DESC' : - $this->dir = self::$DIRECTION_DES; - break; - default: - $this->dir = self::$DIRECTION_UNKNOWN; - } - } - - public function writeBin($handle) { - fwrite($handle,pack('L2',$this->type,$this->dir)); - } - - public function loadBin($handle) { - $res = unpack('L2data',fread($handle,4*2)); - $this->type = $res['data1']; - $this->dir = $res['data2']; - } - - public function dump() { - echo " => SortPartCacheObject : type = "; - switch ($this->type) - { - case self::$TYPE_START : - echo "start"; - break; - case self::$TYPE_STOP : - echo "stop"; - break; - case self::$TYPE_DURATION_SEC : - echo "duration seconde"; - break; - case self::$TYPE_DURATION_MIN : - echo "duration minute"; - break; - case self::$TYPE_DURATION_HOUR : - echo "duration hour"; - break; - default: - echo "unknown"; - } - echo ", direction = "; - switch ($this->dir) - { - case self::$DIRECTION_ASC : - echo "ASC"; - break; - case self::$DIRECTION_DES : - echo "DESC"; - break; - default: - echo "unknown"; - } - echo PHP_EOL; - } -} - -class SortCacheObject -{ - protected $parts = array(); - - function __construct() { - } - - public function getParts() { - return $this->parts; - } - - public function reset() { - $this->parts = array(); - } - - public function isEmpty() { - return (count($this->parts) == 0); - } - - public function loadFromObject($sort_obj) { - $this->reset(); - foreach ($sort_obj as $sort_part) - { - $part = new SortPartCacheObject(); - $part->loadFromObject($sort_part); - array_push($this->parts, $part); - } - } - - public function isSameFromObject($sort_obj) { - $sort = new SortCacheObject(); - $sort->loadFromObject($sort_obj); - return $this->isSame($sort); - } - - public function isSame($sort) { - if (count($this->parts) != count($sort->getParts())) - return false; - - $identique = true; - for ($i = 0; $i < count($this->parts); ++$i) - { - if (!$this->parts[$i]->isSame($sort->getParts()[$i])) - { - return false; - } - } - - return true; - } - - public function apply($intervals) { - $sorted_indexes = array(); - - global $global_parts, $global_intervals; - $global_parts = $this->parts; - $global_intervals = $intervals; - - foreach ($intervals as $interval) - array_push($sorted_indexes, $interval->getIndex()); - - if (count($global_parts) == 0) - return $sorted_indexes; - - usort($sorted_indexes, function ($index_a, $index_b) { - global $global_parts, $global_intervals; - foreach ($global_parts as $part) - { - $res = $part->compare($global_intervals[$index_a], $global_intervals[$index_b]); - if ($res != 0) - return $res; - } - return $index_a-$index_b; - }); - - return $sorted_indexes; - } - - public function writeBin($handle) { - fwrite($handle,pack('L',count($this->parts))); - foreach ($this->parts as $part) - $part->writeBin($handle); - } - - public function loadBin($handle) { - $this->reset(); - $res = unpack('Lcount',fread($handle,4)); - for ($i = 0; $i < $res['count']; ++$i) - { - $part = new SortPartCacheObject(); - $part->loadBin($handle); - array_push($this->parts, $part); - } - } - - public function dump() { - echo " => SortCacheObject : number of parts = ".count($this->parts).PHP_EOL; - foreach ($this->parts as $part) - $part->dump(); - } -} - -class FilterPartCacheObject -{ - public static $TYPE_UNKNOWN = 0; - public static $TYPE_START = 1; - public static $TYPE_STOP = 2; - public static $TYPE_DURATION_SEC = 3; - public static $TYPE_DURATION_MIN = 4; - public static $TYPE_DURATION_HOUR = 5; - - public static $OPERATION_UNKNOWN = 0; - public static $OPERATION_LT = 1; - public static $OPERATION_GT = 2; - public static $OPERATION_EQ = 3; - - protected $type; - protected $op; - protected $value; - - function __construct() { - $this->type = self::$TYPE_UNKNOWN; - $this->op = self::$OPERATION_UNKNOWN; - $this->value = 0.; - } - - public function getType() { - return $this->type; - } - - public function getOp() { - return $this->op; - } - - public function getValue() { - return $this->value; - } - - public function isSame($part) { - return (($this->type == $part->getType()) && ($this->op == $part->getOp()) && ($this->value == $part->getValue())); - } - - public function toFiltered($interval) { - switch ($this->type) { - case self::$TYPE_START : - { - switch ($this->op) { - case self::$OPERATION_LT : - return ($interval->getStartToStamp() < $this->value); - case self::$OPERATION_GT : - return ($interval->getStartToStamp() > $this->value); - case self::$OPERATION_EQ : - return (!(($interval->getStartToStamp() >= $this->value) && ($interval->getStartToStamp() <= $this->value+86400))); - default : - return false; - } - } - break; - case self::$TYPE_STOP : - { - switch ($this->op) { - case self::$OPERATION_LT : - return ($interval->getStopToStamp() < $this->value); - case self::$OPERATION_GT : - return ($interval->getStopToStamp() > $this->value); - case self::$OPERATION_EQ : - return (!(($interval->getStopToStamp() >= $this->value) && ($interval->getStopToStamp() <= $this->value+86400))); - default : - return false; - } - } - break; - case self::$TYPE_DURATION_SEC : - case self::$TYPE_DURATION_MIN : - case self::$TYPE_DURATION_HOUR : - { - $value = $this->value; - if ($this->type == self::$TYPE_DURATION_MIN) - $value *= 60; - else if ($this->type == self::$TYPE_DURATION_HOUR) - $value *= 3600; - switch ($this->op) { - case self::$OPERATION_LT : - return ($interval->getDuration() < $value); - case self::$OPERATION_GT : - return ($interval->getDuration() > $value); - case self::$OPERATION_EQ : - return ($interval->getDuration() != $value); - default : - return false; - } - } - break; - default: - return false; - } - } - - public function loadFromObject($part_obj) { - $this->value = 0.; - switch ($part_obj->field) - { - case 'start' : - $this->value = TimeUtils::iso2stamp($part_obj->value); - $this->type = self::$TYPE_START; - break; - case 'stop' : - $this->value = TimeUtils::iso2stamp($part_obj->value); - $this->type = self::$TYPE_STOP; - break; - case 'durationMin' : - $this->value = $part_obj->value; - $this->type = self::$TYPE_DURATION_MIN; - break; - case 'durationHour' : - $this->value = $part_obj->value; - $this->type = self::$TYPE_DURATION_HOUR; - break; - case 'durationSec' : - $this->value = $part_obj->value; - $this->type = self::$TYPE_DURATION_SEC; - break; - default: - $this->value = 0.; - $this->type = self::$TYPE_UNKNOWN; - } - - switch ($part_obj->comparison) - { - case 'lt' : - $this->op = self::$OPERATION_LT; - break; - case 'gt' : - $this->op = self::$OPERATION_GT; - break; - case 'eq' : - $this->op = self::$OPERATION_EQ; - break; - default: - $this->op = self::$OPERATION_UNKNOWN; - } - } - - public function writeBin($handle) { - fwrite($handle,pack('L2',$this->type,$this->op)); - fwrite($handle,pack('f',$this->value)); - } - - public function loadBin($handle) { - $res = unpack('L2data',fread($handle,4*2)); - $this->type = $res['data1']; - $this->op = $res['data2']; - - $res = unpack('fvalue',fread($handle,4)); - $this->value = $res['value']; - } - - public function dump() { - echo " => FilterPartCacheObject : type = "; - switch ($this->type) - { - case self::$TYPE_START : - echo "start"; - break; - case self::$TYPE_STOP : - echo "stop"; - break; - case self::$TYPE_DURATION_SEC : - echo "duration seconde"; - break; - case self::$TYPE_DURATION_MIN : - echo "duration minute"; - break; - case self::$TYPE_DURATION_HOUR : - echo "duration hour"; - break; - default: - echo "unknown"; - } - echo ", operation = "; - switch ($this->op) - { - case self::$OPERATION_LT : - echo "lt"; - break; - case self::$OPERATION_GT : - echo "gt"; - break; - case self::$OPERATION_EQ : - echo "eq"; - break; - default: - echo "unknown"; - } - echo ", value = ".$this->value.PHP_EOL; - } -} - -class FilterCacheObject -{ - protected $parts = array(); - - function __construct() { - - } - - public function getParts() { - return $this->parts; - } - - public function reset() { - $this->parts = array(); - } - - public function isEmpty() { - return (count($this->parts) == 0); - } - - public function loadFromJSON($filter_json) { - $this->reset(); - $filter_obj = json_decode($filter_json); - - foreach ($filter_obj as $filter_part) - { - $part = new FilterPartCacheObject(); - $part->loadFromObject($filter_part); - array_push($this->parts, $part); - } - } - - public function isSame($filter) { - if (count($this->parts) != count($filter->getParts())) - return false; - - $identique = true; - for ($i = 0; $i < count($this->parts); ++$i) - { - if (!$this->parts[$i]->isSame($filter->getParts()[$i])) - { - return false; - } - } - - return true; - } - - public function isSameFromJSON($filter_json) { - $filter = new FilterCacheObject(); - $filter->loadFromJSON($filter_json); - return $this->isSame($filter); - } - - public function toFiltered($interval) { - foreach ($this->parts as $part) - { - if ($part->toFiltered($interval)) - return true; - } - return false; - } - - public function writeBin($handle) { - fwrite($handle,pack('L',count($this->parts))); - foreach ($this->parts as $part) - $part->writeBin($handle); - } - - public function loadBin($handle) { - $this->reset(); - $res = unpack('Lcount',fread($handle,4)); - for ($i = 0; $i < $res['count']; ++$i) - { - $part = new FilterPartCacheObject(); - $part->loadBin($handle); - array_push($this->parts, $part); - } - } - - public function dump() { - echo " => FilterCacheObject : number of parts = ".count($this->parts).PHP_EOL; - foreach ($this->parts as $part) - $part->dump(); - } -} - class TimeTableCacheMgr { protected static $cache_file = "cacheTT"; -- libgit2 0.21.2