| 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'; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 return element; | 97 return element; |
| 98 } | 98 } |
| 99 | 99 |
| 100 /** | 100 /** |
| 101 * Returns a unique integer that corresponds to the given [Element]. | 101 * Returns a unique integer that corresponds to the given [Element]. |
| 102 * | 102 * |
| 103 * If [forKey] is `true` then [element] is a part of a key, so it should use | 103 * If [forKey] is `true` then [element] is a part of a key, so it should use |
| 104 * file paths instead of [Element] location URIs. | 104 * file paths instead of [Element] location URIs. |
| 105 */ | 105 */ |
| 106 int encode(Element element, bool forKey) { | 106 int encode(Element element, bool forKey) { |
| 107 List<int> path = _getLocationPath(element, forKey); | 107 ElementLocationImpl location = element.location; |
| 108 int index = _pathToIndex[path]; | 108 // check the location has a cached id |
| 109 if (index == null) { | 109 if (!identical(location.indexOwner, this)) { |
| 110 index = _indexToPath.length; | 110 location.indexKeyId = null; |
| 111 _pathToIndex[path] = index; | 111 location.indexLocationId = null; |
| 112 _indexToPath.add(path); | |
| 113 } | 112 } |
| 113 if (forKey) { |
| 114 int id = location.indexKeyId; |
| 115 if (id != null) { |
| 116 return id; |
| 117 } |
| 118 } else { |
| 119 int id = location.indexLocationId; |
| 120 if (id != null) { |
| 121 return id; |
| 122 } |
| 123 } |
| 124 // prepare an id |
| 125 List<int> path = _getLocationPath(element, location, forKey); |
| 126 int index = _encodePath(path); |
| 127 // put the id into the location |
| 128 if (forKey) { |
| 129 location.indexOwner = this; |
| 130 location.indexKeyId = index; |
| 131 } else { |
| 132 location.indexOwner = this; |
| 133 location.indexLocationId = index; |
| 134 } |
| 135 // done |
| 114 return index; | 136 return index; |
| 115 } | 137 } |
| 116 | 138 |
| 117 /** | 139 /** |
| 118 * Returns an integer that corresponds to an approximated location of [element
]. | 140 * Returns an integer that corresponds to an approximated location of [element
]. |
| 119 */ | 141 */ |
| 120 int encodeHash(Element element) { | 142 int encodeHash(Element element) { |
| 121 List<int> path = _getLocationPathLimited(element); | 143 List<int> path = _getLocationPathLimited(element); |
| 144 int index = _encodePath(path); |
| 145 return index; |
| 146 } |
| 147 |
| 148 int _encodePath(List<int> path) { |
| 122 int index = _pathToIndex[path]; | 149 int index = _pathToIndex[path]; |
| 123 if (index == null) { | 150 if (index == null) { |
| 124 index = _indexToPath.length; | 151 index = _indexToPath.length; |
| 125 _pathToIndex[path] = index; | 152 _pathToIndex[path] = index; |
| 126 _indexToPath.add(path); | 153 _indexToPath.add(path); |
| 127 } | 154 } |
| 128 return index; | 155 return index; |
| 129 } | 156 } |
| 130 | 157 |
| 131 List<String> _getLocationComponents(List<int> path) { | 158 List<String> _getLocationComponents(List<int> path) { |
| 132 int length = path.length; | 159 int length = path.length; |
| 133 List<String> components = new List<String>(); | 160 List<String> components = new List<String>(); |
| 134 for (int i = 0; i < length; i++) { | 161 for (int i = 0; i < length; i++) { |
| 135 int componentId = path[i]; | 162 int componentId = path[i]; |
| 136 String component = _stringCodec.decode(componentId); | 163 String component = _stringCodec.decode(componentId); |
| 137 if (i < length - 1 && path[i + 1] < 0) { | 164 if (i < length - 1 && path[i + 1] < 0) { |
| 138 component += '@${(-path[i + 1])}'; | 165 component += '@${(-path[i + 1])}'; |
| 139 i++; | 166 i++; |
| 140 } | 167 } |
| 141 components.add(component); | 168 components.add(component); |
| 142 } | 169 } |
| 143 return components; | 170 return components; |
| 144 } | 171 } |
| 145 | 172 |
| 146 /** | 173 /** |
| 147 * If [usePath] is `true` then [Source] path should be used instead of URI. | 174 * If [usePath] is `true` then [Source] path should be used instead of URI. |
| 148 */ | 175 */ |
| 149 List<int> _getLocationPath(Element element, bool usePath) { | 176 List<int> _getLocationPath(Element element, ElementLocation location, |
| 177 bool usePath) { |
| 150 // prepare the location components | 178 // prepare the location components |
| 151 List<String> components = element.location.components; | 179 List<String> components = location.components; |
| 152 if (usePath) { | 180 if (usePath) { |
| 153 LibraryElement library = element.library; | 181 LibraryElement library = element.library; |
| 154 if (library != null) { | 182 if (library != null) { |
| 183 components = components.toList(); |
| 155 components[0] = library.source.fullName; | 184 components[0] = library.source.fullName; |
| 156 if (element.enclosingElement is CompilationUnitElement) { | 185 if (element.enclosingElement is CompilationUnitElement) { |
| 157 components[1] = library.definingCompilationUnit.source.fullName; | 186 components[1] = library.definingCompilationUnit.source.fullName; |
| 158 } | 187 } |
| 159 } | 188 } |
| 160 } | 189 } |
| 161 // encode the location | 190 // encode the location |
| 162 int length = components.length; | 191 int length = components.length; |
| 163 if (_hasLocalOffset(components)) { | 192 if (_hasLocalOffset(components)) { |
| 164 List<int> path = new List<int>(); | 193 List<int> path = new List<int>(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 int encode(String name) { | 288 int encode(String name) { |
| 260 int index = nameToIndex[name]; | 289 int index = nameToIndex[name]; |
| 261 if (index == null) { | 290 if (index == null) { |
| 262 index = _indexToName.length; | 291 index = _indexToName.length; |
| 263 nameToIndex[name] = index; | 292 nameToIndex[name] = index; |
| 264 _indexToName.add(name); | 293 _indexToName.add(name); |
| 265 } | 294 } |
| 266 return index; | 295 return index; |
| 267 } | 296 } |
| 268 } | 297 } |
| OLD | NEW |