Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: runtime/bin/vmservice/client/lib/src/service/cache.dart

Issue 192443004: Complete the switch to ServiceObject (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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);
turnidge 2014/03/10 21:03:28 What do you think of blank lines between these dec
18 /// Returns true if [this] caches objects with service [type].
19 bool cachesType(String type);
turnidge 2014/03/10 21:03:28 I'm not convinced that we want to have a data-driv
20 /// Upgrades [obj] into a [T].
turnidge 2014/03/10 21:03:28 More verbose comment about what "upgrading" means.
21 T upgrade(ObservableMap map);
turnidge 2014/03/10 21:03:28 Could upgrade be private (_upgrade) or is it part
Cutch 2014/03/11 03:17:48 Done.
22
23 /// Returns true if [this] has [id] in its cache.
24 bool contains(String id) {
25 assert(cachesId(id));
26 return _cache[id] != null;
27 }
28
29 /// Gets [id] from the cache. Returns null if not contained.
30 T operator[](String id) {
31 assert(cachesId(id));
32 return _cache[id];
33 }
34
35 /// Caches [so] with [id].
36 operator[]=(String id, T so) {
turnidge 2014/03/10 21:03:28 "so" -> obj?
Cutch 2014/03/11 03:17:48 Done.
37 assert(cachesId(id));
38 assert(cachesType(so.serviceType));
39 _cache[id] = so;
40 }
41
42 /// Gets [id] from the cache or makes a network request for [id].
43 Future<T> get(String id) {
44 assert(cachesId(id));
45 T cached = _cache[id];
46 if (cached != null) {
47 return new Future.value(cached);
48 }
49 return isolate.fetch(id).then(_addToCache);
50 }
51
52 /// If [obj] is cached, return the cached object. Otherwise, upgrades [obj]
53 /// and adds the upgraded value to the cache.
54 T cachedOrUpgradeAndAdd(ObservableMap obj) {
turnidge 2014/03/10 21:03:28 I would like a different name. Maybe something li
Cutch 2014/03/11 03:17:48 I've renamed it to putIfAbsent. This gets called b
55 assert(ServiceObject.isServiceMap(obj));
56 String id = obj['id'];
57 var type = obj['type'];
58 assert(cachesId(id));
59 assert(cachesType(type));
60 if (contains(id)) {
61 return this[id];
62 }
63 return _addToCache(upgrade(obj));
64 }
65
66 T _addToCache(T so) {
67 this[so.id] = so;
68 return so;
69 }
70 }
71
72 class ScriptCache extends ServiceObjectCache<Script> {
73 ScriptCache(Isolate isolate) : super(isolate);
74
75 bool cachesId(String id) => _matcher.hasMatch(id);
76 bool cachesType(String type) => ServiceObject.unreffedType(type) == 'Script';
turnidge 2014/03/10 21:03:28 unreffedType... the double f is weird. Maybe "str
Cutch 2014/03/11 03:17:48 Done.
77 Script upgrade(ObservableMap obj) => new Script.fromMap(isolate, obj);
78 static final RegExp _matcher = new RegExp(r'scripts/.+');
79
80 void _processCoverage(ServiceMap coverage) {
81 assert(coverage.serviceType == 'CodeCoverage');
82 var coverageList = coverage['coverage'];
83 assert(coverageList != null);
84 coverageList.forEach((scriptCoverage) {
85 _processScriptCoverage(scriptCoverage);
86 });
87 }
88
89 void _processScriptCoverage(ObservableMap scriptCoverage) {
90 // Because the coverage data was upgraded into a ServiceObject,
91 // the script can be directly accessed.
92 Script script = scriptCoverage['script'];
93 script._processHits(scriptCoverage['hits']);
94 }
95 }
96
97 class CodeCache extends ServiceObjectCache<Code> {
98 CodeCache(Isolate isolate) : super(isolate);
99
100 bool cachesId(String id) => _matcher.hasMatch(id);
101 bool cachesType(String type) => ServiceObject.unreffedType(type) == 'Code';
102 Code upgrade(ObservableMap obj) => new Code.fromMap(isolate, obj);
103
104 static final RegExp _matcher = new RegExp(r'code/.+');
105
106 List<Code> topExclusive(int count) {
107 var codeList = _cache.values.toList();
108 codeList.sort((Code a, Code b) {
109 return b.exclusiveTicks - a.exclusiveTicks;
110 });
111 if (codeList.length < count) {
112 return codeList;
113 }
114 codeList.length = count;
115 return codeList;
116 }
117
118 void _resetProfileData() {
119 _cache.forEach((k, Code code) {
120 code.resetProfileData();
121 });
122 }
123
124 void _updateProfileData(ServiceMap profile, List<Code> codeTable) {
125 var codes = profile['codes'];
126 var sampleCount = profile['samples'];
127 for (var profileCode in codes) {
128 Code code = profileCode['code'];
129 code.updateProfileData(profileCode, codeTable, sampleCount);
130 }
131 }
132 }
133
134 class ClassCache extends ServiceObjectCache<ServiceMap> {
135 ClassCache(Isolate isolate) : super(isolate);
136
137 bool cachesId(String id) => _matcher.hasMatch(id);
138 bool cachesType(String type) => ServiceObject.unreffedType(type) == 'Class';
139 ServiceMap upgrade(ObservableMap obj) => new ServiceMap.fromMap(isolate, obj);
140
141 static final RegExp _matcher = new RegExp(r'classes/\d+$');
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698