From cc5d0104dcb9e0f9fe438e101b38fec5e6bcb891 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Tue, 6 Oct 2020 15:08:18 -0400 Subject: [PATCH 01/12] Version option deprecation fix and replacements --- CHANGELOG.md | 1 + src/Client.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52fbcaa86f..be96018aa3 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791) * Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791) +* Removed deprecated `version` option [(deprecated in `6.7.0`)](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update.html) and added `if_seq_no` / `if_primary_term` that replaced it ### Deprecated * Deprecated Match query class and introduced MatchQuery instead for PHP 8.0 compatibility reason [#1799](https://github.com/ruflin/Elastica/pull/1799) ### Removed diff --git a/src/Client.php b/src/Client.php index 9a570fd53c..690972c108 100644 --- a/src/Client.php +++ b/src/Client.php @@ -277,7 +277,8 @@ public function updateDocument($id, $data, $index, array $options = []): Respons $docOptions = $data->getOptions( [ - 'version', + 'if_seq_no', + 'if_primary_term', 'version_type', 'routing', 'percolate', From f1fc76184cc2fa12142939934e54398f638d2a46 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 11:38:23 -0400 Subject: [PATCH 02/12] Use new if_seq_no and if_primary_term --- src/AbstractUpdateAction.php | 78 +++++++++++++++++++++++++++++++++++- src/Client.php | 8 ++-- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index 54f1418ead..8414c45c39 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -65,6 +65,71 @@ public function getIndex() } /** + * Sets the sequence number of a document for use with optimistic concurrency control. + * + * @param int $number Sequence Number + * + * @return $this + * + * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html + */ + public function setSequenceNumber($number) + { + return $this->setParam('if_seq_no', $number); + } + + /** + * Returns document version. + * + * @return int|string Document version + */ + public function getSequenceNumber() + { + return $this->getParam('if_seq_no'); + } + + /** + * @return bool + */ + public function hasSequenceNumber() + { + return $this->hasParam('if_seq_no'); + } + + /** + * Sets the prmary term of a document for use with optimistic concurrency control. + * + * @param int $term Primary Term + * + * @return $this + * + * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html + */ + public function setPrimaryTerm($term) + { + return $this->setParam('if_primary_term', $term); + } + + /** + * Returns document version. + * + * @return int|string Document version + */ + public function getPrimaryTerm() + { + return $this->getParam('if_primary_term'); + } + + /** + * @return bool + */ + public function hasPrimaryTerm() + { + return $this->hasParam('if_primary_term'); + } + + /** + * @deprecated * Sets the version of a document for use with optimistic concurrency control. * * @param int $version Document version @@ -75,10 +140,13 @@ public function getIndex() */ public function setVersion($version) { - return $this->setParam('version', (int) $version); + \trigger_error('Elastica\AbstractUpdateAction\setVersion is deprecated. Elastica\AbstractUpdateAction\setSequenceNumber and Elastica\AbstractUpdateAction\setPrimaryTerm instead.', E_USER_DEPRECATED); + + return $this; } /** + * @deprecated * Returns document version. * * @return int|string Document version @@ -89,6 +157,7 @@ public function getVersion() } /** + * @deprecated * @return bool */ public function hasVersion() @@ -97,6 +166,7 @@ public function hasVersion() } /** + * @deprecated * Sets the version_type of a document * Default in ES is internal, but you can set to external to use custom versioning. * @@ -106,10 +176,13 @@ public function hasVersion() */ public function setVersionType($versionType) { - return $this->setParam('version_type', $versionType); + \trigger_error('Elastica\AbstractUpdateAction\setVersionType is deprecated. Elastica\AbstractUpdateAction\setSequenceNumber and Elastica\AbstractUpdateAction\setPrimaryTerm instead.', E_USER_DEPRECATED); + + return $this; } /** + * @deprecated * Returns document version type. * * @return int|string Document version type @@ -120,6 +193,7 @@ public function getVersionType() } /** + * @deprecated * @return bool */ public function hasVersionType() diff --git a/src/Client.php b/src/Client.php index 690972c108..04805182a1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -279,7 +279,6 @@ public function updateDocument($id, $data, $index, array $options = []): Respons [ 'if_seq_no', 'if_primary_term', - 'version_type', 'routing', 'percolate', 'parent', @@ -312,8 +311,11 @@ public function updateDocument($id, $data, $index, array $options = []): Respons && ($data->isAutoPopulate() || $this->getConfigValue(['document', 'autoPopulate'], false)) ) { $responseData = $response->getData(); - if (isset($responseData['_version'])) { - $data->setVersion($responseData['_version']); + if (isset($responseData['_seq_no'])) { + $data->setSequenceNumber($responseData['_seq_no']); + } + if (isset($responseData['_primary_term'])) { + $data->setPrimaryTerm($responseData['_primary_term']); } } From 0fae95eb8000da2b6231cc29db9ac9f3378887ac Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 11:45:24 -0400 Subject: [PATCH 03/12] Changelog for if_seq_no and if_primary_term --- CHANGELOG.md | 3 ++- src/AbstractUpdateAction.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be96018aa3..8089c63e8c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,12 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Backward Compatibility Breaks ### Added * Ability to specify the type of authentication manually by the `auth_type` parameter (in the client class config) was added (allowed values are `basic, digest, gssnegotiate, ntlm`) +* Added `if_seq_no` / `if_primary_term` to replace `version` for [optimistic concurrency control](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html) ### Changed * Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791) * Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791) -* Removed deprecated `version` option [(deprecated in `6.7.0`)](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update.html) and added `if_seq_no` / `if_primary_term` that replaced it ### Deprecated * Deprecated Match query class and introduced MatchQuery instead for PHP 8.0 compatibility reason [#1799](https://github.com/ruflin/Elastica/pull/1799) +* Deprecated `version`/`version_type` options [(deprecated in `6.7.0`)](https://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update.html) and added `if_seq_no` / `if_primary_term` that replaced it ### Removed ### Fixed * fixed issue [1789](https://github.com/ruflin/Elastica/issues/1789) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index 8414c45c39..157840ef15 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -140,7 +140,7 @@ public function hasPrimaryTerm() */ public function setVersion($version) { - \trigger_error('Elastica\AbstractUpdateAction\setVersion is deprecated. Elastica\AbstractUpdateAction\setSequenceNumber and Elastica\AbstractUpdateAction\setPrimaryTerm instead.', E_USER_DEPRECATED); + \trigger_error('Elastica\AbstractUpdateAction::setVersion is deprecated. Use Elastica\AbstractUpdateAction::setSequenceNumber and Elastica\AbstractUpdateAction::setPrimaryTerm instead.', E_USER_DEPRECATED); return $this; } @@ -176,7 +176,7 @@ public function hasVersion() */ public function setVersionType($versionType) { - \trigger_error('Elastica\AbstractUpdateAction\setVersionType is deprecated. Elastica\AbstractUpdateAction\setSequenceNumber and Elastica\AbstractUpdateAction\setPrimaryTerm instead.', E_USER_DEPRECATED); + \trigger_error('Elastica\AbstractUpdateAction::setVersionType is deprecated. Use Elastica\AbstractUpdateAction::setSequenceNumber and Elastica\AbstractUpdateAction::setPrimaryTerm instead.', E_USER_DEPRECATED); return $this; } From 955d3ede719a9aa1b6d9a07dccd3b04f92e2b560 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 12:45:50 -0400 Subject: [PATCH 04/12] Removed deprecated setVersion() --- src/Bulk.php | 7 +++++-- src/Index.php | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Bulk.php b/src/Bulk.php index ba7b09db6e..7bcd3b7de6 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -323,8 +323,11 @@ protected function _processResponse(Response $response): ResponseSet if (!$data->hasId() && isset($bulkResponseData['_id'])) { $data->setId($bulkResponseData['_id']); } - if (isset($bulkResponseData['_version'])) { - $data->setVersion($bulkResponseData['_version']); + if (isset($responseData['_seq_no'])) { + $data->setSequenceNumber($responseData['_seq_no']); + } + if (isset($responseData['_primary_term'])) { + $data->setPrimaryTerm($responseData['_primary_term']); } } } diff --git a/src/Index.php b/src/Index.php index 02df3fddf7..8f54ea8574 100644 --- a/src/Index.php +++ b/src/Index.php @@ -215,8 +215,11 @@ public function addDocument(Document $doc): Response if (isset($data['_id']) && !$doc->hasId()) { $doc->setId($data['_id']); } - if (isset($data['_version'])) { - $doc->setVersion($data['_version']); + if (isset($responseData['_seq_no'])) { + $doc->setSequenceNumber($responseData['_seq_no']); + } + if (isset($responseData['_primary_term'])) { + $doc->setPrimaryTerm($responseData['_primary_term']); } } From d15845ef40314b2bae73dc7f8d91d05b613adcd8 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 13:21:12 -0400 Subject: [PATCH 05/12] removed version from document --- src/Index.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Index.php b/src/Index.php index 8f54ea8574..68228e6353 100644 --- a/src/Index.php +++ b/src/Index.php @@ -275,10 +275,7 @@ public function getDocument($id, array $options = []): Document $data = []; } - $document = new Document($id, $data, $this->getName()); - $document->setVersion($result['_version']); - - return $document; + return new Document($id, $data, $this->getName()); } /** From edccc890217b970da795472a84a0e830c7f498a0 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 15:45:14 -0400 Subject: [PATCH 06/12] type hints and keep set version from returned document --- src/AbstractUpdateAction.php | 139 ++++++++++++----------------------- src/Bulk.php | 3 + src/Index.php | 24 ++++-- 3 files changed, 71 insertions(+), 95 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index 157840ef15..b28ebd1839 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -59,7 +59,7 @@ public function setIndex($index): self * * @return string Index name */ - public function getIndex() + public function getIndex(): string { return $this->getParam('_index'); } @@ -73,31 +73,31 @@ public function getIndex() * * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html */ - public function setSequenceNumber($number) + public function setSequenceNumber(int $number): self { - return $this->setParam('if_seq_no', $number); + return $this->setParam('_seq_no', $number); } /** * Returns document version. * - * @return int|string Document version + * @return int Document version */ - public function getSequenceNumber() + public function getSequenceNumber(): int { - return $this->getParam('if_seq_no'); + return $this->getParam('_seq_no'); } /** * @return bool */ - public function hasSequenceNumber() + public function hasSequenceNumber(): bool { - return $this->hasParam('if_seq_no'); + return $this->hasParam('_seq_no'); } /** - * Sets the prmary term of a document for use with optimistic concurrency control. + * Sets the primary term of a document for use with optimistic concurrency control. * * @param int $term Primary Term * @@ -105,31 +105,30 @@ public function hasSequenceNumber() * * @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html */ - public function setPrimaryTerm($term) + public function setPrimaryTerm(int $term): self { - return $this->setParam('if_primary_term', $term); + return $this->setParam('_primary_term', $term); } /** * Returns document version. * - * @return int|string Document version + * @return int Document version */ - public function getPrimaryTerm() + public function getPrimaryTerm(): int { - return $this->getParam('if_primary_term'); + return $this->getParam('_primary_term'); } /** * @return bool */ - public function hasPrimaryTerm() + public function hasPrimaryTerm(): bool { - return $this->hasParam('if_primary_term'); + return $this->hasParam('_primary_term'); } /** - * @deprecated * Sets the version of a document for use with optimistic concurrency control. * * @param int $version Document version @@ -138,69 +137,29 @@ public function hasPrimaryTerm() * * @see https://www.elastic.co/blog/versioning */ - public function setVersion($version) + public function setVersion(int $version): self { - \trigger_error('Elastica\AbstractUpdateAction::setVersion is deprecated. Use Elastica\AbstractUpdateAction::setSequenceNumber and Elastica\AbstractUpdateAction::setPrimaryTerm instead.', E_USER_DEPRECATED); - - return $this; + return $this->setParam('_version', $version); } /** - * @deprecated * Returns document version. * - * @return int|string Document version + * @return int Document version */ - public function getVersion() + public function getVersion(): int { return $this->getParam('version'); } /** - * @deprecated * @return bool */ - public function hasVersion() + public function hasVersion(): bool { return $this->hasParam('version'); } - /** - * @deprecated - * Sets the version_type of a document - * Default in ES is internal, but you can set to external to use custom versioning. - * - * @param string $versionType Document version type - * - * @return $this - */ - public function setVersionType($versionType) - { - \trigger_error('Elastica\AbstractUpdateAction::setVersionType is deprecated. Use Elastica\AbstractUpdateAction::setSequenceNumber and Elastica\AbstractUpdateAction::setPrimaryTerm instead.', E_USER_DEPRECATED); - - return $this; - } - - /** - * @deprecated - * Returns document version type. - * - * @return int|string Document version type - */ - public function getVersionType() - { - return $this->getParam('version_type'); - } - - /** - * @deprecated - * @return bool - */ - public function hasVersionType() - { - return $this->hasParam('version_type'); - } - /** * Set operation type. * @@ -208,7 +167,7 @@ public function hasVersionType() * * @return $this */ - public function setOpType($opType) + public function setOpType(string $opType): self { return $this->setParam('op_type', $opType); } @@ -218,7 +177,7 @@ public function setOpType($opType) * * @return string */ - public function getOpType() + public function getOpType(): string { return $this->getParam('op_type'); } @@ -226,7 +185,7 @@ public function getOpType() /** * @return bool */ - public function hasOpType() + public function hasOpType(): bool { return $this->hasParam('op_type'); } @@ -238,7 +197,7 @@ public function hasOpType() * * @return $this */ - public function setRouting($value) + public function setRouting(string $value): self { return $this->setParam('routing', $value); } @@ -248,7 +207,7 @@ public function setRouting($value) * * @return string */ - public function getRouting() + public function getRouting(): string { return $this->getParam('_routing'); } @@ -256,7 +215,7 @@ public function getRouting() /** * @return bool */ - public function hasRouting() + public function hasRouting(): bool { return $this->hasParam('_routing'); } @@ -266,7 +225,7 @@ public function hasRouting() * * @return $this */ - public function setFields($fields) + public function setFields($fields): self { if (\is_array($fields)) { $fields = \implode(',', $fields); @@ -278,7 +237,7 @@ public function setFields($fields) /** * @return $this */ - public function setFieldsSource() + public function setFieldsSource(): self { return $this->setFields('_source'); } @@ -286,7 +245,7 @@ public function setFieldsSource() /** * @return string */ - public function getFields() + public function getFields(): string { return $this->getParam('fields'); } @@ -294,7 +253,7 @@ public function getFields() /** * @return bool */ - public function hasFields() + public function hasFields(): bool { return $this->hasParam('fields'); } @@ -304,7 +263,7 @@ public function hasFields() * * @return $this */ - public function setRetryOnConflict($num) + public function setRetryOnConflict(int $num): self { return $this->setParam('retry_on_conflict', (int) $num); } @@ -312,7 +271,7 @@ public function setRetryOnConflict($num) /** * @return int */ - public function getRetryOnConflict() + public function getRetryOnConflict(): int { return $this->getParam('retry_on_conflict'); } @@ -320,7 +279,7 @@ public function getRetryOnConflict() /** * @return bool */ - public function hasRetryOnConflict() + public function hasRetryOnConflict(): bool { return $this->hasParam('_retry_on_conflict'); } @@ -354,7 +313,7 @@ public function getRefresh() /** * @return bool */ - public function hasRefresh() + public function hasRefresh(): bool { return $this->hasParam('refresh'); } @@ -364,15 +323,15 @@ public function hasRefresh() * * @return $this */ - public function setTimeout($timeout) + public function setTimeout(string $timeout): self { return $this->setParam('timeout', $timeout); } /** - * @return bool + * @return string */ - public function getTimeout() + public function getTimeout(): string { return $this->getParam('timeout'); } @@ -380,7 +339,7 @@ public function getTimeout() /** * @return bool */ - public function hasTimeout() + public function hasTimeout(): bool { return $this->hasParam('timeout'); } @@ -390,7 +349,7 @@ public function hasTimeout() * * @return $this */ - public function setConsistency($timeout) + public function setConsistency(string $timeout): self { return $this->setParam('consistency', $timeout); } @@ -398,7 +357,7 @@ public function setConsistency($timeout) /** * @return string */ - public function getConsistency() + public function getConsistency(): string { return $this->getParam('consistency'); } @@ -406,7 +365,7 @@ public function getConsistency() /** * @return bool */ - public function hasConsistency() + public function hasConsistency(): bool { return $this->hasParam('consistency'); } @@ -416,7 +375,7 @@ public function hasConsistency() * * @return $this */ - public function setReplication($timeout) + public function setReplication(string $timeout): self { return $this->setParam('replication', $timeout); } @@ -424,7 +383,7 @@ public function setReplication($timeout) /** * @return string */ - public function getReplication() + public function getReplication(): string { return $this->getParam('replication'); } @@ -432,7 +391,7 @@ public function getReplication() /** * @return bool */ - public function hasReplication() + public function hasReplication(): bool { return $this->hasParam('replication'); } @@ -442,7 +401,7 @@ public function hasReplication() * * @return $this */ - public function setUpsert($data) + public function setUpsert($data): self { $document = Document::create($data); $this->_upsert = $document; @@ -453,7 +412,7 @@ public function setUpsert($data) /** * @return Document */ - public function getUpsert() + public function getUpsert(): Document { return $this->_upsert; } @@ -461,7 +420,7 @@ public function getUpsert() /** * @return bool */ - public function hasUpsert() + public function hasUpsert(): bool { return null !== $this->_upsert; } @@ -471,7 +430,7 @@ public function hasUpsert() * * @return array */ - public function getOptions(array $fields = []) + public function getOptions(array $fields = []): array { if (!empty($fields)) { return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields))); diff --git a/src/Bulk.php b/src/Bulk.php index 7bcd3b7de6..cc776c801c 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -323,6 +323,9 @@ protected function _processResponse(Response $response): ResponseSet if (!$data->hasId() && isset($bulkResponseData['_id'])) { $data->setId($bulkResponseData['_id']); } + if (isset($responseData['_version'])) { + $data->setVersion($responseData['_version']); + } if (isset($responseData['_seq_no'])) { $data->setSequenceNumber($responseData['_seq_no']); } diff --git a/src/Index.php b/src/Index.php index 68228e6353..fa01122382 100644 --- a/src/Index.php +++ b/src/Index.php @@ -215,11 +215,14 @@ public function addDocument(Document $doc): Response if (isset($data['_id']) && !$doc->hasId()) { $doc->setId($data['_id']); } - if (isset($responseData['_seq_no'])) { - $doc->setSequenceNumber($responseData['_seq_no']); + if (isset($data['_version'])) { + $doc->setVersion($data['_version']); } - if (isset($responseData['_primary_term'])) { - $doc->setPrimaryTerm($responseData['_primary_term']); + if (isset($data['_seq_no'])) { + $doc->setSequenceNumber($data['_seq_no']); + } + if (isset($data['_primary_term'])) { + $doc->setPrimaryTerm($data['_primary_term']); } } @@ -275,7 +278,18 @@ public function getDocument($id, array $options = []): Document $data = []; } - return new Document($id, $data, $this->getName()); + $doc = new Document($id, $data, $this->getName()); + if (isset($data['_version'])) { + $doc->setVersion($data['_version']); + } + if (isset($data['_seq_no'])) { + $doc->setSequenceNumber($data['_seq_no']); + } + if (isset($data['_primary_term'])) { + $doc->setPrimaryTerm($data['_primary_term']); + } + + return $doc; } /** From ac3f4ad5bd46899446fcb6d88171e4538afab2bd Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 15:54:53 -0400 Subject: [PATCH 07/12] fixed $bulkResponseData --- src/Bulk.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Bulk.php b/src/Bulk.php index cc776c801c..9fe8d74734 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -323,14 +323,14 @@ protected function _processResponse(Response $response): ResponseSet if (!$data->hasId() && isset($bulkResponseData['_id'])) { $data->setId($bulkResponseData['_id']); } - if (isset($responseData['_version'])) { - $data->setVersion($responseData['_version']); + if (isset($bulkResponseData['_version'])) { + $data->setVersion($bulkResponseData['_version']); } - if (isset($responseData['_seq_no'])) { - $data->setSequenceNumber($responseData['_seq_no']); + if (isset($bulkResponseData['_seq_no'])) { + $data->setSequenceNumber($bulkResponseData['_seq_no']); } - if (isset($responseData['_primary_term'])) { - $data->setPrimaryTerm($responseData['_primary_term']); + if (isset($bulkResponseData['_primary_term'])) { + $data->setPrimaryTerm($bulkResponseData['_primary_term']); } } } From df38de855d72b98669c589681e297d18b45b7d99 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 16:10:22 -0400 Subject: [PATCH 08/12] only use _version --- src/AbstractUpdateAction.php | 18 +++++++++--------- src/Client.php | 12 ++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index b28ebd1839..e235fd7014 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -75,7 +75,7 @@ public function getIndex(): string */ public function setSequenceNumber(int $number): self { - return $this->setParam('_seq_no', $number); + return $this->setParam('if_seq_no', $number); } /** @@ -85,7 +85,7 @@ public function setSequenceNumber(int $number): self */ public function getSequenceNumber(): int { - return $this->getParam('_seq_no'); + return $this->getParam('if_seq_no'); } /** @@ -93,7 +93,7 @@ public function getSequenceNumber(): int */ public function hasSequenceNumber(): bool { - return $this->hasParam('_seq_no'); + return $this->hasParam('if_seq_no'); } /** @@ -107,7 +107,7 @@ public function hasSequenceNumber(): bool */ public function setPrimaryTerm(int $term): self { - return $this->setParam('_primary_term', $term); + return $this->setParam('if_primary_term', $term); } /** @@ -117,7 +117,7 @@ public function setPrimaryTerm(int $term): self */ public function getPrimaryTerm(): int { - return $this->getParam('_primary_term'); + return $this->getParam('if_primary_term'); } /** @@ -125,11 +125,11 @@ public function getPrimaryTerm(): int */ public function hasPrimaryTerm(): bool { - return $this->hasParam('_primary_term'); + return $this->hasParam('if_primary_term'); } /** - * Sets the version of a document for use with optimistic concurrency control. + * Sets the version of a document. * * @param int $version Document version * @@ -149,7 +149,7 @@ public function setVersion(int $version): self */ public function getVersion(): int { - return $this->getParam('version'); + return $this->getParam('_version'); } /** @@ -157,7 +157,7 @@ public function getVersion(): int */ public function hasVersion(): bool { - return $this->hasParam('version'); + return $this->hasParam('_version'); } /** diff --git a/src/Client.php b/src/Client.php index 04805182a1..9b7ab256d3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -277,15 +277,15 @@ public function updateDocument($id, $data, $index, array $options = []): Respons $docOptions = $data->getOptions( [ - 'if_seq_no', + 'consistency', 'if_primary_term', - 'routing', - 'percolate', + 'if_seq_no', 'parent', - 'retry_on_conflict', - 'consistency', - 'replication', + 'percolate', 'refresh', + 'replication', + 'retry_on_conflict', + 'routing', 'timeout', ] ); From 22548535e0400a087dee941f1d43689e88dc5235 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 16:30:33 -0400 Subject: [PATCH 09/12] optimized duplicate code --- src/AbstractUpdateAction.php | 22 ++++++++++++++++++++++ src/Bulk.php | 10 +--------- src/Client.php | 8 +------- src/Index.php | 20 ++------------------ 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index e235fd7014..38f6ce70de 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -438,4 +438,26 @@ public function getOptions(array $fields = []): array return \array_filter($this->getParams()); } + + /** + * @param array $responseData + * + * @return $this + */ + public function setVersionParams(array $responseData) :self + { + if (isset($responseData['_version'])) { + $this->setVersion($responseData['_version']); + } + + if (isset($data['_seq_no'])) { + $this->setSequenceNumber($responseData['_seq_no']); + } + + if (isset($data['_primary_term'])) { + $this->setPrimaryTerm($responseData['_primary_term']); + } + + return $this; + } } diff --git a/src/Bulk.php b/src/Bulk.php index 9fe8d74734..ffc2db0bc6 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -323,15 +323,7 @@ protected function _processResponse(Response $response): ResponseSet if (!$data->hasId() && isset($bulkResponseData['_id'])) { $data->setId($bulkResponseData['_id']); } - if (isset($bulkResponseData['_version'])) { - $data->setVersion($bulkResponseData['_version']); - } - if (isset($bulkResponseData['_seq_no'])) { - $data->setSequenceNumber($bulkResponseData['_seq_no']); - } - if (isset($bulkResponseData['_primary_term'])) { - $data->setPrimaryTerm($bulkResponseData['_primary_term']); - } + $data->setVersionParams($bulkResponseData); } } diff --git a/src/Client.php b/src/Client.php index 9b7ab256d3..ff42f8738e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -310,13 +310,7 @@ public function updateDocument($id, $data, $index, array $options = []): Respons && $data instanceof Document && ($data->isAutoPopulate() || $this->getConfigValue(['document', 'autoPopulate'], false)) ) { - $responseData = $response->getData(); - if (isset($responseData['_seq_no'])) { - $data->setSequenceNumber($responseData['_seq_no']); - } - if (isset($responseData['_primary_term'])) { - $data->setPrimaryTerm($responseData['_primary_term']); - } + $data->setVersionParams($response->getData()); } return $response; diff --git a/src/Index.php b/src/Index.php index fa01122382..4ee9d4a9be 100644 --- a/src/Index.php +++ b/src/Index.php @@ -215,15 +215,7 @@ public function addDocument(Document $doc): Response if (isset($data['_id']) && !$doc->hasId()) { $doc->setId($data['_id']); } - if (isset($data['_version'])) { - $doc->setVersion($data['_version']); - } - if (isset($data['_seq_no'])) { - $doc->setSequenceNumber($data['_seq_no']); - } - if (isset($data['_primary_term'])) { - $doc->setPrimaryTerm($data['_primary_term']); - } + $doc->setVersionParams($data); } return $response; @@ -279,15 +271,7 @@ public function getDocument($id, array $options = []): Document } $doc = new Document($id, $data, $this->getName()); - if (isset($data['_version'])) { - $doc->setVersion($data['_version']); - } - if (isset($data['_seq_no'])) { - $doc->setSequenceNumber($data['_seq_no']); - } - if (isset($data['_primary_term'])) { - $doc->setPrimaryTerm($data['_primary_term']); - } + $doc->setVersionParams($data); return $doc; } From d794d2b9e21b56d3c77109c5864a7729065265f7 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 16:32:13 -0400 Subject: [PATCH 10/12] style fix --- src/AbstractUpdateAction.php | 46 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index 38f6ce70de..03601782e8 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -64,6 +64,30 @@ public function getIndex(): string return $this->getParam('_index'); } + /** + * Sets the version parameters of a document for use with optimistic concurrency control. + * + * @param array $responseData + * + * @return $this + */ + public function setVersionParams(array $responseData): self + { + if (isset($responseData['_version'])) { + $this->setVersion($responseData['_version']); + } + + if (isset($data['_seq_no'])) { + $this->setSequenceNumber($responseData['_seq_no']); + } + + if (isset($data['_primary_term'])) { + $this->setPrimaryTerm($responseData['_primary_term']); + } + + return $this; + } + /** * Sets the sequence number of a document for use with optimistic concurrency control. * @@ -438,26 +462,4 @@ public function getOptions(array $fields = []): array return \array_filter($this->getParams()); } - - /** - * @param array $responseData - * - * @return $this - */ - public function setVersionParams(array $responseData) :self - { - if (isset($responseData['_version'])) { - $this->setVersion($responseData['_version']); - } - - if (isset($data['_seq_no'])) { - $this->setSequenceNumber($responseData['_seq_no']); - } - - if (isset($data['_primary_term'])) { - $this->setPrimaryTerm($responseData['_primary_term']); - } - - return $this; - } } From 8863cee86a7f1cce93bc2c2e87c038168470bb8b Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Wed, 7 Oct 2020 16:57:40 -0400 Subject: [PATCH 11/12] phpcs fixes --- src/AbstractUpdateAction.php | 70 ------------------------------------ 1 file changed, 70 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index 03601782e8..268fda3792 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -67,8 +67,6 @@ public function getIndex(): string /** * Sets the version parameters of a document for use with optimistic concurrency control. * - * @param array $responseData - * * @return $this */ public function setVersionParams(array $responseData): self @@ -112,9 +110,6 @@ public function getSequenceNumber(): int return $this->getParam('if_seq_no'); } - /** - * @return bool - */ public function hasSequenceNumber(): bool { return $this->hasParam('if_seq_no'); @@ -144,9 +139,6 @@ public function getPrimaryTerm(): int return $this->getParam('if_primary_term'); } - /** - * @return bool - */ public function hasPrimaryTerm(): bool { return $this->hasParam('if_primary_term'); @@ -176,9 +168,6 @@ public function getVersion(): int return $this->getParam('_version'); } - /** - * @return bool - */ public function hasVersion(): bool { return $this->hasParam('_version'); @@ -198,17 +187,12 @@ public function setOpType(string $opType): self /** * Get operation type. - * - * @return string */ public function getOpType(): string { return $this->getParam('op_type'); } - /** - * @return bool - */ public function hasOpType(): bool { return $this->hasParam('op_type'); @@ -228,17 +212,12 @@ public function setRouting(string $value): self /** * Get routing parameter. - * - * @return string */ public function getRouting(): string { return $this->getParam('_routing'); } - /** - * @return bool - */ public function hasRouting(): bool { return $this->hasParam('_routing'); @@ -266,25 +245,17 @@ public function setFieldsSource(): self return $this->setFields('_source'); } - /** - * @return string - */ public function getFields(): string { return $this->getParam('fields'); } - /** - * @return bool - */ public function hasFields(): bool { return $this->hasParam('fields'); } /** - * @param int $num - * * @return $this */ public function setRetryOnConflict(int $num): self @@ -292,17 +263,11 @@ public function setRetryOnConflict(int $num): self return $this->setParam('retry_on_conflict', (int) $num); } - /** - * @return int - */ public function getRetryOnConflict(): int { return $this->getParam('retry_on_conflict'); } - /** - * @return bool - */ public function hasRetryOnConflict(): bool { return $this->hasParam('_retry_on_conflict'); @@ -334,17 +299,12 @@ public function getRefresh() : $refresh; } - /** - * @return bool - */ public function hasRefresh(): bool { return $this->hasParam('refresh'); } /** - * @param string $timeout - * * @return $this */ public function setTimeout(string $timeout): self @@ -352,25 +312,17 @@ public function setTimeout(string $timeout): self return $this->setParam('timeout', $timeout); } - /** - * @return string - */ public function getTimeout(): string { return $this->getParam('timeout'); } - /** - * @return bool - */ public function hasTimeout(): bool { return $this->hasParam('timeout'); } /** - * @param string $timeout - * * @return $this */ public function setConsistency(string $timeout): self @@ -378,25 +330,17 @@ public function setConsistency(string $timeout): self return $this->setParam('consistency', $timeout); } - /** - * @return string - */ public function getConsistency(): string { return $this->getParam('consistency'); } - /** - * @return bool - */ public function hasConsistency(): bool { return $this->hasParam('consistency'); } /** - * @param string $timeout - * * @return $this */ public function setReplication(string $timeout): self @@ -404,17 +348,11 @@ public function setReplication(string $timeout): self return $this->setParam('replication', $timeout); } - /** - * @return string - */ public function getReplication(): string { return $this->getParam('replication'); } - /** - * @return bool - */ public function hasReplication(): bool { return $this->hasParam('replication'); @@ -433,17 +371,11 @@ public function setUpsert($data): self return $this; } - /** - * @return Document - */ public function getUpsert(): Document { return $this->_upsert; } - /** - * @return bool - */ public function hasUpsert(): bool { return null !== $this->_upsert; @@ -451,8 +383,6 @@ public function hasUpsert(): bool /** * @param array $fields if empty array all options will be returned - * - * @return array */ public function getOptions(array $fields = []): array { From af0b1d5cf439292f81437f000b46586ce0133b09 Mon Sep 17 00:00:00 2001 From: Thomas Picquet Date: Thu, 8 Oct 2020 07:49:26 -0400 Subject: [PATCH 12/12] revert type hint changes --- src/AbstractUpdateAction.php | 132 +++++++++++++++++++++++++---------- src/Client.php | 2 - 2 files changed, 97 insertions(+), 37 deletions(-) diff --git a/src/AbstractUpdateAction.php b/src/AbstractUpdateAction.php index 268fda3792..d29a861549 100644 --- a/src/AbstractUpdateAction.php +++ b/src/AbstractUpdateAction.php @@ -59,7 +59,7 @@ public function setIndex($index): self * * @return string Index name */ - public function getIndex(): string + public function getIndex() { return $this->getParam('_index'); } @@ -153,24 +153,27 @@ public function hasPrimaryTerm(): bool * * @see https://www.elastic.co/blog/versioning */ - public function setVersion(int $version): self + public function setVersion($version) { - return $this->setParam('_version', $version); + return $this->setParam('version', (int) $version); } /** * Returns document version. * - * @return int Document version + * @return int|string Document version */ - public function getVersion(): int + public function getVersion() { - return $this->getParam('_version'); + return $this->getParam('version'); } - public function hasVersion(): bool + /** + * @return bool + */ + public function hasVersion() { - return $this->hasParam('_version'); + return $this->hasParam('version'); } /** @@ -180,20 +183,25 @@ public function hasVersion(): bool * * @return $this */ - public function setOpType(string $opType): self + public function setOpType($opType) { return $this->setParam('op_type', $opType); } /** * Get operation type. + * + * @return string */ - public function getOpType(): string + public function getOpType() { return $this->getParam('op_type'); } - public function hasOpType(): bool + /** + * @return bool + */ + public function hasOpType() { return $this->hasParam('op_type'); } @@ -205,20 +213,25 @@ public function hasOpType(): bool * * @return $this */ - public function setRouting(string $value): self + public function setRouting($value) { return $this->setParam('routing', $value); } /** * Get routing parameter. + * + * @return string */ - public function getRouting(): string + public function getRouting() { return $this->getParam('_routing'); } - public function hasRouting(): bool + /** + * @return bool + */ + public function hasRouting() { return $this->hasParam('_routing'); } @@ -228,7 +241,7 @@ public function hasRouting(): bool * * @return $this */ - public function setFields($fields): self + public function setFields($fields) { if (\is_array($fields)) { $fields = \implode(',', $fields); @@ -240,35 +253,49 @@ public function setFields($fields): self /** * @return $this */ - public function setFieldsSource(): self + public function setFieldsSource() { return $this->setFields('_source'); } - public function getFields(): string + /** + * @return string + */ + public function getFields() { return $this->getParam('fields'); } - public function hasFields(): bool + /** + * @return bool + */ + public function hasFields() { return $this->hasParam('fields'); } /** + * @param int $num + * * @return $this */ - public function setRetryOnConflict(int $num): self + public function setRetryOnConflict($num) { return $this->setParam('retry_on_conflict', (int) $num); } - public function getRetryOnConflict(): int + /** + * @return int + */ + public function getRetryOnConflict() { return $this->getParam('retry_on_conflict'); } - public function hasRetryOnConflict(): bool + /** + * @return bool + */ + public function hasRetryOnConflict() { return $this->hasParam('_retry_on_conflict'); } @@ -299,61 +326,88 @@ public function getRefresh() : $refresh; } - public function hasRefresh(): bool + /** + * @return bool + */ + public function hasRefresh() { return $this->hasParam('refresh'); } /** + * @param string $timeout + * * @return $this */ - public function setTimeout(string $timeout): self + public function setTimeout($timeout) { return $this->setParam('timeout', $timeout); } - public function getTimeout(): string + /** + * @return bool + */ + public function getTimeout() { return $this->getParam('timeout'); } - public function hasTimeout(): bool + /** + * @return bool + */ + public function hasTimeout() { return $this->hasParam('timeout'); } /** + * @param string $timeout + * * @return $this */ - public function setConsistency(string $timeout): self + public function setConsistency($timeout) { return $this->setParam('consistency', $timeout); } - public function getConsistency(): string + /** + * @return string + */ + public function getConsistency() { return $this->getParam('consistency'); } - public function hasConsistency(): bool + /** + * @return bool + */ + public function hasConsistency() { return $this->hasParam('consistency'); } /** + * @param string $timeout + * * @return $this */ - public function setReplication(string $timeout): self + public function setReplication($timeout) { return $this->setParam('replication', $timeout); } - public function getReplication(): string + /** + * @return string + */ + public function getReplication() { return $this->getParam('replication'); } - public function hasReplication(): bool + /** + * @return bool + */ + public function hasReplication() { return $this->hasParam('replication'); } @@ -363,7 +417,7 @@ public function hasReplication(): bool * * @return $this */ - public function setUpsert($data): self + public function setUpsert($data) { $document = Document::create($data); $this->_upsert = $document; @@ -371,20 +425,28 @@ public function setUpsert($data): self return $this; } - public function getUpsert(): Document + /** + * @return Document + */ + public function getUpsert() { return $this->_upsert; } - public function hasUpsert(): bool + /** + * @return bool + */ + public function hasUpsert() { return null !== $this->_upsert; } /** * @param array $fields if empty array all options will be returned + * + * @return array */ - public function getOptions(array $fields = []): array + public function getOptions(array $fields = []) { if (!empty($fields)) { return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields))); diff --git a/src/Client.php b/src/Client.php index ff42f8738e..0617fc4458 100644 --- a/src/Client.php +++ b/src/Client.php @@ -278,8 +278,6 @@ public function updateDocument($id, $data, $index, array $options = []): Respons $docOptions = $data->getOptions( [ 'consistency', - 'if_primary_term', - 'if_seq_no', 'parent', 'percolate', 'refresh',