2021-11-23 02:13:14 +00:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License
|
|
|
|
|
2024-03-11 06:45:02 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
#include <cstring>
|
|
|
|
|
2021-12-07 02:13:53 +00:00
|
|
|
#include <gtest/gtest.h>
|
2021-11-23 02:13:14 +00:00
|
|
|
#include <string.h>
|
2023-08-17 13:16:19 +00:00
|
|
|
#include <boost/uuid/uuid.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
2021-11-23 02:13:14 +00:00
|
|
|
|
2023-09-14 06:05:20 +00:00
|
|
|
#include "common/EasyAssert.h"
|
2023-12-18 04:04:42 +00:00
|
|
|
#include "common/Types.h"
|
2022-04-29 05:35:49 +00:00
|
|
|
#include "common/Utils.h"
|
2023-12-18 04:04:42 +00:00
|
|
|
#include "common/Exception.h"
|
2024-03-11 06:45:02 +00:00
|
|
|
#include "knowhere/sparse_utils.h"
|
|
|
|
#include "pb/schema.pb.h"
|
2022-04-29 05:35:49 +00:00
|
|
|
#include "query/Utils.h"
|
2022-05-25 14:06:00 +00:00
|
|
|
#include "test_utils/DataGen.h"
|
2021-11-23 02:13:14 +00:00
|
|
|
|
2022-04-29 05:35:49 +00:00
|
|
|
TEST(Util, StringMatch) {
|
|
|
|
using namespace milvus;
|
|
|
|
using namespace milvus::query;
|
|
|
|
|
|
|
|
ASSERT_ANY_THROW(Match(1, 2, OpType::PrefixMatch));
|
2023-03-10 01:47:54 +00:00
|
|
|
ASSERT_ANY_THROW(Match(std::string("not_match_operation"),
|
|
|
|
std::string("not_match"),
|
|
|
|
OpType::LessEqual));
|
2022-04-29 05:35:49 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(PrefixMatch("prefix1", "prefix"));
|
|
|
|
ASSERT_TRUE(PostfixMatch("1postfix", "postfix"));
|
2023-03-10 01:47:54 +00:00
|
|
|
ASSERT_TRUE(Match(
|
|
|
|
std::string("prefix1"), std::string("prefix"), OpType::PrefixMatch));
|
|
|
|
ASSERT_TRUE(Match(
|
|
|
|
std::string("1postfix"), std::string("postfix"), OpType::PostfixMatch));
|
2022-04-29 05:35:49 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(PrefixMatch("", "longer"));
|
|
|
|
ASSERT_FALSE(PostfixMatch("", "longer"));
|
|
|
|
|
|
|
|
ASSERT_FALSE(PrefixMatch("dontmatch", "prefix"));
|
|
|
|
ASSERT_FALSE(PostfixMatch("dontmatch", "postfix"));
|
|
|
|
}
|
2022-05-25 14:06:00 +00:00
|
|
|
|
2023-06-14 03:44:38 +00:00
|
|
|
TEST(Util, OutOfRange) {
|
|
|
|
using milvus::query::out_of_range;
|
|
|
|
|
|
|
|
ASSERT_FALSE(out_of_range<int32_t>(
|
|
|
|
static_cast<int64_t>(std::numeric_limits<int32_t>::max()) - 1));
|
|
|
|
ASSERT_FALSE(out_of_range<int32_t>(
|
|
|
|
static_cast<int64_t>(std::numeric_limits<int32_t>::min()) + 1));
|
|
|
|
|
|
|
|
ASSERT_TRUE(out_of_range<int32_t>(
|
|
|
|
static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1));
|
|
|
|
ASSERT_TRUE(out_of_range<int32_t>(
|
|
|
|
static_cast<int64_t>(std::numeric_limits<int32_t>::min()) - 1));
|
|
|
|
}
|
2023-08-03 07:25:09 +00:00
|
|
|
|
|
|
|
TEST(Util, upper_bound) {
|
|
|
|
using milvus::Timestamp;
|
|
|
|
using milvus::segcore::ConcurrentVector;
|
|
|
|
using milvus::segcore::upper_bound;
|
|
|
|
|
|
|
|
std::vector<Timestamp> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
|
|
|
ConcurrentVector<Timestamp> timestamps(1);
|
2024-03-11 06:45:02 +00:00
|
|
|
timestamps.set_data_raw(0, data.data(), data.size());
|
2023-08-03 07:25:09 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(1, upper_bound(timestamps, 0, data.size(), 0));
|
|
|
|
ASSERT_EQ(5, upper_bound(timestamps, 0, data.size(), 4));
|
|
|
|
ASSERT_EQ(10, upper_bound(timestamps, 0, data.size(), 10));
|
|
|
|
}
|
2023-08-17 13:16:19 +00:00
|
|
|
|
2023-10-17 18:24:09 +00:00
|
|
|
// A simple wrapper that removes a temporary file.
|
|
|
|
struct TmpFileWrapper {
|
|
|
|
int fd = -1;
|
|
|
|
std::string filename;
|
|
|
|
|
|
|
|
TmpFileWrapper(const std::string& _filename) : filename{_filename} {
|
2023-12-18 04:04:42 +00:00
|
|
|
fd = open(filename.c_str(),
|
|
|
|
O_RDWR | O_CREAT | O_EXCL,
|
|
|
|
S_IRUSR | S_IWUSR | S_IXUSR);
|
2023-10-17 18:24:09 +00:00
|
|
|
}
|
|
|
|
TmpFileWrapper(const TmpFileWrapper&) = delete;
|
|
|
|
TmpFileWrapper(TmpFileWrapper&&) = delete;
|
2023-12-18 04:04:42 +00:00
|
|
|
TmpFileWrapper&
|
|
|
|
operator=(const TmpFileWrapper&) = delete;
|
|
|
|
TmpFileWrapper&
|
|
|
|
operator=(TmpFileWrapper&&) = delete;
|
2023-10-17 18:24:09 +00:00
|
|
|
~TmpFileWrapper() {
|
|
|
|
if (fd != -1) {
|
|
|
|
close(fd);
|
|
|
|
remove(filename.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-17 13:16:19 +00:00
|
|
|
TEST(Util, read_from_fd) {
|
|
|
|
auto uuid = boost::uuids::random_generator()();
|
|
|
|
auto uuid_string = boost::uuids::to_string(uuid);
|
|
|
|
auto file = std::string("/tmp/") + uuid_string;
|
|
|
|
|
2023-10-17 18:24:09 +00:00
|
|
|
auto tmp_file = TmpFileWrapper(file);
|
|
|
|
ASSERT_NE(tmp_file.fd, -1);
|
|
|
|
|
2023-08-17 13:16:19 +00:00
|
|
|
size_t data_size = 100 * 1024 * 1024; // 100M
|
|
|
|
auto index_data = std::shared_ptr<uint8_t[]>(new uint8_t[data_size]);
|
|
|
|
auto max_loop = size_t(INT_MAX) / data_size + 1; // insert data > 2G
|
|
|
|
for (int i = 0; i < max_loop; ++i) {
|
2023-10-17 18:24:09 +00:00
|
|
|
auto size_write = write(tmp_file.fd, index_data.get(), data_size);
|
2023-08-17 13:16:19 +00:00
|
|
|
ASSERT_GE(size_write, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto read_buf =
|
|
|
|
std::shared_ptr<uint8_t[]>(new uint8_t[data_size * max_loop]);
|
|
|
|
EXPECT_NO_THROW(milvus::index::ReadDataFromFD(
|
2023-10-17 18:24:09 +00:00
|
|
|
tmp_file.fd, read_buf.get(), data_size * max_loop));
|
2023-08-17 13:16:19 +00:00
|
|
|
|
|
|
|
// On Linux, read() (and similar system calls) will transfer at most 0x7ffff000 (2,147,479,552) bytes once
|
2023-12-18 04:04:42 +00:00
|
|
|
EXPECT_THROW(
|
|
|
|
milvus::index::ReadDataFromFD(
|
|
|
|
tmp_file.fd, read_buf.get(), data_size * max_loop, INT_MAX),
|
|
|
|
milvus::SegcoreError);
|
2023-08-17 13:16:19 +00:00
|
|
|
}
|
2024-01-25 02:07:00 +00:00
|
|
|
|
|
|
|
TEST(Util, get_common_prefix) {
|
|
|
|
std::string str1 = "";
|
|
|
|
std::string str2 = "milvus";
|
|
|
|
auto common_prefix = milvus::GetCommonPrefix(str1, str2);
|
|
|
|
EXPECT_STREQ(common_prefix.c_str(), "");
|
|
|
|
|
|
|
|
str1 = "milvus";
|
|
|
|
str2 = "milvus is great";
|
|
|
|
common_prefix = milvus::GetCommonPrefix(str1, str2);
|
|
|
|
EXPECT_STREQ(common_prefix.c_str(), "milvus");
|
|
|
|
|
|
|
|
str1 = "milvus";
|
|
|
|
str2 = "";
|
|
|
|
common_prefix = milvus::GetCommonPrefix(str1, str2);
|
|
|
|
EXPECT_STREQ(common_prefix.c_str(), "");
|
2024-03-11 06:45:02 +00:00
|
|
|
}
|
2024-07-12 02:17:36 +00:00
|
|
|
|
|
|
|
TEST(Util, dis_closer){
|
|
|
|
EXPECT_TRUE(milvus::query::dis_closer(0.1, 0.2, "L2"));
|
|
|
|
EXPECT_FALSE(milvus::query::dis_closer(0.2, 0.1, "L2"));
|
|
|
|
EXPECT_FALSE(milvus::query::dis_closer(0.1, 0.1, "L2"));
|
|
|
|
|
|
|
|
EXPECT_TRUE(milvus::query::dis_closer(0.2, 0.1, "IP"));
|
|
|
|
EXPECT_FALSE(milvus::query::dis_closer(0.1, 0.2, "IP"));
|
|
|
|
EXPECT_FALSE(milvus::query::dis_closer(0.1, 0.1, "IP"));
|
|
|
|
}
|