| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library index.store.codec; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analysis_server/src/index/store/collection.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/index.dart'; |
| 13 |
| 14 |
| 15 /** |
| 16 * A helper that encodes/decodes [AnalysisContext]s from/to integers. |
| 17 */ |
| 18 class ContextCodec { |
| 19 /** |
| 20 * A table mapping contexts to their unique indices. |
| 21 */ |
| 22 Map<AnalysisContext, int> _contextToIndex = new HashMap<AnalysisContext, int>( |
| 23 ); |
| 24 |
| 25 /** |
| 26 * A table mapping indices to the corresponding contexts. |
| 27 */ |
| 28 Map<int, AnalysisContext> _indexToContext = new HashMap<int, AnalysisContext>( |
| 29 ); |
| 30 |
| 31 /** |
| 32 * The next id to assign. |
| 33 */ |
| 34 int _nextId = 0; |
| 35 |
| 36 /** |
| 37 * Returns the [AnalysisContext] that corresponds to the given index. |
| 38 */ |
| 39 AnalysisContext decode(int index) => _indexToContext[index]; |
| 40 |
| 41 /** |
| 42 * Returns an unique index for the given [AnalysisContext]. |
| 43 */ |
| 44 int encode(AnalysisContext context) { |
| 45 int index = _contextToIndex[context]; |
| 46 if (index == null) { |
| 47 index = _nextId++; |
| 48 _contextToIndex[context] = index; |
| 49 _indexToContext[index] = context; |
| 50 } |
| 51 return index; |
| 52 } |
| 53 |
| 54 /** |
| 55 * Removes the given [context]. |
| 56 */ |
| 57 void remove(AnalysisContext context) { |
| 58 int id = _contextToIndex.remove(context); |
| 59 if (id != null) { |
| 60 _indexToContext.remove(id); |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 |
| 66 /** |
| 67 * A helper that encodes/decodes [Element]s to/from integers. |
| 68 */ |
| 69 class ElementCodec { |
| 70 /** |
| 71 * A list that works as a mapping of integers to element encodings (in form of
integer arrays). |
| 72 */ |
| 73 List<List<int>> _indexToPath = []; |
| 74 |
| 75 /** |
| 76 * A table mapping element locations (in form of integer arrays) into a single
integer. |
| 77 */ |
| 78 IntArrayToIntMap _pathToIndex = new IntArrayToIntMap(); |
| 79 |
| 80 final StringCodec _stringCodec; |
| 81 |
| 82 ElementCodec(this._stringCodec); |
| 83 |
| 84 /** |
| 85 * Returns an [Element] that corresponds to the given location. |
| 86 * |
| 87 * @param context the [AnalysisContext] to find [Element] in |
| 88 * @param index an integer corresponding to the [Element] |
| 89 * @return the [Element] or `null` |
| 90 */ |
| 91 Element decode(AnalysisContext context, int index) { |
| 92 List<int> path = _indexToPath[index]; |
| 93 List<String> components = _getLocationComponents(path); |
| 94 ElementLocation location = new ElementLocationImpl.con3(components); |
| 95 return context.getElement(location); |
| 96 } |
| 97 |
| 98 /** |
| 99 * Returns a unique integer that corresponds to the given [Element]. |
| 100 */ |
| 101 int encode(Element element) { |
| 102 List<int> path = _getLocationPath(element); |
| 103 int index = _pathToIndex[path]; |
| 104 if (index == null) { |
| 105 index = _indexToPath.length; |
| 106 _pathToIndex[path] = index; |
| 107 _indexToPath.add(path); |
| 108 } |
| 109 return index; |
| 110 } |
| 111 |
| 112 List<String> _getLocationComponents(List<int> path) { |
| 113 int length = path.length; |
| 114 List<String> components = new List<String>(); |
| 115 for (int i = 0; i < length; i++) { |
| 116 int componentId = path[i]; |
| 117 String component = _stringCodec.decode(componentId); |
| 118 if (i < length - 1 && path[i + 1] < 0) { |
| 119 component += '@${(-path[i + 1])}'; |
| 120 i++; |
| 121 } |
| 122 components.add(component); |
| 123 } |
| 124 return components; |
| 125 } |
| 126 |
| 127 List<int> _getLocationPath(Element element) { |
| 128 List<String> components = element.location.components; |
| 129 int length = components.length; |
| 130 if (_hasLocalOffset(components)) { |
| 131 List<int> path = new List<int>(); |
| 132 for (String component in components) { |
| 133 int atOffset = component.indexOf('@'); |
| 134 if (atOffset == -1) { |
| 135 path.add(_stringCodec.encode(component)); |
| 136 } else { |
| 137 String preAtString = component.substring(0, atOffset); |
| 138 String atString = component.substring(atOffset + 1); |
| 139 path.add(_stringCodec.encode(preAtString)); |
| 140 path.add(-1 * int.parse(atString)); |
| 141 } |
| 142 } |
| 143 return path; |
| 144 } else { |
| 145 List<int> path = new List<int>.filled(length, 0); |
| 146 for (int i = 0; i < length; i++) { |
| 147 String component = components[i]; |
| 148 path[i] = _stringCodec.encode(component); |
| 149 } |
| 150 return path; |
| 151 } |
| 152 } |
| 153 |
| 154 bool _hasLocalOffset(List<String> components) { |
| 155 for (String component in components) { |
| 156 if (component.indexOf('@') != -1) { |
| 157 return true; |
| 158 } |
| 159 } |
| 160 return false; |
| 161 } |
| 162 } |
| 163 |
| 164 |
| 165 /** |
| 166 * A helper that encodes/decodes [Relationship]s to/from integers. |
| 167 */ |
| 168 class RelationshipCodec { |
| 169 final StringCodec _stringCodec; |
| 170 |
| 171 RelationshipCodec(this._stringCodec); |
| 172 |
| 173 Relationship decode(int idIndex) { |
| 174 String id = _stringCodec.decode(idIndex); |
| 175 return Relationship.getRelationship(id); |
| 176 } |
| 177 |
| 178 int encode(Relationship relationship) { |
| 179 String id = relationship.identifier; |
| 180 return _stringCodec.encode(id); |
| 181 } |
| 182 } |
| 183 |
| 184 |
| 185 /** |
| 186 * A helper that encodes/decodes [String]s from/to integers. |
| 187 */ |
| 188 class StringCodec { |
| 189 /** |
| 190 * A table mapping names to their unique indices. |
| 191 */ |
| 192 final Map<String, int> nameToIndex = {}; |
| 193 |
| 194 /** |
| 195 * A table mapping indices to the corresponding strings. |
| 196 */ |
| 197 List<String> _indexToName = []; |
| 198 |
| 199 /** |
| 200 * Returns the [String] that corresponds to the given index. |
| 201 */ |
| 202 String decode(int index) => _indexToName[index]; |
| 203 |
| 204 /** |
| 205 * Returns an unique index for the given [String]. |
| 206 */ |
| 207 int encode(String name) { |
| 208 int index = nameToIndex[name]; |
| 209 if (index == null) { |
| 210 index = _indexToName.length; |
| 211 nameToIndex[name] = index; |
| 212 _indexToName.add(name); |
| 213 } |
| 214 return index; |
| 215 } |
| 216 } |
| OLD | NEW |