Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit 32eace4

Browse files
Fixed memory issues with Vpack in order to get rewind() to work properly
1 parent 953e3f9 commit 32eace4

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/cursor.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace arangodb { namespace fuerte { namespace php {
44

5-
Cursor::Cursor(Connection* connection, Vpack* vpack): vpack(vpack), connection(connection)
5+
Cursor::Cursor(Connection* connection, Vpack* vpack): vpack(*vpack), connection(connection)
66
{
7-
this->loadFirstBatch();
7+
this->rewind();
88
}
99

1010
void Cursor::setOption(int option, int value)
@@ -18,7 +18,7 @@ namespace arangodb { namespace fuerte { namespace php {
1818

1919
void Cursor::loadFirstBatch()
2020
{
21-
Request request("/_api/cursor", this->vpack);
21+
Request request("/_api/cursor", &this->vpack);
2222
request.setHttpMethod(Request::METHOD_POST);
2323

2424
Response* response = this->connection->sendRequest(&request);
@@ -115,6 +115,8 @@ namespace arangodb { namespace fuerte { namespace php {
115115

116116
void Cursor::rewind()
117117
{
118+
this->position = 0;
119+
this->loadFirstBatch();
118120
}
119121

120122
long Cursor::count() {

src/cursor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace arangodb { namespace fuerte { namespace php {
1717
{
1818
private:
1919
Connection* connection;
20-
Vpack* vpack;
20+
Vpack vpack;
2121

2222
bool hasMore;
2323
int position = 0;

tests/ConnectionTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,39 @@ public function it_creates_array_from_cursor()
246246

247247
$this->assertCount(100, $data);
248248
}
249+
250+
251+
/**
252+
* @test
253+
*/
254+
public function it_rewinds_cursor_multiple_times()
255+
{
256+
$this->connection = TestUtil::getConnection();
257+
258+
$cursor = $this->connection->query(Vpack::fromArray([
259+
'query' => 'FOR i IN 1..100 RETURN [i, i+1]',
260+
'batchSize' => 10
261+
]));
262+
263+
$iterations = 0;
264+
$dataSet1 = [];
265+
$dataSet2 = [];
266+
267+
while($cursor->valid()) {
268+
$dataSet1[] = $cursor->current();
269+
$cursor->next();
270+
$iterations++;
271+
}
272+
273+
$cursor->rewind();
274+
275+
while($cursor->valid()) {
276+
$dataSet2[] = $cursor->current();
277+
$cursor->next();
278+
$iterations++;
279+
}
280+
281+
$this->assertSame(200, $iterations);
282+
$this->assertTrue($dataSet1 === $dataSet2);
283+
}
249284
}

0 commit comments

Comments
 (0)