Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define DEBUG false |
| 18 | #include "Log.h" |
| 19 | #include "FieldValue.h" |
| 20 | #include "HashableDimensionKey.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace os { |
| 24 | namespace statsd { |
| 25 | |
| 26 | bool Field::matches(const Matcher& matcher) const { |
| 27 | if (mTag != matcher.mMatcher.getTag()) { |
| 28 | return false; |
| 29 | } |
| 30 | if ((mField & matcher.mMask) == matcher.mMatcher.getField()) { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | return false; |
| 35 | }; |
| 36 | |
| 37 | void translateFieldMatcher(int tag, const FieldMatcher& matcher, int depth, int* pos, int* mask, |
| 38 | std::vector<Matcher>* output) { |
| 39 | if (depth > kMaxLogDepth) { |
| 40 | ALOGE("depth > 2"); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | pos[depth] = matcher.field(); |
| 45 | mask[depth] = 0x7f; |
| 46 | |
| 47 | if (matcher.has_position()) { |
| 48 | depth++; |
| 49 | if (depth > 2) { |
| 50 | return; |
| 51 | } |
| 52 | switch (matcher.position()) { |
| 53 | case Position::ANY: |
| 54 | pos[depth] = 0; |
| 55 | mask[depth] = 0; |
| 56 | break; |
| 57 | case Position::FIRST: |
| 58 | pos[depth] = 1; |
| 59 | mask[depth] = 0x7f; |
| 60 | break; |
| 61 | case Position::LAST: |
| 62 | pos[depth] = 0x80; |
| 63 | mask[depth] = 0x80; |
| 64 | break; |
| 65 | case Position::POSITION_UNKNOWN: |
| 66 | pos[depth] = 0; |
| 67 | mask[depth] = 0; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (matcher.child_size() == 0) { |
| 73 | output->push_back(Matcher(Field(tag, pos, depth), encodeMatcherMask(mask, depth))); |
| 74 | Matcher matcher = Matcher(Field(tag, pos, depth), encodeMatcherMask(mask, depth)); |
| 75 | } else { |
| 76 | for (const auto& child : matcher.child()) { |
| 77 | translateFieldMatcher(tag, child, depth + 1, pos, mask, output); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output) { |
| 83 | int pos[] = {1, 1, 1}; |
| 84 | int mask[] = {0x7f, 0x7f, 0x7f}; |
| 85 | int tag = matcher.field(); |
| 86 | for (const auto& child : matcher.child()) { |
| 87 | translateFieldMatcher(tag, child, 0, pos, mask, output); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool isAttributionUidField(const FieldValue& value) { |
| 92 | int field = value.mField.getField() & 0xff007f; |
| 93 | if (field == 0x10001 && value.mValue.getType() == INT) { |
| 94 | return true; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | bool isAttributionUidField(const Field& field, const Value& value) { |
| 100 | int f = field.getField() & 0xff007f; |
| 101 | if (f == 0x10001 && value.getType() == INT) { |
| 102 | return true; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | Value::Value(const Value& from) { |
| 108 | type = from.getType(); |
| 109 | switch (type) { |
| 110 | case INT: |
| 111 | int_value = from.int_value; |
| 112 | break; |
| 113 | case LONG: |
| 114 | long_value = from.long_value; |
| 115 | break; |
| 116 | case FLOAT: |
| 117 | float_value = from.float_value; |
| 118 | break; |
| 119 | case STRING: |
| 120 | str_value = from.str_value; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | std::string Value::toString() const { |
| 126 | switch (type) { |
| 127 | case INT: |
| 128 | return std::to_string(int_value) + "[I]"; |
| 129 | case LONG: |
| 130 | return std::to_string(long_value) + "[L]"; |
| 131 | case FLOAT: |
| 132 | return std::to_string(float_value) + "[F]"; |
| 133 | case STRING: |
| 134 | return str_value + "[S]"; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | bool Value::operator==(const Value& that) const { |
| 139 | if (type != that.getType()) return false; |
| 140 | |
| 141 | switch (type) { |
| 142 | case INT: |
| 143 | return int_value == that.int_value; |
| 144 | case LONG: |
| 145 | return long_value == that.long_value; |
| 146 | case FLOAT: |
| 147 | return float_value == that.float_value; |
| 148 | case STRING: |
| 149 | return str_value == that.str_value; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | bool Value::operator!=(const Value& that) const { |
| 154 | if (type != that.getType()) return true; |
| 155 | switch (type) { |
| 156 | case INT: |
| 157 | return int_value != that.int_value; |
| 158 | case LONG: |
| 159 | return long_value != that.long_value; |
| 160 | case FLOAT: |
| 161 | return float_value != that.float_value; |
| 162 | case STRING: |
| 163 | return str_value != that.str_value; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bool Value::operator<(const Value& that) const { |
| 168 | if (type != that.getType()) return type < that.getType(); |
| 169 | |
| 170 | switch (type) { |
| 171 | case INT: |
| 172 | return int_value < that.int_value; |
| 173 | case LONG: |
| 174 | return long_value < that.long_value; |
| 175 | case FLOAT: |
| 176 | return float_value < that.float_value; |
| 177 | case STRING: |
| 178 | return str_value < that.str_value; |
| 179 | default: |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } // namespace statsd |
| 185 | } // namespace os |
| 186 | } // namespace android |