blob: a31d7a6d85c6cf74a02b7e749a4c327ed6605065 [file] [log] [blame]
Yao Chend5aa01b32017-12-19 16:46:36 -08001/*
2 * Copyright (C) 2017 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#pragma once
18
19#include <utils/JenkinsHash.h>
20#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
21
22namespace android {
23namespace os {
24namespace statsd {
25
26class HashableDimensionKey {
27public:
Yangster-mac20877162017-12-22 17:19:39 -080028 explicit HashableDimensionKey(const DimensionsValue& dimensionsValue)
29 : mDimensionsValue(dimensionsValue){};
Yao Chend5aa01b32017-12-19 16:46:36 -080030
31 HashableDimensionKey(){};
32
33 HashableDimensionKey(const HashableDimensionKey& that)
Yangster-mac20877162017-12-22 17:19:39 -080034 : mDimensionsValue(that.getDimensionsValue()){};
Yao Chend5aa01b32017-12-19 16:46:36 -080035
36 HashableDimensionKey& operator=(const HashableDimensionKey& from) = default;
37
38 std::string toString() const;
39
Yangster-mac20877162017-12-22 17:19:39 -080040 inline const DimensionsValue& getDimensionsValue() const {
41 return mDimensionsValue;
Yao Chend5aa01b32017-12-19 16:46:36 -080042 }
43
Yangster-mac93694462018-01-22 20:49:31 -080044 inline DimensionsValue* getMutableDimensionsValue() {
45 return &mDimensionsValue;
46 }
47
Yao Chend5aa01b32017-12-19 16:46:36 -080048 bool operator==(const HashableDimensionKey& that) const;
49
50 bool operator<(const HashableDimensionKey& that) const;
51
52 inline const char* c_str() const {
53 return toString().c_str();
54 }
55
56private:
Yangster-mac20877162017-12-22 17:19:39 -080057 DimensionsValue mDimensionsValue;
Yao Chend5aa01b32017-12-19 16:46:36 -080058};
59
Yangster-mac93694462018-01-22 20:49:31 -080060class MetricDimensionKey {
61 public:
62 explicit MetricDimensionKey(const HashableDimensionKey& dimensionKeyInWhat,
63 const HashableDimensionKey& dimensionKeyInCondition)
64 : mDimensionKeyInWhat(dimensionKeyInWhat),
65 mDimensionKeyInCondition(dimensionKeyInCondition) {};
66
67 MetricDimensionKey(){};
68
69 MetricDimensionKey(const MetricDimensionKey& that)
70 : mDimensionKeyInWhat(that.getDimensionKeyInWhat()),
71 mDimensionKeyInCondition(that.getDimensionKeyInCondition()) {};
72
73 MetricDimensionKey& operator=(const MetricDimensionKey& from) = default;
74
75 std::string toString() const;
76
77 inline const HashableDimensionKey& getDimensionKeyInWhat() const {
78 return mDimensionKeyInWhat;
79 }
80
81 inline const HashableDimensionKey& getDimensionKeyInCondition() const {
82 return mDimensionKeyInCondition;
83 }
84
85 bool hasDimensionKeyInCondition() const {
86 return mDimensionKeyInCondition.getDimensionsValue().has_field();
87 }
88
89 bool operator==(const MetricDimensionKey& that) const;
90
91 bool operator<(const MetricDimensionKey& that) const;
92
93 inline const char* c_str() const {
94 return toString().c_str();
95 }
96 private:
97 HashableDimensionKey mDimensionKeyInWhat;
98 HashableDimensionKey mDimensionKeyInCondition;
99};
100
101bool compareDimensionsValue(const DimensionsValue& s1, const DimensionsValue& s2);
102
Yangster-mac94e197c2018-01-02 16:03:03 -0800103android::hash_t hashDimensionsValue(int64_t seed, const DimensionsValue& value);
Yangster-mac20877162017-12-22 17:19:39 -0800104android::hash_t hashDimensionsValue(const DimensionsValue& value);
Yangster-mac93694462018-01-22 20:49:31 -0800105android::hash_t hashMetricDimensionKey(int64_t see, const MetricDimensionKey& dimensionKey);
Yangster-mac20877162017-12-22 17:19:39 -0800106
Yao Chend5aa01b32017-12-19 16:46:36 -0800107} // namespace statsd
108} // namespace os
109} // namespace android
110
111namespace std {
112
113using android::os::statsd::HashableDimensionKey;
Yangster-mac93694462018-01-22 20:49:31 -0800114using android::os::statsd::MetricDimensionKey;
Yao Chend5aa01b32017-12-19 16:46:36 -0800115
116template <>
117struct hash<HashableDimensionKey> {
118 std::size_t operator()(const HashableDimensionKey& key) const {
Yangster-mac20877162017-12-22 17:19:39 -0800119 return hashDimensionsValue(key.getDimensionsValue());
Yao Chend5aa01b32017-12-19 16:46:36 -0800120 }
121};
122
Yangster-mac93694462018-01-22 20:49:31 -0800123template <>
124struct hash<MetricDimensionKey> {
125 std::size_t operator()(const MetricDimensionKey& key) const {
126 android::hash_t hash = hashDimensionsValue(
127 key.getDimensionKeyInWhat().getDimensionsValue());
128 hash = android::JenkinsHashMix(hash,
129 hashDimensionsValue(key.getDimensionKeyInCondition().getDimensionsValue()));
130 return android::JenkinsHashWhiten(hash);
131 }
132};
133} // namespace std