Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
| 26 | class HashableDimensionKey { |
| 27 | public: |
| 28 | explicit HashableDimensionKey(const std::vector<KeyValuePair>& keyValuePairs) |
| 29 | : mKeyValuePairs(keyValuePairs){}; |
| 30 | |
| 31 | HashableDimensionKey(){}; |
| 32 | |
| 33 | HashableDimensionKey(const HashableDimensionKey& that) |
| 34 | : mKeyValuePairs(that.getKeyValuePairs()){}; |
| 35 | |
| 36 | HashableDimensionKey& operator=(const HashableDimensionKey& from) = default; |
| 37 | |
| 38 | std::string toString() const; |
| 39 | |
| 40 | inline const std::vector<KeyValuePair>& getKeyValuePairs() const { |
| 41 | return mKeyValuePairs; |
| 42 | } |
| 43 | |
| 44 | bool operator==(const HashableDimensionKey& that) const; |
| 45 | |
| 46 | bool operator<(const HashableDimensionKey& that) const; |
| 47 | |
| 48 | inline const char* c_str() const { |
| 49 | return toString().c_str(); |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | std::vector<KeyValuePair> mKeyValuePairs; |
| 54 | }; |
| 55 | |
| 56 | } // namespace statsd |
| 57 | } // namespace os |
| 58 | } // namespace android |
| 59 | |
| 60 | namespace std { |
| 61 | |
| 62 | using android::os::statsd::HashableDimensionKey; |
| 63 | using android::os::statsd::KeyValuePair; |
| 64 | |
| 65 | template <> |
| 66 | struct hash<HashableDimensionKey> { |
| 67 | std::size_t operator()(const HashableDimensionKey& key) const { |
| 68 | android::hash_t hash = 0; |
| 69 | for (const auto& pair : key.getKeyValuePairs()) { |
| 70 | hash = android::JenkinsHashMix(hash, android::hash_type(pair.key())); |
| 71 | hash = android::JenkinsHashMix( |
| 72 | hash, android::hash_type(static_cast<int32_t>(pair.value_case()))); |
| 73 | switch (pair.value_case()) { |
| 74 | case KeyValuePair::ValueCase::kValueStr: |
| 75 | hash = android::JenkinsHashMix( |
| 76 | hash, |
| 77 | static_cast<uint32_t>(std::hash<std::string>()(pair.value_str()))); |
| 78 | break; |
| 79 | case KeyValuePair::ValueCase::kValueInt: |
| 80 | hash = android::JenkinsHashMix(hash, android::hash_type(pair.value_int())); |
| 81 | break; |
| 82 | case KeyValuePair::ValueCase::kValueLong: |
| 83 | hash = android::JenkinsHashMix( |
| 84 | hash, android::hash_type(static_cast<int64_t>(pair.value_long()))); |
| 85 | break; |
| 86 | case KeyValuePair::ValueCase::kValueBool: |
| 87 | hash = android::JenkinsHashMix(hash, android::hash_type(pair.value_bool())); |
| 88 | break; |
| 89 | case KeyValuePair::ValueCase::kValueFloat: { |
| 90 | float floatVal = pair.value_float(); |
| 91 | hash = android::JenkinsHashMixBytes(hash, (uint8_t*)&floatVal, sizeof(float)); |
| 92 | break; |
| 93 | } |
| 94 | case KeyValuePair::ValueCase::VALUE_NOT_SET: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | return hash; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | } // namespace std |