• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SRC_TRACE_PROCESSOR_DB_COLUMN_FAKE_STORAGE_H_
18 #define SRC_TRACE_PROCESSOR_DB_COLUMN_FAKE_STORAGE_H_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
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 
34 // Fake implementation of DataLayerChain which can be used in unittests.
35 class FakeStorageChain : public DataLayerChain {
36  public:
37   // Factory function for creating a DataLayerChain which matches all rows from
38   // [0, size).
SearchAll(uint32_t size)39   static std::unique_ptr<DataLayerChain> SearchAll(uint32_t size) {
40     return std::unique_ptr<DataLayerChain>(
41         new FakeStorageChain(size, SearchStrategy::kAll, Range(), BitVector()));
42   }
43 
44   // Factory function for creating a DataLayerChain which matches zero rows.
SearchNone(uint32_t size)45   static std::unique_ptr<DataLayerChain> SearchNone(uint32_t size) {
46     return std::unique_ptr<DataLayerChain>(new FakeStorageChain(
47         size, SearchStrategy::kNone, Range(), BitVector()));
48   }
49 
50   // Factory function for creating a DataLayerChain which matches rows [r.start,
51   // r.end).
SearchSubset(uint32_t size,Range r)52   static std::unique_ptr<DataLayerChain> SearchSubset(uint32_t size, Range r) {
53     return std::unique_ptr<DataLayerChain>(
54         new FakeStorageChain(size, SearchStrategy::kRange, r, BitVector()));
55   }
56 
57   // Factory function for creating a DataLayerChain which matches rows of the
58   // set bit positions of |bv|.
SearchSubset(uint32_t size,BitVector bv)59   static std::unique_ptr<DataLayerChain> SearchSubset(uint32_t size,
60                                                       BitVector bv) {
61     return std::unique_ptr<DataLayerChain>(new FakeStorageChain(
62         size, SearchStrategy::kBitVector, Range(), std::move(bv)));
63   }
64 
65   // Factory function for creating a DataLayerChain which matches rows specified
66   // by |index_vec|.
SearchSubset(uint32_t size,const std::vector<uint32_t> & index_vec)67   static std::unique_ptr<DataLayerChain> SearchSubset(
68       uint32_t size,
69       const std::vector<uint32_t>& index_vec) {
70     BitVector bv(size);
71     for (uint32_t i : index_vec) {
72       bv.Set(i);
73     }
74     return std::unique_ptr<DataLayerChain>(new FakeStorageChain(
75         size, SearchStrategy::kBitVector, Range(), std::move(bv)));
76   }
77 
78   // Implementation of DataLayerChain.
79   SingleSearchResult SingleSearch(FilterOp, SqlValue, uint32_t) const override;
80 
81   SearchValidationResult ValidateSearchConstraints(FilterOp,
82                                                    SqlValue) const override;
83 
84   RangeOrBitVector SearchValidated(FilterOp, SqlValue, Range) const override;
85 
86   void IndexSearchValidated(FilterOp, SqlValue, Indices&) const override;
87 
88   void StableSort(SortToken* start,
89                   SortToken* end,
90                   SortDirection) const override;
91 
92   void Distinct(Indices&) const override;
93 
94   std::optional<Token> MaxElement(Indices&) const override;
95 
96   std::optional<Token> MinElement(Indices&) const override;
97 
98   SqlValue Get_AvoidUsingBecauseSlow(uint32_t index) const override;
99 
100   void Serialize(StorageProto*) const override;
101 
size()102   uint32_t size() const override { return size_; }
103 
DebugString()104   std::string DebugString() const override { return "FakeStorage"; }
105 
106  private:
107   enum SearchStrategy { kNone, kAll, kRange, kBitVector };
108 
109   FakeStorageChain(uint32_t, SearchStrategy, Range, BitVector);
110 
111   uint32_t size_ = 0;
112   SearchStrategy strategy_ = SearchStrategy::kNone;
113   Range range_;
114   BitVector bit_vector_;
115 };
116 
117 }  // namespace perfetto::trace_processor::column
118 
119 #endif  // SRC_TRACE_PROCESSOR_DB_COLUMN_FAKE_STORAGE_H_
120