From 09fd74ef74d6b6800de69848c35a1b581ee41371 Mon Sep 17 00:00:00 2001 From: inabao <37021995+inabao@users.noreply.github.com> Date: Thu, 16 Jan 2025 10:59:00 +0800 Subject: [PATCH] optimize the implementation logic of TestSerializeReaderSet (#332) Signed-off-by: jinjiabao.jjb --- tests/fixtures/test_reader.h | 50 ++++++++++++++++++++++++++++++++++++ tests/test_index.cpp | 16 +++++------- 2 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 tests/fixtures/test_reader.h diff --git a/tests/fixtures/test_reader.h b/tests/fixtures/test_reader.h new file mode 100644 index 00000000..70b521f0 --- /dev/null +++ b/tests/fixtures/test_reader.h @@ -0,0 +1,50 @@ + +// Copyright 2024-present the vsag project +// +// 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. + +#pragma once + +#include + +#include "vsag/binaryset.h" +#include "vsag/readerset.h" + +namespace fixtures { + +class TestReader : public vsag::Reader { +public: + TestReader(vsag::Binary binary) : binary_(binary) { + } + + void + Read(uint64_t offset, uint64_t len, void* dest) override { + memcpy((char*)dest, binary_.data.get() + offset, len); + } + + void + AsyncRead(uint64_t offset, uint64_t len, void* dest, vsag::CallBack callback) override { + Read(offset, len, dest); + callback(vsag::IOErrorCode::IO_SUCCESS, "success"); + } + + uint64_t + Size() const override { + return binary_.size; + } + +private: + vsag::Binary binary_; +}; + +} // namespace fixtures diff --git a/tests/test_index.cpp b/tests/test_index.cpp index 3dc79862..c9f4b183 100644 --- a/tests/test_index.cpp +++ b/tests/test_index.cpp @@ -17,6 +17,7 @@ #include "fixtures/memory_record_allocator.h" #include "fixtures/test_logger.h" +#include "fixtures/test_reader.h" #include "fixtures/thread_pool.h" #include "simd/fp32_simd.h" @@ -432,16 +433,13 @@ TestIndex::TestSerializeReaderSet(const IndexPtr& index_from, const std::string& search_param, const std::string& index_name, bool expected_success) { - auto dir = fixtures::TempDir("serialize"); - auto path = dir.GenerateRandomFile(); - std::ofstream outfile(path, std::ios::out | std::ios::binary); - auto serialize_index = index_from->Serialize(outfile); - REQUIRE(serialize_index.has_value() == expected_success); - outfile.close(); - vsag::ReaderSet rs; - auto reader = vsag::Factory::CreateLocalFileReader(path, 0, 0); - rs.Set(index_name, reader); + auto serialize_binary = index_from->Serialize(); + REQUIRE(serialize_binary.has_value() == expected_success); + auto binary_set = serialize_binary.value(); + for (const auto& key : binary_set.GetKeys()) { + rs.Set(key, std::make_shared(binary_set.Get(key))); + } auto deserialize_index = index_to->Deserialize(rs); REQUIRE(deserialize_index.has_value() == expected_success);