| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library services.src.index.store.codec; | 5 library services.src.index.store.codec; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analysis_services/index/index.dart'; |
| 10 import 'package:analysis_services/src/index/store/collection.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 11 import 'package:analysis_services/src/index/store/collection.dart'; | |
| 12 import 'package:analysis_services/index/index.dart'; | |
| 13 | 13 |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * A helper that encodes/decodes [AnalysisContext]s from/to integers. | 16 * A helper that encodes/decodes [AnalysisContext]s from/to integers. |
| 17 */ | 17 */ |
| 18 class ContextCodec { | 18 class ContextCodec { |
| 19 /** | 19 /** |
| 20 * A table mapping contexts to their unique indices. | 20 * A table mapping contexts to their unique indices. |
| 21 */ | 21 */ |
| 22 Map<AnalysisContext, int> _contextToIndex = | 22 Map<AnalysisContext, int> _contextToIndex = |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 List<int> path = _getLocationPath(element); | 103 List<int> path = _getLocationPath(element); |
| 104 int index = _pathToIndex[path]; | 104 int index = _pathToIndex[path]; |
| 105 if (index == null) { | 105 if (index == null) { |
| 106 index = _indexToPath.length; | 106 index = _indexToPath.length; |
| 107 _pathToIndex[path] = index; | 107 _pathToIndex[path] = index; |
| 108 _indexToPath.add(path); | 108 _indexToPath.add(path); |
| 109 } | 109 } |
| 110 return index; | 110 return index; |
| 111 } | 111 } |
| 112 | 112 |
| 113 /** |
| 114 * Returns an integer that corresponds to an approximated location of the give
n {@link Element}. |
| 115 */ |
| 116 int encodeHash(Element element) { |
| 117 List<int> path = _getLocationPathLimited(element); |
| 118 int index = _pathToIndex[path]; |
| 119 if (index == null) { |
| 120 index = _indexToPath.length; |
| 121 _pathToIndex[path] = index; |
| 122 _indexToPath.add(path); |
| 123 } |
| 124 return index; |
| 125 } |
| 126 |
| 113 List<String> _getLocationComponents(List<int> path) { | 127 List<String> _getLocationComponents(List<int> path) { |
| 114 int length = path.length; | 128 int length = path.length; |
| 115 List<String> components = new List<String>(); | 129 List<String> components = new List<String>(); |
| 116 for (int i = 0; i < length; i++) { | 130 for (int i = 0; i < length; i++) { |
| 117 int componentId = path[i]; | 131 int componentId = path[i]; |
| 118 String component = _stringCodec.decode(componentId); | 132 String component = _stringCodec.decode(componentId); |
| 119 if (i < length - 1 && path[i + 1] < 0) { | 133 if (i < length - 1 && path[i + 1] < 0) { |
| 120 component += '@${(-path[i + 1])}'; | 134 component += '@${(-path[i + 1])}'; |
| 121 i++; | 135 i++; |
| 122 } | 136 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 145 } else { | 159 } else { |
| 146 List<int> path = new List<int>.filled(length, 0); | 160 List<int> path = new List<int>.filled(length, 0); |
| 147 for (int i = 0; i < length; i++) { | 161 for (int i = 0; i < length; i++) { |
| 148 String component = components[i]; | 162 String component = components[i]; |
| 149 path[i] = _stringCodec.encode(component); | 163 path[i] = _stringCodec.encode(component); |
| 150 } | 164 } |
| 151 return path; | 165 return path; |
| 152 } | 166 } |
| 153 } | 167 } |
| 154 | 168 |
| 169 /** |
| 170 * Returns an approximation of the given {@link Element}'s location. |
| 171 */ |
| 172 List<int> _getLocationPathLimited(Element element) { |
| 173 List<String> components = element.location.components; |
| 174 int length = components.length; |
| 175 String firstComponent = components[0]; |
| 176 String lastComponent = components[length - 1]; |
| 177 firstComponent = firstComponent.substring(1); |
| 178 lastComponent = _substringBeforeAt(lastComponent); |
| 179 int firstId = _stringCodec.encode(firstComponent); |
| 180 int lastId = _stringCodec.encode(lastComponent); |
| 181 return <int>[firstId, lastId]; |
| 182 } |
| 183 |
| 155 bool _hasLocalOffset(List<String> components) { | 184 bool _hasLocalOffset(List<String> components) { |
| 156 for (String component in components) { | 185 for (String component in components) { |
| 157 if (component.indexOf('@') != -1) { | 186 if (component.indexOf('@') != -1) { |
| 158 return true; | 187 return true; |
| 159 } | 188 } |
| 160 } | 189 } |
| 161 return false; | 190 return false; |
| 162 } | 191 } |
| 192 |
| 193 String _substringBeforeAt(String str) { |
| 194 int atOffset = str.indexOf('@'); |
| 195 if (atOffset != -1) { |
| 196 str = str.substring(0, atOffset); |
| 197 } |
| 198 return str; |
| 199 } |
| 163 } | 200 } |
| 164 | 201 |
| 165 | 202 |
| 166 /** | 203 /** |
| 167 * A helper that encodes/decodes [Relationship]s to/from integers. | 204 * A helper that encodes/decodes [Relationship]s to/from integers. |
| 168 */ | 205 */ |
| 169 class RelationshipCodec { | 206 class RelationshipCodec { |
| 170 final StringCodec _stringCodec; | 207 final StringCodec _stringCodec; |
| 171 | 208 |
| 172 RelationshipCodec(this._stringCodec); | 209 RelationshipCodec(this._stringCodec); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 int encode(String name) { | 245 int encode(String name) { |
| 209 int index = nameToIndex[name]; | 246 int index = nameToIndex[name]; |
| 210 if (index == null) { | 247 if (index == null) { |
| 211 index = _indexToName.length; | 248 index = _indexToName.length; |
| 212 nameToIndex[name] = index; | 249 nameToIndex[name] = index; |
| 213 _indexToName.add(name); | 250 _indexToName.add(name); |
| 214 } | 251 } |
| 215 return index; | 252 return index; |
| 216 } | 253 } |
| 217 } | 254 } |
| OLD | NEW |