github-actions[bot] commented on code in PR #32418: URL: https://github.com/apache/doris/pull/32418#discussion_r1528444470
########## be/src/olap/rowset/segment_v2/inverted_index_cache.h: ########## @@ -17,59 +17,31 @@ #pragma once -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wshadow-field" -#endif -#include <CLucene.h> // IWYU pragma: keep -#ifdef __clang__ -#pragma clang diagnostic pop -#endif -#include <CLucene/config/repl_wchar.h> -#include <CLucene/util/Misc.h> #include <butil/macros.h> Review Comment: warning: 'butil/macros.h' file not found [clang-diagnostic-error] ```cpp #include <butil/macros.h> ^ ``` ########## be/src/index-tools/index_tool.cpp: ########## @@ -18,26 +18,44 @@ #include <CLucene.h> Review Comment: warning: 'CLucene.h' file not found [clang-diagnostic-error] ```cpp #include <CLucene.h> ^ ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp: ########## @@ -0,0 +1,272 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_reader.h" + +#include <memory> +#include <utility> + +#include "olap/rowset/segment_v2/inverted_index_compound_reader.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" + +namespace doris::segment_v2 { + +Status InvertedIndexFileReader::init(int32_t read_buffer_size, bool open_idx_file_cache) { + _read_buffer_size = read_buffer_size; + _open_idx_file_cache = open_idx_file_cache; + if (_storage_format == InvertedIndexStorageFormatPB::V2) { + return _init_from_v2(read_buffer_size); + } else { + return Status::OK(); + } +} + +Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { + std::unique_lock<std::shared_mutex> lock(_mutex); // Lock for writing + auto index_file_full_path = _index_file_dir / _index_file_name; + try { + bool exists = false; + RETURN_IF_ERROR(_fs->exists(index_file_full_path, &exists)); + if (!exists) { + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "inverted index file {} is not found", index_file_full_path.native()); + } + int64_t file_size = 0; + RETURN_IF_ERROR(_fs->file_size(index_file_full_path, &file_size)); + if (file_size == 0) { + LOG(WARNING) << "inverted index file " << index_file_full_path << " is empty."; + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "inverted index file {} is empty", index_file_full_path.native()); + } + + CLuceneError err; + CL_NS(store)::IndexInput* index_input = nullptr; + auto ok = DorisFSDirectory::FSIndexInput::open(_fs, index_file_full_path.c_str(), + index_input, err, read_buffer_size); + if (!ok) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + index_input->setIdxFileCache(_open_idx_file_cache); + _stream = std::unique_ptr<CL_NS(store)::IndexInput>(index_input); + int32_t version = _stream->readInt(); // Read version number + if (version == InvertedIndexStorageFormatPB::V2) { + DCHECK(version == _storage_format); + int32_t numIndices = _stream->readInt(); // Read number of indices + ReaderFileEntry* entry = nullptr; + + for (int32_t i = 0; i < numIndices; ++i) { + int64_t indexId = _stream->readInt(); // Read index ID + int32_t suffix_length = _stream->readInt(); // Read suffix length + std::vector<uint8_t> suffix_data(suffix_length); + _stream->readBytes(suffix_data.data(), suffix_length); + std::string suffix_str(suffix_data.begin(), suffix_data.end()); + + int32_t numFiles = _stream->readInt(); // Read number of files in the index + + // true, true means it will deconstruct key and value + auto fileEntries = std::make_unique<EntriesType>(true, true); + + for (int32_t j = 0; j < numFiles; ++j) { + entry = _CLNEW ReaderFileEntry(); + + int32_t file_name_length = _stream->readInt(); + // aid will destruct in EntriesType map. + char* aid = (char*)malloc(file_name_length + 1); + _stream->readBytes(reinterpret_cast<uint8_t*>(aid), file_name_length); + aid[file_name_length] = '\0'; + //stream->readString(tid, CL_MAX_PATH); + entry->file_name = std::string(aid, file_name_length); + entry->offset = _stream->readLong(); + entry->length = _stream->readLong(); + + fileEntries->put(aid, entry); + } + + _indices_entries.emplace(std::make_pair(indexId, std::move(suffix_str)), + std::move(fileEntries)); + } + } else { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "unknown inverted index format {}", version); + } + } catch (CLuceneError& err) { + if (_stream != nullptr) { + try { + _stream->close(); + } catch (CLuceneError& err) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when close idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + } + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when init idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + return Status::OK(); +} + +Result<InvertedIndexDirectoryMap> InvertedIndexFileReader::get_all_directories() { + InvertedIndexDirectoryMap res; + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + for (auto& index : _indices_entries) { + auto index_id = index.first.first; + auto index_suffix = index.first.second; + LOG(INFO) << "index_id:" << index_id << " index_suffix:" << index_suffix; + auto ret = _open(index_id, index_suffix); + if (!ret.has_value()) { + return ResultError(ret.error()); + } + res.emplace(std::make_pair(index_id, index_suffix), std::move(ret.value())); + } + return res; +} + +Result<std::unique_ptr<DorisCompoundReader>> InvertedIndexFileReader::_open( + int64_t index_id, std::string& index_suffix) const { + std::unique_ptr<DorisCompoundReader> compound_reader; + + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + DorisFSDirectory* dir = nullptr; + auto file_name = InvertedIndexDescriptor::get_index_file_name(_segment_file_name, index_id, + index_suffix); + try { + dir = DorisFSDirectoryFactory::getDirectory(_fs, _index_file_dir.c_str()); + + compound_reader = std::make_unique<DorisCompoundReader>( + dir, file_name.c_str(), _read_buffer_size, _open_idx_file_cache); + } catch (CLuceneError& err) { + if (dir != nullptr) { + dir->close(); + _CLDELETE(dir) + } + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + (_index_file_dir / file_name).native(), err.what())); + } + } else { + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + if (_stream == nullptr) { + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "CLuceneError occur when open idx file {}, stream is nullptr", + (_index_file_dir / _index_file_name).native())); + } + + // Check if the specified index exists + auto index_it = _indices_entries.find(std::make_pair(index_id, index_suffix)); + if (index_it == _indices_entries.end()) { + std::ostringstream errMsg; + errMsg << "No index with id " << index_id << " found"; + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + (_index_file_dir / _index_file_name).native(), errMsg.str())); + } + // Need to clone resource here, because index searcher cache need it. + bool own_index_input = true; + compound_reader = std::make_unique<DorisCompoundReader>( + _stream->clone(), index_it->second.get(), own_index_input, _read_buffer_size); + } + return compound_reader; +} +Result<std::unique_ptr<DorisCompoundReader>> InvertedIndexFileReader::open( + const TabletIndex* index_meta) const { + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + return _open(index_id, index_suffix); +} + +std::string InvertedIndexFileReader::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileReader::index_file_exist(const TabletIndex* index_meta, bool* res) const { + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + auto index_file_path = _index_file_dir / InvertedIndexDescriptor::get_index_file_name( + _segment_file_name, index_meta->index_id(), + index_meta->get_index_suffix()); + return _fs->exists(index_file_path, res); + } else { + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + if (_stream == nullptr) { + *res = false; + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "idx file {} is not opened", (_index_file_dir / _index_file_name).native()); + } + // Check if the specified index exists + auto index_it = _indices_entries.find( + std::make_pair(index_meta->index_id(), index_meta->get_index_suffix())); + if (index_it == _indices_entries.end()) { + *res = false; + } else { + *res = true; + } + } + return Status::OK(); +} + +Status InvertedIndexFileReader::has_null(const TabletIndex* index_meta, bool* res) const { + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + *res = true; + return Status::OK(); + } + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + if (_stream == nullptr) { + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "idx file {} is not opened", (_index_file_dir / _index_file_name).native()); + } + // Check if the specified index exists + auto index_it = _indices_entries.find( + std::make_pair(index_meta->index_id(), index_meta->get_index_suffix())); + if (index_it == _indices_entries.end()) { + *res = false; + } else { + auto null_bitmap_file_name = InvertedIndexDescriptor::get_temporary_null_bitmap_file_name(); + auto* entries = index_it->second.get(); + ReaderFileEntry* e = entries->get((char*)(null_bitmap_file_name.c_str())); + if (e == nullptr) { + *res = false; + return Status::OK(); + } + // roaring bitmap cookie header size is 5 + if (e->length <= 5) { + *res = false; + } else { + *res = true; + } + } + return Status::OK(); +} + +void InvertedIndexFileReader::debug_file_entries() { Review Comment: warning: method 'debug_file_entries' can be made static [readability-convert-member-functions-to-static] be/src/olap/rowset/segment_v2/inverted_index_file_reader.h:67: ```diff - void debug_file_entries(); + static void debug_file_entries(); ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp: ########## @@ -0,0 +1,272 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_reader.h" + +#include <memory> +#include <utility> + +#include "olap/rowset/segment_v2/inverted_index_compound_reader.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" + +namespace doris::segment_v2 { + +Status InvertedIndexFileReader::init(int32_t read_buffer_size, bool open_idx_file_cache) { + _read_buffer_size = read_buffer_size; + _open_idx_file_cache = open_idx_file_cache; + if (_storage_format == InvertedIndexStorageFormatPB::V2) { + return _init_from_v2(read_buffer_size); + } else { + return Status::OK(); + } +} + +Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { + std::unique_lock<std::shared_mutex> lock(_mutex); // Lock for writing + auto index_file_full_path = _index_file_dir / _index_file_name; + try { + bool exists = false; + RETURN_IF_ERROR(_fs->exists(index_file_full_path, &exists)); + if (!exists) { + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "inverted index file {} is not found", index_file_full_path.native()); + } + int64_t file_size = 0; + RETURN_IF_ERROR(_fs->file_size(index_file_full_path, &file_size)); + if (file_size == 0) { + LOG(WARNING) << "inverted index file " << index_file_full_path << " is empty."; + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "inverted index file {} is empty", index_file_full_path.native()); + } + + CLuceneError err; + CL_NS(store)::IndexInput* index_input = nullptr; + auto ok = DorisFSDirectory::FSIndexInput::open(_fs, index_file_full_path.c_str(), + index_input, err, read_buffer_size); + if (!ok) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + index_input->setIdxFileCache(_open_idx_file_cache); + _stream = std::unique_ptr<CL_NS(store)::IndexInput>(index_input); + int32_t version = _stream->readInt(); // Read version number + if (version == InvertedIndexStorageFormatPB::V2) { + DCHECK(version == _storage_format); + int32_t numIndices = _stream->readInt(); // Read number of indices + ReaderFileEntry* entry = nullptr; + + for (int32_t i = 0; i < numIndices; ++i) { + int64_t indexId = _stream->readInt(); // Read index ID + int32_t suffix_length = _stream->readInt(); // Read suffix length + std::vector<uint8_t> suffix_data(suffix_length); + _stream->readBytes(suffix_data.data(), suffix_length); + std::string suffix_str(suffix_data.begin(), suffix_data.end()); + + int32_t numFiles = _stream->readInt(); // Read number of files in the index + + // true, true means it will deconstruct key and value + auto fileEntries = std::make_unique<EntriesType>(true, true); + + for (int32_t j = 0; j < numFiles; ++j) { + entry = _CLNEW ReaderFileEntry(); + + int32_t file_name_length = _stream->readInt(); + // aid will destruct in EntriesType map. + char* aid = (char*)malloc(file_name_length + 1); + _stream->readBytes(reinterpret_cast<uint8_t*>(aid), file_name_length); + aid[file_name_length] = '\0'; + //stream->readString(tid, CL_MAX_PATH); + entry->file_name = std::string(aid, file_name_length); + entry->offset = _stream->readLong(); + entry->length = _stream->readLong(); + + fileEntries->put(aid, entry); + } + + _indices_entries.emplace(std::make_pair(indexId, std::move(suffix_str)), + std::move(fileEntries)); + } + } else { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "unknown inverted index format {}", version); + } + } catch (CLuceneError& err) { + if (_stream != nullptr) { + try { + _stream->close(); + } catch (CLuceneError& err) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when close idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + } + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when init idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + return Status::OK(); +} + +Result<InvertedIndexDirectoryMap> InvertedIndexFileReader::get_all_directories() { + InvertedIndexDirectoryMap res; + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + for (auto& index : _indices_entries) { + auto index_id = index.first.first; + auto index_suffix = index.first.second; + LOG(INFO) << "index_id:" << index_id << " index_suffix:" << index_suffix; + auto ret = _open(index_id, index_suffix); + if (!ret.has_value()) { + return ResultError(ret.error()); + } + res.emplace(std::make_pair(index_id, index_suffix), std::move(ret.value())); + } + return res; +} + +Result<std::unique_ptr<DorisCompoundReader>> InvertedIndexFileReader::_open( + int64_t index_id, std::string& index_suffix) const { + std::unique_ptr<DorisCompoundReader> compound_reader; + + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + DorisFSDirectory* dir = nullptr; + auto file_name = InvertedIndexDescriptor::get_index_file_name(_segment_file_name, index_id, + index_suffix); + try { + dir = DorisFSDirectoryFactory::getDirectory(_fs, _index_file_dir.c_str()); + + compound_reader = std::make_unique<DorisCompoundReader>( + dir, file_name.c_str(), _read_buffer_size, _open_idx_file_cache); + } catch (CLuceneError& err) { + if (dir != nullptr) { + dir->close(); + _CLDELETE(dir) + } + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + (_index_file_dir / file_name).native(), err.what())); + } + } else { + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + if (_stream == nullptr) { + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "CLuceneError occur when open idx file {}, stream is nullptr", + (_index_file_dir / _index_file_name).native())); + } + + // Check if the specified index exists + auto index_it = _indices_entries.find(std::make_pair(index_id, index_suffix)); + if (index_it == _indices_entries.end()) { + std::ostringstream errMsg; + errMsg << "No index with id " << index_id << " found"; + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + (_index_file_dir / _index_file_name).native(), errMsg.str())); + } + // Need to clone resource here, because index searcher cache need it. + bool own_index_input = true; + compound_reader = std::make_unique<DorisCompoundReader>( + _stream->clone(), index_it->second.get(), own_index_input, _read_buffer_size); + } + return compound_reader; +} +Result<std::unique_ptr<DorisCompoundReader>> InvertedIndexFileReader::open( + const TabletIndex* index_meta) const { + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + return _open(index_id, index_suffix); +} + +std::string InvertedIndexFileReader::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileReader::index_file_exist(const TabletIndex* index_meta, bool* res) const { Review Comment: warning: method 'index_file_exist' can be made static [readability-convert-member-functions-to-static] be/src/olap/rowset/segment_v2/inverted_index_file_reader.h:69: ```diff - Status index_file_exist(const TabletIndex* index_meta, bool* res) const; + static Status index_file_exist(const TabletIndex* index_meta, bool* res) ; ``` ```suggestion Status InvertedIndexFileReader::index_file_exist(const TabletIndex* index_meta, bool* res) { ``` ########## be/src/olap/rowset/segment_v2/inverted_index_searcher.cpp: ########## @@ -21,12 +21,12 @@ #include <CLucene/util/bkd/bkd_reader.h> #include "common/config.h" -#include "olap/rowset/segment_v2/inverted_index_compound_directory.h" #include "olap/rowset/segment_v2/inverted_index_compound_reader.h" #include "olap/rowset/segment_v2/inverted_index_desc.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" namespace doris::segment_v2 { -Status FulltextIndexSearcherBuilder::build(DorisCompoundReader* directory, +Status FulltextIndexSearcherBuilder::build(lucene::store::Directory* directory, Review Comment: warning: method 'build' can be made static [readability-convert-member-functions-to-static] ```suggestion static Status FulltextIndexSearcherBuilder::build(lucene::store::Directory* directory, ``` ########## be/src/olap/rowset/segment_v2/segment.cpp: ########## @@ -115,6 +116,21 @@ Status Segment::_open() { return Status::OK(); } +Status Segment::_open_inverted_index() { Review Comment: warning: method '_open_inverted_index' can be made static [readability-convert-member-functions-to-static] be/src/olap/rowset/segment_v2/segment.h:204: ```diff - Status _open_inverted_index(); + static Status _open_inverted_index(); ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp: ########## @@ -0,0 +1,272 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_reader.h" + +#include <memory> +#include <utility> + +#include "olap/rowset/segment_v2/inverted_index_compound_reader.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" + +namespace doris::segment_v2 { + +Status InvertedIndexFileReader::init(int32_t read_buffer_size, bool open_idx_file_cache) { + _read_buffer_size = read_buffer_size; + _open_idx_file_cache = open_idx_file_cache; + if (_storage_format == InvertedIndexStorageFormatPB::V2) { + return _init_from_v2(read_buffer_size); + } else { + return Status::OK(); + } +} + +Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { Review Comment: warning: method '_init_from_v2' can be made static [readability-convert-member-functions-to-static] be/src/olap/rowset/segment_v2/inverted_index_file_reader.h:74: ```diff - Status _init_from_v2(int32_t read_buffer_size); + static Status _init_from_v2(int32_t read_buffer_size); ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp: ########## @@ -0,0 +1,272 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_reader.h" + +#include <memory> +#include <utility> + +#include "olap/rowset/segment_v2/inverted_index_compound_reader.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" + +namespace doris::segment_v2 { + +Status InvertedIndexFileReader::init(int32_t read_buffer_size, bool open_idx_file_cache) { + _read_buffer_size = read_buffer_size; + _open_idx_file_cache = open_idx_file_cache; + if (_storage_format == InvertedIndexStorageFormatPB::V2) { + return _init_from_v2(read_buffer_size); + } else { + return Status::OK(); + } +} + +Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { Review Comment: warning: function '_init_from_v2' exceeds recommended size/complexity thresholds [readability-function-size] ```cpp Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { ^ ``` <details> <summary>Additional context</summary> **be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp:38:** 85 lines including whitespace and comments (threshold 80) ```cpp Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { ^ ``` </details> ########## be/src/olap/rowset/segment_v2/inverted_index_compaction.h: ########## @@ -16,19 +16,23 @@ // under the License. #pragma once +#include <CLucene.h> Review Comment: warning: 'CLucene.h' file not found [clang-diagnostic-error] ```cpp #include <CLucene.h> ^ ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_reader.h: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 <CLucene.h> // IWYU pragma: keep Review Comment: warning: 'CLucene.h' file not found [clang-diagnostic-error] ```cpp #include <CLucene.h> // IWYU pragma: keep ^ ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_writer.cpp: ########## @@ -0,0 +1,414 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_writer.h" + +#include "common/status.h" +#include "io/fs/file_writer.h" +#include "io/fs/local_file_system.h" +#include "olap/rowset/segment_v2/inverted_index_desc.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" +#include "runtime/exec_env.h" + +namespace doris::segment_v2 { + +std::string InvertedIndexFileWriter::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileWriter::initialize(InvertedIndexDirectoryMap& indices_dirs) { + _indices_dirs = std::move(indices_dirs); + return Status::OK(); +} + +Result<DorisFSDirectory*> InvertedIndexFileWriter::open(const TabletIndex* index_meta) { + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + auto tmp_file_dir = ExecEnv::GetInstance()->get_tmp_file_dirs()->get_tmp_file_dir(); + _lfs = io::global_local_filesystem(); + auto lfs_index_path = InvertedIndexDescriptor::get_temporary_index_path( + tmp_file_dir / _segment_file_name, index_meta->index_id(), + index_meta->get_index_suffix()); + auto index_path = InvertedIndexDescriptor::get_temporary_index_path( + (_index_file_dir / _segment_file_name).native(), index_id, index_suffix); + + bool exists = false; + auto st = _fs->exists(lfs_index_path.c_str(), &exists); + if (!st.ok()) { + LOG(ERROR) << "index_path:" << lfs_index_path << " exists error:" << st; + return ResultError(st); + } + if (exists) { + LOG(ERROR) << "try to init a directory:" << lfs_index_path << " already exists"; + return ResultError(Status::InternalError("init_fulltext_index directory already exists")); + } + + bool can_use_ram_dir = true; + bool use_compound_file_writer = false; + auto* dir = DorisFSDirectoryFactory::getDirectory(_lfs, lfs_index_path.c_str(), + use_compound_file_writer, can_use_ram_dir, + nullptr, _fs, index_path.c_str()); + _indices_dirs.emplace(std::make_pair(index_id, index_suffix), + std::unique_ptr<DorisFSDirectory>(dir)); + return dir; +} + +Status InvertedIndexFileWriter::delete_index(const TabletIndex* index_meta) { + if (!index_meta) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>("Index metadata is null."); + } + + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + + // Check if the specified index exists + auto index_it = _indices_dirs.find(std::make_pair(index_id, index_suffix)); + if (index_it == _indices_dirs.end()) { + std::ostringstream errMsg; + errMsg << "No inverted index with id " << index_id << " and suffix " << index_suffix + << " found."; + LOG(WARNING) << errMsg.str(); + return Status::OK(); + } + + _indices_dirs.erase(index_it); + return Status::OK(); +} + +size_t InvertedIndexFileWriter::headerLength() { Review Comment: warning: method 'headerLength' can be made static [readability-convert-member-functions-to-static] be/src/olap/rowset/segment_v2/inverted_index_file_writer.h:77: ```diff - size_t headerLength(); + static size_t headerLength(); ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp: ########## @@ -0,0 +1,272 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_reader.h" + +#include <memory> +#include <utility> + +#include "olap/rowset/segment_v2/inverted_index_compound_reader.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" + +namespace doris::segment_v2 { + +Status InvertedIndexFileReader::init(int32_t read_buffer_size, bool open_idx_file_cache) { + _read_buffer_size = read_buffer_size; + _open_idx_file_cache = open_idx_file_cache; + if (_storage_format == InvertedIndexStorageFormatPB::V2) { + return _init_from_v2(read_buffer_size); + } else { + return Status::OK(); + } +} + +Status InvertedIndexFileReader::_init_from_v2(int32_t read_buffer_size) { + std::unique_lock<std::shared_mutex> lock(_mutex); // Lock for writing + auto index_file_full_path = _index_file_dir / _index_file_name; + try { + bool exists = false; + RETURN_IF_ERROR(_fs->exists(index_file_full_path, &exists)); + if (!exists) { + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "inverted index file {} is not found", index_file_full_path.native()); + } + int64_t file_size = 0; + RETURN_IF_ERROR(_fs->file_size(index_file_full_path, &file_size)); + if (file_size == 0) { + LOG(WARNING) << "inverted index file " << index_file_full_path << " is empty."; + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "inverted index file {} is empty", index_file_full_path.native()); + } + + CLuceneError err; + CL_NS(store)::IndexInput* index_input = nullptr; + auto ok = DorisFSDirectory::FSIndexInput::open(_fs, index_file_full_path.c_str(), + index_input, err, read_buffer_size); + if (!ok) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + index_input->setIdxFileCache(_open_idx_file_cache); + _stream = std::unique_ptr<CL_NS(store)::IndexInput>(index_input); + int32_t version = _stream->readInt(); // Read version number + if (version == InvertedIndexStorageFormatPB::V2) { + DCHECK(version == _storage_format); + int32_t numIndices = _stream->readInt(); // Read number of indices + ReaderFileEntry* entry = nullptr; + + for (int32_t i = 0; i < numIndices; ++i) { + int64_t indexId = _stream->readInt(); // Read index ID + int32_t suffix_length = _stream->readInt(); // Read suffix length + std::vector<uint8_t> suffix_data(suffix_length); + _stream->readBytes(suffix_data.data(), suffix_length); + std::string suffix_str(suffix_data.begin(), suffix_data.end()); + + int32_t numFiles = _stream->readInt(); // Read number of files in the index + + // true, true means it will deconstruct key and value + auto fileEntries = std::make_unique<EntriesType>(true, true); + + for (int32_t j = 0; j < numFiles; ++j) { + entry = _CLNEW ReaderFileEntry(); + + int32_t file_name_length = _stream->readInt(); + // aid will destruct in EntriesType map. + char* aid = (char*)malloc(file_name_length + 1); + _stream->readBytes(reinterpret_cast<uint8_t*>(aid), file_name_length); + aid[file_name_length] = '\0'; + //stream->readString(tid, CL_MAX_PATH); + entry->file_name = std::string(aid, file_name_length); + entry->offset = _stream->readLong(); + entry->length = _stream->readLong(); + + fileEntries->put(aid, entry); + } + + _indices_entries.emplace(std::make_pair(indexId, std::move(suffix_str)), + std::move(fileEntries)); + } + } else { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "unknown inverted index format {}", version); + } + } catch (CLuceneError& err) { + if (_stream != nullptr) { + try { + _stream->close(); + } catch (CLuceneError& err) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when close idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + } + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when init idx file {}, error msg: {}", + index_file_full_path.native(), err.what()); + } + return Status::OK(); +} + +Result<InvertedIndexDirectoryMap> InvertedIndexFileReader::get_all_directories() { + InvertedIndexDirectoryMap res; + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + for (auto& index : _indices_entries) { + auto index_id = index.first.first; + auto index_suffix = index.first.second; + LOG(INFO) << "index_id:" << index_id << " index_suffix:" << index_suffix; + auto ret = _open(index_id, index_suffix); + if (!ret.has_value()) { + return ResultError(ret.error()); + } + res.emplace(std::make_pair(index_id, index_suffix), std::move(ret.value())); + } + return res; +} + +Result<std::unique_ptr<DorisCompoundReader>> InvertedIndexFileReader::_open( + int64_t index_id, std::string& index_suffix) const { + std::unique_ptr<DorisCompoundReader> compound_reader; + + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + DorisFSDirectory* dir = nullptr; + auto file_name = InvertedIndexDescriptor::get_index_file_name(_segment_file_name, index_id, + index_suffix); + try { + dir = DorisFSDirectoryFactory::getDirectory(_fs, _index_file_dir.c_str()); + + compound_reader = std::make_unique<DorisCompoundReader>( + dir, file_name.c_str(), _read_buffer_size, _open_idx_file_cache); + } catch (CLuceneError& err) { + if (dir != nullptr) { + dir->close(); + _CLDELETE(dir) + } + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + (_index_file_dir / file_name).native(), err.what())); + } + } else { + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + if (_stream == nullptr) { + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "CLuceneError occur when open idx file {}, stream is nullptr", + (_index_file_dir / _index_file_name).native())); + } + + // Check if the specified index exists + auto index_it = _indices_entries.find(std::make_pair(index_id, index_suffix)); + if (index_it == _indices_entries.end()) { + std::ostringstream errMsg; + errMsg << "No index with id " << index_id << " found"; + return ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when open idx file {}, error msg: {}", + (_index_file_dir / _index_file_name).native(), errMsg.str())); + } + // Need to clone resource here, because index searcher cache need it. + bool own_index_input = true; + compound_reader = std::make_unique<DorisCompoundReader>( + _stream->clone(), index_it->second.get(), own_index_input, _read_buffer_size); + } + return compound_reader; +} +Result<std::unique_ptr<DorisCompoundReader>> InvertedIndexFileReader::open( + const TabletIndex* index_meta) const { + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + return _open(index_id, index_suffix); +} + +std::string InvertedIndexFileReader::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileReader::index_file_exist(const TabletIndex* index_meta, bool* res) const { + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + auto index_file_path = _index_file_dir / InvertedIndexDescriptor::get_index_file_name( + _segment_file_name, index_meta->index_id(), + index_meta->get_index_suffix()); + return _fs->exists(index_file_path, res); + } else { + std::shared_lock<std::shared_mutex> lock(_mutex); // Lock for reading + if (_stream == nullptr) { + *res = false; + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( + "idx file {} is not opened", (_index_file_dir / _index_file_name).native()); + } + // Check if the specified index exists + auto index_it = _indices_entries.find( + std::make_pair(index_meta->index_id(), index_meta->get_index_suffix())); + if (index_it == _indices_entries.end()) { + *res = false; + } else { + *res = true; + } + } + return Status::OK(); +} + +Status InvertedIndexFileReader::has_null(const TabletIndex* index_meta, bool* res) const { Review Comment: warning: method 'has_null' can be made static [readability-convert-member-functions-to-static] be/src/olap/rowset/segment_v2/inverted_index_file_reader.h:70: ```diff - Status has_null(const TabletIndex* index_meta, bool* res) const; + static Status has_null(const TabletIndex* index_meta, bool* res) ; ``` ```suggestion Status InvertedIndexFileReader::has_null(const TabletIndex* index_meta, bool* res) { ``` ########## be/src/olap/rowset/segment_v2/inverted_index_reader.h: ########## @@ -18,7 +18,6 @@ #pragma once #include <CLucene/util/bkd/bkd_reader.h> Review Comment: warning: 'CLucene/util/bkd/bkd_reader.h' file not found [clang-diagnostic-error] ```cpp #include <CLucene/util/bkd/bkd_reader.h> ^ ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_writer.h: ########## @@ -0,0 +1,97 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 <CLucene.h> // IWYU pragma: keep Review Comment: warning: 'CLucene.h' file not found [clang-diagnostic-error] ```cpp #include <CLucene.h> // IWYU pragma: keep ^ ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_writer.cpp: ########## @@ -0,0 +1,414 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_writer.h" + +#include "common/status.h" +#include "io/fs/file_writer.h" +#include "io/fs/local_file_system.h" +#include "olap/rowset/segment_v2/inverted_index_desc.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" +#include "runtime/exec_env.h" + +namespace doris::segment_v2 { + +std::string InvertedIndexFileWriter::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileWriter::initialize(InvertedIndexDirectoryMap& indices_dirs) { Review Comment: warning: method 'initialize' can be made static [readability-convert-member-functions-to-static] ```suggestion static Status InvertedIndexFileWriter::initialize(InvertedIndexDirectoryMap& indices_dirs) { ``` ########## be/src/olap/tablet_schema.h: ########## @@ -333,6 +333,14 @@ class TabletSchema { segment_v2::CompressionTypePB compression_type() const { return _compression_type; } const std::vector<TabletIndex>& indexes() const { return _indexes; } + bool has_inverted_index() const { Review Comment: warning: method 'has_inverted_index' can be made static [readability-convert-member-functions-to-static] ```suggestion static bool has_inverted_index() { ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_writer.cpp: ########## @@ -0,0 +1,414 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_writer.h" + +#include "common/status.h" +#include "io/fs/file_writer.h" +#include "io/fs/local_file_system.h" +#include "olap/rowset/segment_v2/inverted_index_desc.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" +#include "runtime/exec_env.h" + +namespace doris::segment_v2 { + +std::string InvertedIndexFileWriter::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileWriter::initialize(InvertedIndexDirectoryMap& indices_dirs) { + _indices_dirs = std::move(indices_dirs); + return Status::OK(); +} + +Result<DorisFSDirectory*> InvertedIndexFileWriter::open(const TabletIndex* index_meta) { + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + auto tmp_file_dir = ExecEnv::GetInstance()->get_tmp_file_dirs()->get_tmp_file_dir(); + _lfs = io::global_local_filesystem(); + auto lfs_index_path = InvertedIndexDescriptor::get_temporary_index_path( + tmp_file_dir / _segment_file_name, index_meta->index_id(), + index_meta->get_index_suffix()); + auto index_path = InvertedIndexDescriptor::get_temporary_index_path( + (_index_file_dir / _segment_file_name).native(), index_id, index_suffix); + + bool exists = false; + auto st = _fs->exists(lfs_index_path.c_str(), &exists); + if (!st.ok()) { + LOG(ERROR) << "index_path:" << lfs_index_path << " exists error:" << st; + return ResultError(st); + } + if (exists) { + LOG(ERROR) << "try to init a directory:" << lfs_index_path << " already exists"; + return ResultError(Status::InternalError("init_fulltext_index directory already exists")); + } + + bool can_use_ram_dir = true; + bool use_compound_file_writer = false; + auto* dir = DorisFSDirectoryFactory::getDirectory(_lfs, lfs_index_path.c_str(), + use_compound_file_writer, can_use_ram_dir, + nullptr, _fs, index_path.c_str()); + _indices_dirs.emplace(std::make_pair(index_id, index_suffix), + std::unique_ptr<DorisFSDirectory>(dir)); + return dir; +} + +Status InvertedIndexFileWriter::delete_index(const TabletIndex* index_meta) { + if (!index_meta) { + return Status::Error<ErrorCode::INVALID_ARGUMENT>("Index metadata is null."); + } + + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + + // Check if the specified index exists + auto index_it = _indices_dirs.find(std::make_pair(index_id, index_suffix)); + if (index_it == _indices_dirs.end()) { + std::ostringstream errMsg; + errMsg << "No inverted index with id " << index_id << " and suffix " << index_suffix + << " found."; + LOG(WARNING) << errMsg.str(); + return Status::OK(); + } + + _indices_dirs.erase(index_it); + return Status::OK(); +} + +size_t InvertedIndexFileWriter::headerLength() { + size_t header_size = 0; + header_size += + sizeof(int) * 2; // Account for the size of the version number and number of indices + for (const auto& entry : _indices_dirs) { + auto suffix = entry.first.second; + header_size += sizeof(int); // index id + header_size += 4; // index suffix name size + header_size += suffix.length(); // index suffix name + header_size += sizeof(int); // index file count + const auto& dir = entry.second; + std::vector<std::string> files; + dir->list(&files); + + for (auto file : files) { + header_size += 4; // file name size + header_size += file.length(); // file name + header_size += 8; // file offset + header_size += 8; // file size + } + } + return header_size; +} + +Status InvertedIndexFileWriter::close() { + if (_indices_dirs.empty()) { + return Status::OK(); + } + try { + if (_storage_format == InvertedIndexStorageFormatPB::V1) { + for (const auto& entry : _indices_dirs) { + const auto& dir = entry.second; + auto* cfsWriter = _CLNEW DorisCompoundFileWriter(dir.get()); + // write compound file + _file_size += cfsWriter->writeCompoundFile(); + // delete index path, which contains separated inverted index files + if (std::strcmp(dir->getObjectName(), "DorisFSDirectory") == 0) { + auto* compound_dir = static_cast<DorisFSDirectory*>(dir.get()); + compound_dir->deleteDirectory(); + } + _CLDELETE(cfsWriter) + } + } else { + _file_size = write(); + for (const auto& entry : _indices_dirs) { + const auto& dir = entry.second; + // delete index path, which contains separated inverted index files + if (std::strcmp(dir->getObjectName(), "DorisFSDirectory") == 0) { + auto* compound_dir = static_cast<DorisFSDirectory*>(dir.get()); + compound_dir->deleteDirectory(); + } + } + } + } catch (CLuceneError& err) { + return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( + "CLuceneError occur when close idx file {}, error msg: {}", + InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name), + err.what()); + } + return Status::OK(); +} +size_t InvertedIndexFileWriter::write() { Review Comment: warning: function 'write' exceeds recommended size/complexity thresholds [readability-function-size] ```cpp size_t InvertedIndexFileWriter::write() { ^ ``` <details> <summary>Additional context</summary> **be/src/olap/rowset/segment_v2/inverted_index_file_writer.cpp:155:** 84 lines including whitespace and comments (threshold 80) ```cpp size_t InvertedIndexFileWriter::write() { ^ ``` </details> ########## be/src/olap/rowset/segment_v2/inverted_index_searcher.h: ########## @@ -17,7 +17,14 @@ #pragma once -#include <CLucene.h> +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wshadow-field" +#endif +#include <CLucene.h> // IWYU pragma: keep Review Comment: warning: 'CLucene.h' file not found [clang-diagnostic-error] ```cpp #include <CLucene.h> // IWYU pragma: keep ^ ``` ########## be/src/olap/rowset/segment_v2/inverted_index_writer.cpp: ########## @@ -546,72 +511,85 @@ class InvertedIndexColumnWriterImpl : public InvertedIndexColumnWriter { faststring buf; buf.resize(size); _null_bitmap.write(reinterpret_cast<char*>(buf.data()), false); - null_bitmap_out->writeBytes(reinterpret_cast<uint8_t*>(buf.data()), size); + null_bitmap_out->writeBytes(buf.data(), size); null_bitmap_out->close(); } } Status finish() override { Review Comment: warning: function 'finish' has cognitive complexity of 52 (threshold 50) [readability-function-cognitive-complexity] ```cpp Status finish() override { ^ ``` <details> <summary>Additional context</summary> **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:519:** +1, including nesting penalty of 0, nesting level increased to 1 ```cpp if (_dir != nullptr) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:526:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp if constexpr (field_is_numeric_type(field_type)) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:543:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp DBUG_EXECUTE_IF("InvertedIndexWriter._set_bkd_data_out_nullptr", ^ ``` **be/src/util/debug_points.h:34:** expanded from macro 'DBUG_EXECUTE_IF' ```cpp if (UNLIKELY(config::enable_debug_points)) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:543:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp DBUG_EXECUTE_IF("InvertedIndexWriter._set_bkd_data_out_nullptr", ^ ``` **be/src/util/debug_points.h:36:** expanded from macro 'DBUG_EXECUTE_IF' ```cpp if (dp) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:545:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp if (data_out != nullptr && meta_out != nullptr && index_out != nullptr) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:550:** +1, nesting level increased to 3 ```cpp } else { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:559:** +1, nesting level increased to 2 ```cpp } else if constexpr (field_is_slice_type(field_type)) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:566:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp DBUG_EXECUTE_IF( ^ ``` **be/src/util/debug_points.h:34:** expanded from macro 'DBUG_EXECUTE_IF' ```cpp if (UNLIKELY(config::enable_debug_points)) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:566:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp DBUG_EXECUTE_IF( ^ ``` **be/src/util/debug_points.h:36:** expanded from macro 'DBUG_EXECUTE_IF' ```cpp if (dp) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:572:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp } catch (CLuceneError& e) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:573:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp FINALLY_CLOSE_OUTPUT(null_bitmap_out) ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:66:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp } catch (...) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:574:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp FINALLY_CLOSE_OUTPUT(meta_out) ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:66:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp } catch (...) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:575:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp FINALLY_CLOSE_OUTPUT(data_out) ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:66:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp } catch (...) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:576:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp FINALLY_CLOSE_OUTPUT(index_out) ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:66:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp } catch (...) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:577:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp if constexpr (field_is_numeric_type(field_type)) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:578:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp FINALLY_CLOSE_OUTPUT(_dir) ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:65:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp if (x != nullptr) x->close(); \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:578:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp FINALLY_CLOSE_OUTPUT(_dir) ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:66:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp } catch (...) { \ ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:579:** +1, nesting level increased to 3 ```cpp } else if constexpr (field_is_slice_type(field_type)) { ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:580:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp FINALLY_CLOSE_OUTPUT(_index_writer); ^ ``` **be/src/olap/rowset/segment_v2/inverted_index_writer.cpp:66:** expanded from macro 'FINALLY_CLOSE_OUTPUT' ```cpp } catch (...) { \ ^ ``` </details> ########## be/src/olap/rowset/segment_v2/inverted_index_searcher.cpp: ########## @@ -51,15 +51,14 @@ return Status::OK(); } -Status BKDIndexSearcherBuilder::build(DorisCompoundReader* directory, +Status BKDIndexSearcherBuilder::build(lucene::store::Directory* directory, Review Comment: warning: method 'build' can be made static [readability-convert-member-functions-to-static] ```suggestion static Status BKDIndexSearcherBuilder::build(lucene::store::Directory* directory, ``` ########## be/src/olap/rowset/segment_v2/inverted_index_file_writer.cpp: ########## @@ -0,0 +1,414 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +#include "olap/rowset/segment_v2/inverted_index_file_writer.h" + +#include "common/status.h" +#include "io/fs/file_writer.h" +#include "io/fs/local_file_system.h" +#include "olap/rowset/segment_v2/inverted_index_desc.h" +#include "olap/rowset/segment_v2/inverted_index_fs_directory.h" +#include "olap/tablet_schema.h" +#include "runtime/exec_env.h" + +namespace doris::segment_v2 { + +std::string InvertedIndexFileWriter::get_index_file_path(const TabletIndex* index_meta) const { + return InvertedIndexDescriptor::get_index_file_name(_index_file_dir / _segment_file_name, + index_meta->index_id(), + index_meta->get_index_suffix()); +} + +Status InvertedIndexFileWriter::initialize(InvertedIndexDirectoryMap& indices_dirs) { + _indices_dirs = std::move(indices_dirs); + return Status::OK(); +} + +Result<DorisFSDirectory*> InvertedIndexFileWriter::open(const TabletIndex* index_meta) { + auto index_id = index_meta->index_id(); + auto index_suffix = index_meta->get_index_suffix(); + auto tmp_file_dir = ExecEnv::GetInstance()->get_tmp_file_dirs()->get_tmp_file_dir(); + _lfs = io::global_local_filesystem(); + auto lfs_index_path = InvertedIndexDescriptor::get_temporary_index_path( + tmp_file_dir / _segment_file_name, index_meta->index_id(), + index_meta->get_index_suffix()); + auto index_path = InvertedIndexDescriptor::get_temporary_index_path( + (_index_file_dir / _segment_file_name).native(), index_id, index_suffix); + + bool exists = false; + auto st = _fs->exists(lfs_index_path.c_str(), &exists); + if (!st.ok()) { + LOG(ERROR) << "index_path:" << lfs_index_path << " exists error:" << st; + return ResultError(st); + } + if (exists) { + LOG(ERROR) << "try to init a directory:" << lfs_index_path << " already exists"; + return ResultError(Status::InternalError("init_fulltext_index directory already exists")); + } + + bool can_use_ram_dir = true; + bool use_compound_file_writer = false; + auto* dir = DorisFSDirectoryFactory::getDirectory(_lfs, lfs_index_path.c_str(), + use_compound_file_writer, can_use_ram_dir, + nullptr, _fs, index_path.c_str()); + _indices_dirs.emplace(std::make_pair(index_id, index_suffix), + std::unique_ptr<DorisFSDirectory>(dir)); + return dir; +} + +Status InvertedIndexFileWriter::delete_index(const TabletIndex* index_meta) { Review Comment: warning: method 'delete_index' can be made static [readability-convert-member-functions-to-static] ```suggestion static Status delete_index(const TabletIndex* index_meta); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org