/**
  * Update an existing edge in a graph, identified by graph name and edge id
  *
  * This will update the edge on the server
  *
  * This will throw if the edge cannot be updated
  *
  * If policy is set to error (locally or globally through the ConnectionOptions)
  * and the passed edge-document has a _rev value set, the database will check
  * that the revision of the to-be-replaced document is the same as the one given.<br><br>
  *
  * @throws Exception
  *
  * @param mixed  $graph     - graph name as a string or instance of Graph
  * @param mixed  $edgeId    - edge id as string or number
  * @param mixed  $label     - label for the edge or ''
  * @param Edge   $document  - patch edge-document which contains the attributes and values to be updated
  * @param mixed  $options   optional, array of options (see below)
  * <p>
  * <li><b>policy</b> - update policy to be used in case of conflict ('error', 'last' or NULL [use default])</li>
  * <li><b>keepNull</b> - can be used to instruct ArangoDB to delete existing attributes instead setting their values to null. Defaults to true (keep attributes when set to null)</li>
  * <li><b>waitForSync</b> - can be used to force synchronisation of the document update operation to disk even in case that the waitForSync flag had been disabled for the entire collection</li>
  * </p>
  * @param string $collection  - if one uses a graph with more than one vertex collection one must provide the collection                          
  *
  * @return bool - always true, will throw if there is an error
  * @since 1.2
  */
 public function updateEdge($graph, $edgeId, $label, Edge $document, $options = array(), $collection = null)
 {
     if ($graph instanceof Graph) {
         $graph = $graph->getKey();
     }
     $parts = explode("/", $edgeId);
     if (count($parts) === 2) {
         $edgeId = $parts[1];
         $collection = $parts[0];
     }
     if (count($this->getEdgeCollections($graph)) !== 1 && $collection === null) {
         throw new ClientException('A collection must be provided.');
     } else {
         if (count($this->getEdgeCollections($graph)) === 1 && $collection === null) {
             $collection = $this->getEdgeCollections($graph);
             $collection = $collection[0];
         }
     }
     $options = array_merge(array(self::OPTION_REVISION => false), $options);
     // This preserves compatibility for the old policy parameter.
     $params = array();
     $params = $this->validateAndIncludeOldSingleParameterInParams($options, $params, ConnectionOptions::OPTION_UPDATE_POLICY);
     $params = $this->includeOptionsInParams($options, $params, array('waitForSync' => $this->getConnectionOption(ConnectionOptions::OPTION_WAIT_SYNC), 'keepNull' => true));
     $policy = null;
     //Include the revision for conditional updates if required
     if ($options[self::OPTION_REVISION] === true) {
         $revision = $document->getRevision();
         if (!is_null($revision)) {
             $params[ConnectionOptions::OPTION_REVISION] = $revision;
         }
     } elseif ($options[self::OPTION_REVISION]) {
         $params[ConnectionOptions::OPTION_REVISION] = $options[self::OPTION_REVISION];
     }
     if (!is_null($label)) {
         $document->set('$label', $label);
     }
     $url = UrlHelper::buildUrl(Urls::URL_GRAPH, array($graph, Urls::URLPART_EDGE, $collection, $edgeId));
     $url = UrlHelper::appendParamsUrl($url, $params);
     $result = $this->getConnection()->patch($url, $this->json_encode_wrapper($document->getAll()));
     $json = $result->getJson();
     $edge = $json['edge'];
     $document->setRevision($edge[Edge::ENTRY_REV]);
     return true;
 }
 /**
  * test for replacing a edge using replace()
  */
 public function testReplaceEdge()
 {
     $connection = $this->connection;
     $edgeHandler = new EdgeHandler($connection);
     $edgeCollection = $this->edgeCollection;
     $document1 = new Document();
     $document2 = new Document();
     $documentHandler = new DocumentHandler($connection);
     $edgeDocument = new Edge();
     $document1->someAttribute = 'someValue1';
     $document2->someAttribute = 'someValue2';
     $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document1);
     $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document2);
     $documentHandle1 = $document1->getHandle();
     $documentHandle2 = $document2->getHandle();
     $edgeDocument->set('label', null);
     $edgeDocument->set('labelt', "as");
     $edgeId = $edgeHandler->saveEdge($edgeCollection->getName(), $documentHandle1, $documentHandle2, $edgeDocument);
     $this->assertTrue(is_numeric($edgeId), 'Did not return an id!');
     $edgePutDocument = new Edge();
     $edgePutDocument->set('_id', $edgeDocument->getHandle());
     $edgePutDocument->set('_rev', $edgeDocument->getRevision());
     $edgePutDocument->set('labels', "as");
     $result = $edgeHandler->replace($edgePutDocument);
     $this->assertTrue($result);
     $resultingEdge = $edgeHandler->get($edgeCollection->getId(), $edgeId);
     $this->assertObjectHasAttribute('_id', $resultingEdge, '_id field should exist, empty or with an id');
     $this->assertTrue($resultingEdge->label == null, 'Should be :null, is: ' . $resultingEdge->label);
     $this->assertTrue($resultingEdge->labelt == null, 'Should be :null, is: ' . $resultingEdge->labelt);
     $this->assertTrue($resultingEdge->labels == "as");
     $response = $edgeHandler->delete($resultingEdge);
     $this->assertTrue($response, 'Delete should return true!');
 }
 public function testCreateMixedBatchWithPartIds()
 {
     $edgeCollection = $this->edgeCollection;
     $batch = new Batch($this->connection);
     $this->assertInstanceOf('\\triagens\\ArangoDb\\Batch', $batch);
     // Create collection
     $connection = $this->connection;
     $collection = new Collection();
     $collectionHandler = new CollectionHandler($connection);
     $name = 'ArangoDB_PHP_TestSuite_TestCollection_02';
     $collection->setName($name);
     $batch->nextBatchPartId('testCollection1');
     $response = $collectionHandler->add($collection);
     $this->assertTrue(is_numeric($response), 'Did not return a fake numeric id!');
     $batch->process();
     $resultingCollectionId = $batch->getProcessedPartResponse('testCollection1');
     $testCollection1Part = $batch->getPart('testCollection1');
     $this->assertTrue($testCollection1Part->getHttpCode() == 200, 'Did not return an HttpCode 200!');
     $resultingCollection = $collectionHandler->get($batch->getProcessedPartResponse('testCollection1'));
     $resultingAttribute = $resultingCollection->getName();
     $this->assertTrue($name === $resultingAttribute, 'The created collection name and resulting collection name do not match!');
     $this->assertEquals(Collection::getDefaultType(), $resultingCollection->getType());
     $batch = new Batch($this->connection);
     // Create documents
     $documentHandler = $this->documentHandler;
     $batch->nextBatchPartId('doc1');
     $document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
     $documentId = $documentHandler->add($resultingCollectionId, $document);
     $this->assertTrue(is_numeric($documentId), 'Did not return a fake numeric id!');
     for ($i = 0; $i <= 10; ++$i) {
         $document = Document::createFromArray(array('someAttribute' => 'someValue' . $i, 'someOtherAttribute' => 'someOtherValue2' . $i));
         $documentId = $documentHandler->add($resultingCollectionId, $document);
     }
     $this->assertTrue(is_numeric($documentId), 'Did not return a fake numeric id!');
     $batch->process();
     // try getting processed response through batchpart
     $testDocument1PartResponse = $batch->getPart('doc1')->getProcessedResponse();
     // try getting it from batch
     $testDocument2PartResponse = $batch->getProcessedPartResponse(1);
     $batch = new Batch($this->connection);
     $docId1 = explode('/', $testDocument1PartResponse);
     $docId2 = explode('/', $testDocument2PartResponse);
     $documentHandler->getById($resultingCollectionId, $docId1[1]);
     $documentHandler->getById($resultingCollectionId, $docId2[1]);
     $batch->process();
     $document1 = $batch->getProcessedPartResponse(0);
     $document2 = $batch->getProcessedPartResponse(1);
     $batch = new Batch($this->connection);
     // test edge creation
     $edgeDocument = new Edge();
     $edgeDocumentHandler = new EdgeHandler($connection);
     $edgeDocument->set('label', 'knows');
     $edgeDocumentHandler->saveEdge($edgeCollection->getName(), $document1->getHandle(), $document2->getHandle(), $edgeDocument);
     $batch->process();
     $edge = $batch->getProcessedPartResponse(0);
     $this->assertFalse(is_a($edge, 'triagens\\ArangoDb\\HttpResponse'), 'Edge batch creation did return an error: ' . print_r($edge, true));
     $this->assertTrue($edge == !'', 'Edge batch creation did return empty string: ' . print_r($edge, true));
     $batch = new Batch($this->connection);
     $document = new Document();
     $documentHandler = new DocumentHandler($connection);
     $document->someAttribute = 'someValue';
     $documentHandler->add($resultingCollection->getId(), $document);
     // set the next batchpart id
     $batch->nextBatchPartId('myBatchPart');
     // set cursor options for the next batchpart
     $batch->nextBatchPartCursorOptions(array("sanitize" => true));
     // set batchsize to 10, so we can test if an additional http request is done when we getAll() a bit later
     $statement = new Statement($connection, array("query" => '', "count" => true, "batchSize" => 10, "sanitize" => true));
     $statement->setQuery('FOR a IN `ArangoDB_PHP_TestSuite_TestCollection_02` RETURN a');
     $statement->execute();
     $documentHandler->removeById($resultingCollectionId, $docId1[1]);
     $documentHandler->removeById($resultingCollectionId, $docId2[1]);
     $batch->nextBatchPartId('docsAfterRemoval');
     $collectionHandler->getAllIds($resultingCollectionId);
     $batch->process();
     $stmtCursor = $batch->getProcessedPartResponse('myBatchPart');
     $this->assertTrue(count($stmtCursor->getAll()) == 13, 'At the time of statement execution there should be 13 documents found! Found: ' . count($stmtCursor->getAll()));
     // This fails but we'll just make a note because such a query is not needed to be batched.
     // $docsAfterRemoval=$batch->getProcessedPartResponse('docsAfterRemoval');
     // $this->assertTrue(count($docsAfterRemoval) == 1, 'At the time of statement execution there should be 3 documents found! Found: '.count($stmtCursor->getAll()));
     // Get previously created collection and delete it, from inside a batch
     $batch = new Batch($this->connection);
     $collectionHandler->delete($resultingCollectionId);
     $batch->process();
 }
}
$nv = get_next_int64($fp);
$ne = get_next_int64($fp);
$off = get_int64_array($fp, $nv + 1);
$ind = get_int64_array($fp, $ne);
$wgt = get_int64_array($fp, $ne);
fclose($fp);
print "Inserting into DB...\n";
//$batch = new Batch($connection);
//$batch->activate();
$vertices = array_fill(0, $nv, "-1");
$time_start = microtime(true);
for ($v = 0; $v < $nv; $v++) {
    $s = $vertices[$v];
    if ($s == "-1") {
        $s = new_vertex($documents, $u, $nv);
        $vertices[$v] = $s;
    }
    for ($i = $off[$v]; $i < $off[$v + 1]; $i++) {
        $u = $ind[$i];
        $d = $vertices[$u];
        if ($d == "-1") {
            $d = new_vertex($documents, $u, $nv);
            $vertices[$u] = $d;
        }
        $edg = new Edge();
        $edg->set("weight", $wgt[$i]);
        $edges->saveEdge($edge, $s, $d, $edg, true);
    }
}
//$batch->process();
 /**
  * Try to create, head and delete a edge
  */
 public function testCreateHeadAndDeleteEdgeWithRevision()
 {
     $connection = $this->connection;
     $edgeHandler = new EdgeHandler($connection);
     $edgeCollection = $this->edgeCollection;
     $document1 = new Document();
     $document2 = new Document();
     $documentHandler = new DocumentHandler($connection);
     $edgeDocument = new Edge();
     $document1->someAttribute = 'someValue1';
     $document2->someAttribute = 'someValue2';
     $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document1);
     $documentHandler->add('ArangoDBPHPTestSuiteTestCollection01', $document2);
     $documentHandle1 = $document1->getHandle();
     $documentHandle2 = $document2->getHandle();
     $edgeDocument->set('label', 'knows');
     $edgeId = $edgeHandler->saveEdge($edgeCollection->getName(), $documentHandle1, $documentHandle2, $edgeDocument);
     try {
         $edgeHandler->getHead($edgeCollection->getId(), $edgeId, "12345", true);
     } catch (\Exception $e412) {
     }
     $this->assertEquals($e412->getCode(), 412);
     try {
         $edgeHandler->getHead($edgeCollection->getId(), "notExisting");
     } catch (\Exception $e404) {
     }
     $this->assertEquals($e404->getCode(), 404);
     $result304 = $edgeHandler->getHead($edgeCollection->getId(), $edgeId, $edgeDocument->getRevision(), false);
     $this->assertEquals($result304["etag"], '"' . $edgeDocument->getRevision() . '"');
     $this->assertEquals($result304["content-length"], 0);
     $this->assertEquals($result304["httpCode"], 304);
     $result200 = $edgeHandler->getHead($edgeCollection->getId(), $edgeId, $edgeDocument->getRevision(), true);
     $this->assertEquals($result200["etag"], '"' . $edgeDocument->getRevision() . '"');
     $this->assertNotEquals($result200["content-length"], 0);
     $this->assertEquals($result200["httpCode"], 200);
     $documentHandler->delete($document1);
     $documentHandler->delete($document2);
     $edgeHandler->deleteById($edgeCollection->getName(), $edgeId);
 }