• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_DB_FILE_MODELS_H_
16 #define IORAP_SRC_DB_FILE_MODELS_H_
17 
18 #include <optional>
19 #include <ostream>
20 #include <string>
21 
22 namespace iorap::db {
23 
24 struct VersionedComponentName {
VersionedComponentNameVersionedComponentName25   VersionedComponentName(std::string package,
26                          std::string activity,
27                          int64_t version)
28     : package_{std::move(package)},
29       activity_{std::move(activity)},
30       version_{version} {
31   }
32 
GetPackageVersionedComponentName33   const std::string& GetPackage() const {
34     return package_;
35   }
36 
GetActivityVersionedComponentName37   const std::string& GetActivity() const {
38     return activity_;
39   }
40 
GetVersionVersionedComponentName41   int GetVersion() const {
42     return version_;
43   }
44 
45  private:
46   std::string package_;
47   std::string activity_;
48   int64_t version_;
49 };
50 
51 inline std::ostream& operator<<(std::ostream& os, const VersionedComponentName& vcn) {
52   os << vcn.GetPackage() << "/" << vcn.GetActivity() << "@" << vcn.GetVersion();
53   return os;
54 }
55 
56 class FileModelBase {
57  protected:
58   FileModelBase(VersionedComponentName vcn);
59 
60   virtual std::string SubDir() const = 0;
61   // Include the last file component only (/a/b/c.txt -> c.txt)
62   virtual std::string BaseFile() const = 0;
63 
~FileModelBase()64   virtual ~FileModelBase() {}
65 
66  public:
67   virtual std::string ModelName() const = 0;
68   // Return the absolute file path to this FileModel.
69   std::string FilePath() const;
70 
71   // Include the full path minus the basefile (/a/b/c.txt -> /a/b)
72   std::string BaseDir() const;
73 
74   // Create the base dir recursively (e.g. mkdir -p $basedir).
75   bool MkdirWithParents();
76  private:
77   VersionedComponentName vcn_;
78   std::string subdir_;
79   std::string filename_;
80   std::string root_path_;
81 };
82 
83 inline std::ostream& operator<<(std::ostream& os, const FileModelBase& fmb) {
84   os << fmb.ModelName() << "{'" << fmb.FilePath() << "'}";
85   return os;
86 }
87 
88 class DbHandle;
89 
90 class PerfettoTraceFileModel : public FileModelBase {
91  protected:
ModelName()92   virtual std::string ModelName() const override { return "PerfettoTrace"; }
SubDir()93   virtual std::string SubDir() const override { return "raw_traces"; }
94   virtual std::string BaseFile() const override;
95 
96   PerfettoTraceFileModel(VersionedComponentName vcn,
97                          uint64_t timestamp);
98 
99  public:
100   static PerfettoTraceFileModel CalculateNewestFilePath(VersionedComponentName vcn);
101   static void DeleteOlderFiles(DbHandle& db, VersionedComponentName vcn);
102 
~PerfettoTraceFileModel()103   virtual ~PerfettoTraceFileModel() {}
104  private:
105   uint64_t timestamp_;
106 };
107 
108 class CompiledTraceFileModel : public FileModelBase {
109  protected:
ModelName()110   virtual std::string ModelName() const override { return "CompiledTrace"; }
SubDir()111   virtual std::string SubDir() const override { return "compiled_traces"; }
112   virtual std::string BaseFile() const override;
113 
114   CompiledTraceFileModel(VersionedComponentName vcn);
115 
116  public:
117   static CompiledTraceFileModel CalculateNewestFilePath(VersionedComponentName vcn);
118 
~CompiledTraceFileModel()119   virtual ~CompiledTraceFileModel() {}
120 };
121 
122 }   // namespace iorap::db
123 
124 #endif  // IORAP_SRC_DB_FILE_MODELS_H_
125