Skip to content

Commit

Permalink
[Refactor](inverted index) new inverted index reader implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
airborne12 committed Sep 20, 2024
1 parent 00be77e commit 7083db8
Show file tree
Hide file tree
Showing 24 changed files with 487 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "olap/rowset/segment_v2/inverted_index/query_v2/roaring_query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/term_query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

Status BooleanQuery::Builder::set_op(OperatorType type) {
_op = DORIS_TRY(OperatorFactory::create(type));
Expand Down Expand Up @@ -88,4 +88,4 @@ void BooleanQuery::search_by_skiplist(const std::shared_ptr<roaring::Roaring>& r

void BooleanQuery::search_by_bitmap(const std::shared_ptr<roaring::Roaring>& result) {}

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "olap/rowset/segment_v2/inverted_index/query_v2/node.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

enum class OperatorType;
class BooleanQuery : public Query {
Expand Down Expand Up @@ -53,4 +53,4 @@ class BooleanQuery : public Query {
Node _op;
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "olap/rowset/segment_v2/inverted_index/query_v2/roaring_query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/term_query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

Status ConjunctionOp::init() {
if (_childrens.size() < 2) {
Expand Down Expand Up @@ -91,4 +91,4 @@ int64_t ConjunctionOp::cost() const {
return visit_node(*_lead1, Cost {});
}

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/operator.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

class ConjunctionOp : public Operator {
public:
Expand All @@ -43,4 +43,4 @@ class ConjunctionOp : public Operator {

using ConjunctionOpPtr = std::shared_ptr<ConjunctionOp>;

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "olap/rowset/segment_v2/inverted_index/query_v2/roaring_query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/term_query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

DisjunctionOp::~DisjunctionOp() {
while (!_pq.empty()) {
Expand Down Expand Up @@ -83,4 +83,4 @@ int64_t DisjunctionOp::cost() const {
return _cost;
}

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/operator.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

class DisjunctionOp : public Operator {
public:
Expand Down Expand Up @@ -56,4 +56,4 @@ class DisjunctionOp : public Operator {

using DisjunctionOpPtr = std::shared_ptr<DisjunctionOp>;

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "olap/rowset/segment_v2/inverted_index/query_v2/conjunction_op.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/disjunction_op.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

Result<Node> OperatorFactory::create(OperatorType query_type) {
switch (query_type) {
Expand All @@ -33,4 +33,4 @@ Result<Node> OperatorFactory::create(OperatorType query_type) {
}
}

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/node.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

enum class QueryType;
enum class OperatorType;
Expand All @@ -38,4 +38,4 @@ class OperatorFactory {
static Result<Node> create(OperatorType query_type);
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "olap/rowset/segment_v2/inverted_index/query_v2/roaring_query.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/term_query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

template <typename... Args>
Result<Node> QueryFactory::create(QueryType query_type, Args&&... args) {
Expand All @@ -44,4 +44,4 @@ Result<Node> QueryFactory::create(QueryType query_type, Args&&... args) {
}
}

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
4 changes: 2 additions & 2 deletions be/src/olap/rowset/segment_v2/inverted_index/query_v2/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "common/status.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

class ConjunctionOp;
class DisjunctionOp;
Expand Down Expand Up @@ -125,4 +125,4 @@ struct Cost {
}
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/node.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

enum class OperatorType { OP_AND = 0, OP_OR };

Expand All @@ -42,4 +42,4 @@ class Operator {
std::vector<Node> _childrens;
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

class PhraseQuery : public Query {
public:
Expand All @@ -35,4 +35,4 @@ class PhraseQuery : public Query {
int64_t cost() const { return -1; }
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
4 changes: 2 additions & 2 deletions be/src/olap/rowset/segment_v2/inverted_index/query_v2/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include "common/status.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

enum class QueryType { TERM_QUERY, PHRASE_QUERY, ROARING_QUERY };

Expand All @@ -49,4 +49,4 @@ class Query {
virtual ~Query() = default;
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
121 changes: 121 additions & 0 deletions be/src/olap/rowset/segment_v2/inverted_index/query_v2/query_result.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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.

#pragma once
#include <memory>
#include <roaring/roaring.hh>

namespace doris::segment_v2::inverted_index {
class InvertedIndexResultBitmap {
private:
std::shared_ptr<roaring::Roaring> _data_bitmap = nullptr;
std::shared_ptr<roaring::Roaring> _null_bitmap = nullptr;

public:
// Default constructor
InvertedIndexResultBitmap() = default;

~InvertedIndexResultBitmap() = default;

// Constructor with arguments
InvertedIndexResultBitmap(std::shared_ptr<roaring::Roaring> data_bitmap,
std::shared_ptr<roaring::Roaring> null_bitmap)
: _data_bitmap(std::move(data_bitmap)), _null_bitmap(std::move(null_bitmap)) {}

// Copy constructor
InvertedIndexResultBitmap(const InvertedIndexResultBitmap& other)
: _data_bitmap(std::make_shared<roaring::Roaring>(*other._data_bitmap)),
_null_bitmap(std::make_shared<roaring::Roaring>(*other._null_bitmap)) {}

// Move constructor
InvertedIndexResultBitmap(InvertedIndexResultBitmap&& other) noexcept
: _data_bitmap(std::move(other._data_bitmap)),
_null_bitmap(std::move(other._null_bitmap)) {}

// Copy assignment operator
InvertedIndexResultBitmap& operator=(const InvertedIndexResultBitmap& other) {
if (this != &other) { // Prevent self-assignment
_data_bitmap = std::make_shared<roaring::Roaring>(*other._data_bitmap);
_null_bitmap = std::make_shared<roaring::Roaring>(*other._null_bitmap);
}
return *this;
}

// Move assignment operator
InvertedIndexResultBitmap& operator=(InvertedIndexResultBitmap&& other) noexcept {
if (this != &other) { // Prevent self-assignment
_data_bitmap = std::move(other._data_bitmap);
_null_bitmap = std::move(other._null_bitmap);
}
return *this;
}

// Operator &=
InvertedIndexResultBitmap& operator&=(const InvertedIndexResultBitmap& other) {
if (_data_bitmap && _null_bitmap && other._data_bitmap && other._null_bitmap) {
auto new_null_bitmap = (*_data_bitmap & *other._null_bitmap) |
(*_null_bitmap & *other._data_bitmap) |
(*_null_bitmap & *other._null_bitmap);
*_data_bitmap &= *other._data_bitmap;
*_null_bitmap = std::move(new_null_bitmap);
}
return *this;
}

// Operator |=
InvertedIndexResultBitmap& operator|=(const InvertedIndexResultBitmap& other) {
if (_data_bitmap && _null_bitmap && other._data_bitmap && other._null_bitmap) {
auto new_null_bitmap = (*_null_bitmap | *other._null_bitmap) - *_data_bitmap;
*_data_bitmap |= *other._data_bitmap;
*_null_bitmap = std::move(new_null_bitmap);
}
return *this;
}

// NOT operation
const InvertedIndexResultBitmap& op_not(const roaring::Roaring* universe) const {
if (_data_bitmap && _null_bitmap) {
*_data_bitmap = *universe - *_data_bitmap - *_null_bitmap;
// The _null_bitmap remains unchanged.
}
return *this;
}

// Operator -=
InvertedIndexResultBitmap& operator-=(const InvertedIndexResultBitmap& other) {
if (_data_bitmap && _null_bitmap && other._data_bitmap && other._null_bitmap) {
*_data_bitmap -= *other._data_bitmap;
*_data_bitmap -= *other._null_bitmap;
*_null_bitmap -= *other._null_bitmap;
}
return *this;
}

void mask_out_null() {
if (_data_bitmap && _null_bitmap) {
*_data_bitmap -= *_null_bitmap;
}
}

const std::shared_ptr<roaring::Roaring>& get_data_bitmap() const { return _data_bitmap; }

const std::shared_ptr<roaring::Roaring>& get_null_bitmap() const { return _null_bitmap; }

// Check if both bitmaps are empty
bool is_empty() const { return (_data_bitmap == nullptr && _null_bitmap == nullptr); }
};
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include "olap/rowset/segment_v2/inverted_index/query_v2/roaring_query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

RoaringQuery::RoaringQuery(const std::shared_ptr<roaring::Roaring>& roaring)
: _roaring(roaring), _iter(_roaring->end()), _end(_roaring->end()) {}

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "gutil/integral_types.h"
#include "olap/rowset/segment_v2/inverted_index/query_v2/query.h"

namespace doris::segment_v2::idx_query_v2 {
namespace doris::segment_v2::inverted_index {

class RoaringQuery : public Query {
public:
Expand Down Expand Up @@ -60,4 +60,4 @@ class RoaringQuery : public Query {
roaring::Roaring::const_iterator _end;
};

} // namespace doris::segment_v2::idx_query_v2
} // namespace doris::segment_v2::inverted_index
Loading

0 comments on commit 7083db8

Please sign in to comment.