| 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/analysis/index_core.dart'; | 9 import 'package:analysis_server/analysis/index_core.dart'; |
| 10 import 'package:analysis_server/src/services/index/index.dart'; | 10 import 'package:analysis_server/src/services/index/index.dart'; |
| 11 import 'package:analysis_server/src/services/index/indexable_element.dart'; | |
| 12 import 'package:analyzer/src/generated/element.dart'; | |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/generated/utilities_general.dart'; | |
| 16 | 13 |
| 17 /** | 14 /** |
| 18 * A helper that encodes/decodes [AnalysisContext]s from/to integers. | 15 * A helper that encodes/decodes [AnalysisContext]s from/to integers. |
| 19 */ | 16 */ |
| 20 class ContextCodec { | 17 class ContextCodec { |
| 21 /** | 18 /** |
| 22 * A table mapping contexts to their unique indices. | 19 * A table mapping contexts to their unique indices. |
| 23 */ | 20 */ |
| 24 Map<AnalysisContext, int> _contextToIndex = | 21 Map<AnalysisContext, int> _contextToIndex = |
| 25 new HashMap<AnalysisContext, int>(); | 22 new HashMap<AnalysisContext, int>(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // objects rather than elements. | 69 // objects rather than elements. |
| 73 final StringCodec _stringCodec; | 70 final StringCodec _stringCodec; |
| 74 | 71 |
| 75 ElementCodec(this._stringCodec); | 72 ElementCodec(this._stringCodec); |
| 76 | 73 |
| 77 /** | 74 /** |
| 78 * Returns an [IndexableObject] that corresponds to the given identifiers. | 75 * Returns an [IndexableObject] that corresponds to the given identifiers. |
| 79 */ | 76 */ |
| 80 IndexableObject decode( | 77 IndexableObject decode( |
| 81 AnalysisContext context, int fileId, int offset, int kindId) { | 78 AnalysisContext context, int fileId, int offset, int kindId) { |
| 82 String filePath = _stringCodec.decode(fileId); | |
| 83 IndexableObjectKind kind = IndexableObjectKind.getKind(kindId); | 79 IndexableObjectKind kind = IndexableObjectKind.getKind(kindId); |
| 84 if (kind == null) { | 80 if (kind == null) { |
| 85 return null; | 81 return null; |
| 86 } else if (kind is IndexableNameKind) { | 82 } else if (kind is IndexableNameKind) { |
| 87 String name = _stringCodec.decode(offset); | 83 String name = _stringCodec.decode(offset); |
| 88 return new IndexableName(name); | 84 return new IndexableName(name); |
| 89 } | 85 } |
| 86 String filePath = _stringCodec.decode(fileId); |
| 90 return kind.decode(context, filePath, offset); | 87 return kind.decode(context, filePath, offset); |
| 91 } | 88 } |
| 92 | 89 |
| 93 /** | 90 /** |
| 94 * Returns the first component of the [indexable] id. | 91 * Returns the first component of the [indexable] id. |
| 95 * In the most cases it is an encoding of the [indexable]'s file path. | 92 * In the most cases it is an encoding of the [indexable]'s file path. |
| 96 * If the given [indexable] is not defined in a file, returns `-1`. | 93 * If the given [indexable] is not defined in a file, returns `-1`. |
| 97 */ | 94 */ |
| 98 int encode1(IndexableObject indexable) { | 95 int encode1(IndexableObject indexable) { |
| 99 Source source = indexable.source; | 96 Source source = indexable.source; |
| 100 if (source == null) { | 97 if (source == null) { |
| 101 return -1; | 98 return -1; |
| 102 } | 99 } |
| 103 String filePath = source.fullName; | 100 String filePath = source.fullName; |
| 104 return _stringCodec.encode(filePath); | 101 return _stringCodec.encode(filePath); |
| 105 } | 102 } |
| 106 | 103 |
| 107 /** | 104 /** |
| 108 * Returns the second component of the [indexable] id. | 105 * Returns the second component of the [indexable] id. |
| 109 * In the most cases it is the [indexable]'s name offset. | 106 * In the most cases it is the [indexable]'s name offset. |
| 110 */ | 107 */ |
| 111 int encode2(IndexableObject indexable) { | 108 int encode2(IndexableObject indexable) { |
| 112 if (indexable is IndexableName) { | 109 if (indexable is IndexableName) { |
| 113 String name = indexable.name; | 110 String name = indexable.name; |
| 114 return _stringCodec.encode(name); | 111 return _stringCodec.encode(name); |
| 115 } | 112 } |
| 116 int offset = indexable.offset; | 113 return indexable.offset; |
| 117 if (offset < 0) { | |
| 118 return _stringCodec.encode(indexable.name); | |
| 119 } | |
| 120 return offset; | |
| 121 } | 114 } |
| 122 | 115 |
| 123 /** | 116 /** |
| 124 * Returns the third component of the [indexable] id. | 117 * Returns the third component of the [indexable] id. |
| 125 * In the most cases it is the [indexable]'s kind. | 118 * In the most cases it is the [indexable]'s kind. |
| 126 */ | 119 */ |
| 127 int encode3(IndexableObject indexable) { | 120 int encode3(IndexableObject indexable) { |
| 128 return indexable.kind.index; | 121 return indexable.kind.index; |
| 129 } | 122 } |
| 130 | 123 |
| 131 /** | 124 /** |
| 132 * Returns an integer that corresponds to the name of [indexable]. | 125 * Returns an integer that corresponds to the name of [indexable]. |
| 133 */ | 126 */ |
| 134 int encodeHash(IndexableObject indexable) { | 127 int encodeHash(IndexableObject indexable) { |
| 135 // TODO(brianwilkerson) Consider moving this to IndexableObjectKind so that | 128 return indexable.kind.encodeHash(_stringCodec.encode, indexable); |
| 136 // we don't have to break encapsulation. | |
| 137 String elementName = indexable.name; // was: indexable.displayName; | |
| 138 int elementNameId = _stringCodec.encode(elementName); | |
| 139 if (indexable is IndexableElement) { | |
| 140 LibraryElement libraryElement = indexable.element.library; | |
| 141 if (libraryElement != null) { | |
| 142 String libraryPath = libraryElement.source.fullName; | |
| 143 int libraryPathId = _stringCodec.encode(libraryPath); | |
| 144 return JenkinsSmiHash.combine(libraryPathId, elementNameId); | |
| 145 } | |
| 146 } | |
| 147 return elementNameId; | |
| 148 } | 129 } |
| 149 } | 130 } |
| 150 | 131 |
| 151 /** | 132 /** |
| 152 * A helper that encodes/decodes [Relationship]s to/from integers. | 133 * A helper that encodes/decodes [Relationship]s to/from integers. |
| 153 */ | 134 */ |
| 154 class RelationshipCodec { | 135 class RelationshipCodec { |
| 155 final StringCodec _stringCodec; | 136 final StringCodec _stringCodec; |
| 156 | 137 |
| 157 RelationshipCodec(this._stringCodec); | 138 RelationshipCodec(this._stringCodec); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 int encode(String name) { | 173 int encode(String name) { |
| 193 int index = nameToIndex[name]; | 174 int index = nameToIndex[name]; |
| 194 if (index == null) { | 175 if (index == null) { |
| 195 index = _indexToName.length; | 176 index = _indexToName.length; |
| 196 nameToIndex[name] = index; | 177 nameToIndex[name] = index; |
| 197 _indexToName.add(name); | 178 _indexToName.add(name); |
| 198 } | 179 } |
| 199 return index; | 180 return index; |
| 200 } | 181 } |
| 201 } | 182 } |
| OLD | NEW |