Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
d63c3fad
Commit
d63c3fad
authored
Jun 21, 2019
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'v1.4'
parents
770e2afd
a1852b20
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
25 deletions
+100
-25
ChangeStream.php
src/ChangeStream.php
+36
-25
WatchFunctionalTest.php
tests/Operation/WatchFunctionalTest.php
+64
-0
No files found.
src/ChangeStream.php
View file @
d63c3fad
...
@@ -103,22 +103,7 @@ class ChangeStream implements Iterator
...
@@ -103,22 +103,7 @@ class ChangeStream implements Iterator
{
{
try
{
try
{
$this
->
csIt
->
next
();
$this
->
csIt
->
next
();
if
(
$this
->
valid
())
{
$this
->
onIteration
(
true
);
if
(
$this
->
hasAdvanced
)
{
$this
->
key
++
;
}
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
/* If the cursorId is 0, the server has invalidated the cursor so we
* will never perform another getMore. This means that we cannot
* resume and we can therefore unset the resumeCallable, which will
* free any reference to Watch. This will also free the only
* reference to an implicit session, since any such reference
* belongs to Watch. */
if
((
string
)
$this
->
getCursorId
()
===
'0'
)
{
$this
->
resumeCallable
=
null
;
}
}
catch
(
RuntimeException
$e
)
{
}
catch
(
RuntimeException
$e
)
{
$this
->
resumeOrThrow
(
$e
);
$this
->
resumeOrThrow
(
$e
);
}
}
...
@@ -133,14 +118,7 @@ class ChangeStream implements Iterator
...
@@ -133,14 +118,7 @@ class ChangeStream implements Iterator
{
{
try
{
try
{
$this
->
csIt
->
rewind
();
$this
->
csIt
->
rewind
();
if
(
$this
->
valid
())
{
$this
->
onIteration
(
false
);
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
// As with next(), free the callable once we know it will never be used.
if
((
string
)
$this
->
getCursorId
()
===
'0'
)
{
$this
->
resumeCallable
=
null
;
}
}
catch
(
RuntimeException
$e
)
{
}
catch
(
RuntimeException
$e
)
{
$this
->
resumeOrThrow
(
$e
);
$this
->
resumeOrThrow
(
$e
);
}
}
...
@@ -158,7 +136,7 @@ class ChangeStream implements Iterator
...
@@ -158,7 +136,7 @@ class ChangeStream implements Iterator
/**
/**
* Extracts the resume token (i.e. "_id" field) from the change document.
* Extracts the resume token (i.e. "_id" field) from the change document.
*
*
* @param array|
documen
t $document Change document
* @param array|
objec
t $document Change document
* @return mixed
* @return mixed
* @throws InvalidArgumentException
* @throws InvalidArgumentException
* @throws ResumeTokenException if the resume token is not found or invalid
* @throws ResumeTokenException if the resume token is not found or invalid
...
@@ -212,6 +190,38 @@ class ChangeStream implements Iterator
...
@@ -212,6 +190,38 @@ class ChangeStream implements Iterator
return
true
;
return
true
;
}
}
/**
* Perform housekeeping after an iteration event (i.e. next or rewind).
*
* @param boolean $isNext Whether the iteration event was a call to next()
* @throws ResumeTokenException
*/
private
function
onIteration
(
$isNext
)
{
/* If the cursorId is 0, the server has invalidated the cursor and we
* will never perform another getMore nor need to resume since any
* remaining results (up to and including the invalidate event) will
* have been received in the last response. Therefore, we can unset the
* resumeCallable. This will free any reference to Watch as well as the
* only reference to any implicit session created therein. */
if
((
string
)
$this
->
getCursorId
()
===
'0'
)
{
$this
->
resumeCallable
=
null
;
}
if
(
!
$this
->
valid
())
{
return
;
}
/* Increment the key if the iteration event was a call to next() and we
* have already advanced past the first result. */
if
(
$isNext
&&
$this
->
hasAdvanced
)
{
$this
->
key
++
;
}
$this
->
hasAdvanced
=
true
;
$this
->
resumeToken
=
$this
->
extractResumeToken
(
$this
->
csIt
->
current
());
}
/**
/**
* Creates a new changeStream after a resumable server error.
* Creates a new changeStream after a resumable server error.
*
*
...
@@ -222,6 +232,7 @@ class ChangeStream implements Iterator
...
@@ -222,6 +232,7 @@ class ChangeStream implements Iterator
$newChangeStream
=
call_user_func
(
$this
->
resumeCallable
,
$this
->
resumeToken
);
$newChangeStream
=
call_user_func
(
$this
->
resumeCallable
,
$this
->
resumeToken
);
$this
->
csIt
=
$newChangeStream
->
csIt
;
$this
->
csIt
=
$newChangeStream
->
csIt
;
$this
->
csIt
->
rewind
();
$this
->
csIt
->
rewind
();
$this
->
onIteration
(
false
);
}
}
/**
/**
...
...
tests/Operation/WatchFunctionalTest.php
View file @
d63c3fad
...
@@ -314,6 +314,70 @@ class WatchFunctionalTest extends FunctionalTestCase
...
@@ -314,6 +314,70 @@ class WatchFunctionalTest extends FunctionalTestCase
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
}
}
public
function
testResumeTokenIsUpdatedAfterResuming
()
{
$this
->
insertDocument
([
'_id'
=>
1
]);
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$this
->
defaultOptions
);
$changeStream
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$changeStream
->
rewind
();
$this
->
assertNull
(
$changeStream
->
current
());
$this
->
insertDocument
([
'_id'
=>
2
]);
$changeStream
->
next
();
$this
->
assertTrue
(
$changeStream
->
valid
());
$expectedResult
=
[
'_id'
=>
$changeStream
->
current
()
->
_id
,
'operationType'
=>
'insert'
,
'fullDocument'
=>
[
'_id'
=>
2
],
'ns'
=>
[
'db'
=>
$this
->
getDatabaseName
(),
'coll'
=>
$this
->
getCollectionName
()],
'documentKey'
=>
[
'_id'
=>
2
],
];
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
$this
->
killChangeStreamCursor
(
$changeStream
);
$this
->
insertDocument
([
'_id'
=>
3
]);
$changeStream
->
next
();
$this
->
assertTrue
(
$changeStream
->
valid
());
$expectedResult
=
[
'_id'
=>
$changeStream
->
current
()
->
_id
,
'operationType'
=>
'insert'
,
'fullDocument'
=>
[
'_id'
=>
3
],
'ns'
=>
[
'db'
=>
$this
->
getDatabaseName
(),
'coll'
=>
$this
->
getCollectionName
()],
'documentKey'
=>
[
'_id'
=>
3
],
];
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
/* Triggering a consecutive failure will allow us to test whether the
* resume token was properly updated after the last resume. If the
* resume token updated, the next result will be {_id: 4}; otherwise,
* we'll see {_id: 3} returned again. */
$this
->
killChangeStreamCursor
(
$changeStream
);
$this
->
insertDocument
([
'_id'
=>
4
]);
$changeStream
->
next
();
$this
->
assertTrue
(
$changeStream
->
valid
());
$expectedResult
=
[
'_id'
=>
$changeStream
->
current
()
->
_id
,
'operationType'
=>
'insert'
,
'fullDocument'
=>
[
'_id'
=>
4
],
'ns'
=>
[
'db'
=>
$this
->
getDatabaseName
(),
'coll'
=>
$this
->
getCollectionName
()],
'documentKey'
=>
[
'_id'
=>
4
],
];
$this
->
assertMatchesDocument
(
$expectedResult
,
$changeStream
->
current
());
}
public
function
testKey
()
public
function
testKey
()
{
{
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$this
->
defaultOptions
);
$operation
=
new
Watch
(
$this
->
manager
,
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
$this
->
defaultOptions
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment