|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +#include "paimon/global_index/lucene/lucene_utils.h" |
| 19 | + |
| 20 | +#include <cstdlib> |
| 21 | +#include <filesystem> |
| 22 | +#include <memory> |
| 23 | +#include <optional> |
| 24 | +#include <string> |
| 25 | +#include <system_error> |
| 26 | +#include <thread> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "cppjieba/Jieba.hpp" |
| 30 | +#include "fmt/format.h" |
| 31 | +#include "gtest/gtest.h" |
| 32 | +#include "paimon/common/utils/path_util.h" |
| 33 | +#include "paimon/fs/file_system.h" |
| 34 | +#include "paimon/testing/utils/testharness.h" |
| 35 | +namespace paimon::lucene::test { |
| 36 | +class LuceneUtilsTest : public ::testing::Test { |
| 37 | + public: |
| 38 | + void SetUp() override { |
| 39 | + const char* dict_dir_env = std::getenv(kJiebaDictDirEnv); |
| 40 | + if (dict_dir_env != nullptr) { |
| 41 | + original_dict_dir_env_ = dict_dir_env; |
| 42 | + } |
| 43 | + ASSERT_OK_AND_ASSIGN(working_dir_, PathUtil::GetWorkingDirectory()); |
| 44 | + ASSERT_OK_AND_ASSIGN(std::string dict_dir, LuceneUtils::GetJiebaDictionaryDir()); |
| 45 | + default_dict_dir_ = std::filesystem::canonical(dict_dir); |
| 46 | + test_dir_ = paimon::test::UniqueTestDirectory::Create(); |
| 47 | + ASSERT_TRUE(test_dir_); |
| 48 | + } |
| 49 | + |
| 50 | + void TearDown() override { |
| 51 | + std::error_code ec; |
| 52 | + std::filesystem::current_path(working_dir_, ec); |
| 53 | + if (original_dict_dir_env_) { |
| 54 | + setenv(kJiebaDictDirEnv, original_dict_dir_env_->c_str(), /*overwrite=*/1); |
| 55 | + } else { |
| 56 | + unsetenv(kJiebaDictDirEnv); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + std::string CopyDictionary(const std::string& relative_dir) const { |
| 61 | + std::string dir = PathUtil::JoinPath(test_dir_->Str(), relative_dir); |
| 62 | + EXPECT_TRUE(paimon::test::TestUtil::CopyDirectory(default_dict_dir_, dir)); |
| 63 | + return dir; |
| 64 | + } |
| 65 | + |
| 66 | + protected: |
| 67 | + std::optional<std::string> original_dict_dir_env_; |
| 68 | + std::string working_dir_; |
| 69 | + std::filesystem::path default_dict_dir_; |
| 70 | + std::unique_ptr<paimon::test::UniqueTestDirectory> test_dir_; |
| 71 | +}; |
| 72 | + |
| 73 | +TEST_F(LuceneUtilsTest, TestGetJiebaSharesOneSegmenter) { |
| 74 | + std::string dict_dir = CopyDictionary("dict"); |
| 75 | + setenv(kJiebaDictDirEnv, dict_dir.c_str(), /*overwrite=*/1); |
| 76 | + |
| 77 | + constexpr size_t kThreadNum = 4; |
| 78 | + std::vector<std::shared_ptr<const cppjieba::Jieba>> jiebas(kThreadNum); |
| 79 | + std::vector<std::thread> threads; |
| 80 | + threads.reserve(kThreadNum); |
| 81 | + for (size_t i = 0; i < kThreadNum; ++i) { |
| 82 | + threads.emplace_back([&jiebas, i]() { |
| 83 | + ASSERT_OK_AND_ASSIGN(jiebas[i], LuceneUtils::GetJieba()); |
| 84 | + std::vector<std::string> words; |
| 85 | + jiebas[i]->CutForSearch("我爱机器学习", words); |
| 86 | + std::vector<std::string> expected = {"我", "爱", "机器", "学习"}; |
| 87 | + ASSERT_EQ(words, expected); |
| 88 | + }); |
| 89 | + } |
| 90 | + for (auto& thread : threads) { |
| 91 | + thread.join(); |
| 92 | + } |
| 93 | + ASSERT_TRUE(jiebas[0]); |
| 94 | + for (const auto& jieba : jiebas) { |
| 95 | + ASSERT_EQ(jieba, jiebas[0]); |
| 96 | + } |
| 97 | + |
| 98 | + std::weak_ptr<const cppjieba::Jieba> cached_jieba = jiebas[0]; |
| 99 | + jiebas.clear(); |
| 100 | + ASSERT_FALSE(cached_jieba.expired()); |
| 101 | + ASSERT_OK_AND_ASSIGN(std::shared_ptr<const cppjieba::Jieba> jieba, LuceneUtils::GetJieba()); |
| 102 | + ASSERT_EQ(jieba, cached_jieba.lock()); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_F(LuceneUtilsTest, TestGetJiebaResolvesRelativeDictionaryDir) { |
| 106 | + std::string relative_dir = default_dict_dir_.filename().string(); |
| 107 | + setenv(kJiebaDictDirEnv, relative_dir.c_str(), /*overwrite=*/1); |
| 108 | + std::filesystem::current_path(test_dir_->Str()); |
| 109 | + ASSERT_NOK_WITH_MSG(LuceneUtils::GetJieba(), |
| 110 | + fmt::format("cannot resolve dictionary dir {} for jieba", relative_dir)); |
| 111 | + |
| 112 | + std::string user_dict = PathUtil::JoinPath(CopyDictionary(relative_dir), "user.dict.utf8"); |
| 113 | + ASSERT_OK(test_dir_->GetFileSystem()->WriteFile(user_dict, "机器学习\n", /*overwrite=*/true)); |
| 114 | + ASSERT_OK_AND_ASSIGN(std::shared_ptr<const cppjieba::Jieba> jieba_with_user_word, |
| 115 | + LuceneUtils::GetJieba()); |
| 116 | + std::filesystem::current_path(default_dict_dir_.parent_path()); |
| 117 | + ASSERT_OK_AND_ASSIGN(std::shared_ptr<const cppjieba::Jieba> default_jieba, |
| 118 | + LuceneUtils::GetJieba()); |
| 119 | + ASSERT_NE(jieba_with_user_word, default_jieba); |
| 120 | + |
| 121 | + std::vector<std::string> words; |
| 122 | + jieba_with_user_word->Cut("机器学习", words); |
| 123 | + ASSERT_EQ(words, std::vector<std::string>({"机器学习"})); |
| 124 | + default_jieba->Cut("机器学习", words); |
| 125 | + ASSERT_EQ(words, std::vector<std::string>({"机器", "学习"})); |
| 126 | + |
| 127 | + setenv(kJiebaDictDirEnv, default_dict_dir_.c_str(), /*overwrite=*/1); |
| 128 | + ASSERT_OK_AND_ASSIGN(std::shared_ptr<const cppjieba::Jieba> jieba, LuceneUtils::GetJieba()); |
| 129 | + ASSERT_EQ(jieba, default_jieba); |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace paimon::lucene::test |
0 commit comments