-
Notifications
You must be signed in to change notification settings - Fork 6
Added functionality to retrieve ids of all docs that match a given filter #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,7 +201,7 @@ public function updateDoc(string $collectionName, string $docId, array $docOrSub | |
$this->assertDocExists($collectionName, $docId); | ||
$this->assertUniqueConstraints($collectionName, $docId, $docOrSubset); | ||
|
||
$this->inMemoryConnection['documents'][$collectionName][$docId] = \array_replace_recursive( | ||
$this->inMemoryConnection['documents'][$collectionName][$docId] = $this->arrayReplaceRecursiveAssocOnly( | ||
$this->inMemoryConnection['documents'][$collectionName][$docId], | ||
$docOrSubset | ||
); | ||
|
@@ -323,6 +323,27 @@ public function filterDocs( | |
return new \ArrayIterator($filteredDocs); | ||
} | ||
|
||
/** | ||
* @param string $collectionName | ||
* @param Filter $filter | ||
* @return array | ||
*/ | ||
public function filterDocIds( | ||
string $collectionName, | ||
Filter $filter | ||
): array { | ||
$this->assertHasCollection($collectionName); | ||
|
||
$docIds = []; | ||
foreach ($this->inMemoryConnection['documents'][$collectionName] as $docId => $doc) { | ||
if ($filter->match($doc, (string)$docId)) { | ||
$docIds[] = $docId; | ||
} | ||
} | ||
|
||
return $docIds; | ||
} | ||
|
||
private function hasDoc(string $collectionName, string $docId): bool | ||
{ | ||
if (! $this->hasCollection($collectionName)) { | ||
|
@@ -404,7 +425,7 @@ private function assertMultiFieldUniqueConstraint(string $collectionName, string | |
|
||
if($this->hasDoc($collectionName, $docId)) { | ||
$effectedDoc = $this->getDoc($collectionName, $docId); | ||
$docOrSubset = \array_replace_recursive($effectedDoc, $docOrSubset); | ||
$docOrSubset = $this->arrayReplaceRecursiveAssocOnly($effectedDoc, $docOrSubset); | ||
} | ||
|
||
$reader = new ArrayReader($docOrSubset); | ||
|
@@ -516,4 +537,39 @@ private function sort(&$docs, OrderBy $orderBy) | |
return $docCmp($docA, $docB, $orderBy); | ||
}); | ||
} | ||
|
||
/** | ||
* The internal array_replace_recursive function also replaces sequential arrays recursively. This method aims to | ||
* behave identical to array_replace_recursive but only when dealing with associative arrays. Sequential arrays | ||
* are handled as if they were scalar types instead. | ||
* | ||
* @param array $array1 | ||
* @param array $array2 | ||
* @return array | ||
*/ | ||
private function arrayReplaceRecursiveAssocOnly(array $array1, array $array2): array | ||
{ | ||
foreach ($array2 as $key2 => $value2) { | ||
$bothValuesArraysAndAtLeastOneAssoc = \is_array($value2) && | ||
isset($array1[$key2]) && \is_array($array1[$key2]) && | ||
!($this->isSequentialArray($value2) && $this->isSequentialArray($array1[$key2])); | ||
|
||
if ($bothValuesArraysAndAtLeastOneAssoc) { | ||
$array1[$key2] = $this->arrayReplaceRecursiveAssocOnly($array1[$key2], $value2); | ||
} else { | ||
$array1[$key2] = $value2; | ||
} | ||
} | ||
|
||
return $array1; | ||
} | ||
|
||
private function isSequentialArray(array $array): bool | ||
{ | ||
if (\count($array) === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would write that as |
||
return true; | ||
} | ||
|
||
return \array_keys($array) === \range(0, \count($array) - 1); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsor @heiglandreas can you take a look at these changes as well? It only affects testing environment and should not cause any existing tests to fail, but at least you should know about the change ;)