Advertisement
Guest User

Untitled

a guest
May 30th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <?php
  2.  
  3. $cb = new Couchbase(array(getenv('PH_CB_HOST')), getenv('PH_CB_USER'), getenv('PH_CB_PASSWORD'), getenv('PH_CB_DB'), true);
  4.  
  5. $testUsuario = "test_channel";
  6.  
  7. $consDocs = $cb->view("dev_test_andr.ddoc","por_canal",array("key"=>$testUsuario, "full_set"=>true));
  8.  
  9. // "por_canal" emits document id based on specific channels
  10.  
  11. echo "Compactando documentos para Canal: {$testUsuario}\n\n";
  12.  
  13. $i = 0;
  14.  
  15. mkdir($testUsuario);
  16.  
  17. foreach($consDocs['rows'] as $datoFila){
  18.  
  19. $doc = $cb->get($datoFila['id']);
  20. // This backups the document prior to compaction
  21. file_put_contents($testUsuario . "/" . $datoFila['id'].".txt", $doc);
  22.  
  23.  
  24. compactarDoc($datoFila['id'], $cb);
  25.  
  26. $i++;
  27.  
  28. if(count($consDocs['rows']) > 0)
  29. $compl = number_format(($i / count($consDocs['rows'])) * 100, 0);
  30. else $compl = 0;
  31.  
  32. echo "\rCompletado: {$i} ({$compl} %)";
  33.  
  34. }
  35. echo "\n";
  36.  
  37. function compactarDoc($docId, $cb){
  38.  
  39. $obj = json_decode($cb->get($docId));
  40.  
  41. $rev_test = $obj->_sync->rev;
  42.  
  43. $_tmp_revs = array();
  44. $_tmp_parents = array();
  45. $_tmp_bodies = array();
  46. $_tmp_pre_deleted = array();
  47. $_tmp_deleted = array();
  48. $_tmp_channels = array();
  49.  
  50. $_tmp_raw_revs = array();
  51.  
  52. foreach($obj->_sync->history->revs as $id_rev => $_nrev){
  53. $_tmp_raw_revs[] = $_nrev;
  54. if(isset($obj->_sync->history->deleted) && in_array($id_rev, $obj->_sync->history->deleted))
  55. $_tmp_pre_deleted[$_nrev] = 0;
  56. }
  57.  
  58. usort($_tmp_raw_revs, function($a, $b){
  59. $tmpa = explode("-", $a);
  60. $tmpb = explode("-", $b);
  61.  
  62. return (intval($tmpa[0]) < intval($tmpb[0]))? 1: -1;
  63.  
  64. });
  65.  
  66. $tmpArr = array_reverse(array_slice($_tmp_raw_revs, 0, 20));
  67.  
  68. for($i = 0; $i < count($tmpArr); $i++){
  69.  
  70. $_tmp_revs[$i] = $tmpArr[$i];
  71. $_tmp_parents[$i] = $i - 1;
  72. $_tmp_bodies[$i] = "";
  73. if(isset($_tmp_pre_deleted[$tmpArr[$i]]))
  74. $_tmp_deleted[] = $i;
  75. }
  76.  
  77. $_tmp_channels = array_slice($obj->_sync->history->channels, 0, count($tmpArr));
  78.  
  79. $obj->_sync->history->revs = $_tmp_revs;
  80. $obj->_sync->history->parents = $_tmp_parents;
  81. $obj->_sync->history->bodies = $_tmp_bodies;
  82. $obj->_sync->history->deleted = $_tmp_deleted;
  83. $obj->_sync->history->channels = $_tmp_channels;
  84.  
  85. $cb->set($docId, json_encode($obj));
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement