kangpinghuang commented on a change in pull request #1474: Add segment iterator URL: https://github.com/apache/incubator-doris/pull/1474#discussion_r304711035
########## File path: be/src/olap/rowset/segment_v2/segment.cpp ########## @@ -0,0 +1,198 @@ +// 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.h" + +#include "common/logging.h" // LOG +#include "env/env.h" // RandomAccessFile +#include "olap/field_info.h" // FieldInfo +#include "olap/rowset/segment_v2/column_reader.h" // ColumnReader +#include "olap/rowset/segment_v2/segment_writer.h" // k_segment_magic_length +#include "olap/rowset/segment_v2/segment_iterator.h" +#include "util/slice.h" // Slice +#include "runtime/vectorized_row_batch.h" // VectorizedRowBatch +#include "olap/short_key_index.h" +#include "olap/olap_index.h" + +namespace doris { +namespace segment_v2 { + +Segment::Segment( + std::string fname, const std::vector<ColumnSchemaV2>& schema, + size_t num_short_keys, uint32_t segment_id) + : _fname(std::move(fname)), + _schema(schema), + _num_short_keys(num_short_keys), + _segment_id(segment_id) { +} + +Segment::~Segment() { + for (auto reader : _column_readers) { + delete reader; + } +} + +Status Segment::open() { + RETURN_IF_ERROR(Env::Default()->new_random_access_file( + _fname, &_input_file)); + RETURN_IF_ERROR(_input_file->size(&_file_size)); + // 24: 2 * magic + 2 * checksum + 2 * footer/header length + if (_file_size < 24) { + return Status::Corruption("Bad segment, file size is too small"); + } + // parse footer to get meta + RETURN_IF_ERROR(_parse_footer()); + // parse footer to get meta + RETURN_IF_ERROR(_parse_header()); + // parse short key index + RETURN_IF_ERROR(_parse_index()); + // initial all column reader + RETURN_IF_ERROR(_initial_column_readers()); + return Status::OK(); +} + +Status Segment::new_iterator(std::unique_ptr<SegmentIterator>* output) { + output->reset(new SegmentIterator(this->shared_from_this())); + return Status::OK(); +} + +// read data at offset of input file, and parse data into magic and +// header/footer's length. currently return header/footer's length +Status Segment::_parse_magic_and_len(uint64_t offset, uint32_t* length) { + // read magic and length + uint8_t buf[8]; + Slice slice(buf, 8); + RETURN_IF_ERROR(_input_file->read_at(offset, slice)); + + if (memcmp(slice.data, k_segment_magic, k_segment_magic_length) != 0) { + LOG(WARNING) << "Magic don't match, magic=" << std::string((char*)buf, 4); + return Status::Corruption("Bad segment, file magic don't match"); + } + *length = decode_fixed32_le((uint8_t*)slice.data + 4); + return Status::OK(); +} + +Status Segment::_parse_footer() { + uint64_t offset = _file_size - 8; + uint32_t footer_length = 0; + RETURN_IF_ERROR(_parse_magic_and_len(offset, &footer_length)); + + // check file size footer + checksum + if (offset < footer_length + 4) { + LOG(WARNING) << "Segment file is too small, size=" << _file_size << ", footer_size=" << footer_length; Review comment: Is there any info to get the file path? ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
