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 services.src.index.store.codec; | |
6 | |
7 import 'dart:collection'; | |
8 | |
9 import 'package:analysis_services/index/index.dart'; | |
10 import 'package:analysis_services/src/index/store/collection.dart'; | |
11 import 'package:analyzer/src/generated/element.dart'; | |
12 import 'package:analyzer/src/generated/engine.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 = | |
23 new HashMap<AnalysisContext, int>(); | |
24 | |
25 /** | |
26 * A table mapping indices to the corresponding contexts. | |
27 */ | |
28 Map<int, AnalysisContext> _indexToContext = | |
29 new HashMap<int, AnalysisContext>(); | |
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 final StringCodec _stringCodec; | |
71 | |
72 /** | |
73 * A table mapping element encodings to a single integer. | |
74 */ | |
75 final IntArrayToIntMap _pathToIndex = new IntArrayToIntMap(); | |
76 | |
77 /** | |
78 * A list that works as a mapping of integers to element encodings. | |
79 */ | |
80 final List<List<int>> _indexToPath = <List<int>>[]; | |
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 Element element = context.getElement(location); | |
96 return element; | |
97 } | |
98 | |
99 /** | |
100 * Returns a unique integer that corresponds to the given [Element]. | |
101 */ | |
102 int encode(Element element) { | |
103 List<int> path = _getLocationPath(element); | |
104 int index = _pathToIndex[path]; | |
105 if (index == null) { | |
106 index = _indexToPath.length; | |
107 _pathToIndex[path] = index; | |
108 _indexToPath.add(path); | |
109 } | |
110 return index; | |
111 } | |
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 | |
127 List<String> _getLocationComponents(List<int> path) { | |
128 int length = path.length; | |
129 List<String> components = new List<String>(); | |
130 for (int i = 0; i < length; i++) { | |
131 int componentId = path[i]; | |
132 String component = _stringCodec.decode(componentId); | |
133 if (i < length - 1 && path[i + 1] < 0) { | |
134 component += '@${(-path[i + 1])}'; | |
135 i++; | |
136 } | |
137 components.add(component); | |
138 } | |
139 return components; | |
140 } | |
141 | |
142 List<int> _getLocationPath(Element element) { | |
143 List<String> components = element.location.components; | |
144 int length = components.length; | |
145 if (_hasLocalOffset(components)) { | |
146 List<int> path = new List<int>(); | |
147 for (String component in components) { | |
148 int atOffset = component.indexOf('@'); | |
149 if (atOffset == -1) { | |
150 path.add(_stringCodec.encode(component)); | |
151 } else { | |
152 String preAtString = component.substring(0, atOffset); | |
153 String atString = component.substring(atOffset + 1); | |
154 path.add(_stringCodec.encode(preAtString)); | |
155 path.add(-1 * int.parse(atString)); | |
156 } | |
157 } | |
158 return path; | |
159 } else { | |
160 List<int> path = new List<int>.filled(length, 0); | |
161 for (int i = 0; i < length; i++) { | |
162 String component = components[i]; | |
163 path[i] = _stringCodec.encode(component); | |
164 } | |
165 return path; | |
166 } | |
167 } | |
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 lastComponent = _substringBeforeAt(lastComponent); | |
178 int firstId = _stringCodec.encode(firstComponent); | |
179 int lastId = _stringCodec.encode(lastComponent); | |
180 return <int>[firstId, lastId]; | |
181 } | |
182 | |
183 bool _hasLocalOffset(List<String> components) { | |
184 for (String component in components) { | |
185 if (component.indexOf('@') != -1) { | |
186 return true; | |
187 } | |
188 } | |
189 return false; | |
190 } | |
191 | |
192 String _substringBeforeAt(String str) { | |
193 int atOffset = str.indexOf('@'); | |
194 if (atOffset != -1) { | |
195 str = str.substring(0, atOffset); | |
196 } | |
197 return str; | |
198 } | |
199 } | |
200 | |
201 | |
202 /** | |
203 * A helper that encodes/decodes [Relationship]s to/from integers. | |
204 */ | |
205 class RelationshipCodec { | |
206 final StringCodec _stringCodec; | |
207 | |
208 RelationshipCodec(this._stringCodec); | |
209 | |
210 Relationship decode(int idIndex) { | |
211 String id = _stringCodec.decode(idIndex); | |
212 return Relationship.getRelationship(id); | |
213 } | |
214 | |
215 int encode(Relationship relationship) { | |
216 String id = relationship.identifier; | |
217 return _stringCodec.encode(id); | |
218 } | |
219 } | |
220 | |
221 | |
222 /** | |
223 * A helper that encodes/decodes [String]s from/to integers. | |
224 */ | |
225 class StringCodec { | |
226 /** | |
227 * A table mapping names to their unique indices. | |
228 */ | |
229 final Map<String, int> nameToIndex = new HashMap<String, int>(); | |
230 | |
231 /** | |
232 * A table mapping indices to the corresponding strings. | |
233 */ | |
234 final List<String> _indexToName = <String>[]; | |
235 | |
236 /** | |
237 * Returns the [String] that corresponds to the given index. | |
238 */ | |
239 String decode(int index) => _indexToName[index]; | |
240 | |
241 /** | |
242 * Returns an unique index for the given [String]. | |
243 */ | |
244 int encode(String name) { | |
245 int index = nameToIndex[name]; | |
246 if (index == null) { | |
247 index = _indexToName.length; | |
248 nameToIndex[name] = index; | |
249 _indexToName.add(name); | |
250 } | |
251 return index; | |
252 } | |
253 } | |
OLD | NEW |