github-actions[bot] commented on code in PR #30145:
URL: https://github.com/apache/doris/pull/30145#discussion_r1495366043


##########
be/src/olap/rowset/segment_v2/inverted_index_file_writer.cpp:
##########
@@ -0,0 +1,422 @@
+// 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 "olap/rowset/segment_v2/inverted_index_compound_directory.h"
+#include "olap/rowset/segment_v2/inverted_index_desc.h"
+#include "olap/tablet_schema.h"
+
+namespace doris::segment_v2 {
+
+std::string InvertedIndexFileWriter::get_index_file_path(const TabletIndex* 
index_meta) const {
+    return _index_file_dir /
+           InvertedIndexDescriptor::get_index_file_name(_index_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<DorisCompoundDirectory*> InvertedIndexFileWriter::open(const 
TabletIndex* index_meta) {
+    auto index_id = index_meta->index_id();
+    auto index_suffix = index_meta->get_index_suffix();
+    auto index_path = InvertedIndexDescriptor::get_temporary_index_path(
+            (_index_file_dir / _index_file_name).native(), index_id, 
index_suffix);
+
+    bool exists = false;
+    auto st = _fs->exists(index_path.c_str(), &exists);
+    if (!st.ok()) {
+        LOG(ERROR) << "index_path:" << index_path << " exists error:" << st;
+        return ResultError(st);
+    }
+    if (exists) {
+        LOG(ERROR) << "try to init a directory:" << index_path << " already 
exists";
+        return ResultError(Status::InternalError("init_fulltext_index 
directory already exists"));
+    }
+    auto* dir = DorisCompoundDirectoryFactory::getDirectory(_fs, 
index_path.c_str());
+    _indices_dirs.emplace(std::make_pair(index_id, index_suffix),
+                          std::unique_ptr<DorisCompoundDirectory>(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) {
+                auto index_id = entry.first.first;
+                auto index_suffix = entry.first.second;
+                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::string(dir->getObjectName()) == 
"DorisCompoundDirectory") {
+                    auto temp_dir = 
InvertedIndexDescriptor::get_temporary_index_path(
+                            _index_file_dir / _index_file_name, index_id, 
index_suffix);
+                    auto* compound_dir = 
static_cast<DorisCompoundDirectory*>(dir.get());
+                    if (compound_dir->getDirName() == temp_dir) {
+                        compound_dir->deleteDirectory();
+                    } else {
+                        LOG(ERROR) << "try to delete wrong inverted index 
temporal directory, "
+                                      "wrong path is "
+                                   << compound_dir->getDirName() << " actual 
needed: " << temp_dir;
+                    }
+                }
+                _CLDELETE(cfsWriter)
+            }
+        } else {
+            _file_size = write();
+            for (const auto& entry : _indices_dirs) {
+                auto index_id = entry.first.first;
+                auto index_suffix = entry.first.second;
+                const auto& dir = entry.second;
+                // delete index path, which contains separated inverted index 
files
+                if (std::string(dir->getObjectName()) == 
"DorisCompoundDirectory") {
+                    auto temp_dir = 
InvertedIndexDescriptor::get_temporary_index_path(
+                            _index_file_dir / _index_file_name, index_id, 
index_suffix);
+                    auto* compound_dir = 
static_cast<DorisCompoundDirectory*>(dir.get());
+                    if (compound_dir->getDirName() == temp_dir) {
+                        compound_dir->deleteDirectory();
+                    } else {
+                        LOG(ERROR) << "try to delete wrong inverted index 
temporal directory, "
+                                      "wrong path is "
+                                   << compound_dir->getDirName() << " actual 
needed: " << temp_dir;
+                    }
+                }
+            }
+        }
+    } 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 / 
_index_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:163:** 84 
lines including whitespace and comments (threshold 80)
   ```cpp
   size_t InvertedIndexFileWriter::write() {
                                   ^
   ```
   
   </details>
   



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