Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
5341fdb6
Commit
5341fdb6
authored
Jan 29, 2014
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reset prefetch iterator if prefetch or query settings are changed (#219)
parent
45274934
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
0 deletions
+67
-0
library/Solarium/Plugin/PrefetchIterator.php
library/Solarium/Plugin/PrefetchIterator.php
+13
-0
tests/Solarium/Tests/Plugin/PrefetchIteratorTest.php
tests/Solarium/Tests/Plugin/PrefetchIteratorTest.php
+54
-0
No files found.
library/Solarium/Plugin/PrefetchIterator.php
View file @
5341fdb6
...
...
@@ -104,6 +104,7 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable
*/
public
function
setPrefetch
(
$value
)
{
$this
->
resetData
();
return
$this
->
setOption
(
'prefetch'
,
$value
);
}
...
...
@@ -126,6 +127,7 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable
public
function
setQuery
(
$query
)
{
$this
->
query
=
$query
;
$this
->
resetData
();
return
$this
;
}
...
...
@@ -225,4 +227,15 @@ class PrefetchIterator extends Plugin implements \Iterator, \Countable
$this
->
documents
=
$this
->
result
->
getDocuments
();
$this
->
start
+=
$this
->
getPrefetch
();
}
/**
* Reset any cached data / position
*/
protected
function
resetData
()
{
$this
->
position
=
null
;
$this
->
result
=
null
;
$this
->
documents
=
null
;
$this
->
start
=
0
;
}
}
tests/Solarium/Tests/Plugin/PrefetchIteratorTest.php
View file @
5341fdb6
...
...
@@ -90,6 +90,8 @@ class PrefetchIteratorTest extends \PHPUnit_Framework_TestCase
{
$result
=
$this
->
getResult
();
$mockClient
=
$this
->
getMock
(
'Solarium\Core\Client\Client'
,
array
(
'execute'
));
// Important: if prefetch or query settings are not changed, the query should be executed only once!
$mockClient
->
expects
(
$this
->
exactly
(
1
))
->
method
(
'execute'
)
->
will
(
$this
->
returnValue
(
$result
));
$this
->
plugin
->
initPlugin
(
$mockClient
,
array
());
...
...
@@ -110,6 +112,58 @@ class PrefetchIteratorTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
$result
->
getDocuments
(),
$results2
);
}
public
function
testIteratorResetOnSetPrefetch
()
{
$result
=
$this
->
getResult
();
$mockClient
=
$this
->
getMock
(
'Solarium\Core\Client\Client'
,
array
(
'execute'
));
$mockClient
->
expects
(
$this
->
exactly
(
2
))
->
method
(
'execute'
)
->
will
(
$this
->
returnValue
(
$result
));
$this
->
plugin
->
initPlugin
(
$mockClient
,
array
());
$this
->
plugin
->
setQuery
(
$this
->
query
);
$results1
=
array
();
foreach
(
$this
->
plugin
as
$doc
)
{
$results1
[]
=
$doc
;
}
$this
->
plugin
->
setPrefetch
(
1000
);
// the second foreach should trigger a reset and a second query execution (checked by mock)
$results2
=
array
();
foreach
(
$this
->
plugin
as
$doc
)
{
$results2
[]
=
$doc
;
}
$this
->
assertEquals
(
$result
->
getDocuments
(),
$results1
);
$this
->
assertEquals
(
$result
->
getDocuments
(),
$results2
);
}
public
function
testIteratorResetOnSetQuery
()
{
$result
=
$this
->
getResult
();
$mockClient
=
$this
->
getMock
(
'Solarium\Core\Client\Client'
,
array
(
'execute'
));
$mockClient
->
expects
(
$this
->
exactly
(
2
))
->
method
(
'execute'
)
->
will
(
$this
->
returnValue
(
$result
));
$this
->
plugin
->
initPlugin
(
$mockClient
,
array
());
$this
->
plugin
->
setQuery
(
$this
->
query
);
$results1
=
array
();
foreach
(
$this
->
plugin
as
$doc
)
{
$results1
[]
=
$doc
;
}
$this
->
plugin
->
setQuery
(
$this
->
query
);
// the second foreach should trigger a reset and a second query execution (checked by mock)
$results2
=
array
();
foreach
(
$this
->
plugin
as
$doc
)
{
$results2
[]
=
$doc
;
}
$this
->
assertEquals
(
$result
->
getDocuments
(),
$results1
);
$this
->
assertEquals
(
$result
->
getDocuments
(),
$results2
);
}
public
function
getResult
()
{
$numFound
=
5
;
...
...
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