Campustream 1.0
A social network MQP for WPI
application/lib/PhlockDB/Phlock/Cursor.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class Phlock_Cursor implements Iterator {
00004         
00005         private $client;
00006         private $term;
00007         private $cursor = -1;  // IMPORTANT: starting cursor
00008         private $result = null;
00009         
00010         private $page;
00011         private $page_size;
00012         private $page_marker;
00013         
00014         public function __construct(FlockDBClient $client, Phlock_QueryTerm $term) {
00015                 $this->client = $client;
00016                 $this->term = $term;
00017         }
00018         
00019         public function size() {
00020                 $result = $this->client->count2(array($this->toThrift()));
00021                 $unpack = unpack('V*', $result);  // one-based array
00022                 return $unpack[1];
00023         }
00024         
00025         public function current() {
00026                 if ($this->page_marker >= $this->page_size) {
00027                         $this->nextPage();
00028                         $this->getResult();
00029                 }
00030                 return $this->page[$this->page_marker];
00031         }
00032         
00033         public function key() {
00034                 return "$this->cursor:$this->page_marker";
00035         }
00036         
00037         public function next() {
00038                 $this->page_marker++;
00039         }
00040         
00041         public function rewind() {
00042                 $cursor = -1;
00043                 $result = null;
00044         }
00045         
00046         public function valid() {
00047                 $this->getResult();
00048                 return $this->page_marker < $this->page_size || $this->hasNextPage();
00049         }
00050         
00051         public function currentPage() {
00052                 $result = $this->getResult();
00053                 return $this->page;
00054         }
00055         
00056         public function hasNextPage() {
00057                 return (bool)$this->getResult()->next_cursor;
00058         }
00059         
00060         public function nextPage() {
00061                 $this->cursor = $this->getResult()->next_cursor;
00062                 $this->result = null;
00063         }
00064         
00065         private function getResult() {
00066                 if ($this->result === null) {
00067                         $this->result = $this->query();
00068                         $this->page = $this->unpackResultIds($this->result->ids);
00069                         $this->page_size = count($this->page);
00070                         $this->page_marker = 0;
00071                 }
00072                 return $this->result;
00073         }
00074         
00075         private function query() {
00076                 $page = new Flock_Page(array(
00077                         'count'=>10,
00078                         'cursor'=>$this->cursor
00079                 ));
00080                 $query = new Flock_SelectQuery(array(
00081                         'operations'=>$this->toThrift(),
00082                         'page'=>$page
00083                 ));
00084                 $results = $this->client->select2(array($query));
00085                 return $results[0];
00086         }
00087         
00088         public function toThrift() {
00089                 $operation = new Flock_SelectOperation(array(
00090                         'operation_type'=>Flock_SelectOperationType::SimpleQuery,
00091                         'term'=>$this->term->toThrift()
00092                 ));
00093                 return array($operation);
00094         }
00095         
00096         public function unpackResultIds($ids) {
00097                 $results = array();
00098                 $i = 0;
00099                 $i64 = $shift = 0;
00100                 while (isset($ids[$i])) {
00101                         $i64 += ord($ids[$i]) << $shift;
00102                         $shift += 8;
00103                         if ($shift === 64) {
00104                                 $results[] = $i64;
00105                                 $i64 = $shift = 0;
00106                         }
00107                         $i++;
00108                 }
00109                 return $results;
00110         }
00111 }