• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_STORAGE_H_
18 #define SRC_TRACE_PROCESSOR_DB_COLUMN_STORAGE_H_
19 
20 #include "src/trace_processor/containers/nullable_vector.h"
21 
22 namespace perfetto {
23 namespace trace_processor {
24 
25 // Base class for allowing type erasure when defining plug-in implementations
26 // of backing storage for columns.
27 class ColumnStorageBase {
28  public:
29   ColumnStorageBase() = default;
30   virtual ~ColumnStorageBase();
31 
32   ColumnStorageBase(const ColumnStorageBase&) = delete;
33   ColumnStorageBase& operator=(const ColumnStorageBase&) = delete;
34 
35   ColumnStorageBase(ColumnStorageBase&&) = default;
36   ColumnStorageBase& operator=(ColumnStorageBase&&) noexcept = default;
37 };
38 
39 // Class used for implementing storage for non-null columns.
40 template <typename T>
41 class ColumnStorage : public ColumnStorageBase {
42  public:
43   ColumnStorage() = default;
44 
45   explicit ColumnStorage(const ColumnStorage&) = delete;
46   ColumnStorage& operator=(const ColumnStorage&) = delete;
47 
48   ColumnStorage(ColumnStorage&&) = default;
49   ColumnStorage& operator=(ColumnStorage&&) noexcept = default;
50 
Get(uint32_t idx)51   T Get(uint32_t idx) const { return vector_[idx]; }
Append(T val)52   void Append(T val) { vector_.emplace_back(val); }
Set(uint32_t idx,T val)53   void Set(uint32_t idx, T val) { vector_[idx] = val; }
size()54   uint32_t size() const { return static_cast<uint32_t>(vector_.size()); }
ShrinkToFit()55   void ShrinkToFit() { vector_.shrink_to_fit(); }
vector()56   const std::vector<T>& vector() const { return vector_; }
57 
58   template <bool IsDense>
Create()59   static ColumnStorage<T> Create() {
60     static_assert(!IsDense, "Invalid for non-null storage to be dense.");
61     return ColumnStorage<T>();
62   }
63 
64  private:
65   std::vector<T> vector_;
66 };
67 
68 // Class used for implementing storage for nullable columns.
69 template <typename T>
70 class ColumnStorage<std::optional<T>> : public ColumnStorageBase {
71  public:
72   ColumnStorage() = default;
73 
74   explicit ColumnStorage(const ColumnStorage&) = delete;
75   ColumnStorage& operator=(const ColumnStorage&) = delete;
76 
77   ColumnStorage(ColumnStorage&&) = default;
78   ColumnStorage& operator=(ColumnStorage&&) noexcept = default;
79 
Get(uint32_t idx)80   std::optional<T> Get(uint32_t idx) const { return nv_.Get(idx); }
Append(T val)81   void Append(T val) { nv_.Append(val); }
Append(std::optional<T> val)82   void Append(std::optional<T> val) { nv_.Append(std::move(val)); }
Set(uint32_t idx,T val)83   void Set(uint32_t idx, T val) { nv_.Set(idx, val); }
size()84   uint32_t size() const { return nv_.size(); }
IsDense()85   bool IsDense() const { return nv_.IsDense(); }
ShrinkToFit()86   void ShrinkToFit() { nv_.ShrinkToFit(); }
87   // For dense columns the size of the vector is equal to size of the bit
88   // vector. For sparse it's equal to count set bits of the bit vector.
non_null_vector()89   const std::vector<T>& non_null_vector() const {
90     return nv_.non_null_vector();
91   }
non_null_bit_vector()92   const BitVector& non_null_bit_vector() const {
93     return nv_.non_null_bit_vector();
94   }
95 
96   template <bool IsDense>
Create()97   static ColumnStorage<std::optional<T>> Create() {
98     return IsDense
99                ? ColumnStorage<std::optional<T>>(NullableVector<T>::Dense())
100                : ColumnStorage<std::optional<T>>(NullableVector<T>::Sparse());
101   }
102 
103  private:
ColumnStorage(NullableVector<T> nv)104   explicit ColumnStorage(NullableVector<T> nv) : nv_(std::move(nv)) {}
105 
106   NullableVector<T> nv_;
107 };
108 
109 }  // namespace trace_processor
110 }  // namespace perfetto
111 
112 #endif  // SRC_TRACE_PROCESSOR_DB_COLUMN_STORAGE_H_
113