Esempio n. 1
0
 public function getGroupsByReport($reportId, $params = [])
 {
     $limit = isset($params['limit']) ? $params['limit'] : 0;
     $skip = isset($params['skip']) ? $params['skip'] : 0;
     if (isset($params['limit'])) {
         $query = \CouchbaseViewQuery::from('family', 'by_report')->stale(1)->key($reportId)->limit($limit)->skip($skip);
     } else {
         $query = \CouchbaseViewQuery::from('family', 'by_report')->stale(1)->key($reportId);
     }
     $result = [];
     try {
         $res = $this->cb->query($query, null, true);
         if (!empty($res)) {
             $result['items'] = [];
             $count = 0;
             foreach ($res['rows'] as $item) {
                 $result['items'][] = $item['value'];
                 $count++;
             }
             $result['totalRecords'] = $count;
         }
     } catch (\CouchbaseException $e) {
         $result['error'] = $e->getMessage();
     }
     return $result;
 }
Esempio n. 2
0
 public function getUsername($username)
 {
     $query = \CouchbaseViewQuery::from('person', 'username')->stale(1)->key($username);
     $person = [];
     try {
         $res = $this->cb->query($query, false, true);
         if (!empty($res)) {
             foreach ($res['rows'] as $item) {
                 $person = $item['value'];
             }
         }
     } catch (\CouchbaseException $e) {
         $person['error'] = $e->getMessage();
     }
     return $person;
 }
 /**
  * Purge CatalogSearch Cache Keys
  *
  * @param array tags
  * @return array of cache key ids (couchbase ids)
  */
 public function purgeCatalogSearchKeys($tags = array())
 {
     $status = true;
     $cache_key_ids = array();
     $size_tag_array = count($tags);
     $size_tag_ceil = 0;
     if ($size_tag_array > 0) {
         $designDoc = "cache";
         $designView = "all_search_tags";
         $docs = $this->_bucketTags->manager()->getDesignDocuments();
         if (isset($docs[$designDoc])) {
             if ($size_tag_array > $this->_tagSize) {
                 $size_tag_ceil = ceil($size_tag_array / $this->_tagSize);
             }
             //Reindex array
             $tags_reindexed_array = array_values($tags);
             if ($size_tag_ceil === 0) {
                 foreach ($tags_reindexed_array as $tag) {
                     $finished = false;
                     //Get First Key
                     $query = CouchbaseViewQuery::from($designDoc, $designView)->key($tag)->order(CouchbaseViewQuery::ORDER_ASCENDING)->limit(1);
                     $result = $this->_bucketTags->query($query);
                     if (count($result['rows']) > 0) {
                         $startKey = $result['rows'][0]['value'];
                         $endKey = null;
                         while (!$finished) {
                             if ($endKey) {
                                 $query = CouchbaseViewQuery::from($designDoc, $designView)->key($tag)->order(CouchbaseViewQuery::ORDER_ASCENDING)->limit($this->_viewPaginationLimit)->range($startKey, $endKey, true);
                             } else {
                                 $query = CouchbaseViewQuery::from($designDoc, $designView)->key($tag)->order(CouchbaseViewQuery::ORDER_ASCENDING)->limit($this->_viewPaginationLimit);
                             }
                             $result = $this->_bucketTags->query($query);
                             $countRows = count($result['rows']);
                             // No results means we are done here
                             if (0 === $countRows) {
                                 break;
                             }
                             // We can try to calculate if this is the last page based on the number of results
                             if ($countRows <= $this->_viewPaginationLimit) {
                                 $finished = true;
                             }
                             // Loop through rows in this paginated result set
                             $numLoops = min($countRows, $this->_viewPaginationLimit);
                             for ($i = 0; $i < $numLoops; $i++) {
                                 foreach ($result['rows'][$i] as $row) {
                                     if (strpos($row['key'], $row['value']) !== false) {
                                         $search_cache_tag = $row['value'];
                                         $search_cache_key = str_replace('_CATALOG_SEARCH', '', $search_cache_tag);
                                         $cache_key_ids[] = $search_cache_key;
                                     }
                                 }
                             }
                             // Limit is $this->_viewPaginationLimit + 1 so on 0-indexed array a key of $this->_viewPaginationLimit means we fulfilled that limit
                             if (array_key_exists($this->_viewPaginationLimit, $result['rows'])) {
                                 $endKey = $result['rows'][$this->_viewPaginationLimit]['key'];
                             }
                         }
                     }
                 }
             } else {
                 for ($j = 0; $j < $size_tag_ceil; $j++) {
                     $temp_tag = array();
                     for ($i = 0; $i < $this->_tagSize; $i++) {
                         $iterator = $i + $j * $this->_tagSize;
                         if ($iterator < count($tags)) {
                             $temp_tag[] = $tags_reindexed_array[$iterator];
                         }
                         foreach ($temp_tag as $tag) {
                             $finished = false;
                             //Get First Key
                             $query = CouchbaseViewQuery::from($designDoc, $designView)->key($tag)->order(CouchbaseViewQuery::ORDER_ASCENDING)->limit(1);
                             $result = $this->_bucketTags->query($query);
                             if (count($result['rows']) > 0) {
                                 $startKey = $result['rows'][0]['value'];
                                 $endKey = null;
                                 while (!$finished) {
                                     if ($endKey) {
                                         $query = CouchbaseViewQuery::from($designDoc, $designView)->key($tag)->order(CouchbaseViewQuery::ORDER_ASCENDING)->limit($this->_viewPaginationLimit)->range($startKey, $endKey, true);
                                     } else {
                                         $query = CouchbaseViewQuery::from($designDoc, $designView)->key($tag)->order(CouchbaseViewQuery::ORDER_ASCENDING)->limit($this->_viewPaginationLimit);
                                     }
                                     $result = $this->_bucketTags->query($query);
                                     $countRows = count($result['rows']);
                                     // No results means we are done here
                                     if (0 === $countRows) {
                                         break;
                                     }
                                     // We can try to calculate if this is the last page based on the number of results
                                     if ($countRows <= $this->_viewPaginationLimit) {
                                         $finished = true;
                                     }
                                     // Loop through rows in this paginated result set
                                     $numLoops = min($countRows, $this->_viewPaginationLimit);
                                     for ($i = 0; $i < $numLoops; $i++) {
                                         foreach ($result['rows'][$i] as $row) {
                                             if (strpos($row['key'], $row['value']) !== false) {
                                                 $search_cache_tag = $row['value'];
                                                 $search_cache_key = str_replace('_CATALOG_SEARCH', '', $search_cache_tag);
                                                 $cache_key_ids[] = $search_cache_key;
                                             }
                                         }
                                     }
                                     // Limit is $this->_viewPaginationLimit + 1 so on 0-indexed array a key of $this->_viewPaginationLimit means we fulfilled that limit
                                     if (array_key_exists($this->_viewPaginationLimit, $result['rows'])) {
                                         $endKey = $result['rows'][$this->_viewPaginationLimit]['key'];
                                     }
                                 }
                             }
                         }
                     }
                     //Too many tags at once, flush and cycle to the next lot.
                 }
                 if (count($cache_key_ids) > 0) {
                     $removeFPC = $this->_bucket->remove($cache_key_ids);
                     $removeTags = $this->cleanTags($cache_key_ids);
                     $status = $removeFPC & $removeTags;
                 }
             }
         }
     }
     return $status;
 }
 public function toQuery()
 {
     $query = CouchbaseViewQuery::from($this->designDocumentName, $this->viewName);
     if (!is_null($this->offset)) {
         $query = $query->skip($this->offset);
     }
     if (!is_null($this->limit)) {
         $query = $query->limit($this->limit);
     }
     if (!is_null($this->stale)) {
         $query = $query->stale($this->stale === false ? 'false' : $this->stale);
     }
     $custom = array();
     if (!is_null($this->descending)) {
         $custom['descending'] = $this->descending ? 'true' : 'false';
     }
     if (!is_null($this->startKeyDocId)) {
         $custom['startkey_docid'] = $this->startKeyDocId;
     }
     if (!is_null($this->endKeyDocId)) {
         $custom['endkey_docid'] = $this->endKeyDocId;
     }
     if (!is_null($this->group)) {
         $custom['group'] = $this->group ? 'true' : 'false';
     }
     if (!is_null($this->groupLevel)) {
         $custom['group_level'] = $this->groupLevel;
     }
     if (!is_null($this->inclusiveEnd)) {
         $custom['inclusive_end'] = $this->inclusiveEnd ? 'true' : 'false';
     }
     if (!is_null($this->reduce)) {
         $custom['reduce'] = $this->reduce ? 'true' : 'false';
     }
     if (!is_null($this->connectionTimeout)) {
         $custom['connection_timeout'] = $this->connectionTimeout;
     }
     if (count($this->startKey)) {
         $custom['startkey'] = json_encode(array_values($this->startKey));
     }
     if (count($this->endKey)) {
         $custom['endkey'] = json_encode(array_values($this->endKey));
     }
     if (is_array($this->key) && count($this->key)) {
         $custom['key'] = json_encode(array_values($this->key));
     } elseif ($this->key) {
         $custom['key'] = json_encode($this->key);
     }
     if (count($this->keys)) {
         $custom['keys'] = json_encode(array_values($this->keys));
     }
     if (count($custom)) {
         $query = $query->custom($custom);
     }
     if (!is_null($this->offset) && !is_null($this->limit)) {
         $this->offset += $this->limit;
     }
     return $query;
 }
Esempio n. 5
0
 /**
  * Migrate existing mysql db to couchbase
  *
  */
 public function index()
 {
     $myCluster = new \CouchbaseCluster('couchbase://localhost');
     $myBucket = $myCluster->openBucket('5sportal');
     $tables = DB::select('show tables');
     if (!empty($tables)) {
         foreach ($tables as $table) {
             $table = array_values(get_object_vars($table))[0];
             $data = $this->getData($table);
             if (!empty($data)) {
                 foreach ($data as $row) {
                     $item = (array) $row;
                     $docId = $table . '_' . $item['id'];
                     //cast int data type
                     foreach ($item as $field => $val) {
                         if (is_numeric($val)) {
                             $item[$field] = (int) $val;
                         }
                     }
                     if ($table == 'person') {
                         $item['role'] = $item['type'];
                         $item['type'] = $table;
                         $myBucket->upsert($docId, $item);
                     } elseif ($table == 'report') {
                         $item['report_type'] = $item['type'];
                         $item['type'] = $table;
                         $myBucket->upsert($docId, $item);
                     } elseif ($table == 'family') {
                         //do nothing
                     } else {
                         $item['type'] = $table;
                         $myBucket->upsert($docId, $item);
                     }
                     echo 'Inserting document: ' . $docId . '<br/>';
                 }
             }
         }
         $members = $this->processReportMembers();
         //delete existing family in the document
         $query = \CouchbaseViewQuery::from('family', 'by_report')->stale(1);
         $res = $myBucket->query($query, null, true);
         foreach ($res['rows'] as $row) {
             try {
                 $myBucket->remove($row['id']);
             } catch (\CouchbaseException $e) {
                 echo $e->getMessage() . "<br/>";
                 continue;
             }
         }
         foreach ($members as $id => $val) {
             $tmp = explode('_', $id);
             $item = ['type' => 'family', 'report_id' => (int) end($tmp), 'members' => $val];
             $myBucket->upsert($id, $item);
         }
         $tags = $this->processItemTags();
         //delete existing tags in the document
         $query = \CouchbaseViewQuery::from('item', 'item_tags')->stale(1);
         $res = $myBucket->query($query, null, true);
         foreach ($res['rows'] as $row) {
             try {
                 $myBucket->remove($row['id']);
             } catch (\CouchbaseException $e) {
                 echo $e->getMessage() . "<br/>";
                 continue;
             }
         }
         foreach ($tags as $id => $val) {
             $tmp = explode('_', $id);
             $item = ['type' => 'tags', 'item_id' => (int) end($tmp), 'tags' => $val];
             $myBucket->upsert($id, $item);
         }
     }
     return 'Setup done.';
 }