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 index.split_store; | 5 library engine.src.index.split_store; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
11 | 11 |
12 import 'package:analysis_server/src/index/store/collection.dart'; | 12 import 'package:analyzer/index/index.dart'; |
| 13 import 'package:analyzer/index/index_store.dart'; |
13 import 'package:analyzer/src/generated/element.dart'; | 14 import 'package:analyzer/src/generated/element.dart'; |
14 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
15 import 'package:analyzer/src/generated/index.dart'; | |
16 import 'package:analyzer/src/generated/java_engine.dart'; | 16 import 'package:analyzer/src/generated/java_engine.dart'; |
17 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
18 import 'package:analysis_server/src/index/store/codec.dart'; | 18 import 'package:analyzer/src/index/store/codec.dart'; |
| 19 import 'package:analyzer/src/index/store/collection.dart'; |
19 | 20 |
20 | 21 |
21 /** | 22 /** |
22 * A manager for files content. | 23 * A manager for files content. |
23 */ | 24 */ |
24 abstract class FileManager { | 25 abstract class FileManager { |
25 /** | 26 /** |
26 * Removes all files. | 27 * Removes all files. |
27 */ | 28 */ |
28 void clear(); | 29 void clear(); |
(...skipping 14 matching lines...) Expand all Loading... |
43 Future write(String name, List<int> bytes); | 44 Future write(String name, List<int> bytes); |
44 } | 45 } |
45 | 46 |
46 | 47 |
47 /** | 48 /** |
48 * A [FileManager] based [NodeManager]. | 49 * A [FileManager] based [NodeManager]. |
49 */ | 50 */ |
50 class FileNodeManager implements NodeManager { | 51 class FileNodeManager implements NodeManager { |
51 static int _VERSION = 1; | 52 static int _VERSION = 1; |
52 | 53 |
| 54 final FileManager _fileManager; |
| 55 final Logger _logger; |
| 56 |
53 final ContextCodec contextCodec; | 57 final ContextCodec contextCodec; |
54 | |
55 final ElementCodec elementCodec; | 58 final ElementCodec elementCodec; |
56 | |
57 final StringCodec stringCodec; | 59 final StringCodec stringCodec; |
58 | 60 final RelationshipCodec _relationshipCodec; |
59 final FileManager _fileManager; | |
60 | 61 |
61 int _locationCount = 0; | 62 int _locationCount = 0; |
62 | 63 |
63 final Logger _logger; | |
64 | |
65 Map<String, int> _nodeLocationCounts = new HashMap<String, int>(); | 64 Map<String, int> _nodeLocationCounts = new HashMap<String, int>(); |
66 | 65 |
67 final RelationshipCodec _relationshipCodec; | |
68 | |
69 FileNodeManager(this._fileManager, this._logger, this.stringCodec, | 66 FileNodeManager(this._fileManager, this._logger, this.stringCodec, |
70 this.contextCodec, this.elementCodec, this._relationshipCodec); | 67 this.contextCodec, this.elementCodec, this._relationshipCodec); |
71 | 68 |
72 @override | 69 @override |
73 int get locationCount => _locationCount; | 70 int get locationCount => _locationCount; |
74 | 71 |
75 @override | 72 @override |
76 void clear() { | 73 void clear() { |
77 _fileManager.clear(); | 74 _fileManager.clear(); |
78 } | 75 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 204 } |
208 | 205 |
209 | 206 |
210 /** | 207 /** |
211 * A single index file in-memory presentation. | 208 * A single index file in-memory presentation. |
212 */ | 209 */ |
213 class IndexNode { | 210 class IndexNode { |
214 final AnalysisContext context; | 211 final AnalysisContext context; |
215 | 212 |
216 final ElementCodec _elementCodec; | 213 final ElementCodec _elementCodec; |
| 214 final RelationshipCodec _relationshipCodec; |
217 | 215 |
218 Map<RelationKeyData, List<LocationData>> _relations = | 216 Map<RelationKeyData, List<LocationData>> _relations = |
219 new HashMap<RelationKeyData, List<LocationData>>(); | 217 new HashMap<RelationKeyData, List<LocationData>>(); |
220 | 218 |
221 final RelationshipCodec _relationshipCodec; | |
222 | 219 |
223 IndexNode(this.context, this._elementCodec, this._relationshipCodec); | 220 IndexNode(this.context, this._elementCodec, this._relationshipCodec); |
224 | 221 |
225 /** | 222 /** |
226 * Returns number of locations in this node. | 223 * Returns number of locations in this node. |
227 */ | 224 */ |
228 int get locationCount { | 225 int get locationCount { |
229 int locationCount = 0; | 226 int locationCount = 0; |
230 for (List<LocationData> locations in _relations.values) { | 227 for (List<LocationData> locations in _relations.values) { |
231 locationCount += locations.length; | 228 locationCount += locations.length; |
232 } | 229 } |
233 return locationCount; | 230 return locationCount; |
234 } | 231 } |
235 | 232 |
236 /** | 233 /** |
237 * Returns the recorded relations. | 234 * Returns the recorded relations. |
238 */ | 235 */ |
239 Map<RelationKeyData, List<LocationData>> get relations => _relations; | 236 Map<RelationKeyData, List<LocationData>> get relations => _relations; |
240 | 237 |
241 /** | 238 /** |
242 * Sets relations data. This method is used during loading data from a storage
. | 239 * Sets relations data. |
| 240 * This method is used during loading data from a storage. |
243 */ | 241 */ |
244 void set relations(Map<RelationKeyData, List<LocationData>> relations) { | 242 void set relations(Map<RelationKeyData, List<LocationData>> relations) { |
245 this._relations.clear(); | 243 _relations = relations; |
246 this._relations.addAll(relations); | |
247 } | 244 } |
248 | 245 |
249 /** | 246 /** |
250 * Return the locations of the elements that have the given relationship with
the given element. | 247 * Returns the locations of the elements that have the given relationship with |
| 248 * the given element. |
251 * | 249 * |
252 * @param element the the element that has the relationship with the locations
to be returned | 250 * [element] - the the element that has the relationship with the locations to |
253 * @param relationship the [Relationship] between the given element and the lo
cations to be | 251 * be returned. |
254 * returned | 252 * [relationship] - the [Relationship] between the given [element] and the |
| 253 * locations to be returned |
255 */ | 254 */ |
256 List<Location> getRelationships(Element element, Relationship relationship) { | 255 List<Location> getRelationships(Element element, Relationship relationship) { |
257 // prepare key | 256 // prepare key |
258 RelationKeyData key = new RelationKeyData.forObject(_elementCodec, | 257 RelationKeyData key = new RelationKeyData.forObject(_elementCodec, |
259 _relationshipCodec, element, relationship); | 258 _relationshipCodec, element, relationship); |
260 // find LocationData(s) | 259 // find LocationData(s) |
261 List<LocationData> locationDatas = _relations[key]; | 260 List<LocationData> locationDatas = _relations[key]; |
262 if (locationDatas == null) { | 261 if (locationDatas == null) { |
263 return Location.EMPTY_ARRAY; | 262 return Location.EMPTY_ARRAY; |
264 } | 263 } |
265 // convert to Location(s) | 264 // convert to Location(s) |
266 List<Location> locations = <Location>[]; | 265 List<Location> locations = <Location>[]; |
267 for (LocationData locationData in locationDatas) { | 266 for (LocationData locationData in locationDatas) { |
268 Location location = locationData.getLocation(context, _elementCodec); | 267 Location location = locationData.getLocation(context, _elementCodec); |
269 if (location != null) { | 268 if (location != null) { |
270 locations.add(location); | 269 locations.add(location); |
271 } | 270 } |
272 } | 271 } |
273 return locations; | 272 return locations; |
274 } | 273 } |
275 | 274 |
276 /** | 275 /** |
277 * Records that the given element and location have the given relationship. | 276 * Records that the given [element] and [location] have the given [relationshi
p]. |
278 * | 277 * |
279 * @param element the element that is related to the location | 278 * [element] - the [Element] that is related to the location. |
280 * @param relationship the [Relationship] between the element and the location | 279 * [relationship] - the [Relationship] between [element] and [location]. |
281 * @param location the [Location] where relationship happens | 280 * [location] - the [Location] where relationship happens. |
282 */ | 281 */ |
283 void recordRelationship(Element element, Relationship relationship, | 282 void recordRelationship(Element element, Relationship relationship, |
284 Location location) { | 283 Location location) { |
285 RelationKeyData key = new RelationKeyData.forObject(_elementCodec, | 284 RelationKeyData key = new RelationKeyData.forObject(_elementCodec, |
286 _relationshipCodec, element, relationship); | 285 _relationshipCodec, element, relationship); |
287 // prepare LocationData(s) | 286 // prepare LocationData(s) |
288 List<LocationData> locationDatas = _relations[key]; | 287 List<LocationData> locationDatas = _relations[key]; |
289 if (locationDatas == null) { | 288 if (locationDatas == null) { |
290 locationDatas = <LocationData>[]; | 289 locationDatas = <LocationData>[]; |
291 _relations[key] = locationDatas; | 290 _relations[key] = locationDatas; |
292 } | 291 } |
293 // add new LocationData | 292 // add new LocationData |
294 locationDatas.add(new LocationData.forObject(_elementCodec, location)); | 293 locationDatas.add(new LocationData.forObject(_elementCodec, location)); |
295 } | 294 } |
296 } | 295 } |
297 | 296 |
298 | 297 |
299 /** | 298 /** |
300 * A container with information about a [Location]. | 299 * A container with information about a [Location]. |
301 */ | 300 */ |
302 class LocationData { | 301 class LocationData { |
303 final int elementId; | 302 final int elementId; |
| 303 final int offset; |
304 final int length; | 304 final int length; |
305 final int offset; | |
306 | 305 |
307 LocationData.forData(this.elementId, this.offset, this.length); | 306 LocationData.forData(this.elementId, this.offset, this.length); |
308 | 307 |
309 LocationData.forObject(ElementCodec elementCodec, Location location) | 308 LocationData.forObject(ElementCodec elementCodec, Location location) |
310 : elementId = elementCodec.encode(location.element), | 309 : elementId = elementCodec.encode(location.element), |
311 offset = location.offset, | 310 offset = location.offset, |
312 length = location.length; | 311 length = location.length; |
313 | 312 |
314 @override | 313 @override |
315 int get hashCode { | 314 int get hashCode { |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 void doneIndex() { | 598 void doneIndex() { |
600 if (_currentNode != null) { | 599 if (_currentNode != null) { |
601 _nodeManager.putNode(_currentNodeName, _currentNode); | 600 _nodeManager.putNode(_currentNodeName, _currentNode); |
602 _currentNodeName = null; | 601 _currentNodeName = null; |
603 _currentNodeNameId = -1; | 602 _currentNodeNameId = -1; |
604 _currentNode = null; | 603 _currentNode = null; |
605 _currentContextId = -1; | 604 _currentContextId = -1; |
606 } | 605 } |
607 } | 606 } |
608 | 607 |
609 @override | 608 Future<List<Location>> getRelationships(Element element, |
610 List<Location> getRelationships(Element element, Relationship relationship) { | |
611 // TODO(scheglov) make IndexStore interface async | |
612 return <Location>[]; | |
613 } | |
614 | |
615 Future<List<Location>> getRelationshipsAsync(Element element, | |
616 Relationship relationship) { | 609 Relationship relationship) { |
617 // special support for UniverseElement | 610 // special support for UniverseElement |
618 if (identical(element, UniverseElement.INSTANCE)) { | 611 if (identical(element, UniverseElement.INSTANCE)) { |
619 List<Location> locations = _getRelationshipsUniverse(relationship); | 612 List<Location> locations = _getRelationshipsUniverse(relationship); |
620 return new Future.value(locations); | 613 return new Future.value(locations); |
621 } | 614 } |
622 // prepare node names | 615 // prepare node names |
623 String name = _getElementName(element); | 616 String name = _getElementName(element); |
624 int nameId = _stringCodec.encode(name); | 617 int nameId = _stringCodec.encode(name); |
625 List<int> nodeNameIds = _nameToNodeNames.get(nameId); | 618 List<int> nodeNameIds = _nameToNodeNames.get(nameId); |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 return new Uint8List.fromList(_buffer.takeBytes()); | 882 return new Uint8List.fromList(_buffer.takeBytes()); |
890 } | 883 } |
891 | 884 |
892 void writeInt(int value) { | 885 void writeInt(int value) { |
893 _buffer.addByte((value & 0xFF000000) >> 24); | 886 _buffer.addByte((value & 0xFF000000) >> 24); |
894 _buffer.addByte((value & 0x00FF0000) >> 16); | 887 _buffer.addByte((value & 0x00FF0000) >> 16); |
895 _buffer.addByte((value & 0x0000FF00) >> 8); | 888 _buffer.addByte((value & 0x0000FF00) >> 8); |
896 _buffer.addByte(value & 0xFF); | 889 _buffer.addByte(value & 0xFF); |
897 } | 890 } |
898 } | 891 } |
OLD | NEW |