diff options
| author | Yuriy Andamasov <yuriy@andamasov.com> | 2026-05-23 14:22:38 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-23 14:22:38 +0300 |
| commit | c75bb17724896fb0eff97fcefce01c3a2ce9d049 (patch) | |
| tree | ded9334a63052323a074c3531273ee1c667f8855 /src | |
| parent | 830ea6cff237235f3b0f21c58d54f062656eee45 (diff) | |
| parent | ed0dee0333acf8a1c79845bba6455506c8210bad (diff) | |
| download | phorge-elasticsearch-modern-c75bb17724896fb0eff97fcefce01c3a2ce9d049.tar.gz phorge-elasticsearch-modern-c75bb17724896fb0eff97fcefce01c3a2ce9d049.zip | |
Merge pull request #4 from vyos/task/e1-setversion
E1: Add setVersion() with strict validation
Diffstat (limited to 'src')
| -rw-r--r-- | src/__phutil_library_map__.php | 4 | ||||
| -rw-r--r-- | src/__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php | 69 | ||||
| -rw-r--r-- | src/engine/VyOSElasticModernFulltextStorageEngine.php | 25 |
3 files changed, 98 insertions, 0 deletions
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 298810e..291fb03 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -10,12 +10,16 @@ phutil_register_library_map(array( '__library_version__' => 2, 'class' => array( 'VyOSElasticModernFulltextStorageEngine' => 'engine/VyOSElasticModernFulltextStorageEngine.php', + 'VyOSElasticModernFulltextStorageEngineTestCase' => '__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php', + 'VyOSElasticModernFulltextStorageEngineTestDouble' => '__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php', 'VyOSElasticModernHost' => 'host/VyOSElasticModernHost.php', 'VyOSElasticModernHostTestCase' => '__tests__/VyOSElasticModernHostTestCase.php', ), 'function' => array(), 'xmap' => array( 'VyOSElasticModernFulltextStorageEngine' => 'PhabricatorFulltextStorageEngine', + 'VyOSElasticModernFulltextStorageEngineTestCase' => 'PhutilTestCase', + 'VyOSElasticModernFulltextStorageEngineTestDouble' => 'VyOSElasticModernFulltextStorageEngine', 'VyOSElasticModernHost' => 'PhabricatorSearchHost', 'VyOSElasticModernHostTestCase' => 'PhutilTestCase', ), diff --git a/src/__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php b/src/__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php new file mode 100644 index 0000000..7c0755c --- /dev/null +++ b/src/__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php @@ -0,0 +1,69 @@ +<?php + +/** + * Minimal concrete subclass for unit-testing the abstract engine. + * Only the abstract stubs from PhabricatorFulltextStorageEngine are + * satisfied here; no real Elasticsearch I/O occurs in tests. + */ +final class VyOSElasticModernFulltextStorageEngineTestDouble + extends VyOSElasticModernFulltextStorageEngine { + + public function reindexAbstractDocument( + PhabricatorSearchAbstractDocument $doc) { + // no-op in tests + } + + public function executeSearch(PhabricatorSavedQuery $query) { + return array(); + } + + public function indexExists() { + return false; + } + + public function getIndexStats() { + return array(); + } + +} + +final class VyOSElasticModernFulltextStorageEngineTestCase + extends PhutilTestCase { + + private function newEngine() { + return new VyOSElasticModernFulltextStorageEngineTestDouble(); + } + + public function testSetVersionAcceptsSevenAndAbove() { + foreach (array(7, 8, 9, 100) as $v) { + $engine = $this->newEngine(); + $caught = null; + try { + $engine->setVersion($v); + } catch (Exception $e) { + $caught = $e; + } + $this->assertEqual( + null, + $caught, + pht('Expected no exception for version=%d.', $v)); + $this->assertEqual($v, $engine->getVersion()); + } + } + + public function testSetVersionRejectsBelowSeven() { + foreach (array(0, 1, 2, 5, 6) as $v) { + $engine = $this->newEngine(); + $caught = null; + try { + $engine->setVersion($v); + } catch (Exception $e) { + $caught = $e; + } + $this->assertTrue( + $caught !== null, + pht('Expected an exception for version=%d.', $v)); + } + } + +} diff --git a/src/engine/VyOSElasticModernFulltextStorageEngine.php b/src/engine/VyOSElasticModernFulltextStorageEngine.php index f11eeab..2570311 100644 --- a/src/engine/VyOSElasticModernFulltextStorageEngine.php +++ b/src/engine/VyOSElasticModernFulltextStorageEngine.php @@ -7,6 +7,31 @@ abstract class VyOSElasticModernFulltextStorageEngine extends PhabricatorFulltextStorageEngine { + private $version = null; + + public function setVersion($version) { + $version = (int)$version; + if ($version < 7) { + throw new Exception( + pht( + 'Unsupported Elasticsearch version "%d" for the '. + '"elasticsearch-modern" engine. This engine supports version 7 '. + 'and above (Elasticsearch 7.x, 8.x, or OpenSearch 1.x/2.x/3.x). '. + 'For ES 5.x, use the bundled "elasticsearch" engine instead.', + $version)); + } + $this->version = $version; + return $this; + } + + public function getVersion() { + if ($this->version === null) { + throw new Exception( + pht('Version not configured; call setVersion() or setService() first.')); + } + return $this->version; + } + public function getEngineIdentifier() { return 'elasticsearch-modern'; } |
