Blame view

php/classes/CatalogCacheMgr.php 8.97 KB
d18b535d   elena   catalog draft + c...
1
2
3
4
5
6
7
8
9
10
11
12
<?php

/**
 * @class CatalogCacheMgr
 */
 
 
class CatIntervalCacheObject extends IntervalCacheObject
{
        // for catalog   
	private $params      = array();
	
4a438b99   elena   catalog draft
13
14
	public function toArray() 
	{
d18b535d   elena   catalog draft + c...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
		$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 
4a438b99   elena   catalog draft
34
35
36
	public function setParams($params) 
	{
	    $this->params = $params;		
d18b535d   elena   catalog draft + c...
37
38
	}
	
4a438b99   elena   catalog draft
39
40
	public function getParams() 
	{		  	   
d18b535d   elena   catalog draft + c...
41
42
43
	   return $this->params;		
	}
			
4a438b99   elena   catalog draft
44
45
	public function writeBin($handle, $paramsNumber, $paramsSizes) 
	{
d18b535d   elena   catalog draft + c...
46
47
48
49
50
	       fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified));
	       for ($i = 0; $i < $paramsNumber; $i++) {
	         $paramString = $this->params[$i];
	         $paramArray =  explode(',',$this->params[$i]);
	         for ($j = 0; $j < $paramsSizes[$i]; $j++) fwrite($handle,pack('d', $paramArray[$j]));
4a438b99   elena   catalog draft
51
	       }	        
d18b535d   elena   catalog draft + c...
52
53
	}
	
4a438b99   elena   catalog draft
54
55
	public function loadBin($handle, $paramsNumber, $paramsSizes) 
	{
d18b535d   elena   catalog draft + c...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
		$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));
			  $this->params[$i] .= $val['val'];
			  if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ',';
		      }		       
		}
		 
	}
	
4a438b99   elena   catalog draft
75
76
	public function dump() 
	{
d18b535d   elena   catalog draft + c...
77
78
79
80
81
82
83
84
85
		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();
        
4a438b99   elena   catalog draft
86
87
88
89
90
91
92
93
	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);
d18b535d   elena   catalog draft + c...
94

4a438b99   elena   catalog draft
95
96
97
98
99
100
101
102
103
104
		$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;
d18b535d   elena   catalog draft + c...
105
106
	}
    
4a438b99   elena   catalog draft
107
108
	public function setParamsNumber($number)
	{
d18b535d   elena   catalog draft + c...
109
110
111
	    $this->paramsNumber = $number;
	}
	
4a438b99   elena   catalog draft
112
113
114
115
	public function setParamsSizes($params)
	{
		for ($i = 0; $i < $this->paramsNumber; $i++)	         
			$this->paramsSizes[$i] = $params[$i]['size'];		   
d18b535d   elena   catalog draft + c...
116
117
	}
	
4a438b99   elena   catalog draft
118
119
	public function writeBin($handle) 
	{
d18b535d   elena   catalog draft + c...
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	  //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]));
          
	  //Intervals
	  fwrite($handle,pack('L2',count($this->intervals), $this->lastId));
	  

	  foreach($this->intervals as $interval)
		  $interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes);
	  
	  //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'];
		}

		//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);			
			array_push($this->intervals, $interval);
		}
4a438b99   elena   catalog draft
217
		
d18b535d   elena   catalog draft + c...
218
219
220
221
222
223
224
225
226
227
228
		//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;
	}
d18b535d   elena   catalog draft + c...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}

class CatalogCacheMgr extends TimeTableCacheMgr 
{
		
	protected static $cache_file                = "cacheCat";
	
	protected $ttMgr = null;
	protected $cache = null;

	function __construct() {
	         	        
		$this->ttMgr = new CatalogMgr();
	}
	
	
	public function initFromTmpObject($folderId, $name) {
	 
		//Create new cache		 
		$this->cache = new CatalogCacheObject();

		//Load intervals from TmpObject file (Statistics Module)
		$intervals_res = $this->ttMgr->getTmpObject($folderId, $name);
 
		if (!isset($intervals_res))
			return array('success' => false, 'message' => 'Cannot get Tmp Object');

		if (array_key_exists('intervals', $intervals_res))
		{
			foreach ($intervals_res['intervals'] as $interval)
			{
				//Add interval
				$this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']);
				
			}
		}

		$this->cache->setIsModified(true);
		
		$paramHeaders = $intervals_res['parameters'];
		
		$this->cache->setParamsNumber(count($paramHeaders));
		$this->cache->setParamsSizes($paramHeaders);
		
		unset($intervals_res);		
		
		//Update cache
		$this->cache->updateIndexes();

		//Save cache file
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 
		             'status' => $this->cache->getStatus(), 'parameters' => $paramHeaders);
	}
	
4a438b99   elena   catalog draft
283
284
	public function initFromTT($id, $typeTT) 
	{
e82af71b   elena   catalog draft
285
		//Create new cache
4a438b99   elena   catalog draft
286
		$this->cache = new CatalogCacheObject();
e82af71b   elena   catalog draft
287

4a438b99   elena   catalog draft
288
		//Load intervals from catalog file and add to cache
e82af71b   elena   catalog draft
289
290
291
292
293
294
295
296
		$intervals_res = $this->ttMgr->loadIntervalsFromTT($id,$typeTT);
		
		if (!$intervals_res['success'])
			return $intervals_res;

		foreach ($intervals_res['intervals'] as $interval)
		{
			//Add interval
4a438b99   elena   catalog draft
297
			$this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']);
e82af71b   elena   catalog draft
298
299
		}
		
4a438b99   elena   catalog draft
300
301
302
303
		$paramHeaders = $intervals_res['parameters'];
	
		$this->cache->setParamsNumber(count($paramHeaders));
		$this->cache->setParamsSizes($paramHeaders);
e82af71b   elena   catalog draft
304
305
306
307
308
309
		unset($intervals_res);

		//Update cache
		$this->cache->updateIndexes();

		//Save cache file
4a438b99   elena   catalog draft
310
311
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(),
			     'status' => $this->cache->getStatus(),'parameters' => $paramHeaders);
e82af71b   elena   catalog draft
312
313
	}

d18b535d   elena   catalog draft + c...
314

4a438b99   elena   catalog draft
315
316
	protected function loadFromFile() 
	{	
d18b535d   elena   catalog draft + c...
317
318
319
320
321
322
323
324
325
326
327
328
		if (!file_exists($this->getCacheFilePath()))
			return false;
		$this->cache = new CatalogCacheObject();
		$handle = fopen($this->getCacheFilePath(), 'rb');
		$result = false;
		if (flock($handle, LOCK_SH))
		{
			$this->cache->loadBin($handle);
			flock( $handle, LOCK_UN );
			$result = true;
		}
		fclose($handle);
4a438b99   elena   catalog draft
329
	return $result;
d18b535d   elena   catalog draft + c...
330
331
	}
	
4a438b99   elena   catalog draft
332
333
	protected function getCacheFilePath() 
	{
d18b535d   elena   catalog draft + c...
334
335
336
		return USERTTDIR.(self::$cache_file);
	}

4a438b99   elena   catalog draft
337
338
	public function saveInTT($id, $action, $token) 
	{	
d18b535d   elena   catalog draft + c...
339
340
341
342
343
344
345
346
347
348
349
350
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		if ($token != $this->cache->getToken())
			return array('success' => false, 'message' => 'Cache token check error');
		
		$this->cache->updateIndexes();
		$this->saveToFile();
		
		$intervals = $this->cache->getIntervalsArray(NULL,NULL,true);
		
		$this->cache->reset();
d18b535d   elena   catalog draft + c...
351

4a438b99   elena   catalog draft
352
353
	return $this->ttMgr->saveIntervals($id, $intervals, $action);
	}		
d18b535d   elena   catalog draft + c...
354
355
   }
?>