| 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_server/src/services/index/index.dart'; | 9 import 'package:analysis_server/src/services/index/index.dart'; |
| 10 import 'package:analysis_server/src/services/index/store/collection.dart'; | 10 import 'package:analysis_server/src/services/index/store/collection.dart'; |
| 11 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 14 | 15 |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * A helper that encodes/decodes [AnalysisContext]s from/to integers. | 18 * A helper that encodes/decodes [AnalysisContext]s from/to integers. |
| 18 */ | 19 */ |
| 19 class ContextCodec { | 20 class ContextCodec { |
| 20 /** | 21 /** |
| 21 * A table mapping contexts to their unique indices. | 22 * A table mapping contexts to their unique indices. |
| 22 */ | 23 */ |
| 23 Map<AnalysisContext, int> _contextToIndex = | 24 Map<AnalysisContext, int> _contextToIndex = |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 location.indexKeyId = index; | 130 location.indexKeyId = index; |
| 130 } else { | 131 } else { |
| 131 location.indexOwner = this; | 132 location.indexOwner = this; |
| 132 location.indexLocationId = index; | 133 location.indexLocationId = index; |
| 133 } | 134 } |
| 134 // done | 135 // done |
| 135 return index; | 136 return index; |
| 136 } | 137 } |
| 137 | 138 |
| 138 /** | 139 /** |
| 139 * Returns an integer that corresponds to an approximated location of [element
]. | 140 * Returns an integer that corresponds to the name of [element]. |
| 140 */ | 141 */ |
| 141 int encodeHash(Element element) { | 142 int encodeHash(Element element) { |
| 142 List<int> path = _getLocationPathLimited(element); | 143 String elementName = element.displayName; |
| 143 int index = _encodePath(path); | 144 int elementNameId = _stringCodec.encode(elementName); |
| 144 return index; | 145 LibraryElement libraryElement = element.library; |
| 146 if (libraryElement != null) { |
| 147 String libraryPath = libraryElement.source.fullName; |
| 148 int libraryPathId = _stringCodec.encode(libraryPath); |
| 149 return JenkinsSmiHash.combine(libraryPathId, elementNameId); |
| 150 } else { |
| 151 return elementNameId; |
| 152 } |
| 145 } | 153 } |
| 146 | 154 |
| 147 /** | 155 /** |
| 148 * Returns a list with the location components of the element with the | 156 * Returns a list with the location components of the element with the |
| 149 * given encoded ID. | 157 * given encoded ID. |
| 150 */ | 158 */ |
| 151 List<String> inspect_decodePath(int id) { | 159 List<String> inspect_decodePath(int id) { |
| 152 List<int> path = _indexToPath[id]; | 160 List<int> path = _indexToPath[id]; |
| 153 return _getLocationComponents(path); | 161 return _getLocationComponents(path); |
| 154 } | 162 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } else { | 244 } else { |
| 237 List<int> path = new List<int>.filled(length, 0); | 245 List<int> path = new List<int>.filled(length, 0); |
| 238 for (int i = 0; i < length; i++) { | 246 for (int i = 0; i < length; i++) { |
| 239 String component = components[i]; | 247 String component = components[i]; |
| 240 path[i] = _stringCodec.encode(component); | 248 path[i] = _stringCodec.encode(component); |
| 241 } | 249 } |
| 242 return path; | 250 return path; |
| 243 } | 251 } |
| 244 } | 252 } |
| 245 | 253 |
| 246 /** | |
| 247 * Returns an approximation of the [element]'s location. | |
| 248 */ | |
| 249 List<int> _getLocationPathLimited(Element element) { | |
| 250 String firstComponent; | |
| 251 { | |
| 252 LibraryElement libraryElement = element.library; | |
| 253 if (libraryElement != null) { | |
| 254 firstComponent = libraryElement.source.fullName; | |
| 255 } else { | |
| 256 firstComponent = 'null'; | |
| 257 } | |
| 258 } | |
| 259 String lastComponent = element.displayName; | |
| 260 int firstId = _stringCodec.encode(firstComponent); | |
| 261 int lastId = _stringCodec.encode(lastComponent); | |
| 262 return <int>[firstId, lastId]; | |
| 263 } | |
| 264 | |
| 265 static bool _hasLocalOffset(List<String> components) { | 254 static bool _hasLocalOffset(List<String> components) { |
| 266 for (String component in components) { | 255 for (String component in components) { |
| 267 if (component.indexOf('@') != -1) { | 256 if (component.indexOf('@') != -1) { |
| 268 return true; | 257 return true; |
| 269 } | 258 } |
| 270 } | 259 } |
| 271 return false; | 260 return false; |
| 272 } | 261 } |
| 273 } | 262 } |
| 274 | 263 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 int encode(String name) { | 307 int encode(String name) { |
| 319 int index = nameToIndex[name]; | 308 int index = nameToIndex[name]; |
| 320 if (index == null) { | 309 if (index == null) { |
| 321 index = _indexToName.length; | 310 index = _indexToName.length; |
| 322 nameToIndex[name] = index; | 311 nameToIndex[name] = index; |
| 323 _indexToName.add(name); | 312 _indexToName.add(name); |
| 324 } | 313 } |
| 325 return index; | 314 return index; |
| 326 } | 315 } |
| 327 } | 316 } |
| OLD | NEW |