1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/db/column/fake_storage.h"
18
19 #include <algorithm>
20 #include <cstdint>
21 #include <iterator>
22 #include <memory>
23 #include <optional>
24 #include <utility>
25
26 #include "perfetto/base/logging.h"
27 #include "perfetto/trace_processor/basic_types.h"
28 #include "src/trace_processor/containers/bit_vector.h"
29 #include "src/trace_processor/db/column/data_layer.h"
30 #include "src/trace_processor/db/column/types.h"
31
32 namespace perfetto::trace_processor::column {
33
FakeStorageChain(uint32_t size,SearchStrategy strategy,Range range,BitVector bv)34 FakeStorageChain::FakeStorageChain(uint32_t size,
35 SearchStrategy strategy,
36 Range range,
37 BitVector bv)
38 : size_(size),
39 strategy_(strategy),
40 range_(range),
41 bit_vector_(std::move(bv)) {}
42
SingleSearch(FilterOp,SqlValue,uint32_t i) const43 SingleSearchResult FakeStorageChain::SingleSearch(FilterOp,
44 SqlValue,
45 uint32_t i) const {
46 PERFETTO_CHECK(i < size_);
47 switch (strategy_) {
48 case kAll:
49 return SingleSearchResult::kMatch;
50 case kNone:
51 return SingleSearchResult::kNoMatch;
52 case kBitVector:
53 return bit_vector_.IsSet(i) ? SingleSearchResult::kMatch
54 : SingleSearchResult::kNoMatch;
55 case kRange:
56 return range_.Contains(i) ? SingleSearchResult::kMatch
57 : SingleSearchResult::kNoMatch;
58 }
59 PERFETTO_FATAL("For GCC");
60 }
61
ValidateSearchConstraints(FilterOp,SqlValue) const62 SearchValidationResult FakeStorageChain::ValidateSearchConstraints(
63 FilterOp,
64 SqlValue) const {
65 return SearchValidationResult::kOk;
66 }
67
SearchValidated(FilterOp,SqlValue,Range in) const68 RangeOrBitVector FakeStorageChain::SearchValidated(FilterOp,
69 SqlValue,
70 Range in) const {
71 switch (strategy_) {
72 case kAll:
73 return RangeOrBitVector(in);
74 case kNone:
75 return RangeOrBitVector(Range());
76 case kRange:
77 return RangeOrBitVector(Range(std::max(in.start, range_.start),
78 std::min(in.end, range_.end)));
79 case kBitVector: {
80 BitVector intersection = bit_vector_.IntersectRange(in.start, in.end);
81 intersection.Resize(in.end, false);
82 return RangeOrBitVector(std::move(intersection));
83 }
84 }
85 PERFETTO_FATAL("For GCC");
86 }
87
IndexSearchValidated(FilterOp,SqlValue,Indices & indices) const88 void FakeStorageChain::IndexSearchValidated(FilterOp,
89 SqlValue,
90 Indices& indices) const {
91 switch (strategy_) {
92 case kAll:
93 return;
94 case kNone:
95 indices.tokens.clear();
96 return;
97 case kRange:
98 indices.tokens.erase(
99 std::remove_if(indices.tokens.begin(), indices.tokens.end(),
100 [this](const Token& token) {
101 return !range_.Contains(token.index);
102 }),
103 indices.tokens.end());
104 return;
105 case kBitVector:
106 indices.tokens.erase(
107 std::remove_if(indices.tokens.begin(), indices.tokens.end(),
108 [this](const Token& token) {
109 return !bit_vector_.IsSet(token.index);
110 }),
111 indices.tokens.end());
112 return;
113 }
114 PERFETTO_FATAL("For GCC");
115 }
116
Distinct(Indices &) const117 void FakeStorageChain::Distinct(Indices&) const {
118 // Fake storage shouldn't implement Distinct as it's not a binary (this index
119 // passes or not) operation on a column.
120 PERFETTO_FATAL("Not implemented");
121 }
122
MaxElement(Indices &) const123 std::optional<Token> FakeStorageChain::MaxElement(Indices&) const {
124 PERFETTO_FATAL("Not implemented");
125 }
MinElement(Indices &) const126 std::optional<Token> FakeStorageChain::MinElement(Indices&) const {
127 PERFETTO_FATAL("Not implemented");
128 }
129
StableSort(Token *,Token *,SortDirection) const130 void FakeStorageChain::StableSort(Token*, Token*, SortDirection) const {
131 PERFETTO_FATAL("Not implemented");
132 }
133
Get_AvoidUsingBecauseSlow(uint32_t) const134 SqlValue FakeStorageChain::Get_AvoidUsingBecauseSlow(uint32_t) const {
135 PERFETTO_FATAL("Not implemented");
136 }
137
138 } // namespace perfetto::trace_processor::column
139