| 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 part of service; |
| 6 |
| 7 /// Abstract [ServiceObjectCache]. |
| 8 abstract class ServiceObjectCache<T extends ServiceObject> { |
| 9 final Isolate isolate; |
| 10 final _cache = new ObservableMap<String, T>(); |
| 11 |
| 12 ServiceObjectCache(this.isolate) { |
| 13 assert(isolate != null); |
| 14 } |
| 15 |
| 16 /// Returns true if [this] caches objects with this [id]. |
| 17 bool cachesId(String id); |
| 18 |
| 19 /// Upgrades [obj] into a [T]. |
| 20 T _upgrade(ObservableMap map); |
| 21 |
| 22 /// Returns true if [this] has [id] in its cache. |
| 23 bool contains(String id) { |
| 24 assert(cachesId(id)); |
| 25 return _cache[id] != null; |
| 26 } |
| 27 |
| 28 /// Gets [id] from the cache. Returns null if not contained. |
| 29 T operator[](String id) { |
| 30 assert(cachesId(id)); |
| 31 return _cache[id]; |
| 32 } |
| 33 |
| 34 /// Caches [serviceObject] with [id]. |
| 35 operator[]=(String id, T serviceObject) { |
| 36 assert(cachesId(id)); |
| 37 _cache[id] = serviceObject; |
| 38 } |
| 39 |
| 40 /// Gets [id] from the cache or makes a network request for [id]. |
| 41 Future<T> get(String id) { |
| 42 assert(cachesId(id)); |
| 43 T cached = _cache[id]; |
| 44 if (cached != null) { |
| 45 return new Future.value(cached); |
| 46 } |
| 47 return isolate.get(id).then(_addToCache); |
| 48 } |
| 49 |
| 50 /// If [obj] is cached, return the cached object. Otherwise, upgrades [obj] |
| 51 /// and adds the upgraded value to the cache. |
| 52 T putIfAbsent(ObservableMap obj) { |
| 53 assert(ServiceObject.isServiceMap(obj)); |
| 54 String id = obj['id']; |
| 55 var type = obj['type']; |
| 56 assert(cachesId(id)); |
| 57 if (contains(id)) { |
| 58 return this[id]; |
| 59 } |
| 60 return _addToCache(_upgrade(obj)); |
| 61 } |
| 62 |
| 63 T _addToCache(T so) { |
| 64 this[so.id] = so; |
| 65 return so; |
| 66 } |
| 67 } |
| 68 |
| 69 class ScriptCache extends ServiceObjectCache<Script> { |
| 70 ScriptCache(Isolate isolate) : super(isolate); |
| 71 |
| 72 bool cachesId(String id) => _matcher.hasMatch(id); |
| 73 Script _upgrade(ObservableMap obj) => new Script.fromMap(isolate, obj); |
| 74 static final RegExp _matcher = new RegExp(r'scripts/.+'); |
| 75 |
| 76 void _processCoverage(ServiceMap coverage) { |
| 77 assert(coverage.serviceType == 'CodeCoverage'); |
| 78 var coverageList = coverage['coverage']; |
| 79 assert(coverageList != null); |
| 80 coverageList.forEach((scriptCoverage) { |
| 81 _processScriptCoverage(scriptCoverage); |
| 82 }); |
| 83 } |
| 84 |
| 85 void _processScriptCoverage(ObservableMap scriptCoverage) { |
| 86 // Because the coverage data was upgraded into a ServiceObject, |
| 87 // the script can be directly accessed. |
| 88 Script script = scriptCoverage['script']; |
| 89 script._processHits(scriptCoverage['hits']); |
| 90 } |
| 91 } |
| 92 |
| 93 class CodeCache extends ServiceObjectCache<Code> { |
| 94 CodeCache(Isolate isolate) : super(isolate); |
| 95 |
| 96 bool cachesId(String id) => _matcher.hasMatch(id); |
| 97 Code _upgrade(ObservableMap obj) => new Code.fromMap(isolate, obj); |
| 98 |
| 99 static final RegExp _matcher = new RegExp(r'code/.+'); |
| 100 |
| 101 List<Code> topExclusive(int count) { |
| 102 var codeList = _cache.values.toList(); |
| 103 codeList.sort((Code a, Code b) { |
| 104 return b.exclusiveTicks - a.exclusiveTicks; |
| 105 }); |
| 106 if (codeList.length < count) { |
| 107 return codeList; |
| 108 } |
| 109 codeList.length = count; |
| 110 return codeList; |
| 111 } |
| 112 |
| 113 void _resetProfileData() { |
| 114 _cache.forEach((k, Code code) { |
| 115 code.resetProfileData(); |
| 116 }); |
| 117 } |
| 118 |
| 119 void _updateProfileData(ServiceMap profile, List<Code> codeTable) { |
| 120 var codes = profile['codes']; |
| 121 var sampleCount = profile['samples']; |
| 122 for (var profileCode in codes) { |
| 123 Code code = profileCode['code']; |
| 124 code.updateProfileData(profileCode, codeTable, sampleCount); |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 class ClassCache extends ServiceObjectCache<ServiceMap> { |
| 130 ClassCache(Isolate isolate) : super(isolate); |
| 131 |
| 132 bool cachesId(String id) => _matcher.hasMatch(id); |
| 133 bool cachesType(String type) => ServiceObject.stripRef(type) == 'Class'; |
| 134 ServiceMap _upgrade(ObservableMap obj) => |
| 135 new ServiceMap.fromMap(isolate, obj); |
| 136 |
| 137 static final RegExp _matcher = new RegExp(r'classes/\d+$'); |
| 138 } |
| OLD | NEW |