kangkaisen commented on a change in pull request #2308: Add BinaryPrefixPage
URL: https://github.com/apache/incubator-doris/pull/2308#discussion_r351650574
 
 

 ##########
 File path: be/src/olap/rowset/segment_v2/binary_prefix_page.cpp
 ##########
 @@ -0,0 +1,250 @@
+// 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/binary_prefix_page.h"
+
+#include <map>
+#include <vector>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include <string>
+
+#include "common/logging.h"
+#include "gutil/strings/substitute.h"
+#include "runtime/mem_pool.h"
+#include "util/coding.h"
+#include "util/faststring.h"
+#include "util/slice.h"
+
+namespace doris {
+namespace segment_v2 {
+
+using strings::Substitute;
+
+Status BinaryPrefixPageBuilder::add(const uint8_t* vals, size_t* add_count) {
+    DCHECK(!_finished);
+    if (*add_count == 0) {
+        return Status::OK();
+    }
+
+    const Slice* src = reinterpret_cast<const Slice*>(vals);
+    if (_count == 0) {
+        _first_entry.assign_copy(reinterpret_cast<const 
uint8_t*>(src->get_data()), src->get_size());
+    }
+
+    int i = 0;
+    for (; i < *add_count; ++i, ++src) {
+        if (is_page_full()) {
+            break;
+        }
+        char* entry = src->data;
+        size_t entry_len = src->size;
+        int old_size = _buffer.size();
+
+        int share_len;
+        if (0 == _count % RESTART_POINT_INTERVAL) {
+            share_len = 0;
+            _restart_points_offset.push_back(old_size);
+        } else {
+            int max_share_len = std::min(_last_entry.size(), entry_len);
+            share_len = max_share_len;
+            for (int i = 0; i < max_share_len; ++i) {
+                if (entry[i] != _last_entry[i]) {
+                    share_len = i;
+                    break;
+                }
+            }
+        }
+        int non_share_len = entry_len - share_len;
+
+        put_varint32(&_buffer, share_len);
+        put_varint32(&_buffer, non_share_len);
+        _buffer.append(entry + share_len, non_share_len);
+
+        _last_entry.clear();
+        _last_entry.append(entry, entry_len);
+
+        ++_count;
+    }
+    *add_count = i;
+    return Status::OK();
+}
+
+OwnedSlice BinaryPrefixPageBuilder::finish() {
+    DCHECK(!_finished);
+    _finished = true;
+    int old_size = _buffer.size();
+    put_fixed32_le(&_buffer, (uint32_t)_count);
+    old_size += sizeof(uint32_t);
+    int restart_point_size = _restart_points_offset.size();
+    for(unsigned int i = 0; i < restart_point_size; ++i) {
+        put_fixed32_le(&_buffer, _restart_points_offset[i]);
+        old_size += sizeof(uint32_t);
+    }
+    put_fixed32_le(&_buffer, restart_point_size);
+    return _buffer.build();
+}
+
+const uint8_t* BinaryPrefixPageDecoder::_decode_value_lengths(const uint8_t* 
ptr,
+                                                              uint32_t* shared,
+                                                              uint32_t* 
non_shared) {
+    const uint8_t* limit = _restarts_ptr - sizeof(uint32_t);
+    if ((ptr = decode_varint32_ptr(ptr, limit, shared)) == nullptr) {
+        return nullptr;
+    }
+    if ((ptr = decode_varint32_ptr(ptr, limit, non_shared)) == nullptr) {
+        return nullptr;
+    }
+    if (limit - ptr < *non_shared) {
+        return nullptr;
+    }
+    return ptr;
+}
+
+Status BinaryPrefixPageDecoder::_read_next_value() {
+    if (_cur_pos >= _num_values) {
+        return Status::NotFound("no more value to read");
+    }
+    uint32_t shared_len;
+    uint32_t non_shared_len;
+    auto data_ptr = _decode_value_lengths(_next_ptr, &shared_len, 
&non_shared_len);
+    if (data_ptr == nullptr) {
+        return Status::Corruption(Substitute("Failed to decode value at 
position $0", _cur_pos));
+    }
+    _current_value.resize(shared_len);
+    _current_value.append(data_ptr, non_shared_len);
+    _next_ptr = data_ptr + non_shared_len;
+    return Status::OK();
+}
+
+Status BinaryPrefixPageDecoder::_seek_to_restart_point(size_t 
restart_point_index) {
+    _cur_pos = restart_point_index * RESTART_POINT_INTERVAL;
+    _next_ptr = _get_restart_point(restart_point_index);
+    return _read_next_value();
+}
+
+Status BinaryPrefixPageDecoder::init() {
+    _cur_pos = 0;
+    _next_ptr = reinterpret_cast<const uint8_t*>(_data.get_data());
+
+    const uint8_t* end = _next_ptr + _data.get_size();
+    _num_restarts = decode_fixed32_le(end - sizeof(uint32_t));
+    _restarts_ptr = end - (_num_restarts + 1) * sizeof(uint32_t);
+    _num_values = decode_fixed32_le(end - (_num_restarts + 2) * 
sizeof(uint32_t));
+    _parsed = true;
+    return _read_next_value();
+}
+
+Status BinaryPrefixPageDecoder::seek_to_position_in_page(size_t pos) {
+    DCHECK(_parsed);
+    DCHECK_LE(pos, _num_values);
+
+    // seek past the last value is valid
+    if (pos == _num_values) {
+        _cur_pos = _num_values;
+        return Status::OK();
+    }
+
+    size_t restart_point_index = pos / RESTART_POINT_INTERVAL;
+    RETURN_IF_ERROR(_seek_to_restart_point(restart_point_index));
+    while (_cur_pos < pos) {
+        _cur_pos++;
+        RETURN_IF_ERROR(_read_next_value());
 
 Review comment:
   Yes. We need. because the `share_len` and `non_share_len` will be different 
for every value, and we need to get `_current_value` after seek.

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

Reply via email to