Blame view

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

/**
 * @class CatalogCacheMgr
 */
 
 
class CatIntervalCacheObject extends IntervalCacheObject
{
c865df36   Elena.Budnik   dateTime datatype...
10
	// for catalog   
d18b535d   elena   catalog draft + c...
11
12
	private $params      = array();
	
4a438b99   elena   catalog draft
13
14
	public function toArray() 
	{
d18b535d   elena   catalog draft + c...
15
16
17
18
19
20
21
		$result = array(
			"cacheId" => $this->id,
			"start"   => $this->getStartToISO(),
			"stop"    => $this->getStopToISO()
		);
		if ($this->isNew)
			$result["isNew"] = true;
c865df36   Elena.Budnik   dateTime datatype...
22
			
d18b535d   elena   catalog draft + c...
23
24
25
		if ($this->isModified)
			$result["isModified"] = true;
			
c865df36   Elena.Budnik   dateTime datatype...
26
27
28
29
30
		for ($i = 0; $i < count($this->params); $i++) 
		{
			$paramObject = array();
			$index = 'param'.sprintf("%d",$i+2);
			$result[$index] = $this->params[$i];		    
d18b535d   elena   catalog draft + c...
31
32
33
34
35
		}		 
		return $result; 
	}

	// for catalog 
4a438b99   elena   catalog draft
36
37
	public function setParams($params) 
	{
c865df36   Elena.Budnik   dateTime datatype...
38
		$this->params = $params;		
d18b535d   elena   catalog draft + c...
39
40
	}
	
4a438b99   elena   catalog draft
41
42
	public function getParams() 
	{		  	   
d18b535d   elena   catalog draft + c...
43
44
45
	   return $this->params;		
	}
			
c865df36   Elena.Budnik   dateTime datatype...
46
	public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) 
4a438b99   elena   catalog draft
47
	{
c865df36   Elena.Budnik   dateTime datatype...
48
49
		fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified));
		for ($i = 0; $i < $paramsNumber; $i++) 
7def71a5   Elena.Budnik   redmine #5380
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
		{
			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 = CacheTools::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]));
			}
c865df36   Elena.Budnik   dateTime datatype...
66
		}	        
d18b535d   elena   catalog draft + c...
67
68
	}
	
c865df36   Elena.Budnik   dateTime datatype...
69
	public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes) 
4a438b99   elena   catalog draft
70
	{
d18b535d   elena   catalog draft + c...
71
72
73
74
75
76
77
78
79
80
		$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;
7def71a5   Elena.Budnik   redmine #5380
81
		  
901ba3f3   Elena.Budnik   upload catalog
82
83
84
		  for ($j = 0; $j < $paramsSizes[$i]; $j++) 
		  {
				$val = unpack('dval',fread($handle,8));
c865df36   Elena.Budnik   dateTime datatype...
85
				
7def71a5   Elena.Budnik   redmine #5380
86
87
88
89
90
91
92
93
94
95
96
97
98
				if ($paramsTypes[$i] == '2') // string
				{
					$this->params[$i] = fread($handle,$val['val']);
				}
				else 
				{
					if ($paramsTypes[$i] == '1')
						$this->params[$i] .= CacheTools::stamp2iso($val['val']);
					else 
						$this->params[$i] .= $val['val'];
						
					if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ',';
				}
901ba3f3   Elena.Budnik   upload catalog
99
			}		       
d18b535d   elena   catalog draft + c...
100
101
102
103
		}
		 
	}
	
4a438b99   elena   catalog draft
104
105
	public function dump() 
	{
d18b535d   elena   catalog draft + c...
106
107
108
109
110
111
		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
{
c865df36   Elena.Budnik   dateTime datatype...
112
113
114
	private $paramsNumber;
	private $paramsSizes = array();
	private $paramsTypes = array();
d18b535d   elena   catalog draft + c...
115
        
4a438b99   elena   catalog draft
116
117
118
119
120
121
122
123
	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...
124

4a438b99   elena   catalog draft
125
126
		$interval->setIsNew($isNew);
		array_push($this->intervals, $interval);
c865df36   Elena.Budnik   dateTime datatype...
127
		
4a438b99   elena   catalog draft
128
129
130
131
		if ($index < 0)
			array_push($this->indexes, count($this->intervals) - 1);
		else
			array_splice($this->indexes, $index, 0, array(count($this->intervals) - 1));
c865df36   Elena.Budnik   dateTime datatype...
132
			
4a438b99   elena   catalog draft
133
134
135
136
		if ($isNew)
			$this->isModified = true;
			
		return $interval;
d18b535d   elena   catalog draft + c...
137
138
	}
    
4a438b99   elena   catalog draft
139
140
	public function setParamsNumber($number)
	{
d18b535d   elena   catalog draft + c...
141
142
143
	    $this->paramsNumber = $number;
	}
	
4a438b99   elena   catalog draft
144
145
146
147
	public function setParamsSizes($params)
	{
		for ($i = 0; $i < $this->paramsNumber; $i++)	         
			$this->paramsSizes[$i] = $params[$i]['size'];		   
d18b535d   elena   catalog draft + c...
148
149
	}
	
c865df36   Elena.Budnik   dateTime datatype...
150
151
152
153
154
155
	public function setParamsTypes($params)
	{
		for ($i = 0; $i < $this->paramsNumber; $i++)	         
			$this->paramsTypes[$i] = $params[$i]['type'];		   
	}
	
4a438b99   elena   catalog draft
156
157
	public function writeBin($handle) 
	{
c865df36   Elena.Budnik   dateTime datatype...
158
159
160
161
162
163
164
165
166
		//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])));
d18b535d   elena   catalog draft + c...
167
	  
c865df36   Elena.Budnik   dateTime datatype...
168
169
		//Modified
		fwrite($handle,pack('L',$this->isModified));
d18b535d   elena   catalog draft + c...
170
	  
c865df36   Elena.Budnik   dateTime datatype...
171
172
		//Filter
		$this->filter->writeBin($handle);
d18b535d   elena   catalog draft + c...
173
	  
c865df36   Elena.Budnik   dateTime datatype...
174
175
		//Sort
		$this->sort->writeBin($handle);
d18b535d   elena   catalog draft + c...
176
	  
c865df36   Elena.Budnik   dateTime datatype...
177
178
		//Params Number
		fwrite($handle,pack('L',$this->paramsNumber));
d18b535d   elena   catalog draft + c...
179
	
c865df36   Elena.Budnik   dateTime datatype...
180
181
182
183
184
185
186
187
188
189
		//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));
d18b535d   elena   catalog draft + c...
190
191
	  

c865df36   Elena.Budnik   dateTime datatype...
192
193
		foreach($this->intervals as $interval)
			$interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes, $this->paramsTypes);
d18b535d   elena   catalog draft + c...
194
	  
c865df36   Elena.Budnik   dateTime datatype...
195
196
197
198
199
		//Indexes
		fwrite($handle,pack('L',count($this->indexes)));
		foreach($this->indexes as $index)
			fwrite($handle,pack('L',$index));
	}
d18b535d   elena   catalog draft + c...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    
    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'];
		}
c865df36   Elena.Budnik   dateTime datatype...
247
248
249
250
251
252
		//ParamsTypes
		for ($i = 0; $i < $this->paramsNumber; $i++) {
		  if (!$res = unpack('Ltype',fread($handle,4)))
			  return false;
		  $this->paramsTypes[$i] = $res['type'];
		}
d18b535d   elena   catalog draft + c...
253
254
255
256
257
258
259
260
		//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);
c865df36   Elena.Budnik   dateTime datatype...
261
			$interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);			
d18b535d   elena   catalog draft + c...
262
263
			array_push($this->intervals, $interval);
		}
4a438b99   elena   catalog draft
264
		
d18b535d   elena   catalog draft + c...
265
266
267
268
269
270
271
272
273
274
275
		//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;
	}
f9c8b272   elena   edit catalog
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
	
	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;
	}
d18b535d   elena   catalog draft + c...
306
307
308
309
310
}

class CatalogCacheMgr extends TimeTableCacheMgr 
{
		
f9c8b272   elena   edit catalog
311
	protected static $cache_file = "cacheCat";
d18b535d   elena   catalog draft + c...
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
	
	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);
c865df36   Elena.Budnik   dateTime datatype...
349
		$this->cache->setParamsTypes($paramHeaders);
d18b535d   elena   catalog draft + c...
350
351
352
353
354
355
356
		
		unset($intervals_res);		
		
		//Update cache
		$this->cache->updateIndexes();

		//Save cache file
901ba3f3   Elena.Budnik   upload catalog
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
		return array('success'=>$this->saveToFile(), 'token'=>$this->cache->getToken(), 
		             'status'=>$this->cache->getStatus(), 'parameters'=>$paramHeaders);
	}
	
	public function initFromUploadedFile($name, $format)
	{
		//Create new cache
		$this->cache = new CatalogCacheObject();

		//Load intervals from uploaded file
		$intervals_res = $this->ttMgr->getUploadedObject($name, $format);

		if (!isset($intervals_res))
			return array('success' => false, 'message' => 'Cannot get Uploaded 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);
c865df36   Elena.Budnik   dateTime datatype...
386
387
		$this->cache->setParamsTypes($paramHeaders);
		
901ba3f3   Elena.Budnik   upload catalog
388
389
390
391
392
393
394
395
		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);
d18b535d   elena   catalog draft + c...
396
397
	}
	
4a438b99   elena   catalog draft
398
399
	public function initFromTT($id, $typeTT) 
	{
e82af71b   elena   catalog draft
400
		//Create new cache
4a438b99   elena   catalog draft
401
		$this->cache = new CatalogCacheObject();
e82af71b   elena   catalog draft
402

4a438b99   elena   catalog draft
403
		//Load intervals from catalog file and add to cache
e82af71b   elena   catalog draft
404
405
406
407
408
409
410
411
		$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
412
			$this->cache->addInterval($interval['start'], $interval['stop'], $interval['paramTable']);
e82af71b   elena   catalog draft
413
414
		}
		
4a438b99   elena   catalog draft
415
		$paramHeaders = $intervals_res['parameters'];
f9c8b272   elena   edit catalog
416
	 
4a438b99   elena   catalog draft
417
418
		$this->cache->setParamsNumber(count($paramHeaders));
		$this->cache->setParamsSizes($paramHeaders);
c865df36   Elena.Budnik   dateTime datatype...
419
420
		$this->cache->setParamsTypes($paramHeaders);
		
e82af71b   elena   catalog draft
421
422
423
424
425
426
		unset($intervals_res);

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

		//Save cache file
4a438b99   elena   catalog draft
427
428
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(),
			     'status' => $this->cache->getStatus(),'parameters' => $paramHeaders);
e82af71b   elena   catalog draft
429
430
	}

d18b535d   elena   catalog draft + c...
431

4a438b99   elena   catalog draft
432
433
	protected function loadFromFile() 
	{	
d18b535d   elena   catalog draft + c...
434
435
436
437
438
439
440
441
442
443
444
445
		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
446
	return $result;
d18b535d   elena   catalog draft + c...
447
448
	}
	
4a438b99   elena   catalog draft
449
450
	protected function getCacheFilePath() 
	{
d18b535d   elena   catalog draft + c...
451
452
453
		return USERTTDIR.(self::$cache_file);
	}

4a438b99   elena   catalog draft
454
455
	public function saveInTT($id, $action, $token) 
	{	
d18b535d   elena   catalog draft + c...
456
457
458
459
460
461
462
463
464
465
466
467
		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...
468

4a438b99   elena   catalog draft
469
	return $this->ttMgr->saveIntervals($id, $intervals, $action);
f9c8b272   elena   edit catalog
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
	}
	
	public function addInterval($index, $start, $stop, $params) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		if (!isset($index))
			$index = 0;

		if (!isset($start))
			$start = date('Y-m-d\TH:i:s');

		if (!isset($stop))
			$stop = date('Y-m-d\TH:i:s');
			
		if (!isset($params))
			$params = [];		 		
		
		$this->cache->addInterval($start, $stop, $params, true, $index);
		
		//$this->cache->updateIndexes();
		
		$this->saveToFile();
		
		return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus());
	}
	
	public function modifyIntervalFromId($obj) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		$this->cache->modifyIntervalFromId($obj);
		
		$this->saveToFile();
		
		return array('success' => true, 'status' => $this->cache->getStatus());
	}
	
	public function initTTCache($nparams) {
		//Create new cache
		$this->cache = new CatalogCacheObject();
		$this->cache->setParamsNumber((int)$nparams);
		$paramHeaders = array();
		
		for ($i = 0; $i < (int)$nparams; $i++) {
			$paramHeaders[$i]['id'] = 'id_'.(string)($i+1);
			$paramHeaders[$i]['name'] = 'param_'.(string)($i+1);
			$paramHeaders[$i]['size'] = 1;
			$paramHeaders[$i]['type'] = 'Float';
			
		}
		$this->cache->setParamsSizes($paramHeaders);
c865df36   Elena.Budnik   dateTime datatype...
522
		$this->cache->setParamsTypes($paramHeaders);
f9c8b272   elena   edit catalog
523
524
525
526
		//Save cache file
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(),
			     'status' => $this->cache->getStatus(), 'parameters' => $paramHeaders);
	}
d18b535d   elena   catalog draft + c...
527
528
   }
?>