imay commented on a change in pull request #1577: Support Segment for BetaRowset
URL: https://github.com/apache/incubator-doris/pull/1577#discussion_r310450904
 
 

 ##########
 File path: be/src/olap/rowset/segment_v2/segment_iterator.cpp
 ##########
 @@ -0,0 +1,259 @@
+// 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/segment_iterator.h"
+
+#include <set>
+
+#include "gutil/strings/substitute.h"
+#include "olap/rowset/segment_v2/segment.h"
+#include "olap/rowset/segment_v2/column_reader.h"
+#include "olap/row_block2.h"
+#include "olap/row_cursor.h"
+#include "olap/short_key_index.h"
+
+using strings::Substitute;
+
+namespace doris {
+namespace segment_v2 {
+
+SegmentIterator::SegmentIterator(std::shared_ptr<Segment> segment,
+                                 const Schema& schema)
+        : _segment(std::move(segment)),
+        _schema(schema),
+        _column_iterators(_schema.num_columns(), nullptr) {
+}
+
+SegmentIterator::~SegmentIterator() {
+    for (auto iter : _column_iterators) {
+        delete iter;
+    }
+}
+
+Status SegmentIterator::init(const StorageReadOptions& opts) {
+    _opts = opts;
+    RETURN_IF_ERROR(_init_short_key_range());
+    RETURN_IF_ERROR(_init_column_iterators());
+    return Status::OK();
+}
+
+// This function will use input key bounds to get a row range.
+Status SegmentIterator::_init_short_key_range() {
+    _lower_rowid = 0;
+    _upper_rowid = num_rows();
+
+    // fast path for empty segment
+    if (_upper_rowid == 0) {
+        return Status::OK();
+    }
+
+    if (_opts.lower_bound == nullptr && _opts.upper_bound == nullptr) {
+        return Status::OK();
+    }
+
+    RETURN_IF_ERROR(_prepare_seek());
+
+    // init row range with short key range
+    if (_opts.upper_bound != nullptr) {
+        // If client want to read upper_bound, the include_upper_bound is 
true. So we
+        // should get the first ordinal at which key is larger than 
upper_bound.
+        // So we call _lookup_ordinal with include_upper_bound's negate
+        RETURN_IF_ERROR(_lookup_ordinal(
+                *_opts.upper_bound, !_opts.include_upper_bound, num_rows(), 
&_upper_rowid));
+    }
+    if (_upper_rowid > 0 && _opts.lower_bound != nullptr) {
+        RETURN_IF_ERROR(_lookup_ordinal(
+                *_opts.lower_bound, _opts.include_lower_bound, _upper_rowid, 
&_lower_rowid));
+    }
+
+    return Status::OK();
+}
+
+// Set up environment for the following seek.
+Status SegmentIterator::_prepare_seek() {
+    std::vector<Field> key_fields;
+    std::set<uint32_t> column_set;
+    if (_opts.lower_bound != nullptr) {
+        for (auto cid : _opts.lower_bound->schema()->column_ids()) {
+            column_set.emplace(cid);
+            key_fields.push_back(*_opts.lower_bound->schema()->column(cid));
+        }
+    }
+    if (_opts.upper_bound != nullptr) {
+        for (auto cid : _opts.upper_bound->schema()->column_ids()) {
+            if (column_set.count(cid) == 0) {
+                
key_fields.push_back(*_opts.upper_bound->schema()->column(cid));
+                column_set.emplace(cid);
+            }
+        }
+    }
+    _seek_schema.reset(new Schema(key_fields, key_fields.size()));
+    _seek_block.reset(new RowBlockV2(*_seek_schema, 1, &_arena));
+
+    // create used column iterator
+    for (auto cid : _seek_schema->column_ids()) {
+        if (_column_iterators[cid] == nullptr) {
+            RETURN_IF_ERROR(_create_column_iterator(cid, 
&_column_iterators[cid]));
+        }
+    }
+
+    return Status::OK();
+}
+
+Status SegmentIterator::_init_column_iterators() {
+    _cur_rowid = _lower_rowid;
+    if (_cur_rowid >= num_rows()) {
+        return Status::OK();
+    }
+    for (auto cid : _schema.column_ids()) {
+        if (_column_iterators[cid] == nullptr) {
+            RETURN_IF_ERROR(_create_column_iterator(cid, 
&_column_iterators[cid]));
+        }
+
+        _column_iterators[cid]->seek_to_ordinal(_cur_rowid);
+    }
+    return Status::OK();
+}
+
+Status SegmentIterator::_create_column_iterator(uint32_t cid, ColumnIterator** 
iter) {
+    _segment->new_column_iterator(cid, iter);
 
 Review comment:
   OK

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@doris.apache.org
For additional commands, e-mail: dev-h...@doris.apache.org

Reply via email to