Changeset 17862 for extensions/EStat/lib


Ignore:
Timestamp:
Sep 11, 2012, 3:27:24 PM (12 years ago)
Author:
grum
Message:

version 0.1.0b
. fix bug on thumb view
. fix SQLite compression managment & 'compress.method' config
. manage number of items per page by config

Location:
extensions/EStat/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/EStat/lib/statDB.class.inc.php

    r17760 r17862  
    99define('ASDF_EXIST_PACKED',   0x01);
    1010define('ASDF_EXIST_UNPACKED', 0x02);
     11define('ASDF_EXIST_LOCK',     0x04);
    1112define('ASDF_EXIST_ALL', ASDF_EXIST_PACKED|ASDF_EXIST_UNPACKED);
    1213
    1314define('ASDF_FILE_EXT', '.db');
    1415define('ASDF_FILE_EXT_ZIP', ASDF_FILE_EXT.'.gz');
     16define('ASDF_FILE_EXT_LOCK', '.lock');
    1517define('ASDF_FILE_ROOT_MONTH', 'M');
    1618define('ASDF_FILE_ROOT_GLOBAL', 'G');
     
    5557  protected $transacOpen=false;
    5658  protected $ipCountryFile='';
    57 
     59  protected $lckFileNumber=0;
     60  //protected $logs=''; for debug
    5861  private $deleteUnpackedWhenClose=ASDF_CLOSE_DO_NOTHING;
    5962
     
    218221   * if $mode=ASDF_OPEN_WRITE and file doesn't exist, create it; if a packed file
    219222   * exist, try to unpack the packed file; in this case the unpacked file will be
    220    * packed and removed automatically when the process will close.
     223   * packed and removed automatically when the process will close
    221224   *
    222225   * if $mode=ASDF_OPEN_READ, and unpacked file doesn't exist, try to unpack
    223226   * packed file (if exists); in this case the unpacked file will be removed
    224    * automatically when the process will close.
     227   * automatically when the process will close (if there's no lock
     228   * files linked to the uncompressed file); a new lock file is created
    225229   *
    226230   * @param Integer $mode: READ or WRITE
     
    230234  {
    231235    $dbFile=$this->getFileName(ASDF_EXIST_UNPACKED, true);
     236    $this->createLockFile();
     237    $this->waitForUnpackedFile();
    232238
    233239    /*
     
    252258        $this->deleteUnpackedWhenClose=ASDF_CLOSE_DO_DELETE; // the unpacked file will be deleted when the process will close
    253259      }
     260    }
     261    else
     262    {
     263      if($this->lckFileNumber>1 and $this->getFileName(ASDF_EXIST_PACKED, true)!='' and $mode==ASDF_OPEN_READ)
     264        $this->deleteUnpackedWhenClose=ASDF_CLOSE_DO_DELETE;
    254265    }
    255266
     
    293304      }
    294305
    295       if(($this->deleteUnpackedWhenClose&ASDF_CLOSE_DO_DELETE)==ASDF_CLOSE_DO_DELETE)
     306      $this->deleteLockFile();
     307      if(($this->deleteUnpackedWhenClose&ASDF_CLOSE_DO_DELETE)==ASDF_CLOSE_DO_DELETE and
     308          !$this->fileIsLocked())
    296309      {
    297310        $this->delete(ASDF_DELETE_UNPACKED);
     
    330343    if($this->fileExist() & ASDF_EXIST_PACKED==ASDF_EXIST_PACKED)
    331344    {
    332       $returned=GPCCompress::gunzip($this->getFileName(ASDF_EXIST_PACKED), $this->getFileName(ASDF_EXIST_UNPACKED));
    333       /*
    334       $dir=dirName(dirName($this->getFileName(ASDF_EXIST_UNPACKED)));
    335       $tH=fopen($dir.'/logs.log', 'a');
    336       if($tH)
    337       {
    338         $dest=$this->getFileName(ASDF_EXIST_UNPACKED);
    339         fwrite($tH, sprintf("%s - %30s - %10d - %s\n", date('Y-m-d H:i:s'), $dest, filesize($dest), $returned?'y':'n'));
    340         fclose($tH);
    341       }
    342 */
     345      $unpackedFile=$this->getFileName(ASDF_EXIST_UNPACKED);
     346
     347      // create a temp file
     348      $fHandle=fopen($unpackedFile, 'w');
     349      fclose($fHandle);
     350
     351      $returned=GPCCompress::gunzip($this->getFileName(ASDF_EXIST_PACKED), $unpackedFile.'.tmp');
     352
     353      unlink($unpackedFile);
     354      if($returned)
     355      {
     356        rename($unpackedFile.'.tmp', $unpackedFile);
     357      }
     358
    343359      return($returned);
    344360    }
     
    422438      case ASDF_EXIST_UNPACKED:
    423439        $fileName=$this->fileDir.$this->fileName.$this->fileRootName.ASDF_FILE_EXT;
     440        break;
     441      case ASDF_EXIST_LOCK:
     442        $fileName=$this->fileDir.$this->fileName.$this->fileRootName.'-'.$this->lckFileNumber.ASDF_FILE_EXT_LOCK;
    424443        break;
    425444    }
     
    517536    if($returned==null) $returned=$value;
    518537    return($returned);
     538  }
     539
     540  /**
     541   * how the file lock system works:
     542   *
     543   * the lock system is used only when for uncompressed files, allowing to know
     544   * if an uncompressed file can be deleted or not; locks function are called only
     545   * in READ mode by function open() and close()
     546   *
     547   * createLockFile()
     548   *  1/ Search for existing lock files
     549   *  2/ If files are found, increase the $lckFileNumber with number of existing files + 1
     550   *  3/ Create the lock file
     551   *
     552   * deleteLockFile()
     553   *  delete the lock file associated with the current $lckFileNumber
     554   *
     555   * fileIsLocked()
     556   *  return true if at least, one lock exists
     557   *
     558   *  ......................................
     559   *          ......................................
     560   *                   ..............
     561   * -+-------+--------+------------+------+-------+-->
     562   * (A)     (B)      (C)          (D)    (E)     (F)
     563   *  ^       ^        ^            ^      ^       ^
     564   *  p1      p2       p3           p3     p1      p2
     565   *  start   start    start        end    end     end
     566   *
     567   * (A) process 1 start: createLockFile() => lock file '1' is created
     568   * (B) process 2 start: createLockFile() => lock file '2' is created
     569   * (C) process 3 start: createLockFile() => lock file '3' is created
     570   * (D) process 3 end:   deleteLockFile() => lock file '3' is deleted
     571   *                                          lock files '1' & '2' still exist (uncompressed SQLite file can't be deleted)
     572   * (E) process 1 end:   deleteLockFile() => lock file '1' is deleted
     573   *                                          lock file '2' still exist (uncompressed SQLite file can't be deleted)
     574   * (F) process 2 end:   deleteLockFile() => lock file '2' is deleted
     575   *                                          there's no lock files (uncompressed SQLite file can be deleted)
     576   *
     577   * @return Boolean: true if the lock file was created, otherwise false
     578   */
     579  protected function createLockFile()
     580  {
     581    $this->lckFileNumber=$this->getNumberOfLockFiles()+1;
     582    $fHandle=fopen($this->getFileName(ASDF_EXIST_LOCK), 'w');
     583    if($fHandle)
     584    {
     585      fclose($fHandle);
     586      return(true);
     587    }
     588    $this->lckFileNumber=0;
     589    return(false);
     590  }
     591
     592  /**
     593   * delete the current lock file
     594   * return true if file is deleted (or file doesn't exists => there's no more file to delete...), otherwise return false
     595   *
     596   * the function also delete all lock files older than current timestamp+max_execution_time (if
     597   * the file still exist, it means that a problem has occured at a moment)
     598   *
     599   * @return Boolean
     600   */
     601  protected function deleteLockFile()
     602  {
     603    $returned=true;
     604    $file=$this->getFileName(ASDF_EXIST_LOCK);
     605    if(file_exists($file)) $returned=unlink($file);
     606
     607    if($returned) $this->lckFileNumber=0;
     608
     609    $regExp="/\\".ASDF_FILE_EXT_LOCK.'/i';
     610    scandir(dirName($this->fileDir));
     611    $files=scandir($this->fileDir);
     612    $timeOut=time()-ini_get('max_execution_time');
     613
     614    foreach($files as $file)
     615    {
     616      if(preg_match($regExp, $file, $result)>0)
     617      {
     618        if(filemtime($this->fileDir.$file)<$timeOut)
     619        {
     620          unlink($this->fileDir.$file);
     621        }
     622      }
     623    }
     624
     625
     626
     627    return($returned);
     628  }
     629
     630  /**
     631   * return true if a lock file exists
     632   *
     633   * @return Boolean
     634   */
     635  protected function fileIsLocked()
     636  {
     637    if($this->getNumberOfLockFiles()>0) return(true);
     638    return(false);
     639  }
     640
     641  /**
     642   * return true the number of existing lock files
     643   *
     644   * @return Integer: number of existing lock files
     645   */
     646  protected function getNumberOfLockFiles()
     647  {
     648    $returned=0;
     649    $regExp='/'.$this->fileName.$this->fileRootName."-(\d+)\\".ASDF_FILE_EXT_LOCK.'/i';
     650    $files=scandir($this->fileDir);
     651    foreach($files as $file)
     652    {
     653      if(preg_match($regExp, $file, $result)>0)
     654      {
     655        $tmp=$result[1]*1;
     656        if($tmp>$returned) $returned=$tmp;
     657      }
     658    }
     659    return($returned);
     660  }
     661
     662  /**
     663   * if the unpacked exist with a '.tmp' extension, assume that an unpacking process
     664   * is running and loop until the file doesn't exist anymore
     665   *
     666   * @param Integer $maxWait: maximum wait time in millisecond...
     667   * @return Boolean: true if file was unpacked in the waiting time, otherwise false
     668   */
     669  protected function waitForUnpackedFile($maxWait=2000)
     670  {
     671    $tmpFile=$this->getFileName(ASDF_EXIST_UNPACKED).'.tmp';
     672    $maxTimer=microtime(true)+$maxWait/1000;
     673    while(file_exists($tmpFile))
     674    {
     675      if(microtime(true)>=$maxTimer) return(false);
     676      usleep(5000); //wait 5milliseconds before next check
     677    }
     678    return(true);
    519679  }
    520680
     
    11901350
    11911351
    1192 }
     1352/*
     1353  for debug
     1354
     1355  private function log($data)
     1356  {
     1357    if($this->logs=='') return(false);
     1358    $tH=fopen(dirName($this->fileDir).'/logs.log', 'a');
     1359    if($tH)
     1360    {
     1361      $time=microtime(true);
     1362      fwrite($tH, sprintf("%s %f - %s\n", $this->logs, $time, $data));
     1363
     1364      fclose($tH);
     1365    }
     1366  }
     1367*/
     1368
     1369
     1370} // StatDB
    11931371
    11941372
     
    12131391
    12141392
    1215 
    12161393?>
  • extensions/EStat/lib/statDBGlobal.class.inc.php

    r17758 r17862  
    240240    if($packed!='')
    241241    {
     242      // packed file was unpacked, pack file only if file was already packed
    242243      if($fmPeriod->pack())
    243         $fmPeriod->delete(ASDF_DELETE_UNPACKED); //packed file was unpacked, keep only packed file
     244        $fmPeriod->delete(ASDF_DELETE_UNPACKED);
    244245    }
    245246    return($returned);
Note: See TracChangeset for help on using the changeset viewer.