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 <optional>
23 #include <utility>
24
25 #include "perfetto/base/logging.h"
26 #include "perfetto/trace_processor/basic_types.h"
27 #include "src/trace_processor/containers/bit_vector.h"
28 #include "src/trace_processor/db/column/data_layer.h"
29 #include "src/trace_processor/db/column/types.h"
30
31 namespace perfetto::trace_processor::column {
32
FakeStorageChain(uint32_t size,SearchStrategy strategy,Range range,BitVector bv)33 FakeStorageChain::FakeStorageChain(uint32_t size,
34 SearchStrategy strategy,
35 Range range,
36 BitVector bv)
37 : size_(size),
38 strategy_(strategy),
39 range_(range),
40 bit_vector_(std::move(bv)) {}
41
SingleSearch(FilterOp,SqlValue,uint32_t i) const42 SingleSearchResult FakeStorageChain::SingleSearch(FilterOp,
43 SqlValue,
44 uint32_t i) const {
45 PERFETTO_CHECK(i < size_);
46 switch (strategy_) {
47 case kAll:
48 return SingleSearchResult::kMatch;
49 case kNone:
50 return SingleSearchResult::kNoMatch;
51 case kBitVector:
52 return bit_vector_.IsSet(i) ? SingleSearchResult::kMatch
53 : SingleSearchResult::kNoMatch;
54 case kRange:
55 return range_.Contains(i) ? SingleSearchResult::kMatch
56 : SingleSearchResult::kNoMatch;
57 }
58 PERFETTO_FATAL("For GCC");
59 }
60
ValidateSearchConstraints(FilterOp,SqlValue) const61 SearchValidationResult FakeStorageChain::ValidateSearchConstraints(
62 FilterOp,
63 SqlValue) const {
64 return SearchValidationResult::kOk;
65 }
66
SearchValidated(FilterOp,SqlValue,Range in) const67 RangeOrBitVector FakeStorageChain::SearchValidated(FilterOp,
68 SqlValue,
69 Range in) const {
70 switch (strategy_) {
71 case kAll:
72 return RangeOrBitVector(in);
73 case kNone:
74 return RangeOrBitVector(Range());
75 case kRange:
76 return RangeOrBitVector(Range(std::max(in.start, range_.start),
77 std::min(in.end, range_.end)));
78 case kBitVector: {
79 BitVector intersection = bit_vector_.IntersectRange(in.start, in.end);
80 intersection.Resize(in.end, false);
81 return RangeOrBitVector(std::move(intersection));
82 }
83 }
84 PERFETTO_FATAL("For GCC");
85 }
86
IndexSearchValidated(FilterOp,SqlValue,Indices & indices) const87 void FakeStorageChain::IndexSearchValidated(FilterOp,
88 SqlValue,
89 Indices& indices) const {
90 switch (strategy_) {
91 case kAll:
92 return;
93 case kNone:
94 indices.tokens.clear();
95 return;
96 case kRange:
97 indices.tokens.erase(
98 std::remove_if(indices.tokens.begin(), indices.tokens.end(),
99 [this](const Token& token) {
100 return !range_.Contains(token.index);
101 }),
102 indices.tokens.end());
103 return;
104 case kBitVector:
105 indices.tokens.erase(
106 std::remove_if(indices.tokens.begin(), indices.tokens.end(),
107 [this](const Token& token) {
108 return !bit_vector_.IsSet(token.index);
109 }),
110 indices.tokens.end());
111 return;
112 }
113 PERFETTO_FATAL("For GCC");
114 }
115
Distinct(Indices &) const116 void FakeStorageChain::Distinct(Indices&) const {
117 // Fake storage shouldn't implement Distinct as it's not a binary (this index
118 // passes or not) operation on a column.
119 PERFETTO_FATAL("Not implemented");
120 }
121
MaxElement(Indices &) const122 std::optional<Token> FakeStorageChain::MaxElement(Indices&) const {
123 PERFETTO_FATAL("Not implemented");
124 }
MinElement(Indices &) const125 std::optional<Token> FakeStorageChain::MinElement(Indices&) const {
126 PERFETTO_FATAL("Not implemented");
127 }
128
StableSort(SortToken *,SortToken *,SortDirection) const129 void FakeStorageChain::StableSort(SortToken*, SortToken*, SortDirection) const {
130 PERFETTO_FATAL("Not implemented");
131 }
132
Get_AvoidUsingBecauseSlow(uint32_t) const133 SqlValue FakeStorageChain::Get_AvoidUsingBecauseSlow(uint32_t) const {
134 PERFETTO_FATAL("Not implemented");
135 }
136
Serialize(StorageProto *) const137 void FakeStorageChain::Serialize(StorageProto*) const {
138 // FakeStorage doesn't really make sense to serialize.
139 PERFETTO_FATAL("Not implemented");
140 }
141
142 } // namespace perfetto::trace_processor::column
143