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_server/src/provisional/index/index_core.dart'; | |
10 import 'package:analysis_server/src/services/index/index.dart'; | |
11 import 'package:analyzer/src/generated/engine.dart'; | |
12 | |
13 /** | |
14 * A helper that encodes/decodes [AnalysisContext]s from/to integers. | |
15 */ | |
16 class ContextCodec { | |
17 /** | |
18 * A table mapping contexts to their unique indices. | |
19 */ | |
20 Map<AnalysisContext, int> _contextToIndex = | |
21 new HashMap<AnalysisContext, int>(); | |
22 | |
23 /** | |
24 * A table mapping indices to the corresponding contexts. | |
25 */ | |
26 Map<int, AnalysisContext> _indexToContext = | |
27 new HashMap<int, AnalysisContext>(); | |
28 | |
29 /** | |
30 * The next id to assign. | |
31 */ | |
32 int _nextId = 0; | |
33 | |
34 /** | |
35 * Returns the [AnalysisContext] that corresponds to the given index. | |
36 */ | |
37 AnalysisContext decode(int index) => _indexToContext[index]; | |
38 | |
39 /** | |
40 * Returns an unique index for the given [AnalysisContext]. | |
41 */ | |
42 int encode(AnalysisContext context) { | |
43 int index = _contextToIndex[context]; | |
44 if (index == null) { | |
45 index = _nextId++; | |
46 _contextToIndex[context] = index; | |
47 _indexToContext[index] = context; | |
48 } | |
49 return index; | |
50 } | |
51 | |
52 /** | |
53 * Removes the given [context]. | |
54 */ | |
55 void remove(AnalysisContext context) { | |
56 int id = _contextToIndex.remove(context); | |
57 if (id != null) { | |
58 _indexToContext.remove(id); | |
59 } | |
60 } | |
61 } | |
62 | |
63 /** | |
64 * A helper that encodes/decodes [IndexableObject]s to/from integers. | |
65 */ | |
66 class ElementCodec { | |
67 // TODO(brianwilkerson) Rename this class now that if encodes indexable | |
68 // objects rather than elements. | |
69 final StringCodec _stringCodec; | |
70 | |
71 ElementCodec(this._stringCodec); | |
72 | |
73 /** | |
74 * Returns an [IndexableObject] that corresponds to the given identifiers. | |
75 */ | |
76 IndexableObject decode( | |
77 AnalysisContext context, int fileId, int offset, int kindId) { | |
78 IndexableObjectKind kind = IndexableObjectKind.getKind(kindId); | |
79 if (kind == null) { | |
80 return null; | |
81 } else if (kind is IndexableNameKind) { | |
82 String name = _stringCodec.decode(offset); | |
83 return new IndexableName(name); | |
84 } | |
85 String filePath = _stringCodec.decode(fileId); | |
86 return kind.decode(context, filePath, offset); | |
87 } | |
88 | |
89 /** | |
90 * Returns the first component of the [indexable] id. | |
91 * In the most cases it is an encoding of the [indexable]'s file path. | |
92 * If the given [indexable] is not defined in a file, returns `-1`. | |
93 */ | |
94 int encode1(IndexableObject indexable) { | |
95 String filePath = indexable.filePath; | |
96 if (filePath == null) { | |
97 return -1; | |
98 } | |
99 return _stringCodec.encode(filePath); | |
100 } | |
101 | |
102 /** | |
103 * Returns the second component of the [indexable] id. | |
104 * In the most cases it is the [indexable]'s name offset. | |
105 */ | |
106 int encode2(IndexableObject indexable) { | |
107 if (indexable is IndexableName) { | |
108 String name = indexable.name; | |
109 return _stringCodec.encode(name); | |
110 } | |
111 return indexable.offset; | |
112 } | |
113 | |
114 /** | |
115 * Returns the third component of the [indexable] id. | |
116 * In the most cases it is the [indexable]'s kind. | |
117 */ | |
118 int encode3(IndexableObject indexable) { | |
119 return indexable.kind.index; | |
120 } | |
121 | |
122 /** | |
123 * Returns an integer that corresponds to the name of [indexable]. | |
124 */ | |
125 int encodeHash(IndexableObject indexable) { | |
126 return indexable.kind.encodeHash(_stringCodec.encode, indexable); | |
127 } | |
128 } | |
129 | |
130 /** | |
131 * A helper that encodes/decodes [Relationship]s to/from integers. | |
132 */ | |
133 class RelationshipCodec { | |
134 final StringCodec _stringCodec; | |
135 | |
136 RelationshipCodec(this._stringCodec); | |
137 | |
138 RelationshipImpl decode(int idIndex) { | |
139 String id = _stringCodec.decode(idIndex); | |
140 return RelationshipImpl.getRelationship(id); | |
141 } | |
142 | |
143 int encode(RelationshipImpl relationship) { | |
144 String id = relationship.identifier; | |
145 return _stringCodec.encode(id); | |
146 } | |
147 } | |
148 | |
149 /** | |
150 * A helper that encodes/decodes [String]s from/to integers. | |
151 */ | |
152 class StringCodec { | |
153 /** | |
154 * A table mapping names to their unique indices. | |
155 */ | |
156 final Map<String, int> nameToIndex = new HashMap<String, int>(); | |
157 | |
158 /** | |
159 * A table mapping indices to the corresponding strings. | |
160 */ | |
161 final List<String> _indexToName = <String>[]; | |
162 | |
163 /** | |
164 * Returns the [String] that corresponds to the given index. | |
165 */ | |
166 String decode(int index) => _indexToName[index]; | |
167 | |
168 /** | |
169 * Returns an unique index for the given [String]. | |
170 */ | |
171 int encode(String name) { | |
172 int index = nameToIndex[name]; | |
173 if (index == null) { | |
174 index = _indexToName.length; | |
175 nameToIndex[name] = index; | |
176 _indexToName.add(name); | |
177 } | |
178 return index; | |
179 } | |
180 } | |
OLD | NEW |