qidaye commented on code in PR #30145:
URL: https://github.com/apache/doris/pull/30145#discussion_r1500191686


##########
be/src/olap/rowset/segment_v2/inverted_index_file_reader.cpp:
##########
@@ -0,0 +1,266 @@
+// 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_directory.h"
+#include "olap/rowset/segment_v2/inverted_index_compound_reader.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) {
+    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 = DorisCompoundDirectory::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;
+    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;

Review Comment:
   debug level may be better.



-- 
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

Reply via email to